sysmodelmgr/com.symbian.smt.gui/src/com/symbian/smt/gui/editors/xmleditor/XMLDoubleClickStrategy.java
changeset 0 522a326673b6
equal deleted inserted replaced
-1:000000000000 0:522a326673b6
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 package com.symbian.smt.gui.editors.xmleditor;
       
    19 
       
    20 import org.eclipse.jface.text.BadLocationException;
       
    21 import org.eclipse.jface.text.IDocument;
       
    22 import org.eclipse.jface.text.ITextDoubleClickStrategy;
       
    23 import org.eclipse.jface.text.ITextViewer;
       
    24 
       
    25 /**
       
    26  * @author barbararosi-schwartz
       
    27  * 
       
    28  */
       
    29 public class XMLDoubleClickStrategy implements ITextDoubleClickStrategy {
       
    30 	protected ITextViewer fText;
       
    31 
       
    32 	public void doubleClicked(ITextViewer part) {
       
    33 		int pos = part.getSelectedRange().x;
       
    34 
       
    35 		if (pos < 0)
       
    36 			return;
       
    37 
       
    38 		fText = part;
       
    39 
       
    40 		if (!selectComment(pos)) {
       
    41 			selectWord(pos);
       
    42 		}
       
    43 	}
       
    44 
       
    45 	protected boolean selectComment(int caretPos) {
       
    46 		IDocument doc = fText.getDocument();
       
    47 		int startPos, endPos;
       
    48 
       
    49 		try {
       
    50 			int pos = caretPos;
       
    51 			char c = ' ';
       
    52 
       
    53 			while (pos >= 0) {
       
    54 				c = doc.getChar(pos);
       
    55 				if (c == '\\') {
       
    56 					pos -= 2;
       
    57 					continue;
       
    58 				}
       
    59 				if (c == Character.LINE_SEPARATOR || c == '\"')
       
    60 					break;
       
    61 				--pos;
       
    62 			}
       
    63 
       
    64 			if (c != '\"')
       
    65 				return false;
       
    66 
       
    67 			startPos = pos;
       
    68 
       
    69 			pos = caretPos;
       
    70 			int length = doc.getLength();
       
    71 			c = ' ';
       
    72 
       
    73 			while (pos < length) {
       
    74 				c = doc.getChar(pos);
       
    75 				if (c == Character.LINE_SEPARATOR || c == '\"')
       
    76 					break;
       
    77 				++pos;
       
    78 			}
       
    79 			if (c != '\"')
       
    80 				return false;
       
    81 
       
    82 			endPos = pos;
       
    83 
       
    84 			int offset = startPos + 1;
       
    85 			int len = endPos - offset;
       
    86 			fText.setSelectedRange(offset, len);
       
    87 			return true;
       
    88 		} catch (BadLocationException x) {
       
    89 		}
       
    90 
       
    91 		return false;
       
    92 	}
       
    93 
       
    94 	private void selectRange(int startPos, int stopPos) {
       
    95 		int offset = startPos + 1;
       
    96 		int length = stopPos - offset;
       
    97 		fText.setSelectedRange(offset, length);
       
    98 	}
       
    99 
       
   100 	protected boolean selectWord(int caretPos) {
       
   101 
       
   102 		IDocument doc = fText.getDocument();
       
   103 		int startPos, endPos;
       
   104 
       
   105 		try {
       
   106 
       
   107 			int pos = caretPos;
       
   108 			char c;
       
   109 
       
   110 			while (pos >= 0) {
       
   111 				c = doc.getChar(pos);
       
   112 				if (!Character.isJavaIdentifierPart(c))
       
   113 					break;
       
   114 				--pos;
       
   115 			}
       
   116 
       
   117 			startPos = pos;
       
   118 
       
   119 			pos = caretPos;
       
   120 			int length = doc.getLength();
       
   121 
       
   122 			while (pos < length) {
       
   123 				c = doc.getChar(pos);
       
   124 				if (!Character.isJavaIdentifierPart(c))
       
   125 					break;
       
   126 				++pos;
       
   127 			}
       
   128 
       
   129 			endPos = pos;
       
   130 			selectRange(startPos, endPos);
       
   131 			return true;
       
   132 
       
   133 		} catch (BadLocationException x) {
       
   134 		}
       
   135 
       
   136 		return false;
       
   137 	}
       
   138 }