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