1 5 package net.sf.panoptes.view.swing.editors; 6 7 import java.awt.BorderLayout ; 8 import java.awt.Dimension ; 9 import java.awt.event.ActionEvent ; 10 import java.awt.event.KeyEvent ; 11 import java.io.BufferedReader ; 12 import java.io.BufferedWriter ; 13 import java.io.FileReader ; 14 import java.io.FileWriter ; 15 import java.io.IOException ; 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 19 import javax.swing.AbstractAction ; 20 import javax.swing.Action ; 21 import javax.swing.BorderFactory ; 22 import javax.swing.ImageIcon ; 23 import javax.swing.JButton ; 24 import javax.swing.JInternalFrame ; 25 import javax.swing.JLabel ; 26 import javax.swing.JMenu ; 27 import javax.swing.JMenuBar ; 28 import javax.swing.JPanel ; 29 import javax.swing.JToolBar ; 30 import javax.swing.KeyStroke ; 31 import javax.swing.border.BevelBorder ; 32 import javax.swing.event.CaretEvent ; 33 import javax.swing.event.CaretListener ; 34 import javax.swing.event.UndoableEditEvent ; 35 import javax.swing.event.UndoableEditListener ; 36 import javax.swing.undo.UndoManager ; 37 38 import net.sf.panoptes.controller.actions.TaskAction; 39 40 import org.jedit.syntax.JEditTextArea; 41 import org.jedit.syntax.PythonTokenMarker; 42 43 49 public class ScriptEditor extends JInternalFrame { 50 51 JEditTextArea editor; 52 JToolBar toolBar; 53 JPanel statusBar; 54 JMenuBar menuBar; 55 JLabel caretStatus; 56 JLabel filenameStatus; 57 private String scriptName = null; 58 private ArrayList listeners = new ArrayList (); 59 60 private Action saveAsAction; 61 private Action saveAction; 62 private Action closeAction; 63 private Action cutAction; 64 private Action pasteAction; 65 private Action copyAction; 66 private Action undoAction; 67 private Action redoAction; 68 69 private UndoManager undoManager = new UndoManager (); 70 private UndoableEditListener undoHandler = new UndoHandler(); 71 72 JPanel contentPane; 73 74 public ScriptEditor() { 75 super("Script Editor", true, true, true); 76 contentPane = new JPanel (new BorderLayout ()); 77 78 createActions(); 79 80 createMenuBar(); 81 createToolBar(); 82 createStatusBar(); 83 createEditor(); 84 85 JPanel topPanel = new JPanel (new BorderLayout ()); 86 topPanel.add(toolBar, BorderLayout.SOUTH); 87 topPanel.add(menuBar, BorderLayout.NORTH); 88 89 contentPane.add(topPanel, BorderLayout.NORTH); 90 contentPane.add(editor, BorderLayout.CENTER); 91 contentPane.add(statusBar, BorderLayout.SOUTH); 92 93 setContentPane(contentPane); 94 } 95 96 public JPanel build() { 97 return contentPane; 98 } 99 100 public void addEditorListener(ScriptEditorListener l) { 101 listeners.add(l); 102 } 103 104 public void removeEditorListener(ScriptEditorListener l) { 105 listeners.remove(l); 106 } 107 108 private void createActions() { 109 ClassLoader cl = ScriptEditor.class.getClassLoader(); 110 111 114 saveAction = new TaskAction("Save script", "icons/save_edit.gif") { 115 public void doTask(ActionEvent e) throws Exception { 116 try { 117 saveFile(); 118 } catch (IOException ioe) { 119 throw new Exception ("Unable to save file", ioe); 120 } 121 122 } 123 }; 124 saveAction.putValue( 125 Action.ACCELERATOR_KEY, 126 KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK)); 127 128 131 saveAsAction = 132 new AbstractAction ( 133 "Save script as...", 134 new ImageIcon (cl.getResource("icons/saveas_edit.gif"))) { 135 public void actionPerformed(ActionEvent e) { 136 } 137 }; 138 139 142 undoAction = 143 new AbstractAction ("Undo", new ImageIcon (cl.getResource("icons/undo_edit.gif"))) { 144 public void actionPerformed(ActionEvent e) { 145 undoManager.undo(); 146 } 147 }; 148 undoAction.putValue( 149 Action.ACCELERATOR_KEY, 150 KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_DOWN_MASK)); 151 152 155 redoAction = 156 new AbstractAction ("Redo", new ImageIcon (cl.getResource("icons/redo_edit.gif"))) { 157 public void actionPerformed(ActionEvent e) { 158 undoManager.redo(); 159 } 160 }; 161 redoAction.putValue( 162 Action.ACCELERATOR_KEY, 163 KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_DOWN_MASK)); 164 165 168 cutAction = 169 new AbstractAction ("Cut", new ImageIcon (cl.getResource("icons/cut_edit.gif"))) { 170 public void actionPerformed(ActionEvent e) { 171 editor.cut(); 172 } 173 }; 174 cutAction.putValue( 175 Action.ACCELERATOR_KEY, 176 KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_DOWN_MASK)); 177 178 181 copyAction = 182 new AbstractAction ("Copy", new ImageIcon (cl.getResource("icons/copy_edit.gif"))) { 183 public void actionPerformed(ActionEvent e) { 184 editor.copy(); 185 } 186 }; 187 copyAction.putValue( 188 Action.ACCELERATOR_KEY, 189 KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK)); 190 191 194 pasteAction = 195 new AbstractAction ("Paste", new ImageIcon (cl.getResource("icons/paste_edit.gif"))) { 196 public void actionPerformed(ActionEvent e) { 197 editor.paste(); 198 } 199 }; 200 pasteAction.putValue( 201 Action.ACCELERATOR_KEY, 202 KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_DOWN_MASK)); 203 204 207 closeAction = new TaskAction("Close editor") { 208 public void doTask(ActionEvent e) throws Exception { 209 close(); 210 } 211 }; 212 updateUndoRedoActions(); 213 } 214 215 private void createMenuBar() { 216 menuBar = new JMenuBar (); 217 JMenu fileMenu = new JMenu ("File"); 218 fileMenu.add(saveAction); 219 fileMenu.add(saveAsAction); 220 fileMenu.addSeparator(); 221 fileMenu.add(closeAction); 222 223 menuBar.add(fileMenu); 224 225 JMenu editMenu = new JMenu ("Edit"); 226 editMenu.add(undoAction); 227 editMenu.add(redoAction); 228 editMenu.addSeparator(); 229 editMenu.add(cutAction); 230 editMenu.add(copyAction); 231 editMenu.add(pasteAction); 232 233 menuBar.add(editMenu); 234 235 } 236 237 private void createStatusBar() { 238 statusBar = new JPanel (new BorderLayout ()); 239 statusBar.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); 240 241 caretStatus = new JLabel (""); 242 statusBar.add(caretStatus, BorderLayout.EAST); 243 244 filenameStatus = new JLabel (""); 245 statusBar.add(filenameStatus, BorderLayout.WEST); 246 } 247 248 private void createToolBar() { 249 toolBar = new JToolBar (); 250 JButton button = null; 251 252 ClassLoader cl = ScriptEditor.class.getClassLoader(); 253 254 toolBar.add(saveAction); 256 toolBar.add(saveAsAction); 257 258 toolBar.addSeparator(); 259 260 toolBar.add(undoAction); 261 toolBar.add(redoAction); 262 263 toolBar.addSeparator(); 264 265 toolBar.add(cutAction); 266 toolBar.add(copyAction); 267 toolBar.add(pasteAction); 268 269 } 270 271 private void createEditor() { 272 editor = new JEditTextArea(); 273 editor.setFocusTraversalKeysEnabled(false); 274 editor.setTokenMarker(new PythonTokenMarker()); 275 editor.setElectricScroll(0); 276 editor.getPainter().setEOLMarkersPainted(false); 277 editor.getPainter().setInvalidLinesPainted(false); 278 editor.addCaretListener(new CaretListener () { 279 public void caretUpdate(CaretEvent e) { 280 caretStatus.setText( 281 editor.getCaretLine() 282 + ":" 283 + (editor.getCaretPosition() 284 - editor.getLineStartOffset(editor.getCaretLine()) 285 + 1)); 286 } 287 }); 288 289 editor.getDocument().addUndoableEditListener(undoHandler); 290 291 } 292 293 public void loadFile(String scriptName) throws IOException { 294 setTitle(scriptName); 295 FileReader fi = new FileReader (scriptName); 296 BufferedReader bi = new BufferedReader (fi); 297 StringBuffer buf = new StringBuffer (); 298 String line; 299 while ((line = bi.readLine()) != null) { 300 buf.append(line); 301 buf.append("\n"); 302 } 303 editor.setText(buf.toString()); 304 filenameStatus.setText(scriptName); 305 this.scriptName = scriptName; 306 editor.updateScrollBars(); 307 editor.select(0, 0); 308 309 } 310 311 protected void saveFile() throws IOException { 312 FileWriter fw = new FileWriter (scriptName); 313 BufferedWriter bw = new BufferedWriter (fw); 314 bw.write(editor.getText()); 315 bw.close(); 316 fw.close(); 317 onSave(); 318 } 319 320 public void highlightError(String errorMessage, int line, int col) { 321 editor.setCaretPosition(editor.getLineStartOffset(line - 1) + col - 1); 322 filenameStatus.setText(errorMessage + " at line " + line); 323 editor.grabFocus(); 324 } 325 326 protected void saveFileAs() { 327 } 328 329 protected void close() { 330 dispose(); 331 } 332 333 protected void onSave() { 334 for (Iterator i = listeners.iterator(); i.hasNext();) { 335 ((ScriptEditorListener) i.next()).scriptSaved(this); 336 } 337 } 338 339 private void updateUndoRedoActions() { 340 undoAction.setEnabled(undoManager.canUndo()); 341 redoAction.setEnabled(undoManager.canRedo()); 342 } 343 class UndoHandler implements UndoableEditListener { 344 345 349 public void undoableEditHappened(UndoableEditEvent e) { 350 undoManager.addEdit(e.getEdit()); 351 updateUndoRedoActions(); 352 } 353 } 354 355 public static void main(String [] args) { 356 ScriptEditor s; 357 try { 358 s = new ScriptEditor(); 359 s.loadFile("src/jelly/scripts/swing/jboss/system/Server.py"); 360 s.setSize(new Dimension (800, 600)); 361 s.show(); 362 } catch (IOException e) { 363 e.printStackTrace(); 365 } 366 } 367 } 368 | Popular Tags |