View Javadoc

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