001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso.editors.xml;
005:
006: import org.eclipse.jface.text.BadLocationException;
007: import org.eclipse.jface.text.IDocument;
008: import org.eclipse.jface.text.ITextDoubleClickStrategy;
009: import org.eclipse.jface.text.ITextViewer;
010:
011: public class XMLDoubleClickStrategy implements ITextDoubleClickStrategy {
012: protected ITextViewer fText;
013:
014: public void doubleClicked(ITextViewer part) {
015: int pos = part.getSelectedRange().x;
016:
017: if (pos < 0)
018: return;
019:
020: fText = part;
021:
022: if (!selectComment(pos)) {
023: selectWord(pos);
024: }
025: }
026:
027: protected boolean selectComment(int caretPos) {
028: IDocument doc = fText.getDocument();
029: int startPos, endPos;
030:
031: try {
032: int pos = caretPos;
033: char c = ' ';
034:
035: while (pos >= 0) {
036: c = doc.getChar(pos);
037: if (c == '\\') {
038: pos -= 2;
039: continue;
040: }
041: if (c == Character.LINE_SEPARATOR || c == '\"')
042: break;
043: --pos;
044: }
045:
046: if (c != '\"')
047: return false;
048:
049: startPos = pos;
050:
051: pos = caretPos;
052: int length = doc.getLength();
053: c = ' ';
054:
055: while (pos < length) {
056: c = doc.getChar(pos);
057: if (c == Character.LINE_SEPARATOR || c == '\"')
058: break;
059: ++pos;
060: }
061: if (c != '\"')
062: return false;
063:
064: endPos = pos;
065:
066: int offset = startPos + 1;
067: int len = endPos - offset;
068: fText.setSelectedRange(offset, len);
069: return true;
070: } catch (BadLocationException x) {/**/
071: }
072:
073: return false;
074: }
075:
076: protected boolean selectWord(int caretPos) {
077:
078: IDocument doc = fText.getDocument();
079: int startPos, endPos;
080:
081: try {
082:
083: int pos = caretPos;
084: char c;
085:
086: while (pos >= 0) {
087: c = doc.getChar(pos);
088: if (!Character.isJavaIdentifierPart(c))
089: break;
090: --pos;
091: }
092:
093: startPos = pos;
094:
095: pos = caretPos;
096: int length = doc.getLength();
097:
098: while (pos < length) {
099: c = doc.getChar(pos);
100: if (!Character.isJavaIdentifierPart(c))
101: break;
102: ++pos;
103: }
104:
105: endPos = pos;
106: selectRange(startPos, endPos);
107: return true;
108:
109: } catch (BadLocationException x) {/**/
110: }
111:
112: return false;
113: }
114:
115: private void selectRange(int startPos, int stopPos) {
116: int offset = startPos + 1;
117: int length = stopPos - offset;
118: fText.setSelectedRange(offset, length);
119: }
120: }
|