KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > domaineditor > DomainEditor


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  * Redistribution and use in source and binary forms, with or without
4  * modification, is permitted provided that the following conditions are met: -
5  * Redistributions of source code must retain the above copyright notice, this
6  * list of conditions and the following disclaimer. - Redistributions in binary
7  * form must reproduce the above copyright notice, this list of conditions and
8  * the following disclaimer in the documentation and/or other materials
9  * provided with the distribution. - All advertising materials mentioning
10  * features or use of this software must display the following acknowledgment:
11  * "This product includes Djeneric." - Products derived from this software may
12  * not be called "Djeneric" nor may "Djeneric" appear in their names without
13  * prior written permission of Genimen BV. - Redistributions of any form
14  * whatsoever must retain the following acknowledgment: "This product includes
15  * Djeneric." THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND
16  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
17  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV,
19  * DJENERIC.ORG, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */

27 package com.genimen.djeneric.tools.modeler.domaineditor;
28
29 import java.awt.BorderLayout JavaDoc;
30 import java.awt.Dimension JavaDoc;
31 import java.awt.event.ActionEvent JavaDoc;
32 import java.awt.event.FocusEvent JavaDoc;
33 import java.awt.event.MouseAdapter JavaDoc;
34 import java.awt.event.MouseEvent JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Arrays JavaDoc;
37
38 import javax.swing.DefaultComboBoxModel JavaDoc;
39 import javax.swing.DefaultListSelectionModel JavaDoc;
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JComboBox JavaDoc;
42 import javax.swing.JLabel JavaDoc;
43 import javax.swing.JPanel JavaDoc;
44 import javax.swing.JScrollPane JavaDoc;
45 import javax.swing.JSplitPane JavaDoc;
46 import javax.swing.JTabbedPane JavaDoc;
47 import javax.swing.JTable JavaDoc;
48 import javax.swing.JTextArea JavaDoc;
49 import javax.swing.ListSelectionModel JavaDoc;
50 import javax.swing.event.ChangeEvent JavaDoc;
51 import javax.swing.event.ListSelectionEvent JavaDoc;
52 import javax.swing.event.ListSelectionListener JavaDoc;
53 import javax.swing.event.TableModelEvent JavaDoc;
54 import javax.swing.table.JTableHeader JavaDoc;
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 JavaDoc implements ListSelectionListener JavaDoc, DjStatusListener, DjCloseable
68 {
69   private static final long serialVersionUID = 1L;
70   final static String JavaDoc DIVIDER_LOCATION = "domaineditor.dividerloc";
71
72   JScrollPane JavaDoc _scrDomains = new JScrollPane JavaDoc();
73   DjTable _grdDomains = new DjTable();
74   ModelEditor _editor;
75   DomainTableModel _domainModel;
76   DomainValueTableModel _domainValueModel;
77   ListSelectionModel JavaDoc _selectionModel;
78   JTabbedPane JavaDoc _tabs = new JTabbedPane JavaDoc();
79   JScrollPane JavaDoc _scrValues = new JScrollPane JavaDoc();
80   JScrollPane JavaDoc _scrUsage = new JScrollPane JavaDoc();
81   JTextArea JavaDoc _edtUsage = new JTextArea JavaDoc();
82   DjTable _grdValues = new DjTable();
83   JSplitPane JavaDoc _splitpanel = new JSplitPane JavaDoc();
84   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
85   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
86   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
87   JButton JavaDoc _butUp = new JButton JavaDoc();
88   JButton JavaDoc _butDelete = new JButton JavaDoc();
89   JButton JavaDoc _butDown = new JButton JavaDoc();
90   JButton JavaDoc _butBottom = new JButton JavaDoc();
91   JButton JavaDoc _butFirst = new JButton JavaDoc();
92   JButton JavaDoc _butCreate = new JButton JavaDoc();
93   JPanel JavaDoc jPanel10 = new JPanel JavaDoc();
94   JLabel JavaDoc _lblActiveTable = new JLabel JavaDoc();
95   DjTable _selectedTable = _grdDomains;
96   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
97   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
98   JLabel JavaDoc _lblPackage = new JLabel JavaDoc();
99   JComboBox JavaDoc _cbbPackage = new JComboBox JavaDoc();
100
101   public DomainEditor(ModelEditor editor)
102   {
103     _editor = editor;
104
105     try
106     {
107       jbInit();
108       _scrDomains.addMouseListener(new MouseAdapter JavaDoc()
109       {
110         public void mouseClicked(MouseEvent JavaDoc e)
111         {
112           doubleClick(e, _grdDomains);
113         }
114       });
115       _scrValues.addMouseListener(new MouseAdapter JavaDoc()
116       {
117         public void mouseClicked(MouseEvent JavaDoc e)
118         {
119           doubleClick(e, _grdValues);
120         }
121       });
122       String JavaDoc divLoc = _editor.getProperty(DIVIDER_LOCATION, "200");
123       _splitpanel.setDividerLocation(Integer.parseInt(divLoc));
124
125       ArrayList JavaDoc packs = new ArrayList JavaDoc();
126       packs.add(_domainModel.getAllPackagesPackage());
127       packs.add(_domainModel.getDefaultPackage());
128       packs.addAll(Arrays.asList(editor.getPersistenceManager().getPackages()));
129       _cbbPackage.setModel(new DefaultComboBoxModel JavaDoc(packs.toArray()));
130
131       filterDomains();
132
133       JTableHeader JavaDoc th = _grdDomains.getTableHeader();
134       th.addMouseListener(_domainModel.getHeaderClickListener(_grdDomains));
135
136     }
137     catch (Exception JavaDoc 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 JavaDoc(_domainModel, -1, _domainModel.getRowCount(),
147         TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
148   }
149
150   private void jbInit() throws Exception JavaDoc
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 JavaDoc(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 JavaDoc()
162     {
163       public void actionPerformed(ActionEvent JavaDoc e)
164       {
165         _butUp_actionPerformed(e);
166       }
167     });
168     _butDelete.addActionListener(new java.awt.event.ActionListener JavaDoc()
169     {
170       public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc(24, 24));
179     _butDown.addActionListener(new java.awt.event.ActionListener JavaDoc()
180     {
181       public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc(24, 24));
190     _butBottom.setPreferredSize(new Dimension JavaDoc(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 JavaDoc()
195     {
196       public void actionPerformed(ActionEvent JavaDoc e)
197       {
198         _butBottom_actionPerformed(e);
199       }
200     });
201     _butFirst.setPreferredSize(new Dimension JavaDoc(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 JavaDoc()
206     {
207       public void actionPerformed(ActionEvent JavaDoc e)
208       {
209         _butFirst_actionPerformed(e);
210       }
211     });
212     _butCreate.setPreferredSize(new Dimension JavaDoc(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 JavaDoc()
217     {
218       public void actionPerformed(ActionEvent JavaDoc e)
219       {
220         _butCreate_actionPerformed(e);
221       }
222     });
223     _grdDomains.addFocusListener(new java.awt.event.FocusAdapter JavaDoc()
224     {
225       public void focusGained(FocusEvent JavaDoc e)
226       {
227         _grdDomains_focusGained(e);
228       }
229     });
230     _lblActiveTable.setText(Messages.getString("global.Domains"));
231     _scrDomains.addFocusListener(new java.awt.event.FocusAdapter JavaDoc()
232     {
233       public void focusGained(FocusEvent JavaDoc e)
234       {
235         _scrDomains_focusGained(e);
236       }
237     });
238     _scrValues.addFocusListener(new java.awt.event.FocusAdapter JavaDoc()
239     {
240       public void focusGained(FocusEvent JavaDoc e)
241       {
242         _scrValues_focusGained(e);
243       }
244     });
245     _grdValues.addFocusListener(new java.awt.event.FocusAdapter JavaDoc()
246     {
247       public void focusGained(FocusEvent JavaDoc 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 JavaDoc()
256     {
257       public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc();
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 JavaDoc("Monospaced", 0, 12));
279     _edtUsage.setEditable(false);
280     _grdValues.setModel(_domainValueModel);
281     _tabs.addChangeListener(new javax.swing.event.ChangeListener JavaDoc()
282     {
283       public void stateChanged(ChangeEvent JavaDoc 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     // There's always 1 or more domains; will succeed
315
}
316
317   public void doubleClick(MouseEvent JavaDoc e, DjTable table)
318   {
319     table.requestFocus();
320     if (e.getClickCount() < 2) return;
321
322     try
323     {
324       table.insertRow();
325     }
326     catch (Exception JavaDoc x)
327     {
328       _editor.setStatusMessage(x);
329     }
330
331   }
332
333   // ListSelectionListener, row selection change of domains table:
334
public void valueChanged(ListSelectionEvent JavaDoc e)
335   {
336     if (_grdDomains.getSelectedRow() != -1)
337     {
338       _domainModel.getDomain(_grdDomains.getSelectedRow()).sort();
339     }
340     _grdValues.tableChanged(new TableModelEvent JavaDoc(_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 JavaDoc msg, boolean isInformative)
349   {
350     _editor.setStatusMessage(msg, isInformative);
351   }
352
353   public void setStatusMessage(Throwable JavaDoc t)
354   {
355     _editor.setStatusMessage(t);
356   }
357
358   public void notifyModified()
359   {
360   }
361
362   void _tabs_stateChanged(ChangeEvent JavaDoc 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 JavaDoc e)
401   {
402     setToolbarFocus(_grdDomains, Messages.getString("global.Domains"));
403   }
404
405   private void setToolbarFocus(DjTable table, String JavaDoc title)
406   {
407     _lblActiveTable.setText(title);
408     setSelectedTable(table);
409   }
410
411   void _scrDomains_focusGained(FocusEvent JavaDoc e)
412   {
413     setToolbarFocus(_grdDomains, Messages.getString("global.Domains"));
414
415   }
416
417   void _scrValues_focusGained(FocusEvent JavaDoc e)
418   {
419     setToolbarFocus(_grdValues, Messages.getString("DomainEditor.DomainValues"));
420
421   }
422
423   void _grdValues_focusGained(FocusEvent JavaDoc e)
424   {
425     setToolbarFocus(_grdValues, Messages.getString("DomainEditor.DomainValues"));
426
427   }
428
429   private void tableFirst(JTable JavaDoc table)
430   {
431     if (table.getRowCount() <= 0) return;
432
433     table.setRowSelectionInterval(0, 0);
434   }
435
436   private void tableBottom(JTable JavaDoc table)
437   {
438     int row = table.getRowCount() - 1;
439     if (row != -1) table.setRowSelectionInterval(row, row);
440   }
441
442   private void tableUp(JTable JavaDoc table)
443   {
444     int row = table.getSelectedRow();
445     if (row > 0) table.setRowSelectionInterval(row - 1, row - 1);
446   }
447
448   private void tableDown(JTable JavaDoc 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 JavaDoc e)
455   {
456     tableDown(getSelectedTable());
457   }
458
459   void _butDelete_actionPerformed(ActionEvent JavaDoc e)
460   {
461     try
462     {
463       getSelectedTable().deleteRow();
464     }
465     catch (Exception JavaDoc x)
466     {
467       setStatusMessage(x.getMessage(), false);
468     }
469   }
470
471   void _butBottom_actionPerformed(ActionEvent JavaDoc e)
472   {
473     tableBottom(getSelectedTable());
474   }
475
476   void _butFirst_actionPerformed(ActionEvent JavaDoc e)
477   {
478     tableFirst(getSelectedTable());
479   }
480
481   void _butCreate_actionPerformed(ActionEvent JavaDoc e)
482   {
483     try
484     {
485       getSelectedTable().insertRow();
486     }
487     catch (Exception JavaDoc x)
488     {
489       setStatusMessage(x.getMessage(), false);
490     }
491   }
492
493   void _butUp_actionPerformed(ActionEvent JavaDoc 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 JavaDoc e)
520   {
521     filterDomains();
522   }
523
524 }
Popular Tags