KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > editors > JimpleDoubleClickStrategy


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jennifer Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

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