KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > editors > xml > XMLDoubleClickStrategy


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package org.terracotta.dso.editors.xml;
5
6 import org.eclipse.jface.text.BadLocationException;
7 import org.eclipse.jface.text.IDocument;
8 import org.eclipse.jface.text.ITextDoubleClickStrategy;
9 import org.eclipse.jface.text.ITextViewer;
10
11 public class XMLDoubleClickStrategy implements ITextDoubleClickStrategy {
12     protected ITextViewer fText;
13
14     public void doubleClicked(ITextViewer part) {
15         int pos = part.getSelectedRange().x;
16
17         if (pos < 0)
18             return;
19
20         fText = part;
21
22         if (!selectComment(pos)) {
23             selectWord(pos);
24         }
25     }
26     protected boolean selectComment(int caretPos) {
27         IDocument doc = fText.getDocument();
28         int startPos, endPos;
29
30         try {
31             int pos = caretPos;
32             char c = ' ';
33
34             while (pos >= 0) {
35                 c = doc.getChar(pos);
36                 if (c == '\\') {
37                     pos -= 2;
38                     continue;
39                 }
40                 if (c == Character.LINE_SEPARATOR || c == '\"')
41                     break;
42                 --pos;
43             }
44
45             if (c != '\"')
46                 return false;
47
48             startPos = pos;
49
50             pos = caretPos;
51             int length = doc.getLength();
52             c = ' ';
53
54             while (pos < length) {
55                 c = doc.getChar(pos);
56                 if (c == Character.LINE_SEPARATOR || c == '\"')
57                     break;
58                 ++pos;
59             }
60             if (c != '\"')
61                 return false;
62
63             endPos = pos;
64
65             int offset = startPos + 1;
66             int len = endPos - offset;
67             fText.setSelectedRange(offset, len);
68             return true;
69         } catch (BadLocationException x) {/**/}
70
71         return false;
72     }
73     protected boolean selectWord(int caretPos) {
74
75         IDocument doc = fText.getDocument();
76         int startPos, endPos;
77
78         try {
79
80             int pos = caretPos;
81             char c;
82
83             while (pos >= 0) {
84                 c = doc.getChar(pos);
85                 if (!Character.isJavaIdentifierPart(c))
86                     break;
87                 --pos;
88             }
89
90             startPos = pos;
91
92             pos = caretPos;
93             int length = doc.getLength();
94
95             while (pos < length) {
96                 c = doc.getChar(pos);
97                 if (!Character.isJavaIdentifierPart(c))
98                     break;
99                 ++pos;
100             }
101
102             endPos = pos;
103             selectRange(startPos, endPos);
104             return true;
105
106         } catch (BadLocationException x) {/**/}
107
108         return false;
109     }
110
111     private void selectRange(int startPos, int stopPos) {
112         int offset = startPos + 1;
113         int length = stopPos - offset;
114         fText.setSelectedRange(offset, length);
115     }
116 }
Popular Tags