Added some more tests,a nd reformtted the code a little.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / text / TextDoubleClickStrategy.java
1 /*
2  * Copyright (c) 2002-2004 Widespace, OU and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://solareclipse.sourceforge.net/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Igor Malinin - initial contribution
10  * 
11  * $Id: TextDoubleClickStrategy.java,v 1.2 2006-10-21 23:13:54 pombredanne Exp $
12  */
13
14 package net.sourceforge.phpeclipse.ui.text;
15
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.ITextDoubleClickStrategy;
19 import org.eclipse.jface.text.ITextViewer;
20
21 /**
22  * @author Igor Malinin
23  */
24 public class TextDoubleClickStrategy implements ITextDoubleClickStrategy {
25
26         /*
27          * @see org.eclipse.jface.text.ITextDoubleClickStrategy#doubleClicked(ITextViewer)
28          */
29         public void doubleClicked(ITextViewer viewer) {
30                 int offset = viewer.getSelectedRange().x;
31                 if (offset < 0) {
32                         return;
33                 }
34
35                 selectWord(viewer, viewer.getDocument(), offset);
36         }
37
38         protected void selectWord(ITextViewer textViewer, IDocument document,
39                         int offset) {
40                 try {
41                         int start = offset;
42                         while (start >= 0) {
43                                 char c = document.getChar(start);
44
45                                 if (!Character.isUnicodeIdentifierPart(c)) {
46                                         break;
47                                 }
48
49                                 --start;
50                         }
51
52                         int length = document.getLength();
53
54                         int end = offset;
55                         while (end < length) {
56                                 char c = document.getChar(end);
57
58                                 if (!Character.isUnicodeIdentifierPart(c)) {
59                                         break;
60                                 }
61
62                                 ++end;
63                         }
64
65                         if (start == end) {
66                                 textViewer.setSelectedRange(start, 0);
67                         } else {
68                                 textViewer.setSelectedRange(start + 1, end - start - 1);
69                         }
70                 } catch (BadLocationException x) {
71                 }
72         }
73 }