1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Eclipse Public License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.eclipse.org/org/documents/epl-v10.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.ide.common.layout;
import static com.android.ide.eclipse.adt.internal.editors.layout.descriptors.LayoutDescriptors.REQUEST_FOCUS;
import com.android.ide.common.api.IMenuCallback;
import com.android.ide.common.api.INode;
import com.android.ide.common.api.INodeHandler;
import com.android.ide.common.api.IViewRule;
import com.android.ide.common.api.InsertType;
import com.android.ide.common.api.MenuAction;
import java.util.List;
/**
* An {@link IViewRule} for android.widget.EditText.
*/
public class EditTextRule extends BaseViewRule {
@Override
public void onCreate(INode node, INode parent, InsertType insertType) {
super.onCreate(node, parent, insertType);
if (parent != null) {
INode focus = findFocus(findRoot(parent));
if (focus == null) {
// Add <requestFocus>
node.appendChild(REQUEST_FOCUS);
}
}
}
/**
* {@inheritDoc}
* <p>
* Adds a "Request Focus" menu item.
*/
@Override
public List<MenuAction> getContextMenu(final INode selectedNode) {
final boolean hasFocus = hasFocus(selectedNode);
final String label = hasFocus ? "Clear Focus" : "Request Focus";
IMenuCallback onChange = new IMenuCallback() {
public void action(MenuAction menuAction, String valueId, Boolean newValue) {
selectedNode.editXml(label, new INodeHandler() {
public void handle(INode node) {
INode focus = findFocus(findRoot(node));
if (focus != null && focus.getParent() != null) {
focus.getParent().removeChild(focus);
}
if (!hasFocus) {
node.appendChild(REQUEST_FOCUS);
}
}
});
}
};
return concatenate(super.getContextMenu(selectedNode),
new MenuAction.Action("_setfocus", label, null, onChange)); //$NON-NLS-1$
}
/** Returns true if the given node currently has focus */
private static boolean hasFocus(INode node) {
INode focus = findFocus(node);
if (focus != null) {
return focus.getParent() == node;
}
return false;
}
/** Returns the root/top level node in the view hierarchy that contains the given node */
private static INode findRoot(INode node) {
// First find the parent
INode root = node;
while (root != null) {
INode parent = root.getParent();
if (parent == null) {
break;
} else {
root = parent;
}
}
return root;
}
/** Finds the focus node (not the node containing focus, but the actual request focus node
* under a given node */
private static INode findFocus(INode node) {
if (node.getFqcn().equals(REQUEST_FOCUS)) {
return node;
}
for (INode child : node.getChildren()) {
INode focus = findFocus(child);
if (focus != null) {
return focus;
}
}
return null;
}
}
|