KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > schema > SchemaDoubleClickStrategy


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.schema;
12
13 import org.eclipse.jface.text.*;
14
15
16 public class SchemaDoubleClickStrategy implements ITextDoubleClickStrategy {
17     protected ITextViewer fText;
18     protected int fPos;
19     protected int fStartPos;
20     protected int fEndPos;
21     protected static char[] fgBrackets = { '(', ')', '"', '"' };
22    
23     public SchemaDoubleClickStrategy() {
24         super();
25     }
26 public void doubleClicked(ITextViewer part) {
27     fPos = part.getSelectedRange().x;
28
29     if (fPos < 0)
30         return;
31
32     fText = part;
33
34     if (!selectComment())
35         selectWord();
36 }
37 protected boolean matchComment() {
38     IDocument doc = fText.getDocument();
39
40     try {
41         int pos = fPos;
42         char c = ' ';
43
44         while (pos >= 0) {
45             c = doc.getChar(pos);
46             if (Character.isWhitespace(c) || c == '\"')
47                 break;
48             --pos;
49         }
50
51         if (c != '\"')
52             return false;
53
54         fStartPos = pos;
55
56         pos = fPos;
57         int length = doc.getLength();
58         c = ' ';
59
60         while (pos < length) {
61             c = doc.getChar(pos);
62             if (Character.isWhitespace(c) || c == '\"')
63                 break;
64             ++pos;
65         }
66         if (c != '\"')
67             return false;
68
69         fEndPos = pos;
70
71         return true;
72
73     } catch (BadLocationException x) {
74     }
75
76     return false;
77 }
78     protected boolean matchWord() {
79
80         IDocument doc = fText.getDocument();
81
82         try {
83
84             int pos = fPos;
85             char c;
86
87             while (pos >= 0) {
88                 c = doc.getChar(pos);
89                 if (!Character.isJavaIdentifierPart(c))
90                     break;
91                 --pos;
92             }
93
94             fStartPos = pos;
95
96             pos = fPos;
97             int length = doc.getLength();
98
99             while (pos < length) {
100                 c = doc.getChar(pos);
101                 if (!Character.isJavaIdentifierPart(c))
102                     break;
103                 ++pos;
104             }
105
106             fEndPos = pos;
107
108             return true;
109
110         } catch (BadLocationException x) {
111         }
112
113         return false;
114     }
115     protected boolean selectComment() {
116         if (matchComment()) {
117             fText.setSelectedRange(fStartPos + 1, fEndPos);
118             return true;
119         }
120         return false;
121     }
122     protected void selectWord() {
123         if (matchWord())
124             fText.setSelectedRange(fStartPos + 1, fEndPos);
125     }
126 }
127
Popular Tags