01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.text;
11:
12: import org.eclipse.jface.text.BadLocationException;
13: import org.eclipse.jface.text.IDocument;
14: import org.eclipse.jface.text.ITextDoubleClickStrategy;
15: import org.eclipse.jface.text.ITextViewer;
16:
17: public class XMLDoubleClickStrategy implements ITextDoubleClickStrategy {
18: protected ITextViewer fText;
19:
20: public void doubleClicked(ITextViewer part) {
21: int pos = part.getSelectedRange().x;
22: if (pos > 0) {
23: fText = part;
24: selectWord(pos);
25: }
26: }
27:
28: protected boolean selectWord(int caretPos) {
29:
30: IDocument doc = fText.getDocument();
31: int startPos, endPos;
32:
33: try {
34:
35: int pos = caretPos;
36: char c;
37:
38: while (pos >= 0) {
39: c = doc.getChar(pos);
40: if (!Character.isJavaIdentifierPart(c) && c != '.')
41: break;
42: --pos;
43: }
44:
45: startPos = pos;
46:
47: pos = caretPos;
48: int length = doc.getLength();
49:
50: while (pos < length) {
51: c = doc.getChar(pos);
52: if (!Character.isJavaIdentifierPart(c) && c != '.')
53: break;
54: ++pos;
55: }
56:
57: endPos = pos;
58: selectRange(startPos, endPos);
59: return true;
60:
61: } catch (BadLocationException x) {
62: }
63:
64: return false;
65: }
66:
67: private void selectRange(int startPos, int stopPos) {
68: int offset = startPos + 1;
69: int length = stopPos - offset;
70: fText.setSelectedRange(offset, length);
71: }
72: }
|