1 30 31 package com.genimen.djeneric.tools.modeler.userperspective; 32 33 import java.awt.BorderLayout ; 34 import java.awt.Component ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.MouseEvent ; 37 import java.util.ArrayList ; 38 39 import javax.swing.DefaultListModel ; 40 import javax.swing.JButton ; 41 import javax.swing.JLabel ; 42 import javax.swing.JList ; 43 import javax.swing.JOptionPane ; 44 import javax.swing.JPanel ; 45 import javax.swing.JScrollPane ; 46 import javax.swing.ListSelectionModel ; 47 48 import com.genimen.djeneric.language.Messages; 49 import com.genimen.djeneric.structure.ScriptDefinition; 50 import com.genimen.djeneric.tools.modeler.ModelEditor; 51 import com.genimen.djeneric.ui.DjCloseable; 52 import com.genimen.djeneric.ui.DjCloseableCloser; 53 import com.genimen.djeneric.ui.DjTabbedPane; 54 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 55 import com.genimen.djeneric.util.DjLogger; 56 57 public class ScriptEditor extends JPanel implements DjCloseableCloser 58 { 59 private static final long serialVersionUID = 1L; 60 BorderLayout borderLayout1 = new BorderLayout (); 61 JPanel jPanel2 = new JPanel (); 62 JScrollPane jScrollPane1 = new JScrollPane (); 63 BorderLayout borderLayout2 = new BorderLayout (); 64 JList _lstScripts = new JList (); 65 JPanel jPanel3 = new JPanel (); 66 BorderLayout borderLayout3 = new BorderLayout (); 67 JPanel jPanel4 = new JPanel (); 68 DjVerticalFlowLayout flowLayout1 = new DjVerticalFlowLayout(); 69 JButton _butEdit = new JButton (); 70 JButton _butCreate = new JButton (); 71 JButton _butDelete = new JButton (); 72 DjTabbedPane _tabs = new DjTabbedPane(); 73 ViewEditor _viewEditor; 74 ModelEditor _editor; 75 JLabel jLabel1 = new JLabel (); 76 JButton _butMoveUp = new JButton (); 77 JButton _butMoveDown = new JButton (); 78 79 public ScriptEditor() 80 { 81 try 82 { 83 jbInit(); 84 } 85 catch (Exception ex) 86 { 87 DjLogger.log(ex); 88 } 89 } 90 91 public void setEditorReferences(ViewEditor viewEditor) 92 { 93 _editor = viewEditor.getModelEditor(); 94 _viewEditor = viewEditor; 95 } 96 97 void jbInit() throws Exception 98 { 99 this.setLayout(borderLayout1); 100 jPanel2.setLayout(borderLayout2); 101 jPanel3.setLayout(borderLayout3); 102 jPanel4.setLayout(flowLayout1); 103 _butEdit.setText(Messages.getString("global.Edit")); 104 _butEdit.addActionListener(new java.awt.event.ActionListener () 105 { 106 public void actionPerformed(ActionEvent e) 107 { 108 _butEdit_actionPerformed(e); 109 } 110 }); 111 _butCreate.setText(Messages.getString("global.Create")); 112 _butCreate.addActionListener(new java.awt.event.ActionListener () 113 { 114 public void actionPerformed(ActionEvent e) 115 { 116 _butCreate_actionPerformed(e); 117 } 118 }); 119 _butDelete.setText(Messages.getString("global.Delete")); 120 _butDelete.addActionListener(new java.awt.event.ActionListener () 121 { 122 public void actionPerformed(ActionEvent e) 123 { 124 _butDelete_actionPerformed(e); 125 } 126 }); 127 _lstScripts.addMouseListener(new java.awt.event.MouseAdapter () 128 { 129 public void mouseClicked(MouseEvent e) 130 { 131 _lstScripts_mouseClicked(e); 132 } 133 }); 134 _lstScripts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 135 _butMoveDown.setText(Messages.getString("global.MoveDown")); 136 _butMoveDown.addActionListener(new java.awt.event.ActionListener () 137 { 138 public void actionPerformed(ActionEvent e) 139 { 140 _butMoveDown_actionPerformed(e); 141 } 142 }); 143 _butMoveUp.setText(Messages.getString("global.MoveUp")); 144 jLabel1.setText(" "); 145 _butMoveUp.addActionListener(new java.awt.event.ActionListener () 146 { 147 public void actionPerformed(ActionEvent e) 148 { 149 _butMoveUp_actionPerformed(e); 150 } 151 }); 152 jPanel4.add(_butMoveUp, null); 153 jPanel4.add(_butMoveDown, null); 154 jPanel4.add(jLabel1, null); 155 jPanel4.add(_butEdit, null); 156 jPanel4.add(_butCreate, null); 157 jPanel4.add(_butDelete, null); 158 this.add(jPanel2, BorderLayout.NORTH); 159 jPanel2.add(jScrollPane1, BorderLayout.CENTER); 160 jPanel2.add(jPanel3, BorderLayout.EAST); 161 jPanel3.add(jPanel4, BorderLayout.SOUTH); 162 jScrollPane1.getViewport().add(_lstScripts, null); 163 this.add(_tabs, BorderLayout.CENTER); 164 } 165 166 void _butEdit_actionPerformed(ActionEvent e) 167 { 168 editSelectedScript(); 169 } 170 171 private void editSelectedScript() 172 { 173 if (_lstScripts.getModel().getSize() > 0) 174 { 175 ScriptDefinition script = (ScriptDefinition) _lstScripts.getSelectedValue(); 176 if (script != null) 177 { 178 openEditor(script); 179 } 180 } 181 } 182 183 void _butCreate_actionPerformed(ActionEvent e) 184 { 185 ScriptDefinition sd = new ScriptDefinition(Messages.getString("ScriptEditor.NewScript"), 186 "script NewScript \"New Script\"\n" + "{\n" + " firstAction;\n" + "}\n" + "\n" + "constructor\n" + "{\n" 187 + "}\n" + "\n" + "controller\n" + "{\n" + " * -> traceEvents;\n" + "}\n" + "\n" + "action traceEvents\n" 188 + "{\n" + " trace(\"event: \" + $event);\n" + "}\n" + "\n" + "action firstAction \"First Action\"\n" 189 + "{\n" + "}\n\n" + "action lastAction \"Last Action\"\n" + "{\n" + " commit;\n" + " terminate script;\n" 190 + "}\n"); 191 192 _viewEditor.addScript(sd, _lstScripts.getSelectedIndex() + 1); 193 openEditor(sd); 194 synchronize(); 195 _lstScripts.setSelectedValue(sd, true); 196 } 197 198 protected void synchronize() 199 { 200 ScriptDefinition curr = null; 201 202 if (_lstScripts.getModel().getSize() > 0) 203 { 204 curr = (ScriptDefinition) _lstScripts.getSelectedValue(); 205 } 206 207 DefaultListModel model = new DefaultListModel (); 208 ScriptDefinition[] defs = _viewEditor.getScripts(); 209 int idx = 0; 210 for (int i = 0; i < defs.length; i++) 211 { 212 model.addElement(defs[i]); 213 if (defs[i] == curr) idx = i; 214 } 215 _lstScripts.setModel(model); 216 _lstScripts.setSelectedIndex(idx); 217 218 } 219 220 protected void openEditor(ScriptDefinition sd) 221 { 222 ScriptEditorPanel sep = findScriptEditor(sd); 223 if (sep == null) 224 { 225 sep = new ScriptEditorPanel(sd, this, _editor); 226 _tabs.addTab(sd.toString(), ModelEditor.getImageIcon("script.gif"), sep); 227 _tabs.validate(); 228 } 229 _tabs.setSelectedComponent(sep); 230 sep.requestFocus(); 231 } 232 233 protected boolean isOkToDelete(ScriptDefinition script) 234 { 235 int result = JOptionPane.showOptionDialog(this, Messages.getString("ScriptEditor.DeleteScript", script.getTitle()), 236 Messages.getString("ScriptEditor.DeleteScript1"), 237 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, 238 new String []{Messages.getString("global.Delete"), 239 Messages.getString("global.Cancel")}, null); 240 return result == 0; 241 } 242 243 protected ScriptEditorPanel findScriptEditor(ScriptDefinition sd) 244 { 245 for (int i = 0; i < _tabs.getComponentCount(); i++) 246 { 247 if (_tabs.getComponent(i) instanceof ScriptEditorPanel) 248 { 249 ScriptEditorPanel sep = (ScriptEditorPanel) _tabs.getComponent(i); 250 if (sep.getScriptDefinition() == sd) 251 { 252 return sep; 253 } 254 } 255 } 256 return null; 257 } 258 259 void _butDelete_actionPerformed(ActionEvent e) 260 { 261 ScriptDefinition script = (ScriptDefinition) _lstScripts.getSelectedValue(); 262 if (script != null) 263 { 264 if (isOkToDelete(script)) 265 { 266 script.setMarkedForDelete(true); 267 _viewEditor.deleteScript(script); 268 269 ScriptEditorPanel sep = findScriptEditor(script); 270 if (sep != null) _tabs.remove(sep); 271 synchronize(); 272 } 273 } 274 } 275 276 public void closeCloseable(DjCloseable closeable) 277 { 278 _tabs.remove((Component ) closeable); 279 synchronize(); 280 } 281 282 void _lstScripts_mouseClicked(MouseEvent e) 283 { 284 if (e.getClickCount() > 1) editSelectedScript(); 285 286 } 287 288 void _butMoveUp_actionPerformed(ActionEvent e) 289 { 290 if (_lstScripts.getModel().getSize() > 0) 291 { 292 int idx = _lstScripts.getSelectedIndex(); 293 if (idx > 0) 294 { 295 ArrayList lst = _viewEditor.getScriptsList(); 296 Object o = lst.get(idx - 1); 297 lst.set(idx - 1, lst.get(idx)); 298 lst.set(idx, o); 299 synchronize(); 300 } 301 } 302 } 303 304 void _butMoveDown_actionPerformed(ActionEvent e) 305 { 306 if (_lstScripts.getModel().getSize() > 0) 307 { 308 int idx = _lstScripts.getSelectedIndex(); 309 if (idx < _lstScripts.getModel().getSize() - 1) 310 { 311 ArrayList lst = _viewEditor.getScriptsList(); 312 Object o = lst.get(idx + 1); 313 lst.set(idx + 1, lst.get(idx)); 314 lst.set(idx, o); 315 synchronize(); 316 } 317 } 318 } 319 320 public boolean hasEditorsOpen() 321 { 322 return _tabs.getComponentCount() > 0; 323 } 324 } | Popular Tags |