KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
36
37 import javax.swing.DefaultComboBoxModel JavaDoc;
38 import javax.swing.JCheckBox JavaDoc;
39 import javax.swing.JComboBox JavaDoc;
40 import javax.swing.JTable JavaDoc;
41 import javax.swing.event.TableModelEvent JavaDoc;
42 import javax.swing.table.TableCellEditor JavaDoc;
43 import javax.swing.table.TableColumn JavaDoc;
44 import javax.swing.table.TableColumnModel JavaDoc;
45
46 import com.genimen.djeneric.language.Messages;
47 import com.genimen.djeneric.repository.DjDomain;
48 import com.genimen.djeneric.repository.DjDomainValue;
49 import com.genimen.djeneric.repository.DjExtent;
50 import com.genimen.djeneric.repository.DjPersistenceManager;
51 import com.genimen.djeneric.repository.DjProperty;
52 import com.genimen.djeneric.repository.DjPropertyType;
53 import com.genimen.djeneric.repository.DjRelation;
54 import com.genimen.djeneric.repository.DjSession;
55 import com.genimen.djeneric.repository.exceptions.CatalogException;
56 import com.genimen.djeneric.repository.exceptions.DjenericException;
57 import com.genimen.djeneric.tools.modeler.ModelEditor;
58 import com.genimen.djeneric.ui.DjCellEditor;
59 import com.genimen.djeneric.ui.DjCheckCellRenderer;
60 import com.genimen.djeneric.ui.DjModelColumn;
61 import com.genimen.djeneric.ui.DjTable;
62 import com.genimen.djeneric.ui.DjTableModel;
63 import com.genimen.djeneric.util.DjString;
64
65 public class PropertyTableModel extends DjTableModel
66 {
67   private static final long serialVersionUID = 1L;
68   private DjPersistenceManager _mgr;
69
70   private final int _colCount = 12;
71
72   private final int IDX_SEQ = 0;
73   private final int IDX_NAME = 1;
74   private final int IDX_ALIAS = 2;
75   private final int IDX_PROMPT = 3;
76   private final int IDX_TYPE = 4;
77   private final int IDX_DEFAULT_VALUE = 5;
78   private final int IDX_REQUIRED = 6;
79   private final int IDX_QUERYABLE = 7;
80   private final int IDX_SORT_ORDER = 8;
81   private final int IDX_PART_OF_UID = 9;
82   private final int IDX_DESCRIPTION = 10;
83   private final int IDX_MAPPING = 11;
84
85   private ArrayList JavaDoc _newProperties = new ArrayList JavaDoc();
86
87   public PropertyTableModel(DjPersistenceManager mgr, DjExtent extent)
88   {
89     setExtent(extent);
90     _mgr = mgr;
91
92     _columns = new DjModelColumn[_colCount];
93
94     _columns[IDX_SEQ] = new DjModelColumn(Messages.getString("global.Seq"), 30, Messages
95         .getString("PropertyTableModel.SequentialOrder"), new InheritanceIndicationRenderer(extent,
96         InheritanceIndicationRenderer.PROPERTY));
97     _columns[IDX_NAME] = new DjModelColumn(Messages.getString("global.Name"), 110, Messages
98         .getString("PropertyTableModel.NameOfProp"));
99     _columns[IDX_ALIAS] = new DjModelColumn(Messages.getString("global.Alias"), 110, Messages
100         .getString("PropertyTableModel.AliasOfProp"));
101     _columns[IDX_PROMPT] = new DjModelColumn(Messages.getString("global.Prompt"), 110, Messages
102         .getString("PropertyTableModel.PromptForDsp"));
103     _columns[IDX_TYPE] = new DjModelColumn(Messages.getString("global.Type"), 120, Messages
104         .getString("PropertyTableModel.TypeOfProp"));
105     _columns[IDX_DEFAULT_VALUE] = new DjModelColumn(Messages.getString("global.Default"), 80, Messages
106         .getString("PropertyTableModel.DefaultValue"));
107     _columns[IDX_MAPPING] = new DjModelColumn(Messages.getString("global.Mapping"), 80, Messages
108         .getString("PropertyTableModel.MappingToPersist"));
109     _columns[IDX_REQUIRED] = new DjModelColumn(Messages.getString("global.Req"), 30,
110         Messages.getString("global.IsReq"), new DjCheckCellRenderer());
111     _columns[IDX_PART_OF_UID] = new DjModelColumn(Messages.getString("global.UID"), 30, Messages
112         .getString("global.IsUID"), new DjCheckCellRenderer());
113     _columns[IDX_QUERYABLE] = new DjModelColumn(Messages.getString("global.Qry"), 30, Messages
114         .getString("global.IsQuery"), new DjCheckCellRenderer());
115     _columns[IDX_SORT_ORDER] = new DjModelColumn(Messages.getString("global.SortOrder"), 65, Messages
116         .getString("PropertyTableModel.DefaultSortOrder"));
117     _columns[IDX_DESCRIPTION] = new DjModelColumn(Messages.getString("global.Description"), 300, Messages
118         .getString("PropertyTableModel.DescriptionOrTooltip"));
119   }
120
121   public DjProperty[] getNewProperties()
122   {
123     return (DjProperty[]) _newProperties.toArray(new DjProperty[0]);
124   }
125
126   public void autoAdjustMappings() throws DjenericException
127   {
128     // New properties can be automapped safely (no data affected)
129
_extent.autoAdjustMappings(getNewProperties());
130
131     // If there are any mappings invalid now, we can still remap them
132
// if the extent is empty. If it is not empty then the user has
133
// to do the mapping since this will involve data conversion or
134
// simply loss of data
135
DjProperty[] invalids = _extent.getPropertiesWithInvalidMapping();
136     if (invalids.length > 0)
137     {
138       DjSession session = _mgr.createSession();
139       try
140       {
141         if (_extent.isEmpty(session))
142         {
143           _extent.autoAdjustMappings(invalids);
144         }
145       }
146       finally
147       {
148         session.close();
149       }
150     }
151   }
152
153   public TableCellEditor JavaDoc getColumnEditor(int column)
154   {
155     if (column == IDX_TYPE)
156     {
157       return new DjCellEditor(new JComboBox JavaDoc())
158       {
159         private static final long serialVersionUID = 1L;
160
161         public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row, int column)
162         {
163           JComboBox JavaDoc cbb = (JComboBox JavaDoc) editorComponent;
164           cbb.setEditable(true);
165           cbb.setModel(new DefaultComboBoxModel JavaDoc(_mgr.getValidTypeNames()));
166           delegate.setValue(value);
167           return editorComponent;
168         }
169       };
170     }
171     else if (column == IDX_MAPPING)
172     {
173       return new MappingCellEditor(_extent, this);
174     }
175     else if (column == IDX_REQUIRED || column == IDX_QUERYABLE || column == IDX_PART_OF_UID)
176     {
177       return new DjCellEditor(new JCheckBox JavaDoc());
178     }
179     else if (column == IDX_DEFAULT_VALUE)
180     {
181       return new DefaultValueCellEditor(new JComboBox JavaDoc());
182     }
183
184     return super.getColumnEditor(column);
185   }
186
187   String JavaDoc findNextFreeMapping()
188   {
189     try
190     {
191       return _extent.findNextFreeMapping(DjPersistenceManager.MAPPING_STR);
192     }
193     catch (CatalogException e)
194     {
195       // ignore, try again
196
}
197     try
198     {
199       return _extent.findNextFreeMapping(DjPersistenceManager.MAPPING_NUM);
200     }
201     catch (CatalogException e1)
202     {
203       // ignore, try again
204
}
205     try
206     {
207       return _extent.findNextFreeMapping(DjPersistenceManager.MAPPING_DAT);
208     }
209     catch (CatalogException e2)
210     {
211       setStatusMessage(Messages.getString("PropertyTableModel.NoFreeMaps"), false);
212     }
213
214     return DjPersistenceManager.MAPPING_STR + "01";
215   }
216
217   public int getRowCount()
218   {
219     if (_extent == null) return 0;
220
221     return _extent.getPropertyCount();
222   }
223
224   public int getColumnCount()
225   {
226     return _colCount;
227   }
228
229   public boolean isCellEditable(int row, int column)
230   {
231     if (!isEditable()) return false;
232     DjProperty c = _extent.getProperty(row);
233     if (_extent.isInherited(c)) return false;
234     if (c.getMapping().equals(DjPersistenceManager.MAPPING_OBJECT_ID)) return false;
235
236     return super.isCellEditable(row, column);
237   }
238
239   public Object JavaDoc getValueAt(int nRow, int nCol)
240   {
241     if (nRow < 0 || nRow >= getRowCount()) return "";
242
243     DjProperty c = _extent.getProperty(nRow);
244     if (nCol == IDX_SEQ) return new Integer JavaDoc(c.getSeq());
245     else if (nCol == IDX_SORT_ORDER)
246     {
247       if (c.getSortOrder() == 0) return "";
248       return new Integer JavaDoc(c.getSortOrder());
249     }
250     else if (nCol == IDX_NAME) return c.getName();
251     else if (nCol == IDX_ALIAS) return c.getAlias();
252     else if (nCol == IDX_PROMPT) return c.getPrompt();
253     else if (nCol == IDX_MAPPING) return c.getMapping();
254     else if (nCol == IDX_TYPE) return c.getTypeName();
255     else if (nCol == IDX_DEFAULT_VALUE) return c.getDefaultValue();
256     else if (nCol == IDX_REQUIRED) return new Boolean JavaDoc(c.isRequired());
257     else if (nCol == IDX_PART_OF_UID) return new Boolean JavaDoc(c.isPartOfUID());
258     else if (nCol == IDX_QUERYABLE) return new Boolean JavaDoc(c.isQueryable());
259     else if (nCol == IDX_DESCRIPTION) return c.getDescription();
260     else return Messages.getString("global.InvalidColumn", String.valueOf(nCol));
261   }
262
263   public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex)
264   {
265     if (rowIndex < 0 || rowIndex >= getRowCount()) return;
266     if (aValue == null) return;
267     notifyModified();
268
269     boolean setMessage2Ok = true;
270     try
271     {
272       DjProperty c = _extent.getProperty(rowIndex);
273       if (columnIndex == IDX_SEQ) c.setSeq(convert2Int(aValue.toString()));
274       else if (columnIndex == IDX_SORT_ORDER) c.setSortOrder(convert2Int(aValue.toString()));
275       else if (columnIndex == IDX_NAME)
276       {
277         c.setName(aValue.toString());
278         _extent.updateHashes();
279
280         if (c.getPrompt().equals(Messages.getString("PropertyTableModel.PromptHere")))
281         {
282           c.setPrompt(DjString.initCapNoLowerCase(c.getName()));
283         }
284       }
285       else if (columnIndex == IDX_PROMPT) c.setPrompt(aValue.toString());
286       else if (columnIndex == IDX_ALIAS) c.setAlias(aValue.toString());
287       else if (columnIndex == IDX_MAPPING) c.setMapping(aValue.toString());
288       else if (columnIndex == IDX_TYPE)
289       {
290         DjPropertyType type;
291         try
292         {
293           type = _mgr.getType(aValue.toString());
294         }
295         catch (Exception JavaDoc x)
296         {
297           type = _mgr.looslyFindType(aValue.toString());
298         }
299         c.setType(type);
300       }
301       else if (columnIndex == IDX_DEFAULT_VALUE) c.setDefaultValue(aValue.toString());
302       else if (columnIndex == IDX_REQUIRED)
303       {
304         boolean isReq = ((Boolean JavaDoc) aValue).booleanValue();
305         c.setRequired(isReq);
306       }
307       else if (columnIndex == IDX_PART_OF_UID)
308       {
309         boolean part = ((Boolean JavaDoc) aValue).booleanValue();
310         c.setPartOfUID(part);
311       }
312       else if (columnIndex == IDX_QUERYABLE) c.setQueryable(((Boolean JavaDoc) aValue).booleanValue());
313       else if (columnIndex == IDX_DESCRIPTION) c.setDescription(aValue.toString());
314       if (setMessage2Ok) setStatusMessage(ModelEditor.OK_MSG);
315     }
316     catch (Exception JavaDoc x)
317     {
318       setStatusMessage(x);
319     }
320
321   }
322
323   int counter = 1;
324
325   public int insertRow(int atIdx)
326   {
327     if (!isEditable()) return -1;
328     try
329     {
330       DjProperty c = new DjProperty(Messages.getString("global.newSomething") + counter++, findNextFreeMapping(), null,
331           Messages.getString("PropertyTableModel.PromptHere"), _mgr.getType("String"), false, (_extent
332               .getPropertyCount() + 1) * 10, null, "");
333       _extent.addProperty(c);
334       _newProperties.add(c);
335       notifyModified();
336       setStatusMessage(ModelEditor.OK_MSG);
337       return _extent.getPropertyCount() - 1;
338     }
339     catch (Exception JavaDoc x)
340     {
341       setStatusMessage(x);
342     }
343     return -1;
344   }
345
346   public boolean deleteRow(int atIdx)
347   {
348     if (!isEditable()) return false;
349     try
350     {
351       DjProperty c = _extent.getProperty(atIdx);
352       if (_extent.isInherited(c))
353       {
354         setStatusMessage(Messages.getString("PropertyTableModel.CanNotDeletePropInherited", _extent.getSuper()
355             .getName()), false);
356         return false;
357       }
358
359       DjRelation rels = _extent.getMasterRelationByPropertyName(_extent.getProperty(atIdx).getName());
360       if (rels != null)
361       {
362         setStatusMessage(Messages.getString("PropertyTableModel.CanNotDeletePropInRel", rels.getName()), false);
363         return false;
364       }
365       _extent.removeProperty(_extent.getProperty(atIdx));
366       _newProperties.remove(c);
367       notifyModified();
368       setStatusMessage(ModelEditor.OK_MSG);
369       return true;
370     }
371     catch (Exception JavaDoc x)
372     {
373       setStatusMessage(x);
374     }
375     return false;
376   }
377
378   public MouseAdapter JavaDoc getHeaderClickListener(DjTable table)
379   {
380     return new ColumnListener(table);
381   }
382
383   private final class DefaultValueCellEditor extends DjCellEditor
384   {
385     private static final long serialVersionUID = 1L;
386
387     private DefaultValueCellEditor(JComboBox JavaDoc comboBox)
388     {
389       super(comboBox);
390     }
391
392     public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row, int column)
393     {
394       ArrayList JavaDoc possibles = getValidDefaults(row);
395
396       JComboBox JavaDoc cbb = (JComboBox JavaDoc) editorComponent;
397       cbb.setEditable(true);
398       cbb.setModel(new DefaultComboBoxModel JavaDoc(possibles.toArray()));
399       delegate.setValue(value);
400       return editorComponent;
401     }
402
403     private ArrayList JavaDoc getValidDefaults(int row)
404     {
405       ArrayList JavaDoc possibles = new ArrayList JavaDoc();
406
407       DjProperty prop = _extent.getProperty(row);
408       if (prop != null && prop.getType() instanceof DjDomain)
409       {
410         DjDomain domain = (DjDomain) prop.getType();
411         DjDomainValue[] values = domain.getValidValues();
412         for (int i = 0; i < values.length; i++)
413           possibles.add(values[i].getValue());
414       }
415
416       return possibles;
417     }
418   }
419
420   class ColumnListener extends MouseAdapter JavaDoc
421   {
422     protected DjTable _extent;
423     boolean _sortAsc = true;
424     int _sortCol = -1;
425
426     public ColumnListener(DjTable table)
427     {
428       _extent = table;
429     }
430
431     public void mouseClicked(MouseEvent JavaDoc e)
432     {
433       TableColumnModel JavaDoc colModel = _extent.getColumnModel();
434       int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
435       int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();
436       if (modelIndex < 0) return;
437       if (_sortCol == modelIndex) _sortAsc = !_sortAsc;
438       else _sortCol = modelIndex;
439       for (int i = 0; i < getColumnCount(); i++)
440       {
441         TableColumn JavaDoc column = colModel.getColumn(i);
442         column.setHeaderValue(getColumnName(column.getModelIndex()));
443       }
444       _extent.getTableHeader().repaint();
445       // Collections.sort(_mgr.getPeople(), new PersonComparator(modelIndex,
446
// _sortAsc));
447
_extent.tableChanged(new TableModelEvent JavaDoc(PropertyTableModel.this));
448       _extent.repaint();
449     }
450   }
451 }
Popular Tags