1 30 package com.genimen.djeneric.ui; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Color ; 34 import java.awt.Dimension ; 35 import java.awt.Rectangle ; 36 import java.awt.event.InputEvent ; 37 import java.awt.event.KeyEvent ; 38 import java.awt.event.MouseEvent ; 39 40 import javax.swing.BorderFactory ; 41 import javax.swing.JComponent ; 42 import javax.swing.JLabel ; 43 import javax.swing.JOptionPane ; 44 import javax.swing.JPanel ; 45 import javax.swing.JScrollPane ; 46 import javax.swing.JTextArea ; 47 import javax.swing.SwingConstants ; 48 import javax.swing.border.BevelBorder ; 49 import javax.swing.border.Border ; 50 import javax.swing.event.CaretEvent ; 51 import javax.swing.text.BadLocationException ; 52 import javax.swing.undo.UndoManager ; 53 54 import com.genimen.djeneric.language.Messages; 55 import com.genimen.djeneric.util.DjLogger; 56 import com.genimen.djeneric.util.DjStringReplacer; 57 58 public class DjCodeEditor extends JPanel ![JavaDoc](../../../../../cmn/javadoc.gif) 59 { 60 private static final long serialVersionUID = 1L; 61 public static final String SEPARATOR = " \t\n\r,.!*()-+=[]:;\"<>/"; 62 final UndoManager _undomanager = new UndoManager (); 63 64 BorderLayout borderLayout1 = new BorderLayout (); 65 JTextArea _editor = new JTextArea (); 66 JPanel jPanel1 = new JPanel (); 67 JPanel jPanel2 = new JPanel (); 68 BorderLayout borderLayout2 = new BorderLayout (); 69 Border border1; 70 JLabel _lblRow = new JLabel (); 71 JLabel jLabel1 = new JLabel (); 72 JLabel _lblColumn = new JLabel (); 73 JScrollPane jScrollPane1 = new JScrollPane (); 74 JPanel _buttonPanel = new JPanel (); 75 BorderLayout borderLayout3 = new BorderLayout (); 76 JLabel _lblStatus = new JLabel (); 77 Color _normalColor = _lblStatus.getForeground(); 78 String _orgStatusMessage = ""; 79 80 public DjCodeEditor() 81 { 82 try 83 { 84 jbInit(); 85 getEditor().addKeyListener(new CodeEditor__editor_keyAdapter(this)); 86 getEditor().getDocument().addUndoableEditListener(this._undomanager); 87 _undomanager.setLimit(50000); 88 89 } 90 catch (Exception e) 91 { 92 DjLogger.log(e); 93 } 94 } 95 96 public UndoManager getUndomanager() 97 { 98 return _undomanager; 99 } 100 101 public void clear() 102 { 103 _editor.setText(""); 104 } 105 106 public Object getDisplayedValue() 107 { 108 return _editor.getText(); 109 } 110 111 public JComponent getFocussableComponent() 112 { 113 return _editor; 114 } 115 116 public JTextArea getEditor() 117 { 118 return _editor; 119 } 120 121 protected JPanel getButtonPanel() 122 { 123 return _buttonPanel; 124 } 125 126 private void jbInit() throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 127 { 128 _editor.setFont(new java.awt.Font ("Monospaced", 0, 12)); 129 _editor.addCaretListener(new CodeEditor__editor_caretAdapter(this)); 130 border1 = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.white, new Color (109, 109, 110), 131 new Color (156, 156, 158)); 132 this.setLayout(borderLayout1); 133 jPanel1.setLayout(borderLayout2); 134 jPanel2.setBorder(border1); 135 jPanel2.setPreferredSize(new Dimension (70, 21)); 136 jPanel2.setLayout(null); 137 _lblRow.setHorizontalAlignment(SwingConstants.TRAILING); 138 _lblRow.setText("1"); 139 _lblRow.setBounds(new Rectangle (-1, 4, 30, 15)); 140 jLabel1.setText(":"); 141 jLabel1.setBounds(new Rectangle (33, 4, 3, 15)); 142 _lblColumn.setText("1"); 143 _lblColumn.setBounds(new Rectangle (42, 4, 30, 15)); 144 _buttonPanel.setLayout(borderLayout3); 145 _lblStatus.setText(""); 146 _lblStatus.addMouseListener(new DjCodeEditor__lblStatus_mouseAdapter(this)); 147 this.add(jPanel1, BorderLayout.SOUTH); 148 jPanel1.add(jPanel2, BorderLayout.EAST); 149 jPanel2.add(_lblColumn, null); 150 jPanel2.add(_lblRow, null); 151 jPanel2.add(jLabel1, null); 152 jPanel1.add(_buttonPanel, BorderLayout.CENTER); 153 _buttonPanel.add(_lblStatus, BorderLayout.WEST); 154 this.add(jScrollPane1, BorderLayout.CENTER); 155 jScrollPane1.getViewport().add(_editor, null); 156 } 157 158 void _editor_caretUpdate(CaretEvent e) 159 { 160 int idx = e.getDot(); 161 try 162 { 163 int line = _editor.getLineOfOffset(idx); 164 int column = idx - _editor.getLineStartOffset(line); 165 _lblRow.setText(String.valueOf(line + 1)); 166 _lblColumn.setText(String.valueOf(column + 1)); 167 } 168 catch (BadLocationException e1) 169 { 170 DjLogger.log(e1); 171 } 172 } 173 174 private void wordRight(boolean keepSelection) 175 { 176 String code = getEditor().getText(); 177 int idx = getEditor().getCaretPosition(); 178 int orgIdx = getEditor().getSelectionStart(); 179 180 if (idx >= code.length() - 1) return; 181 182 if (SEPARATOR.indexOf(code.charAt(idx)) != -1) 183 { 184 idx = skipSeparators(code, idx); 185 } 186 else 187 { 188 idx = skipCode(code, idx); 189 } 190 getEditor().setCaretPosition(idx); 191 if (keepSelection) 192 { 193 getEditor().setSelectionStart(orgIdx); 194 getEditor().setSelectionEnd(idx); 195 } 196 } 197 198 private int skipCode(String code, int idx) 199 { 200 201 while (idx < code.length() - 1 && SEPARATOR.indexOf(code.charAt(idx)) == -1) 202 idx++; 203 return idx; 204 } 205 206 private int skipSeparators(String code, int idx) 207 { 208 while (idx < code.length() - 1 && SEPARATOR.indexOf(code.charAt(idx)) != -1) 209 idx++; 210 return idx; 211 } 212 213 private void wordLeft(boolean keepSelection) 214 { 215 String code = getEditor().getText(); 216 int idx = getEditor().getCaretPosition() - 1; 217 if (idx < 0) return; 218 219 int orgIdx = getEditor().getSelectionStart(); 220 221 if (SEPARATOR.indexOf(code.charAt(idx)) != -1) 222 { 223 idx = backupSeparators(code, idx); 224 } 225 else 226 { 227 idx = backupCode(code, idx); 228 } 229 getEditor().setCaretPosition(idx); 230 if (keepSelection) 231 { 232 getEditor().setSelectionStart(orgIdx); 233 getEditor().setSelectionEnd(idx); 234 } 235 } 236 237 private int backupCode(String code, int idx) 238 { 239 240 while (idx > 0 && SEPARATOR.indexOf(code.charAt(idx)) == -1) 241 idx--; 242 if (SEPARATOR.indexOf(code.charAt(idx)) != -1) idx++; 243 return idx; 244 } 245 246 private int backupSeparators(String code, int idx) 247 { 248 while (idx > 0 && SEPARATOR.indexOf(code.charAt(idx)) != -1) 249 idx--; 250 if (SEPARATOR.indexOf(code.charAt(idx)) == -1) idx++; 251 return idx; 252 } 253 254 protected void editorKeypressed(KeyEvent e) 255 { 256 if ((e.getKeyCode() == KeyEvent.VK_RIGHT) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) 257 { 258 wordRight(e.isShiftDown()); 259 e.consume(); 260 } 261 if ((e.getKeyCode() == KeyEvent.VK_LEFT) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0)) 262 { 263 wordLeft(e.isShiftDown()); 264 e.consume(); 265 } 266 if (e.getKeyCode() == KeyEvent.VK_Z) 267 { 268 if (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK) 269 { 270 if (_undomanager.canUndo()) 271 { 272 _undomanager.undo(); 273 } 274 275 } 276 } 277 if (e.getKeyCode() == KeyEvent.VK_Y) 278 { 279 if (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK) 280 { 281 if (_undomanager.canRedo()) 282 { 283 _undomanager.redo(); 284 } 285 286 } 287 } 288 } 289 290 public String getText() 291 { 292 return _editor.getText(); 293 } 294 295 public void setText(String t) 296 { 297 _editor.setText(t); 298 getUndomanager().discardAllEdits(); 299 } 300 301 public void setCaretPosition(int position) 302 { 303 _editor.setCaretPosition(position); 304 } 305 306 public int getLineEndOffset(int line) throws BadLocationException ![JavaDoc](../../../../../cmn/javadoc.gif) 307 { 308 return _editor.getLineEndOffset(line); 309 } 310 311 public int getLineOfOffset(int offset) throws BadLocationException ![JavaDoc](../../../../../cmn/javadoc.gif) 312 { 313 return _editor.getLineOfOffset(offset); 314 } 315 316 public int getLineStartOffset(int line) throws BadLocationException ![JavaDoc](../../../../../cmn/javadoc.gif) 317 { 318 return _editor.getLineStartOffset(line); 319 } 320 321 public int getCaretPosition() 322 { 323 return _editor.getCaretPosition(); 324 } 325 326 public void insert(String str, int pos) 327 { 328 _editor.insert(str, pos); 329 } 330 331 public void setStatusMessage(String msg) 332 { 333 setStatusMessage(msg, true); 334 } 335 336 public void setStatusMessage(String msg, boolean informative) 337 { 338 if (msg == null) msg = Messages.getString("global.Nullpointer"); 339 _orgStatusMessage = msg; 340 341 if (!informative) _lblStatus.setForeground(Color.red); 342 else _lblStatus.setForeground(_normalColor); 343 344 DjStringReplacer sr = new DjStringReplacer(msg); 345 sr.replace("\n", " "); 346 sr.replace("\r", ""); 347 sr.replace(" ", " "); 348 _lblStatus.setText(sr.toString()); 349 } 350 351 void _lblStatus_mouseClicked(MouseEvent e) 352 { 353 if (e.getClickCount() > 1) 354 { 355 JOptionPane.showMessageDialog(this, _orgStatusMessage); 356 } 357 } 358 359 public void requestFocus() 360 { 361 _editor.requestFocus(); 362 } 363 364 public DjEditorPositionInfo getEditorPositionInfo() 365 { 366 return new DjEditorPositionInfo(getEditor(), false); 367 } 368 369 public int getCaretLine() throws BadLocationException ![JavaDoc](../../../../../cmn/javadoc.gif) 370 { 371 return _editor.getLineOfOffset(_editor.getCaretPosition()); 372 } 373 374 public int getCaretColumn() throws BadLocationException ![JavaDoc](../../../../../cmn/javadoc.gif) 375 { 376 int idx = _editor.getCaretPosition(); 377 int line = _editor.getLineOfOffset(idx); 378 return idx - _editor.getLineStartOffset(line); 379 } 380 381 public void setStatusMessage(Throwable t) 382 { 383 DjLogger.log(t); 384 setStatusMessage(t.getMessage(), false); 385 } 386 387 } 388 389 class CodeEditor__editor_caretAdapter implements javax.swing.event.CaretListener ![JavaDoc](../../../../../cmn/javadoc.gif) 390 { 391 DjCodeEditor adaptee; 392 393 CodeEditor__editor_caretAdapter(DjCodeEditor adaptee) 394 { 395 this.adaptee = adaptee; 396 } 397 398 public void caretUpdate(CaretEvent e) 399 { 400 adaptee._editor_caretUpdate(e); 401 } 402 } 403 404 class CodeEditor__editor_keyAdapter extends java.awt.event.KeyAdapter ![JavaDoc](../../../../../cmn/javadoc.gif) 405 { 406 DjCodeEditor adaptee; 407 408 CodeEditor__editor_keyAdapter(DjCodeEditor adaptee) 409 { 410 this.adaptee = adaptee; 411 } 412 413 public void keyPressed(KeyEvent e) 414 { 415 adaptee.editorKeypressed(e); 416 } 417 } 418 419 class DjCodeEditor__lblStatus_mouseAdapter extends java.awt.event.MouseAdapter ![JavaDoc](../../../../../cmn/javadoc.gif) 420 { 421 DjCodeEditor adaptee; 422 423 DjCodeEditor__lblStatus_mouseAdapter(DjCodeEditor adaptee) 424 { 425 this.adaptee = adaptee; 426 } 427 428 public void mouseClicked(MouseEvent e) 429 { 430 adaptee._lblStatus_mouseClicked(e); 431 } 432 } | Popular Tags |