KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > XMLDoubleClickStrategy


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.text;
12
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.ITextDoubleClickStrategy;
16 import org.eclipse.jface.text.ITextViewer;
17
18 public class XMLDoubleClickStrategy implements ITextDoubleClickStrategy {
19     protected ITextViewer fText;
20
21     public void doubleClicked(ITextViewer part) {
22         int pos = part.getSelectedRange().x;
23         if (pos > 0) {
24             fText = part;
25             selectWord(pos);
26         }
27     }
28     
29     protected boolean selectWord(int caretPos) {
30
31         IDocument doc = fText.getDocument();
32         int startPos, endPos;
33
34         try {
35
36             int pos = caretPos;
37             char c;
38
39             while (pos >= 0) {
40                 c = doc.getChar(pos);
41                 if (!Character.isJavaIdentifierPart(c) && c != '.')
42                     break;
43                 --pos;
44             }
45
46             startPos = pos;
47
48             pos = caretPos;
49             int length = doc.getLength();
50
51             while (pos < length) {
52                 c = doc.getChar(pos);
53                 if (!Character.isJavaIdentifierPart(c) && c != '.')
54                     break;
55                 ++pos;
56             }
57
58             endPos = pos;
59             selectRange(startPos, endPos);
60             return true;
61
62         } catch (BadLocationException x) {
63         }
64
65         return false;
66     }
67     
68     private void selectRange(int startPos, int stopPos) {
69         int offset = startPos+1;
70         int length = stopPos - offset;
71         fText.setSelectedRange(offset, length);
72     }
73 }
74
Popular Tags