• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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;
18 
19 import static com.android.SdkConstants.PREFIX_RESOURCE_REF;
20 import static com.android.SdkConstants.PREFIX_THEME_REF;
21 
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
24 import org.eclipse.wst.xml.ui.internal.doubleclick.XMLDoubleClickStrategy;
25 
26 /**
27  * Custom version of {@link XMLDoubleClickStrategy} which is smarter about
28  * selecting portions of resource references, etc.
29  */
30 @SuppressWarnings("restriction") // XML API
31 public class AndroidDoubleClickStrategy extends XMLDoubleClickStrategy {
32     /**
33      * Creates a new {@linkplain AndroidDoubleClickStrategy}
34      */
AndroidDoubleClickStrategy()35     public AndroidDoubleClickStrategy() {
36     }
37 
38     @Override
processElementDoubleClicked()39     protected void processElementDoubleClicked() {
40         // Special case: if you click on the local name portion of an attribute pair,
41         // select only the local name. For example, if you click anywhere in the "text" region
42         // of "android:text", select just the "text" portion.
43         if (fTextRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
44             String regionText = fStructuredDocumentRegion.getText(fTextRegion);
45             int cursor = fCaretPosition - fStructuredDocumentRegion.getStartOffset(fTextRegion);
46             int ns = regionText.indexOf(':');
47             if (cursor > ns) {
48                 int start = ns + 1;
49                 fTextViewer.setSelectedRange(fStructuredDocumentRegion.getStartOffset(fTextRegion)
50                         + start,  fTextRegion.getTextLength() - start);
51                 return;
52             }
53         }
54 
55         super.processElementDoubleClicked();
56     }
57 
58     @Override
getWord(String string, int cursor)59     protected Point getWord(String string, int cursor) {
60         if (string == null) {
61             return null;
62         }
63 
64         // Default implementation will strip off the surrounding quotes etc:
65         Point position = super.getWord(string, cursor);
66 
67         assert cursor >= position.x && cursor <= position.y;
68 
69         // Special case: when you click on a resource identifier name, only select the
70         // name portion
71         if (string.startsWith(PREFIX_RESOURCE_REF, position.x) ||
72                 string.startsWith(PREFIX_THEME_REF, position.x)) {
73             int nameStart = string.indexOf('/', position.x + 1);
74             if (nameStart != -1 && nameStart < cursor) {
75                 position.x = nameStart + 1;
76                 return position;
77             }
78         }
79 
80         // Special case: when you have a dotted name, such as com.android.tools.MyClass,
81         // and you click on the last part, select only that part
82         int lastDot = string.lastIndexOf('.', cursor);
83         if (lastDot >= position.x && lastDot < position.y - 1) {
84             int next = string.indexOf('.', cursor);
85             if (next == -1 || next > position.y) {
86                 position.x = lastDot + 1;
87             }
88         }
89 
90         return position;
91     }
92 }
93