aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/EditTextRule.java
blob: dc600862d93f18e59d4e1c4324ac2a7634628c4c (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * 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.common.layout.LayoutConstants.ATTR_EMS;
import static com.android.ide.eclipse.adt.internal.editors.layout.descriptors.LayoutDescriptors.REQUEST_FOCUS;
import static com.android.util.XmlUtils.ANDROID_URI;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
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.RuleAction;

import java.util.List;

/**
 * An {@link IViewRule} for android.widget.EditText.
 */
public class EditTextRule extends BaseViewRule {

    @Override
    public void onCreate(@NonNull INode node, @NonNull INode parent,
            @NonNull InsertType insertType) {
        super.onCreate(node, parent, insertType);

        if (parent != null) {
            INode focus = findFocus(findRoot(parent));
            if (focus == null) {
                // Add <requestFocus>
                node.appendChild(REQUEST_FOCUS);
            }

            if (parent.getBounds().w >= 320) {
                node.setAttribute(ANDROID_URI, ATTR_EMS, "10"); //$NON-NLS-1$
            }
        }
    }

    /**
     * {@inheritDoc}
     * <p>
     * Adds a "Request Focus" menu item.
     */
    @Override
    public void addContextMenuActions(@NonNull List<RuleAction> actions,
            final @NonNull INode selectedNode) {
        super.addContextMenuActions(actions, selectedNode);

        final boolean hasFocus = hasFocus(selectedNode);
        final String label = hasFocus ? "Clear Focus" : "Request Focus";

        IMenuCallback onChange = new IMenuCallback() {
            @Override
            public void action(
                    @NonNull RuleAction menuAction,
                    @NonNull List<? extends INode> selectedNodes,
                    @Nullable String valueId,
                    @Nullable Boolean newValue) {
                selectedNode.editXml(label, new INodeHandler() {
                    @Override
                    public void handle(@NonNull INode node) {
                        INode focus = findFocus(findRoot(node));
                        if (focus != null && focus.getParent() != null) {
                            focus.getParent().removeChild(focus);
                        }
                        if (!hasFocus) {
                            node.appendChild(REQUEST_FOCUS);
                        }
                    }
                });
            }
        };

        actions.add(RuleAction.createAction("_setfocus", label, onChange, //$NON-NLS-1$
                null, 5, false /*supportsMultipleNodes*/));
        actions.add(RuleAction.createSeparator(7));
    }

    /** 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;
    }

}