KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > JavaStringDoubleClickSelector


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.ui.text.java;
13
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.jface.text.ITextViewer;
18 import org.eclipse.jface.text.TextUtilities;
19
20 /**
21  * Double click strategy aware of Java string and character syntax rules.
22  */

23 public class JavaStringDoubleClickSelector extends JavaDoubleClickSelector {
24
25     private String JavaDoc fPartitioning;
26
27     /**
28      * Creates a new Java string double click selector for the given document partitioning.
29      *
30      * @param partitioning the document partitioning
31      */

32     public JavaStringDoubleClickSelector(String JavaDoc partitioning) {
33         super();
34         fPartitioning= partitioning;
35     }
36
37     /*
38      * @see ITextDoubleClickStrategy#doubleClicked(ITextViewer)
39      */

40     public void doubleClicked(ITextViewer textViewer) {
41
42         int offset= textViewer.getSelectedRange().x;
43
44         if (offset < 0)
45             return;
46
47         IDocument document= textViewer.getDocument();
48
49         IRegion region= match(document, offset);
50         if (region != null && region.getLength() >= 2) {
51             textViewer.setSelectedRange(region.getOffset() + 1, region.getLength() - 2);
52         } else {
53             region= selectWord(document, offset);
54             textViewer.setSelectedRange(region.getOffset(), region.getLength());
55         }
56     }
57
58     private IRegion match(IDocument document, int offset) {
59         try {
60             if ((document.getChar(offset) == '"') || (document.getChar(offset) == '\'') ||
61                 (document.getChar(offset - 1) == '"') || (document.getChar(offset - 1) == '\''))
62             {
63                 return TextUtilities.getPartition(document, fPartitioning, offset, true);
64             }
65         } catch (BadLocationException e) {
66         }
67
68         return null;
69     }
70 }
71
Popular Tags