1 30 package com.genimen.djeneric.ui; 31 32 import javax.swing.JTextArea ; 33 import javax.swing.text.JTextComponent ; 34 35 38 public class DjEditorPositionInfo 39 { 40 private String SKIPCHARS_WOPARAMS = "!*^&{}|<>()-+=[];,/ \t\n\r+-\"'"; 41 private String _skipchars; 42 private String _skipchars2; 43 44 JTextComponent _editor; 45 private String _word = ""; 46 private String _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 editor, boolean supportParameters) 53 { 54 _supportParameters = supportParameters; 55 initSkipChars(); 56 _editor = editor; 57 String 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 if (_start > 0) _start--; 69 70 if (_skipchars.indexOf(code.charAt(_start)) != -1) 72 { 73 _start = _end; 74 _word = ""; 75 determinePrefix(code); 76 return; 77 } 78 79 while (_start > 0 && _skipchars.indexOf(code.charAt(_start)) == -1) 82 _start--; 83 if (_skipchars.indexOf(code.charAt(_start)) != -1) _start++; 87 88 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 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 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 value, int caretOffset) 125 { 126 if (_editor instanceof JTextArea ) 127 { 128 JTextArea ta = (JTextArea ) _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 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 getWord() 156 { 157 return _word; 158 } 159 160 public int getLineStart() 161 { 162 return _lineStart; 163 } 164 165 public JTextComponent getEditor() 166 { 167 return _editor; 168 } 169 170 public void setStart(int start) 171 { 172 _start = start; 173 } 174 175 public String getPrefix() 176 { 177 return _prefix; 178 } 179 180 public String toString() 181 { 182 StringBuffer sb = new StringBuffer (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 |