1 27 package com.genimen.djeneric.tools.modeler.domaineditor; 28 29 import java.awt.BorderLayout ; 30 import java.awt.Dimension ; 31 import java.awt.event.ActionEvent ; 32 import java.awt.event.FocusEvent ; 33 import java.awt.event.MouseAdapter ; 34 import java.awt.event.MouseEvent ; 35 import java.util.ArrayList ; 36 import java.util.Arrays ; 37 38 import javax.swing.DefaultComboBoxModel ; 39 import javax.swing.DefaultListSelectionModel ; 40 import javax.swing.JButton ; 41 import javax.swing.JComboBox ; 42 import javax.swing.JLabel ; 43 import javax.swing.JPanel ; 44 import javax.swing.JScrollPane ; 45 import javax.swing.JSplitPane ; 46 import javax.swing.JTabbedPane ; 47 import javax.swing.JTable ; 48 import javax.swing.JTextArea ; 49 import javax.swing.ListSelectionModel ; 50 import javax.swing.event.ChangeEvent ; 51 import javax.swing.event.ListSelectionEvent ; 52 import javax.swing.event.ListSelectionListener ; 53 import javax.swing.event.TableModelEvent ; 54 import javax.swing.table.JTableHeader ; 55 56 import com.genimen.djeneric.language.Messages; 57 import com.genimen.djeneric.repository.DjDomain; 58 import com.genimen.djeneric.repository.DjExtent; 59 import com.genimen.djeneric.repository.DjPackage; 60 import com.genimen.djeneric.repository.DjProperty; 61 import com.genimen.djeneric.tools.modeler.ModelEditor; 62 import com.genimen.djeneric.ui.DjCloseable; 63 import com.genimen.djeneric.ui.DjStatusListener; 64 import com.genimen.djeneric.ui.DjTable; 65 import com.genimen.djeneric.util.DjLogger; 66 67 public class DomainEditor extends JPanel implements ListSelectionListener , DjStatusListener, DjCloseable 68 { 69 private static final long serialVersionUID = 1L; 70 final static String DIVIDER_LOCATION = "domaineditor.dividerloc"; 71 72 JScrollPane _scrDomains = new JScrollPane (); 73 DjTable _grdDomains = new DjTable(); 74 ModelEditor _editor; 75 DomainTableModel _domainModel; 76 DomainValueTableModel _domainValueModel; 77 ListSelectionModel _selectionModel; 78 JTabbedPane _tabs = new JTabbedPane (); 79 JScrollPane _scrValues = new JScrollPane (); 80 JScrollPane _scrUsage = new JScrollPane (); 81 JTextArea _edtUsage = new JTextArea (); 82 DjTable _grdValues = new DjTable(); 83 JSplitPane _splitpanel = new JSplitPane (); 84 BorderLayout borderLayout1 = new BorderLayout (); 85 JPanel jPanel1 = new JPanel (); 86 BorderLayout borderLayout2 = new BorderLayout (); 87 JButton _butUp = new JButton (); 88 JButton _butDelete = new JButton (); 89 JButton _butDown = new JButton (); 90 JButton _butBottom = new JButton (); 91 JButton _butFirst = new JButton (); 92 JButton _butCreate = new JButton (); 93 JPanel jPanel10 = new JPanel (); 94 JLabel _lblActiveTable = new JLabel (); 95 DjTable _selectedTable = _grdDomains; 96 JPanel jPanel2 = new JPanel (); 97 BorderLayout borderLayout3 = new BorderLayout (); 98 JLabel _lblPackage = new JLabel (); 99 JComboBox _cbbPackage = new JComboBox (); 100 101 public DomainEditor(ModelEditor editor) 102 { 103 _editor = editor; 104 105 try 106 { 107 jbInit(); 108 _scrDomains.addMouseListener(new MouseAdapter () 109 { 110 public void mouseClicked(MouseEvent e) 111 { 112 doubleClick(e, _grdDomains); 113 } 114 }); 115 _scrValues.addMouseListener(new MouseAdapter () 116 { 117 public void mouseClicked(MouseEvent e) 118 { 119 doubleClick(e, _grdValues); 120 } 121 }); 122 String divLoc = _editor.getProperty(DIVIDER_LOCATION, "200"); 123 _splitpanel.setDividerLocation(Integer.parseInt(divLoc)); 124 125 ArrayList packs = new ArrayList (); 126 packs.add(_domainModel.getAllPackagesPackage()); 127 packs.add(_domainModel.getDefaultPackage()); 128 packs.addAll(Arrays.asList(editor.getPersistenceManager().getPackages())); 129 _cbbPackage.setModel(new DefaultComboBoxModel (packs.toArray())); 130 131 filterDomains(); 132 133 JTableHeader th = _grdDomains.getTableHeader(); 134 th.addMouseListener(_domainModel.getHeaderClickListener(_grdDomains)); 135 136 } 137 catch (Exception ex) 138 { 139 DjLogger.log(ex); 140 } 141 } 142 143 private void filterDomains() 144 { 145 _domainModel.setFilter((DjPackage) _cbbPackage.getSelectedItem()); 146 _grdDomains.tableChanged(new TableModelEvent (_domainModel, -1, _domainModel.getRowCount(), 147 TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT)); 148 } 149 150 private void jbInit() throws Exception 151 { 152 this.setLayout(borderLayout1); 153 _splitpanel.setOrientation(JSplitPane.VERTICAL_SPLIT); 154 _splitpanel.setDividerLocation(200); 155 156 jPanel1.setLayout(borderLayout2); 157 _butUp.setPreferredSize(new Dimension (24, 24)); 158 _butUp.setIcon(ModelEditor.getImageIcon("up.gif")); 159 _butUp.setToolTipText(Messages.getString("global.PreviousInList")); 160 _butUp.setRequestFocusEnabled(true); 161 _butUp.addActionListener(new java.awt.event.ActionListener () 162 { 163 public void actionPerformed(ActionEvent e) 164 { 165 _butUp_actionPerformed(e); 166 } 167 }); 168 _butDelete.addActionListener(new java.awt.event.ActionListener () 169 { 170 public void actionPerformed(ActionEvent e) 171 { 172 _butDelete_actionPerformed(e); 173 } 174 }); 175 _butDelete.setRequestFocusEnabled(true); 176 _butDelete.setToolTipText(Messages.getString("global.DeleteCtrl")); 177 _butDelete.setIcon(ModelEditor.getImageIcon("delete.gif")); 178 _butDelete.setPreferredSize(new Dimension (24, 24)); 179 _butDown.addActionListener(new java.awt.event.ActionListener () 180 { 181 public void actionPerformed(ActionEvent e) 182 { 183 _butDown_actionPerformed(e); 184 } 185 }); 186 _butDown.setRequestFocusEnabled(true); 187 _butDown.setToolTipText(Messages.getString("global.NextInList")); 188 _butDown.setIcon(ModelEditor.getImageIcon("down.gif")); 189 _butDown.setPreferredSize(new Dimension (24, 24)); 190 _butBottom.setPreferredSize(new Dimension (24, 24)); 191 _butBottom.setIcon(ModelEditor.getImageIcon("bottom.gif")); 192 _butBottom.setToolTipText(Messages.getString("global.LastInList")); 193 _butBottom.setRequestFocusEnabled(true); 194 _butBottom.addActionListener(new java.awt.event.ActionListener () 195 { 196 public void actionPerformed(ActionEvent e) 197 { 198 _butBottom_actionPerformed(e); 199 } 200 }); 201 _butFirst.setPreferredSize(new Dimension (24, 24)); 202 _butFirst.setIcon(ModelEditor.getImageIcon("top.gif")); 203 _butFirst.setToolTipText(Messages.getString("global.FirstInList")); 204 _butFirst.setRequestFocusEnabled(true); 205 _butFirst.addActionListener(new java.awt.event.ActionListener () 206 { 207 public void actionPerformed(ActionEvent e) 208 { 209 _butFirst_actionPerformed(e); 210 } 211 }); 212 _butCreate.setPreferredSize(new Dimension (24, 24)); 213 _butCreate.setIcon(ModelEditor.getImageIcon("new.gif")); 214 _butCreate.setToolTipText(Messages.getString("global.CreateCtrl")); 215 _butCreate.setRequestFocusEnabled(true); 216 _butCreate.addActionListener(new java.awt.event.ActionListener () 217 { 218 public void actionPerformed(ActionEvent e) 219 { 220 _butCreate_actionPerformed(e); 221 } 222 }); 223 _grdDomains.addFocusListener(new java.awt.event.FocusAdapter () 224 { 225 public void focusGained(FocusEvent e) 226 { 227 _grdDomains_focusGained(e); 228 } 229 }); 230 _lblActiveTable.setText(Messages.getString("global.Domains")); 231 _scrDomains.addFocusListener(new java.awt.event.FocusAdapter () 232 { 233 public void focusGained(FocusEvent e) 234 { 235 _scrDomains_focusGained(e); 236 } 237 }); 238 _scrValues.addFocusListener(new java.awt.event.FocusAdapter () 239 { 240 public void focusGained(FocusEvent e) 241 { 242 _scrValues_focusGained(e); 243 } 244 }); 245 _grdValues.addFocusListener(new java.awt.event.FocusAdapter () 246 { 247 public void focusGained(FocusEvent e) 248 { 249 _grdValues_focusGained(e); 250 } 251 }); 252 253 jPanel2.setLayout(borderLayout3); 254 _lblPackage.setText(" Package "); 255 _cbbPackage.addActionListener(new java.awt.event.ActionListener () 256 { 257 public void actionPerformed(ActionEvent e) 258 { 259 cbbPackage_actionPerformed(e); 260 } 261 }); 262 this.add(_splitpanel, BorderLayout.CENTER); 263 264 _domainModel = new DomainTableModel(_editor.getPersistenceManager()); 265 _domainModel.setEditable(_editor.isModelLocked()); 266 _domainModel.addStatusListener(this); 267 268 _domainValueModel = new DomainValueTableModel(_editor, _grdDomains); 269 _domainValueModel.setEditable(_editor.isModelLocked()); 270 _domainValueModel.addStatusListener(this); 271 272 _selectionModel = new DefaultListSelectionModel (); 273 _selectionModel.addListSelectionListener(this); 274 _selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 275 _grdDomains.setSelectionModel(_selectionModel); 276 277 _grdDomains.setModel(_domainModel); 278 _edtUsage.setFont(new java.awt.Font ("Monospaced", 0, 12)); 279 _edtUsage.setEditable(false); 280 _grdValues.setModel(_domainValueModel); 281 _tabs.addChangeListener(new javax.swing.event.ChangeListener () 282 { 283 public void stateChanged(ChangeEvent e) 284 { 285 _tabs_stateChanged(e); 286 } 287 }); 288 _splitpanel.add(_scrDomains, JSplitPane.TOP); 289 _splitpanel.add(_tabs, JSplitPane.BOTTOM); 290 _tabs.add(_scrValues, Messages.getString("DomainEditor.ValidValues")); 291 _scrValues.getViewport().add(_grdValues); 292 _tabs.add(_scrUsage, Messages.getString("DomainEditor.UsageOfDomain")); 293 this.add(jPanel1, BorderLayout.SOUTH); 294 jPanel1.add(jPanel10, BorderLayout.WEST); 295 jPanel10.add(_butUp, null); 296 jPanel10.add(_butDown, null); 297 jPanel10.add(_butFirst, null); 298 jPanel10.add(_butBottom, null); 299 jPanel10.add(_butCreate, null); 300 jPanel10.add(_butDelete, null); 301 jPanel10.add(_lblActiveTable, null); 302 this.add(jPanel2, BorderLayout.NORTH); 303 jPanel2.add(_lblPackage, BorderLayout.WEST); 304 jPanel2.add(_cbbPackage, BorderLayout.CENTER); 305 _scrUsage.getViewport().add(_edtUsage); 306 _scrDomains.getViewport().add(_grdDomains); 307 } 308 309 public void requestFocus() 310 { 311 _grdDomains.requestFocus(); 312 313 _grdDomains.setRowSelectionInterval(0, 0); 314 } 316 317 public void doubleClick(MouseEvent e, DjTable table) 318 { 319 table.requestFocus(); 320 if (e.getClickCount() < 2) return; 321 322 try 323 { 324 table.insertRow(); 325 } 326 catch (Exception x) 327 { 328 _editor.setStatusMessage(x); 329 } 330 331 } 332 333 public void valueChanged(ListSelectionEvent e) 335 { 336 if (_grdDomains.getSelectedRow() != -1) 337 { 338 _domainModel.getDomain(_grdDomains.getSelectedRow()).sort(); 339 } 340 _grdValues.tableChanged(new TableModelEvent (_domainValueModel, -1, _domainValueModel.getRowCount(), 341 TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT)); 342 if (_tabs.getSelectedIndex() != 0) 343 { 344 updateUsage(); 345 } 346 } 347 348 public void setStatusMessage(String msg, boolean isInformative) 349 { 350 _editor.setStatusMessage(msg, isInformative); 351 } 352 353 public void setStatusMessage(Throwable t) 354 { 355 _editor.setStatusMessage(t); 356 } 357 358 public void notifyModified() 359 { 360 } 361 362 void _tabs_stateChanged(ChangeEvent e) 363 { 364 updateUsage(); 365 } 366 367 public void cleanup() 368 { 369 _editor.setProperty(DIVIDER_LOCATION, "" + _splitpanel.getDividerLocation()); 370 } 371 372 void updateUsage() 373 { 374 if (_grdDomains.getSelectedRow() == -1) return; 375 376 DjDomain dom = _domainModel.getDomain(_grdDomains.getSelectedRow()); 377 378 DjExtent[] extents = _editor.getPersistenceManager().getExtentsSorted(); 379 _edtUsage.setText(""); 380 for (int i = 0; i < extents.length; i++) 381 { 382 DjProperty[] props = extents[i].getPropertiesSorted(); 383 for (int p = 0; p < props.length; p++) 384 { 385 if (!extents[i].isInherited(props[p])) 386 { 387 if (props[p].getType().getTypeName().equals(dom.getTypeName())) 388 { 389 _edtUsage.append(extents[i].getName() + "." + props[p].getName() + "\n"); 390 } 391 } 392 } 393 } 394 if (_edtUsage.getText().trim().length() == 0) 395 { 396 _edtUsage.setText(Messages.getString("DomainEditor.NotUsed")); 397 } 398 } 399 400 void _grdDomains_focusGained(FocusEvent e) 401 { 402 setToolbarFocus(_grdDomains, Messages.getString("global.Domains")); 403 } 404 405 private void setToolbarFocus(DjTable table, String title) 406 { 407 _lblActiveTable.setText(title); 408 setSelectedTable(table); 409 } 410 411 void _scrDomains_focusGained(FocusEvent e) 412 { 413 setToolbarFocus(_grdDomains, Messages.getString("global.Domains")); 414 415 } 416 417 void _scrValues_focusGained(FocusEvent e) 418 { 419 setToolbarFocus(_grdValues, Messages.getString("DomainEditor.DomainValues")); 420 421 } 422 423 void _grdValues_focusGained(FocusEvent e) 424 { 425 setToolbarFocus(_grdValues, Messages.getString("DomainEditor.DomainValues")); 426 427 } 428 429 private void tableFirst(JTable table) 430 { 431 if (table.getRowCount() <= 0) return; 432 433 table.setRowSelectionInterval(0, 0); 434 } 435 436 private void tableBottom(JTable table) 437 { 438 int row = table.getRowCount() - 1; 439 if (row != -1) table.setRowSelectionInterval(row, row); 440 } 441 442 private void tableUp(JTable table) 443 { 444 int row = table.getSelectedRow(); 445 if (row > 0) table.setRowSelectionInterval(row - 1, row - 1); 446 } 447 448 private void tableDown(JTable table) 449 { 450 int row = table.getSelectedRow(); 451 if (row < table.getRowCount() - 1) table.setRowSelectionInterval(row + 1, row + 1); 452 } 453 454 void _butDown_actionPerformed(ActionEvent e) 455 { 456 tableDown(getSelectedTable()); 457 } 458 459 void _butDelete_actionPerformed(ActionEvent e) 460 { 461 try 462 { 463 getSelectedTable().deleteRow(); 464 } 465 catch (Exception x) 466 { 467 setStatusMessage(x.getMessage(), false); 468 } 469 } 470 471 void _butBottom_actionPerformed(ActionEvent e) 472 { 473 tableBottom(getSelectedTable()); 474 } 475 476 void _butFirst_actionPerformed(ActionEvent e) 477 { 478 tableFirst(getSelectedTable()); 479 } 480 481 void _butCreate_actionPerformed(ActionEvent e) 482 { 483 try 484 { 485 getSelectedTable().insertRow(); 486 } 487 catch (Exception x) 488 { 489 setStatusMessage(x.getMessage(), false); 490 } 491 } 492 493 void _butUp_actionPerformed(ActionEvent e) 494 { 495 tableUp(getSelectedTable()); 496 } 497 498 private DjTable getSelectedTable() 499 { 500 return _selectedTable; 501 } 502 503 private void setSelectedTable(DjTable table) 504 { 505 _selectedTable = table; 506 } 507 508 public boolean canClose() 509 { 510 return true; 511 } 512 513 public boolean close() 514 { 515 _editor.toggleDomainsPanel(); 516 return true; 517 } 518 519 void cbbPackage_actionPerformed(ActionEvent e) 520 { 521 filterDomains(); 522 } 523 524 } | Popular Tags |