intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.js.ui / src / net / sourceforge / phpeclipse / js / ui / editors / JSDoubleClickStrategy.java
1 /*
2  * $RCSfile: JSDoubleClickStrategy.java,v $
3  *
4  * Copyright 2002
5  * CH-1700 Fribourg, Switzerland
6  * All rights reserved.
7  *
8  *========================================================================
9  * Modifications history
10  *========================================================================
11  * $Log: not supported by cvs2svn $
12  * Revision 1.1  2004/02/05 03:13:28  agfitzp
13  * Initial submission, outline view is broken due to refactoring
14  *
15  * Revision 1.1  2003/05/28 15:17:12  agfitzp
16  * net.sourceforge.phpeclipse.js.ui 0.0.1 code base
17  *
18  *========================================================================
19 */
20
21 package net.sourceforge.phpeclipse.js.ui.editors;
22
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.ITextDoubleClickStrategy;
26 import org.eclipse.jface.text.ITextViewer;
27
28
29 /**
30  * 
31  *
32  * @author $Author: jsurfer $, $Date: 2004-09-02 18:23:49 $
33  *
34  * @version $Revision: 1.1 $
35  */
36 public class JSDoubleClickStrategy implements ITextDoubleClickStrategy
37 {
38    protected ITextViewer fText;
39
40    /**
41     * Creates a new JSDoubleClickStrategy object.
42     */
43    public JSDoubleClickStrategy()
44    {
45       super();
46    }
47
48    /**
49     *
50     *
51     * @param part 
52     */
53    public void doubleClicked(ITextViewer part)
54    {
55       int pos = part.getSelectedRange().x;
56
57       if(pos < 0)
58       {
59          return;
60       }
61
62       fText = part;
63
64       if(!selectComment(pos))
65       {
66          selectWord(pos);
67       }
68    }
69
70    /**
71     *
72     *
73     * @param caretPos 
74     *
75     * @return 
76     */
77    protected boolean selectComment(int caretPos)
78    {
79       IDocument doc = fText.getDocument();
80       int startPos;
81       int endPos;
82
83       try
84       {
85          int pos = caretPos;
86          char c = ' ';
87
88          while(pos >= 0)
89          {
90             c = doc.getChar(pos);
91
92             if(c == '\\')
93             {
94                pos -= 2;
95
96                continue;
97             }
98
99             if(c == Character.LINE_SEPARATOR || c == '\"')
100             {
101                break;
102             }
103
104             --pos;
105          }
106
107          if(c != '\"')
108          {
109             return false;
110          }
111
112          startPos = pos;
113
114          pos = caretPos;
115
116          int length = doc.getLength();
117          c = ' ';
118
119          while(pos < length)
120          {
121             c = doc.getChar(pos);
122
123             if(c == Character.LINE_SEPARATOR || c == '\"')
124             {
125                break;
126             }
127
128             ++pos;
129          }
130
131          if(c != '\"')
132          {
133             return false;
134          }
135
136          endPos = pos;
137
138          int offset = startPos + 1;
139          int len = endPos - offset;
140          fText.setSelectedRange(offset, len);
141
142          return true;
143       }
144       catch(BadLocationException x)
145       {
146       }
147
148       return false;
149    }
150
151    /**
152     *
153     *
154     * @param caretPos 
155     *
156     * @return 
157     */
158    protected boolean selectWord(int caretPos)
159    {
160       IDocument doc = fText.getDocument();
161       int startPos;
162       int endPos;
163
164       try
165       {
166          int pos = caretPos;
167          char c;
168
169          while(pos >= 0)
170          {
171             c = doc.getChar(pos);
172
173             if(!Character.isJavaIdentifierPart(c))
174             {
175                break;
176             }
177
178             --pos;
179          }
180
181          startPos = pos;
182
183          pos = caretPos;
184
185          int length = doc.getLength();
186
187          while(pos < length)
188          {
189             c = doc.getChar(pos);
190
191             if(!Character.isJavaIdentifierPart(c))
192             {
193                break;
194             }
195
196             ++pos;
197          }
198
199          endPos = pos;
200          selectRange(startPos, endPos);
201
202          return true;
203       }
204       catch(BadLocationException x)
205       {
206       }
207
208       return false;
209    }
210
211    private void selectRange(int startPos, int stopPos)
212    {
213       int offset = startPos + 1;
214       int length = stopPos - offset;
215       fText.setSelectedRange(offset, length);
216    }
217 }