KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > extenteditor > RelationTableModel


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

30 package com.genimen.djeneric.tools.modeler.extenteditor;
31
32 import java.awt.Component JavaDoc;
33 import java.awt.event.MouseAdapter JavaDoc;
34 import java.awt.event.MouseEvent JavaDoc;
35
36 import javax.swing.DefaultComboBoxModel JavaDoc;
37 import javax.swing.JComboBox JavaDoc;
38 import javax.swing.JTable JavaDoc;
39 import javax.swing.JTextField JavaDoc;
40 import javax.swing.event.TableModelEvent JavaDoc;
41 import javax.swing.table.TableCellEditor JavaDoc;
42 import javax.swing.table.TableColumn JavaDoc;
43 import javax.swing.table.TableColumnModel JavaDoc;
44
45 import com.genimen.djeneric.language.Messages;
46 import com.genimen.djeneric.repository.DjExtent;
47 import com.genimen.djeneric.repository.DjPersistenceManager;
48 import com.genimen.djeneric.repository.DjProperty;
49 import com.genimen.djeneric.repository.DjRelation;
50 import com.genimen.djeneric.tools.modeler.ModelEditor;
51 import com.genimen.djeneric.ui.DjCellEditor;
52 import com.genimen.djeneric.ui.DjModelColumn;
53 import com.genimen.djeneric.ui.DjTable;
54 import com.genimen.djeneric.ui.DjTableModel;
55 import com.genimen.djeneric.util.DjLogger;
56
57 public class RelationTableModel extends DjTableModel
58 {
59   private static final long serialVersionUID = 1L;
60   private final int _colCount = 7;
61   private DjExtent _extent;
62   private DjPersistenceManager _mgr;
63
64   private final static String JavaDoc ONE = "1:1";
65   private final static String JavaDoc MANY = "1:n";
66
67   private final int IDX_PROPERTY = 0;
68   private final int IDX_MASTER = 1;
69   private final int IDX_NAME = 2;
70   private final int IDX_CONTAINS = 3;
71   private final int IDX_UNICITY = 4;
72   private final int IDX_RESTRICTION = 5;
73   private final int IDX_DESCRIPTION = 6;
74
75   public RelationTableModel(DjPersistenceManager mgr, DjExtent extent)
76   {
77     _extent = extent;
78     _mgr = mgr;
79
80     _columns = new DjModelColumn[_colCount];
81
82     _columns[IDX_NAME] = new DjModelColumn(Messages.getString("global.Name"), 120, Messages
83         .getString("RelationTableModel.NameOfRel"), new InheritanceIndicationRenderer(extent,
84         InheritanceIndicationRenderer.RELATION));
85     _columns[IDX_MASTER] = new DjModelColumn(Messages.getString("global.Master"), 120, Messages
86         .getString("RelationTableModel.Reference2Master"));
87     _columns[IDX_PROPERTY] = new DjModelColumn(Messages.getString("global.Property"), 100, Messages
88         .getString("RelationTableModel.Property2Master"));
89     _columns[IDX_CONTAINS] = new DjModelColumn(Messages.getString("global.Type"), 80, Messages
90         .getString("RelationTableModel.TypeOfRel"));
91     _columns[IDX_UNICITY] = new DjModelColumn(Messages.getString("RelationTableModel.Unicity"), 80, Messages
92         .getString("RelationTableModel.UnicityDescr"));
93     _columns[IDX_RESTRICTION] = new DjModelColumn(Messages.getString("RelationTableModel.Restriction"), 200, Messages
94         .getString("RelationTableModel.RestrictsSelection"));
95     _columns[IDX_DESCRIPTION] = new DjModelColumn(Messages.getString("global.Description"), 300, Messages
96         .getString("RelationTableModel.DescriptionOfRel"));
97   }
98
99   public TableCellEditor JavaDoc getColumnEditor(int column)
100   {
101     try
102     {
103       if (column == IDX_MASTER)
104       {
105         return new DjCellEditor(new JComboBox JavaDoc(_mgr.getExtentsSorted()));
106       }
107       else if (column == IDX_PROPERTY)
108       {
109         return new DjCellEditor(new JComboBox JavaDoc())
110         {
111           private static final long serialVersionUID = 1L;
112
113           public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row,
114                                                        int column)
115           {
116             JComboBox JavaDoc cbb = (JComboBox JavaDoc) editorComponent;
117             cbb.setModel(new DefaultComboBoxModel JavaDoc(_extent.getPropertyNames()));
118             delegate.setValue(value);
119             return editorComponent;
120           }
121         };
122       }
123       else if (column == IDX_CONTAINS)
124       {
125         return new DjCellEditor(new JComboBox JavaDoc(new String JavaDoc[]{DjPersistenceManager.RELATION_TYPE_CONTAINS,
126             DjPersistenceManager.RELATION_TYPE_REFERENCES}));
127       }
128       else if (column == IDX_UNICITY)
129       {
130         return new DjCellEditor(new JComboBox JavaDoc(new String JavaDoc[]{MANY, ONE}));
131       }
132       else if (column == IDX_RESTRICTION)
133       {
134         JTextField JavaDoc fld = new JTextField JavaDoc();
135         fld.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 12));
136         return new DjCellEditor(fld);
137       }
138
139       return super.getColumnEditor(column);
140     }
141     catch (Exception JavaDoc x)
142     {
143       DjLogger.log(x);
144       return null;
145     }
146   }
147
148   public boolean isCellEditable(int row, int column)
149   {
150     if (!isEditable()) return false;
151
152     DjRelation r = _extent.getMasterRelation(row);
153     if (_extent.isInherited(r)) return false;
154
155     return super.isCellEditable(row, column);
156   }
157
158   public int getRowCount()
159   {
160     if (_extent == null) return 0;
161
162     return _extent.getMasterRelationCount();
163   }
164
165   public DjExtent getExtent()
166   {
167     return _extent;
168   }
169
170   public int getColumnCount()
171   {
172     return _colCount;
173   }
174
175   public Object JavaDoc getValueAt(int nRow, int nCol)
176   {
177     if (nRow < 0 || nRow >= getRowCount()) return "";
178
179     DjRelation r = _extent.getMasterRelation(nRow);
180     if (nCol == IDX_NAME) return r.getName();
181     else if (nCol == IDX_MASTER) return r.getMasterExtent();
182     else if (nCol == IDX_PROPERTY)
183     {
184       DjProperty c = r.getDetailProperty();
185       if (c != null) return c.getName();
186       return null;
187     }
188     else if (nCol == IDX_CONTAINS) return (r.isDetailsContained()
189         ? DjPersistenceManager.RELATION_TYPE_CONTAINS
190         : DjPersistenceManager.RELATION_TYPE_REFERENCES);
191     else if (nCol == IDX_UNICITY)
192     {
193       if (r.getUnicity() == DjRelation.ONE_TO_ONE) return ONE;
194       return MANY;
195     }
196     else if (nCol == IDX_RESTRICTION) return r.getRestrictionExpression();
197     else if (nCol == IDX_DESCRIPTION) return r.getDescription();
198     else return Messages.getString("global.InvalidColumn", String.valueOf(nCol));
199   }
200
201   public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex)
202   {
203     if (rowIndex < 0 || rowIndex >= getRowCount()) return;
204     if (aValue == null) return;
205
206     notifyModified();
207     try
208     {
209       DjRelation r = _extent.getMasterRelation(rowIndex);
210       if (columnIndex == IDX_NAME) r.setName(aValue.toString());
211       else if (columnIndex == IDX_MASTER) r.setMasterExtent((DjExtent) aValue);
212       else if (columnIndex == IDX_PROPERTY) r.setDetailProperty((String JavaDoc) aValue);
213       else if (columnIndex == IDX_CONTAINS) r.setDetailsContained(aValue.toString()
214           .equals(DjPersistenceManager.RELATION_TYPE_CONTAINS));
215       else if (columnIndex == IDX_UNICITY)
216       {
217         if (ONE.equalsIgnoreCase((String JavaDoc) aValue)) r.setUnicity(DjRelation.ONE_TO_ONE);
218         else r.setUnicity(DjRelation.ONE_TO_MANY);
219       }
220       else if (columnIndex == IDX_RESTRICTION)
221       {
222         r.setRestrictionExpression((String JavaDoc) aValue);
223         // Now try to get the restriction to force a validation:
224
r.getRestriction();
225       }
226       else if (columnIndex == IDX_DESCRIPTION) r.setDescription(aValue.toString());
227       setStatusMessage(ModelEditor.OK_MSG);
228     }
229     catch (Exception JavaDoc x)
230     {
231       setStatusMessage(x);
232     }
233
234   }
235
236   int counter = 1;
237
238   public int insertRow(int atIdx)
239   {
240     if (!isEditable()) return -1;
241     try
242     {
243
244       DjRelation r = _mgr.createRelation(getExtent().getDefaultMasterRelationName(), null, _extent, null, true, "");
245       _extent.addMasterRelation(r);
246       notifyModified();
247       setStatusMessage(ModelEditor.OK_MSG);
248       return _extent.getMasterRelationCount() - 1;
249     }
250     catch (Exception JavaDoc x)
251     {
252       setStatusMessage(x.getMessage(), false);
253     }
254     return -1;
255   }
256
257   public boolean deleteRow(int atIdx)
258   {
259     if (!isEditable()) return false;
260     try
261     {
262       DjRelation r = _extent.getMasterRelation(atIdx);
263       if (_extent.isInherited(r))
264       {
265         setStatusMessage(Messages.getString("RelationTableModel.CanNotDelRelInherited", _extent.getSuper().getName()),
266                          false);
267         return false;
268       }
269
270       DjProperty detailProp = _extent.getMasterRelation(atIdx).getDetailProperty();
271       _extent.removeMasterRelation(_extent.getMasterRelation(atIdx));
272       _extent.removeProperty(detailProp);
273       notifyModified();
274       setStatusMessage(ModelEditor.OK_MSG);
275       return true;
276     }
277     catch (Exception JavaDoc x)
278     {
279       setStatusMessage(x);
280     }
281     return false;
282   }
283
284   public MouseAdapter JavaDoc getHeaderClickListener(DjTable table)
285   {
286     return new ColumnListener(table);
287   }
288
289   class ColumnListener extends MouseAdapter JavaDoc
290   {
291     protected DjTable _table;
292     boolean _sortAsc = true;
293     int _sortCol = -1;
294
295     public ColumnListener(DjTable table)
296     {
297       _table = table;
298     }
299
300     public void mouseClicked(MouseEvent JavaDoc e)
301     {
302       TableColumnModel JavaDoc colModel = _table.getColumnModel();
303       int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
304       int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();
305
306       if (modelIndex < 0) return;
307       if (_sortCol == modelIndex) _sortAsc = !_sortAsc;
308       else _sortCol = modelIndex;
309
310       for (int i = 0; i < getColumnCount(); i++)
311       {
312         TableColumn JavaDoc column = colModel.getColumn(i);
313         column.setHeaderValue(getColumnName(column.getModelIndex()));
314       }
315       _table.getTableHeader().repaint();
316
317       // Collections.sort(_mgr.getPeople(), new PersonComparator(modelIndex,
318
// _sortAsc));
319
_table.tableChanged(new TableModelEvent JavaDoc(RelationTableModel.this));
320       _table.repaint();
321     }
322   }
323 }
Popular Tags