View Javadoc

1   /********************************************************************************
2    * Copyright (c) 2005 Prashant Deva.
3    
4    * All rights reserved. This program and the accompanying materials 
5    * are made available under the terms of the Eclipse Public License - v 1.0
6    * which is available at http://www.eclipse.org/legal/epl-v10.html
7    *******************************************************************************/
8   
9   package org.wsmostudio.grounding.sawsdl.ui.text;
10  
11  import org.eclipse.jface.text.*;
12  
13  public class XMLDoubleClickStrategy implements ITextDoubleClickStrategy {
14      protected ITextViewer fText;
15  
16      public void doubleClicked(ITextViewer part) {
17          int pos = part.getSelectedRange().x;
18  
19          if (pos < 0)
20              return;
21  
22          fText = part;
23  
24          if (!selectComment(pos)) {
25              selectWord(pos);
26          }
27      }
28  
29      protected boolean selectComment(int caretPos) {
30          IDocument doc = fText.getDocument();
31          int startPos, endPos;
32  
33          try {
34              int pos = caretPos;
35              char c = ' ';
36  
37              while (pos >= 0) {
38                  c = doc.getChar(pos);
39                  if (c == '//') {
40                      pos -= 2;
41                      continue;
42                  }
43                  if (c == Character.LINE_SEPARATOR || c == '\"')
44                      break;
45                  --pos;
46              }
47  
48              if (c != '\"')
49                  return false;
50  
51              startPos = pos;
52  
53              pos = caretPos;
54              int length = doc.getLength();
55              c = ' ';
56  
57              while (pos < length) {
58                  c = doc.getChar(pos);
59                  if (c == Character.LINE_SEPARATOR || c == '\"')
60                      break;
61                  ++pos;
62              }
63              if (c != '\"')
64                  return false;
65  
66              endPos = pos;
67  
68              int offset = startPos + 1;
69              int len = endPos - offset;
70              fText.setSelectedRange(offset, len);
71              return true;
72          }
73          catch (BadLocationException x) {
74          }
75  
76          return false;
77      }
78  
79      protected boolean selectWord(int caretPos) {
80  
81          IDocument doc = fText.getDocument();
82          int startPos, endPos;
83  
84          try {
85  
86              int pos = caretPos;
87              char c;
88  
89              while (pos >= 0) {
90                  c = doc.getChar(pos);
91                  if (!Character.isJavaIdentifierPart(c))
92                      break;
93                  --pos;
94              }
95  
96              startPos = pos;
97  
98              pos = caretPos;
99              int length = doc.getLength();
100 
101             while (pos < length) {
102                 c = doc.getChar(pos);
103                 if (!Character.isJavaIdentifierPart(c))
104                     break;
105                 ++pos;
106             }
107 
108             endPos = pos;
109             selectRange(startPos, endPos);
110             return true;
111 
112         }
113         catch (BadLocationException x) {
114         }
115 
116         return false;
117     }
118 
119     private void selectRange(int startPos, int stopPos) {
120         int offset = startPos + 1;
121         int length = stopPos - offset;
122         fText.setSelectedRange(offset, length);
123     }
124 }
125 
126 /*
127  * $Log$
128  * Revision 1.2  2006/10/27 08:41:08  alex_simov
129  * no message
130  * Revision 1.1 2006/10/26 13:55:07
131  * alex_simov SA-WSDL Text Editor with syntax highlighting added
132  * 
133  * 
134  */