• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
18 
19 import com.android.ide.eclipse.adt.editors.layout.gscripts.INode;
20 import com.android.ide.eclipse.adt.internal.editors.layout.gre.NodeFactory;
21 import com.android.ide.eclipse.adt.internal.editors.layout.gre.NodeProxy;
22 import com.android.ide.eclipse.adt.internal.editors.layout.gre.RulesEngine;
23 
24 import org.eclipse.swt.graphics.Rectangle;
25 
26 /**
27  * Represents one selection in {@link LayoutCanvas}.
28  */
29 /* package */ class CanvasSelection {
30 
31     /** Current selected view info. Can be null. */
32     private final CanvasViewInfo mCanvasViewInfo;
33 
34     /** Current selection border rectangle. Null when mCanvasViewInfo is null . */
35     private final Rectangle mRect;
36 
37     /** The node proxy for drawing the selection. Null when mCanvasViewInfo is null. */
38     private final NodeProxy mNodeProxy;
39 
40     /** The name displayed over the selection, typically the widget class name. Can be null. */
41     private final String mName;
42 
43 
44     /**
45      * Creates a new {@link CanvasSelection} object.
46      * @param canvasViewInfo The view info being selected. Must not be null.
47      * @param nodeFactory
48      */
CanvasSelection(CanvasViewInfo canvasViewInfo, RulesEngine gre, NodeFactory nodeFactory)49     public CanvasSelection(CanvasViewInfo canvasViewInfo,
50             RulesEngine gre,
51             NodeFactory nodeFactory) {
52 
53         assert canvasViewInfo != null;
54 
55         mCanvasViewInfo = canvasViewInfo;
56 
57         if (canvasViewInfo == null) {
58             mRect = null;
59             mNodeProxy = null;
60         } else {
61             Rectangle r = canvasViewInfo.getSelectionRect();
62             mRect = new Rectangle(r.x, r.y, r.width, r.height);
63             mNodeProxy = nodeFactory.create(canvasViewInfo);
64         }
65 
66         mName = initDisplayName(canvasViewInfo, gre);
67     }
68 
69     /**
70      * Returns the selected view info. Cannot be null.
71      */
getViewInfo()72     public CanvasViewInfo getViewInfo() {
73         return mCanvasViewInfo;
74     }
75 
76     /**
77      * Returns the selection border rectangle.
78      * Cannot be null.
79      */
getRect()80     public Rectangle getRect() {
81         return mRect;
82     }
83 
84     /**
85      * The name displayed over the selection, typically the widget class name.
86      * Can be null.
87      */
getName()88     public String getName() {
89         return mName;
90     }
91 
92     /**
93      * Calls IViewRule.onSelected on the selected view.
94      *
95      * @param gre The rules engines.
96      * @param gcWrapper The GC to use for drawing.
97      * @param isMultipleSelection True if more than one view is selected.
98      */
paintSelection(RulesEngine gre, GCWrapper gcWrapper, boolean isMultipleSelection)99     /*package*/ void paintSelection(RulesEngine gre,
100             GCWrapper gcWrapper,
101             boolean isMultipleSelection) {
102         if (mNodeProxy != null) {
103             gre.callOnSelected(gcWrapper, mNodeProxy, mName, isMultipleSelection);
104         }
105     }
106 
107     /**
108      * Calls IViewRule.onChildSelected on the parent of the selected view, if it has one.
109      *
110      * @param gre The rules engines.
111      * @param gcWrapper The GC to use for drawing.
112      */
paintParentSelection(RulesEngine gre, GCWrapper gcWrapper)113     public void paintParentSelection(RulesEngine gre, GCWrapper gcWrapper) {
114         if (mNodeProxy != null) {
115             INode parent = mNodeProxy.getParent();
116             if (parent instanceof NodeProxy) {
117                 gre.callOnChildSelected(gcWrapper, (NodeProxy)parent, mNodeProxy);
118             }
119         }
120     }
121 
122     //----
123 
initDisplayName(CanvasViewInfo canvasViewInfo, RulesEngine gre)124     private String initDisplayName(CanvasViewInfo canvasViewInfo, RulesEngine gre) {
125         if (canvasViewInfo == null) {
126             return null;
127         }
128 
129         String fqcn = canvasViewInfo.getName();
130         if (fqcn == null) {
131             return null;
132         }
133 
134         String name = gre.callGetDisplayName(canvasViewInfo.getUiViewKey());
135 
136         if (name == null) {
137             // The name is typically a fully-qualified class name. Let's make it a tad shorter.
138 
139             if (fqcn.startsWith("android.")) {                                      // $NON-NLS-1$
140                 // For android classes, convert android.foo.Name to android...Name
141                 int first = fqcn.indexOf('.');
142                 int last = fqcn.lastIndexOf('.');
143                 if (last > first) {
144                     name = fqcn.substring(0, first) + ".." + fqcn.substring(last);   // $NON-NLS-1$
145                 }
146             } else {
147                 // For custom non-android classes, it's best to keep the 2 first segments of
148                 // the namespace, e.g. we want to get something like com.example...MyClass
149                 int first = fqcn.indexOf('.');
150                 first = fqcn.indexOf('.', first + 1);
151                 int last = fqcn.lastIndexOf('.');
152                 if (last > first) {
153                     name = fqcn.substring(0, first) + ".." + fqcn.substring(last);   // $NON-NLS-1$
154                 }
155             }
156         }
157 
158         return name;
159     }
160 }
161