View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.editors;
23  
24  import org.eclipse.jface.text.*;
25  
26  /**
27   * It defines the action when user double click the text from the editor.
28   *
29   * @author Haoning Zhang
30   * @version 1.0
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 }