001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.templateeditor.editors;
011:
012: import org.eclipse.jface.text.*;
013:
014: public class XMLDoubleClickStrategy implements ITextDoubleClickStrategy {
015: protected ITextViewer fText;
016:
017: public void doubleClicked(ITextViewer part) {
018: int pos = part.getSelectedRange().x;
019:
020: if (pos < 0)
021: return;
022:
023: fText = part;
024:
025: if (!selectComment(pos)) {
026: selectWord(pos);
027: }
028: }
029:
030: protected boolean selectComment(int caretPos) {
031: IDocument doc = fText.getDocument();
032: int startPos, endPos;
033:
034: try {
035: int pos = caretPos;
036: char c = ' ';
037:
038: while (pos >= 0) {
039: c = doc.getChar(pos);
040: if (c == '\\') {
041: pos -= 2;
042: continue;
043: }
044: if (c == Character.LINE_SEPARATOR || c == '\"')
045: break;
046: --pos;
047: }
048:
049: if (c != '\"')
050: return false;
051:
052: startPos = pos;
053:
054: pos = caretPos;
055: int length = doc.getLength();
056: c = ' ';
057:
058: while (pos < length) {
059: c = doc.getChar(pos);
060: if (c == Character.LINE_SEPARATOR || c == '\"')
061: break;
062: ++pos;
063: }
064: if (c != '\"')
065: return false;
066:
067: endPos = pos;
068:
069: int offset = startPos + 1;
070: int len = endPos - offset;
071: fText.setSelectedRange(offset, len);
072: return true;
073: } catch (BadLocationException x) {
074: }
075:
076: return false;
077: }
078:
079: protected boolean selectWord(int caretPos) {
080:
081: IDocument doc = fText.getDocument();
082: int startPos, endPos;
083:
084: try {
085:
086: int pos = caretPos;
087: char c;
088:
089: while (pos >= 0) {
090: c = doc.getChar(pos);
091: if (!Character.isJavaIdentifierPart(c))
092: break;
093: --pos;
094: }
095:
096: startPos = pos;
097:
098: pos = caretPos;
099: int length = doc.getLength();
100:
101: while (pos < length) {
102: c = doc.getChar(pos);
103: if (!Character.isJavaIdentifierPart(c))
104: break;
105: ++pos;
106: }
107:
108: endPos = pos;
109: selectRange(startPos, endPos);
110: return true;
111:
112: } catch (BadLocationException x) {
113: }
114:
115: return false;
116: }
117:
118: private void selectRange(int startPos, int stopPos) {
119: int offset = startPos + 1;
120: int length = stopPos - offset;
121: fText.setSelectedRange(offset, length);
122: }
123: }
|