3e70d8f5bf479f3b1fbfe743fc8b9c8d14eb5ddd
[phpeclipse.git] /
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://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     Igor Malinin - initial contribution
10  *
11  * $Id: TagDoubleClickStrategy.java,v 1.3 2006-10-21 23:14:13 pombredanne Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.ui.internal.text;
15
16 import net.sourceforge.phpeclipse.ui.text.TextDoubleClickStrategy;
17
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.ITextViewer;
21 import org.eclipse.jface.text.ITypedRegion;
22
23 /**
24  * 
25  * 
26  * @author Igor Malinin
27  */
28 public class TagDoubleClickStrategy extends TextDoubleClickStrategy {
29         /*
30          * @see org.eclipse.jface.text.ITextDoubleClickStrategy#doubleClicked(ITextViewer)
31          */
32         public void doubleClicked(ITextViewer viewer) {
33                 int offset = viewer.getSelectedRange().x;
34                 if (offset < 0) {
35                         return;
36                 }
37
38                 try {
39                         IDocument document = viewer.getDocument();
40
41                         ITypedRegion region = document.getPartition(offset);
42
43                         int start = region.getOffset();
44
45                         if (offset == start && document.getChar(offset) == '<') {
46                                 region = document.getPartition(offset);
47                                 offset = region.getOffset() + region.getLength();
48
49                                 if (document.getChar(offset - 1) != '>') {
50                                         while (true) {
51                                                 if (offset >= document.getLength()) {
52                                                         break;
53                                                 }
54
55                                                 region = document.getPartition(offset);
56                                                 offset = region.getOffset() + region.getLength();
57
58                                                 if (XMLPartitionScanner.XML_ATTRIBUTE.equals(region
59                                                                 .getType())) {
60                                                         continue;
61                                                 }
62
63                                                 if (XMLPartitionScanner.XML_TAG
64                                                                 .equals(region.getType())) {
65                                                         if (document.getChar(region.getOffset()) == '<') {
66                                                                 break;
67                                                         }
68
69                                                         if (document.getChar(offset - 1) == '>') {
70                                                                 break;
71                                                         }
72
73                                                         continue;
74                                                 }
75
76                                                 offset = region.getOffset();
77                                                 break;
78                                         }
79                                 }
80
81                                 viewer.setSelectedRange(start, offset - start);
82                                 return;
83                         }
84
85                         int end = start + region.getLength();
86
87                         if (offset == end - 1 && document.getChar(offset) == '>') {
88                                 region = document.getPartition(offset);
89                                 offset = region.getOffset();
90
91                                 if (document.getChar(offset) != '<') {
92                                         while (true) {
93                                                 if (offset <= 0) {
94                                                         break;
95                                                 }
96
97                                                 region = document.getPartition(offset - 1);
98                                                 offset = region.getOffset();
99
100                                                 if (XMLPartitionScanner.XML_ATTRIBUTE.equals(region
101                                                                 .getType())) {
102                                                         continue;
103                                                 }
104
105                                                 if (XMLPartitionScanner.XML_TAG
106                                                                 .equals(region.getType())) {
107                                                         if (document.getChar(offset) == '<') {
108                                                                 break;
109                                                         }
110
111                                                         continue;
112                                                 }
113
114                                                 offset += region.getLength();
115                                                 break;
116                                         }
117                                 }
118
119                                 viewer.setSelectedRange(offset, end - offset);
120                                 return;
121                         }
122
123                         super.doubleClicked(viewer);
124                 } catch (BadLocationException e) {
125                 }
126         }
127 }