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