1 30 package com.genimen.djeneric.tools.modeler.userperspective; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Component ; 34 import java.awt.Dimension ; 35 import java.awt.FlowLayout ; 36 import java.awt.event.ActionEvent ; 37 import java.awt.event.MouseEvent ; 38 39 import javax.swing.DefaultListModel ; 40 import javax.swing.JButton ; 41 import javax.swing.JList ; 42 import javax.swing.JOptionPane ; 43 import javax.swing.JPanel ; 44 import javax.swing.JScrollPane ; 45 import javax.swing.tree.DefaultTreeModel ; 46 47 import com.genimen.djeneric.language.Messages; 48 import com.genimen.djeneric.structure.EditorDefinition; 49 import com.genimen.djeneric.structure.ExtentUsage; 50 import com.genimen.djeneric.tools.modeler.ModelEditor; 51 import com.genimen.djeneric.tools.modeler.dialogs.CreateEditorDialog; 52 import com.genimen.djeneric.ui.DjCloseable; 53 import com.genimen.djeneric.ui.DjCloseableCloser; 54 import com.genimen.djeneric.ui.DjTabbedPane; 55 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 56 import com.genimen.djeneric.util.DjLogger; 57 58 64 public class EditorEditor extends JPanel implements DjCloseableCloser 65 { 66 private static final long serialVersionUID = 1L; 67 BorderLayout borderLayout1 = new BorderLayout (); 68 ExtentUsageNode _root; 69 DefaultTreeModel _treeModel; 70 71 ViewEditor _viewEditor; 72 ModelEditor _editor; 73 JPanel jPanel10 = new JPanel (); 74 BorderLayout borderLayout5 = new BorderLayout (); 75 PropertyUsageTableModel _emptyModel = new PropertyUsageTableModel(null); 76 JPanel jPanel15 = new JPanel (); 77 BorderLayout borderLayout7 = new BorderLayout (); 78 JScrollPane jScrollPane2 = new JScrollPane (); 79 JList _lstEditors = new JList (); 80 JPanel jPanel18 = new JPanel (); 81 BorderLayout borderLayout12 = new BorderLayout (); 82 JPanel jPanel19 = new JPanel (); 83 JButton _butNewEditor = new JButton (); 84 DjVerticalFlowLayout flowLayout1 = new DjVerticalFlowLayout(); 85 JButton _butDeleteEditor = new JButton (); 86 DjTabbedPane _tabs = new DjTabbedPane(); 87 JButton _butEdit = new JButton (); 88 JButton _butCopy = new JButton (); 89 90 93 public EditorEditor() 94 { 95 try 96 { 97 jbInit(); 98 } 99 catch (Exception ex) 100 { 101 JOptionPane.showMessageDialog(this, ex.getMessage()); 102 DjLogger.log(ex); 103 } 104 } 105 106 112 public void setEditorReferences(ViewEditor viewEditor) 113 { 114 _editor = viewEditor.getModelEditor(); 115 _viewEditor = viewEditor; 116 } 117 118 public void synchronize() 119 { 120 synchronize(null); 121 } 122 123 public void synchronize(EditorDefinition editorToSelect) 124 { 125 EditorDefinition[] editorDefs = _viewEditor.getEditors(); 126 DefaultListModel model = new DefaultListModel (); 127 int idx = -1; 128 if (editorToSelect == null) 129 { 130 idx = _lstEditors.getSelectedIndex(); 131 } 132 133 for (int i = 0; i < editorDefs.length; i++) 134 { 135 if (editorDefs[i] == editorToSelect) idx = i; 136 model.addElement(editorDefs[i]); 137 } 138 _lstEditors.setModel(model); 139 140 if (idx >= model.getSize()) idx = model.getSize() - 1; 141 142 if (idx != -1) 143 { 144 _lstEditors.setSelectedIndex(idx); 145 _lstEditors.ensureIndexIsVisible(idx); 146 } 147 else if (editorDefs.length > 0) _lstEditors.setSelectedIndex(0); 148 } 149 150 public void clear() 151 { 152 _lstEditors.setModel(new DefaultListModel ()); 153 } 154 155 void jbInit() throws Exception 156 { 157 this.setLayout(borderLayout1); 158 jPanel10.setLayout(borderLayout5); 159 jPanel15.setLayout(borderLayout7); 160 jScrollPane2.setAutoscrolls(true); 161 jScrollPane2.setPreferredSize(new Dimension (260, 100)); 162 jPanel18.setLayout(borderLayout12); 163 _butNewEditor.setText(Messages.getString("global.new")); 164 _butNewEditor.addActionListener(new java.awt.event.ActionListener () 165 { 166 public void actionPerformed(ActionEvent e) 167 { 168 _butNewEditor_actionPerformed(e); 169 } 170 }); 171 jPanel19.setLayout(flowLayout1); 172 flowLayout1.setAlignment(FlowLayout.LEFT); 173 flowLayout1.setVgap(0); 174 flowLayout1.setHorizontalFill(true); 175 _butDeleteEditor.setText(Messages.getString("global.Delete")); 176 _butDeleteEditor.addActionListener(new java.awt.event.ActionListener () 177 { 178 public void actionPerformed(ActionEvent e) 179 { 180 _butDeleteEditor_actionPerformed(e); 181 } 182 }); 183 _butEdit.setText(Messages.getString("global.Edit")); 184 _butEdit.addActionListener(new java.awt.event.ActionListener () 185 { 186 public void actionPerformed(ActionEvent e) 187 { 188 _butEdit_actionPerformed(e); 189 } 190 }); 191 _butCopy.setText(Messages.getString("EditorEditor.Copy")); 192 _butCopy.addActionListener(new java.awt.event.ActionListener () 193 { 194 public void actionPerformed(ActionEvent e) 195 { 196 _butCopy_actionPerformed(e); 197 } 198 }); 199 _lstEditors.addMouseListener(new java.awt.event.MouseAdapter () 200 { 201 public void mouseClicked(MouseEvent e) 202 { 203 _lstEditors_mouseClicked(e); 204 } 205 }); 206 this.add(jPanel10, BorderLayout.NORTH); 207 jPanel10.add(jPanel15, BorderLayout.NORTH); 208 jPanel15.add(jPanel18, BorderLayout.EAST); 209 jPanel18.add(jPanel19, BorderLayout.SOUTH); 210 jPanel19.add(_butEdit, null); 211 jPanel19.add(_butNewEditor, null); 212 jPanel19.add(_butCopy, null); 213 jPanel19.add(_butDeleteEditor, null); 214 jPanel15.add(jScrollPane2, BorderLayout.CENTER); 215 this.add(_tabs, BorderLayout.CENTER); 216 jScrollPane2.getViewport().add(_lstEditors, null); 217 } 218 219 void _butNewEditor_actionPerformed(ActionEvent e) 220 { 221 CreateEditorDialog ced = new CreateEditorDialog(_editor, _viewEditor.getEditors()); 222 if (!ced.wasCanceled()) 223 { 224 ExtentUsage newUsage = new ExtentUsage(ced.getExtent()); 225 newUsage.setMultiRow(false); 226 227 EditorDefinition newEditor = new EditorDefinition(ced.getEditorName(), newUsage); 228 _viewEditor.addEditor(newEditor); 229 synchronize(newEditor); 230 231 EditorEditorPanel editorEditor = editEditor(newEditor); 232 editorEditor.addAllProperties(); 233 } 234 } 235 236 boolean isOkToDelete(EditorDefinition editor) 237 { 238 int result = JOptionPane.showOptionDialog(this, Messages.getString("EditorEditor.DeleteEditor", editor.getName()), 239 Messages.getString("EditorEditor.DeleteEditor1"), 240 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, 241 new String []{Messages.getString("global.Delete"), 242 Messages.getString("global.Cancel")}, null); 243 return result == 0; 244 } 245 246 void _butDeleteEditor_actionPerformed(ActionEvent e) 247 { 248 EditorDefinition editor = (EditorDefinition) _lstEditors.getSelectedValue(); 249 if (editor != null) 250 { 251 if (isOkToDelete(editor)) 252 { 253 EditorEditorPanel ep = getEditor(editor); 254 if (ep != null) _tabs.remove(ep); 255 256 editor.setMarkedForDelete(true); 257 _viewEditor.deleteEditor(editor); 258 synchronize(); 259 } 260 } 261 } 262 263 protected EditorEditorPanel getEditor(EditorDefinition editorDef) 264 { 265 for (int i = 0; i < _tabs.getTabCount(); i++) 266 { 267 Component c = _tabs.getComponent(i); 268 if (c instanceof EditorEditorPanel) 269 { 270 EditorEditorPanel ep = (EditorEditorPanel) c; 271 if (ep.getCurrentEditorDefinition() == editorDef) 272 { 273 return ep; 274 } 275 } 276 } 277 return null; 278 279 } 280 281 void _butEdit_actionPerformed(ActionEvent e) 282 { 283 EditorDefinition editor = (EditorDefinition) _lstEditors.getSelectedValue(); 284 285 editor.removeInvalidExtents(); 287 editEditor(editor); 288 } 289 290 protected EditorEditorPanel editEditor(EditorDefinition editor) 291 { 292 EditorEditorPanel editorEditor = null; 293 294 if (editor != null) 295 { 296 if (editor.getUsages() == null) 297 { 298 JOptionPane.showMessageDialog(this, Messages.getString("ModelEditor.EditorInvalid")); 299 return null; 300 } 301 else 302 { 303 editorEditor = getEditor(editor); 304 if (editorEditor == null) 305 { 306 editorEditor = new EditorEditorPanel(this, _viewEditor, editor); 307 _tabs.addTab(editor.getName(), ModelEditor.getImageIcon("editor.gif"), editorEditor); 308 } 309 _tabs.setSelectedComponent(editorEditor); 310 } 311 } 312 return editorEditor; 313 } 314 315 public void closeCloseable(DjCloseable edt) 316 { 317 _tabs.remove((Component ) edt); 318 synchronize(); 319 } 320 321 void _butCopy_actionPerformed(ActionEvent e) 322 { 323 EditorDefinition editor = (EditorDefinition) _lstEditors.getSelectedValue(); 324 if (editor != null) 325 { 326 EditorDefinition newEditor; 327 newEditor = (EditorDefinition) editor.clone(); 328 newEditor.setName(Messages.getString("global.CopyOf", editor.getName())); 329 _viewEditor.addEditor(newEditor); 330 synchronize(newEditor); 331 } 332 333 } 334 335 void _lstEditors_mouseClicked(MouseEvent e) 336 { 337 if (e.getClickCount() > 1) _butEdit_actionPerformed(null); 338 339 } 340 341 public boolean hasEditorsOpen() 342 { 343 return _tabs.getComponentCount() > 0; 344 } 345 } | Popular Tags |