KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > DjEditorPositionInfo


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.ui;
31
32 import javax.swing.JTextArea JavaDoc;
33 import javax.swing.text.JTextComponent JavaDoc;
34
35 /**
36  * @author Wido Riezebos @created 27 mei 2002
37  */

38 public class DjEditorPositionInfo
39 {
40   private String JavaDoc SKIPCHARS_WOPARAMS = "!*^&{}|<>()-+=[];,/ \t\n\r+-\"'";
41   private String JavaDoc _skipchars;
42   private String JavaDoc _skipchars2;
43
44   JTextComponent JavaDoc _editor;
45   private String JavaDoc _word = "";
46   private String JavaDoc _prefix = "";
47   private int _start = 0;
48   private int _end = 0;
49   private int _lineStart = 0;
50   private boolean _supportParameters = false;
51
52   public DjEditorPositionInfo(JTextComponent JavaDoc editor, boolean supportParameters)
53   {
54     _supportParameters = supportParameters;
55     initSkipChars();
56     _editor = editor;
57     String JavaDoc code = editor.getText();
58
59     _end = editor.getCaretPosition();
60     _start = _end;
61
62     if (code.length() == 0) return;
63
64     if (_end > code.length()) _end = code.length();
65     _start = _end;
66
67     // The char at the position before the cursor determines how to handle; so
68
if (_start > 0) _start--;
69
70     // If we are not within a word then don't look any further
71
if (_skipchars.indexOf(code.charAt(_start)) != -1)
72     {
73       _start = _end;
74       _word = "";
75       determinePrefix(code);
76       return;
77     }
78
79     // We are at the end or within a word.
80
// We need to go back to the start of the word
81
while (_start > 0 && _skipchars.indexOf(code.charAt(_start)) == -1)
82       _start--;
83     // We are probably before the word. This is true if we are now at a
84
// skippable
85
// Set the index at the start of the word then
86
if (_skipchars.indexOf(code.charAt(_start)) != -1) _start++;
87
88     // Some fail-over checks:
89
if (_start == -1) _start = 0;
90     if (_end == -1) _end = _start;
91
92     if (_end > _start) _word = code.substring(_start, _end).trim();
93     else _word = "";
94
95     if (_word.length() == 0) _start = _end;
96
97     // Now move to the end of the word (if we were in the middle of it)
98
// so we will replace the full word later on, not just from start to
99
// cursor.
100
while (_end < code.length() - 1 && _skipchars2.indexOf(code.charAt(_end)) == -1)
101       _end++;
102
103     determinePrefix(code);
104   }
105
106   private void initSkipChars()
107   {
108     _skipchars = SKIPCHARS_WOPARAMS;
109     if (!_supportParameters) _skipchars += ":";
110     _skipchars2 = _skipchars + ".";
111   }
112
113   private void determinePrefix(String JavaDoc code)
114   {
115     _lineStart = _start;
116     if (_lineStart > 0) _lineStart--;
117     while (_lineStart > 0 && code.charAt(_lineStart) != '\n')
118       _lineStart--;
119     if (code.charAt(_lineStart) == '\n') _lineStart++;
120     if (_lineStart > _start) _lineStart = _start;
121     _prefix = code.substring(_lineStart, _start);
122   }
123
124   public void replaceRange(String JavaDoc value, int caretOffset)
125   {
126     if (_editor instanceof JTextArea JavaDoc)
127     {
128       JTextArea JavaDoc ta = (JTextArea JavaDoc) _editor;
129       ta.replaceRange(value, _start, _end);
130     }
131     else
132     {
133       _editor.setSelectionStart(_start);
134       _editor.setSelectionEnd(_end);
135       _editor.replaceSelection(value);
136     }
137     _editor.setCaretPosition(_start + caretOffset);
138   }
139
140   public void replaceRange(String JavaDoc value)
141   {
142     replaceRange(value, value.length());
143   }
144
145   public int getEnd()
146   {
147     return _end;
148   }
149
150   public int getStart()
151   {
152     return _start;
153   }
154
155   public String JavaDoc getWord()
156   {
157     return _word;
158   }
159
160   public int getLineStart()
161   {
162     return _lineStart;
163   }
164
165   public JTextComponent JavaDoc getEditor()
166   {
167     return _editor;
168   }
169
170   public void setStart(int start)
171   {
172     _start = start;
173   }
174
175   public String JavaDoc getPrefix()
176   {
177     return _prefix;
178   }
179
180   public String JavaDoc toString()
181   {
182     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
183     if (_word != null)
184     {
185       sb.append(_word);
186       if (_start <= _word.length() && _end <= _word.length())
187       {
188         sb.append("\nSelected: [" + _word.substring(_start, _end) + "] = " + (_end - _start) + " chars");
189       }
190       else sb.append("\nNo selection");
191     }
192     return sb.toString();
193   }
194 }
Popular Tags