KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > $packageName$ > XMLDoubleClickStrategy


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