1 23 24 package org.gjt.sp.jedit.textarea; 25 26 import java.awt.*; 28 import java.awt.event.MouseEvent ; 29 30 import org.gjt.sp.jedit.*; 31 32 import javax.swing.*; 33 35 47 public class JEditTextArea extends TextArea 48 { 49 53 public JEditTextArea(View view) 54 { 55 super(view); 56 enableEvents(AWTEvent.FOCUS_EVENT_MASK | AWTEvent.KEY_EVENT_MASK); 57 popupEnabled = true; 58 this.view = view; 59 } 61 68 public void smartHome(boolean select) 69 { 70 Macros.Recorder recorder = view.getMacroRecorder(); 71 72 switch(view.getInputHandler().getLastActionCount()) 73 { 74 case 1: 75 if(recorder != null) 76 recorder.record("textArea.goToStartOfWhiteSpace(" + select + ");"); 77 78 goToStartOfWhiteSpace(select); 79 break; 80 case 2: 81 if(recorder != null) 82 recorder.record("textArea.goToStartOfLine(" + select + ");"); 83 84 goToStartOfLine(select); 85 break; 86 default: if(recorder != null) 88 recorder.record("textArea.goToFirstVisibleLine(" + select + ");"); 89 90 goToFirstVisibleLine(select); 91 break; 92 } 93 } 95 102 public void smartEnd(boolean select) 103 { 104 Macros.Recorder recorder = view.getMacroRecorder(); 105 106 switch(view.getInputHandler().getLastActionCount()) 107 { 108 case 1: 109 if(recorder != null) 110 recorder.record("textArea.goToEndOfWhiteSpace(" + select + ");"); 111 112 goToEndOfWhiteSpace(select); 113 break; 114 case 2: 115 if(recorder != null) 116 recorder.record("textArea.goToEndOfLine(" + select + ");"); 117 118 goToEndOfLine(select); 119 break; 120 default: if(recorder != null) 122 recorder.record("textArea.goToLastVisibleLine(" + select + ");"); 123 goToLastVisibleLine(select); 124 break; 125 } 126 } 128 134 public void showGoToLineDialog() 135 { 136 String line = GUIUtilities.input(view,"goto-line",null); 137 if(line == null) 138 return; 139 140 try 141 { 142 int lineNumber = Integer.parseInt(line) - 1; 143 setCaretPosition(getLineStartOffset(lineNumber)); 144 } 145 catch(Exception e) 146 { 147 getToolkit().beep(); 148 } 149 } 151 168 public void userInput(char ch) 169 { 170 if(!isEditable()) 171 { 172 getToolkit().beep(); 173 return; 174 } 175 176 177 if(hiddenCursor != null) 178 getPainter().setCursor(hiddenCursor); 179 180 if(ch == ' ' && Abbrevs.getExpandOnInput() 181 && Abbrevs.expandAbbrev(view,false)) 182 return; 183 184 if(ch == '\t') 185 userInputTab(); 186 else 187 { 188 boolean indent = buffer.isElectricKey(ch, caretLine); 189 String str = String.valueOf(ch); 190 if(getSelectionCount() == 0) 191 { 192 if(!doWordWrap(ch == ' ')) 193 insert(str,indent); 194 } 195 else 196 replaceSelection(str); 197 } 198 } 200 205 public void addExplicitFold() 206 { 207 try 208 { 209 super.addExplicitFold(); 210 } 211 catch (TextAreaException e) 212 { 213 GUIUtilities.error(view,"folding-not-explicit",null); 214 } 215 } 217 222 public void formatParagraph() 223 { 224 try 225 { 226 super.formatParagraph(); 227 } 228 catch (TextAreaException e) 229 { 230 GUIUtilities.error(view,"format-maxlinelen",null); 231 } 232 } 234 protected static void doWordCount(View view, String text) 236 { 237 char[] chars = text.toCharArray(); 238 int characters = chars.length; 239 int words = 0; 240 int lines = 1; 241 242 boolean word = true; 243 for(int i = 0; i < chars.length; i++) 244 { 245 switch(chars[i]) 246 { 247 case '\r': case '\n': 248 lines++; 249 case ' ': case '\t': 250 word = true; 251 break; 252 default: 253 if(word) 254 { 255 words++; 256 word = false; 257 } 258 break; 259 } 260 } 261 262 Object [] args = { characters, words, lines }; 263 GUIUtilities.message(view,"wordcount",args); 264 } 266 271 public void showWordCountDialog() 272 { 273 String selection = getSelectedText(); 274 if(selection != null) 275 { 276 doWordCount(view,selection); 277 return; 278 } 279 280 doWordCount(view,buffer.getText(0,buffer.getLength())); 281 } 283 285 290 public View getView() 291 { 292 return view; 293 } 295 297 299 private View view; 301 private JPopupMenu popup; 302 private boolean popupEnabled; 303 306 312 public boolean isRightClickPopupEnabled() 313 { 314 return popupEnabled; 315 } 317 323 public void setRightClickPopupEnabled(boolean popupEnabled) 324 { 325 this.popupEnabled = popupEnabled; 326 } 328 332 public final JPopupMenu getRightClickPopup() 333 { 334 return popup; 335 } 337 342 public final void setRightClickPopup(JPopupMenu popup) 343 { 344 this.popup = popup; 345 } 347 353 public void handlePopupTrigger(MouseEvent evt) 354 { 355 if(popup.isVisible()) 356 popup.setVisible(false); 357 else 358 { 359 int x = evt.getX(); 360 int y = evt.getY(); 361 362 int dragStart = xyToOffset(x,y, 363 !(painter.isBlockCaretEnabled() 364 || isOverwriteEnabled())); 365 366 if(getSelectionCount() == 0 || multi) 367 moveCaretPosition(dragStart,false); 368 GUIUtilities.showPopupMenu(popup,painter,x,y); 369 } 370 } } 372 | Popular Tags |