KickJava   Java API By Example, From Geeks To Geeks.

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


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.domaineditor;
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 import java.util.Arrays JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.Comparator JavaDoc;
39
40 import javax.swing.DefaultComboBoxModel JavaDoc;
41 import javax.swing.JCheckBox JavaDoc;
42 import javax.swing.JComboBox JavaDoc;
43 import javax.swing.JTable JavaDoc;
44 import javax.swing.event.TableModelEvent JavaDoc;
45 import javax.swing.table.TableCellEditor JavaDoc;
46 import javax.swing.table.TableColumn JavaDoc;
47 import javax.swing.table.TableColumnModel JavaDoc;
48
49 import com.genimen.djeneric.language.Messages;
50 import com.genimen.djeneric.repository.DjDomain;
51 import com.genimen.djeneric.repository.DjPackage;
52 import com.genimen.djeneric.repository.DjPersistenceManager;
53 import com.genimen.djeneric.tools.modeler.ModelEditor;
54 import com.genimen.djeneric.ui.DjCellEditor;
55 import com.genimen.djeneric.ui.DjCheckCellRenderer;
56 import com.genimen.djeneric.ui.DjModelColumn;
57 import com.genimen.djeneric.ui.DjTable;
58 import com.genimen.djeneric.ui.DjTableModel;
59 import com.genimen.djeneric.util.DjString;
60
61 public class DomainTableModel extends DjTableModel
62 {
63   private static final long serialVersionUID = 1L;
64   private DjPersistenceManager _mgr;
65
66   private final int IDX_NAME = 0;
67   private final int IDX_TYPE = 1;
68   private final int IDX_LENGTH = 2;
69   private final int IDX_CASE_CONVERSION = 3;
70   private final int IDX_DECIMALS = 4;
71   private final int IDX_SUPER = 5;
72   private final int IDX_ENFORCE = 6;
73   private final int IDX_FORMAT_MASK = 7;
74   private final int IDX_RE_MASK = 8;
75   private final int IDX_RE_FAIL_MSG = 9;
76   private final int IDX_COMPONENT = 10;
77   private final int IDX_PACKAGE = 11;
78   private final int IDX_WIDTH = 12;
79   private final int IDX_HEIGHT = 13;
80   private final int IDX_DESCRIPTION = 14;
81   private final int _colCount = 15;
82
83   DjDomain _noneDomain = new DjDomain();
84
85   DjPackage _defaultPackage = new DjPackage(Messages
86                                                        .getString("ExtentSelectorDialog.DefaultPackage"));
87   DjPackage _allPackages = new DjPackage(Messages
88                                                        .getString("ExtentSelectorDialog.AllPackages"));
89
90   ArrayList JavaDoc _data = new ArrayList JavaDoc();
91   DjPackage _filterPackage = _allPackages;
92
93   public DomainTableModel(DjPersistenceManager mgr)
94   {
95     _mgr = mgr;
96
97     _columns = new DjModelColumn[_colCount];
98
99     _columns[IDX_NAME] = new DjModelColumn(Messages.getString("global.Name"), 120, Messages
100         .getString("DomainTableModel.NameOfTheDomain"));
101     _columns[IDX_TYPE] = new DjModelColumn(Messages.getString("global.Type"), 80, Messages
102         .getString("DomainTableModel.UnderlyingType"));
103     _columns[IDX_LENGTH] = new DjModelColumn(Messages.getString("DomainTableModel.Len"), 50, Messages
104         .getString("DomainTableModel.MaximumLength"));
105     _columns[IDX_CASE_CONVERSION] = new DjModelColumn(Messages.getString("DomainTableModel.CaseConv"), 90, Messages
106         .getString("DomainTableModel.CaseConvDescr"));
107     _columns[IDX_DECIMALS] = new DjModelColumn(Messages.getString("DomainTableModel.Dec"), 30, Messages
108         .getString("DomainTableModel.Decimals"));
109     _columns[IDX_SUPER] = new DjModelColumn(Messages.getString("global.Super"), 120, Messages
110         .getString("DomainTableModel.SuperToInheritFrom"));
111     _columns[IDX_ENFORCE] = new DjModelColumn(Messages.getString("DomainTableModel.Enforce"), 50, Messages
112         .getString("DomainTableModel.EnforceValueInList"), new DjCheckCellRenderer());
113     _columns[IDX_FORMAT_MASK] = new DjModelColumn(Messages.getString("DomainTableModel.FormatMask"), 100, Messages
114         .getString("DomainTableModel.JavaFormatMask"));
115     _columns[IDX_RE_MASK] = new DjModelColumn(Messages.getString("DomainTableModel.ValidationMask"), 100, Messages
116         .getString("DomainTableModel.RegExp"));
117     _columns[IDX_RE_FAIL_MSG] = new DjModelColumn(Messages.getString("DomainTableModel.MaskFailureMsg"), 100, Messages
118         .getString("DomainTableModel.ValidationMaskMessage"));
119     _columns[IDX_COMPONENT] = new DjModelColumn(Messages.getString("global.Component"), 90, Messages
120         .getString("DomainTableModel.HowToDsp"));
121     _columns[IDX_PACKAGE] = new DjModelColumn(Messages.getString("global.Package"), 90, Messages
122         .getString("DomainTableModel.HowToPackage"));
123     _columns[IDX_WIDTH] = new DjModelColumn(Messages.getString("global.Width"), 40, Messages
124         .getString("DomainTableModel.DisplayWidthPixels"));
125     _columns[IDX_HEIGHT] = new DjModelColumn(Messages.getString("global.Height"), 40, Messages
126         .getString("DomainTableModel.DisplayHeightPixels"));
127     _columns[IDX_DESCRIPTION] = new DjModelColumn(Messages.getString("global.Description"), 200, Messages
128         .getString("DomainTableModel.GeneralDescr"));
129   }
130
131   public TableCellEditor JavaDoc getColumnEditor(int column)
132   {
133     if (column == IDX_TYPE)
134     {
135       JComboBox JavaDoc cbb = new JComboBox JavaDoc(new String JavaDoc[]{"String", "int", "long", "BigDecimal", "Date", "byte[]"});
136       return new DjCellEditor(cbb);
137     }
138     if (column == IDX_CASE_CONVERSION)
139     {
140       JComboBox JavaDoc cbb = new JComboBox JavaDoc(new String JavaDoc[]{Messages.getString("DomainTableModel.CaseNone"),
141           Messages.getString("DomainTableModel.CaseUpper"), Messages.getString("DomainTableModel.CaseLower")});
142       return new DjCellEditor(cbb);
143     }
144     else if (column == IDX_COMPONENT)
145     {
146       return new DjCellEditor(new JComboBox JavaDoc(DjDomain.getComponentTypeNames()));
147     }
148     else if (column == IDX_PACKAGE)
149     {
150       return new DjCellEditor(new JComboBox JavaDoc())
151       {
152         private static final long serialVersionUID = 1L;
153
154         public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row, int column)
155         {
156           ArrayList JavaDoc all = new ArrayList JavaDoc();
157           all.add(_defaultPackage);
158           all.addAll(Arrays.asList(_mgr.getPackages()));
159
160           JComboBox JavaDoc cbb = (JComboBox JavaDoc) editorComponent;
161           cbb.setEditable(false);
162           cbb.setModel(new DefaultComboBoxModel JavaDoc(all.toArray()));
163           delegate.setValue(value);
164           return editorComponent;
165         }
166       };
167     }
168     else if (column == IDX_SUPER)
169     {
170       return new DjCellEditor(new JComboBox JavaDoc())
171       {
172         private static final long serialVersionUID = 1L;
173
174         public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row, int column)
175         {
176           JComboBox JavaDoc cbb = (JComboBox JavaDoc) editorComponent;
177           DjDomain[] curr = _mgr.getDomains();
178           DjDomain[] all = new DjDomain[curr.length];
179           all[0] = _noneDomain;
180           int idx = 1;
181           for (int i = 0; i < curr.length; i++)
182           {
183             if (i != row) all[idx++] = curr[i];
184           }
185
186           cbb.setModel(new DefaultComboBoxModel JavaDoc(all));
187           delegate.setValue(value);
188           return editorComponent;
189         }
190       };
191     }
192     else if (column == IDX_ENFORCE)
193     {
194       return new DjCellEditor(new JCheckBox JavaDoc());
195     }
196
197     return super.getColumnEditor(column);
198   }
199
200   public int getRowCount()
201   {
202     if (_mgr == null) return 0;
203
204     return _data.size();
205   }
206
207   public int getColumnCount()
208   {
209     return _colCount;
210   }
211
212   public Object JavaDoc getValueAt(int nRow, int nCol)
213   {
214     if (nRow < 0 || nRow >= getRowCount()) return "";
215
216     DjDomain c = (DjDomain) _data.get(nRow);
217
218     if (nCol == IDX_NAME) return c.getName();
219     else if (nCol == IDX_CASE_CONVERSION)
220     {
221       String JavaDoc cnv = c.getCaseConversion();
222       if (cnv.equals(DjDomain.CASE_UPPER)) return Messages.getString("DomainTableModel.CaseUpper");
223       if (cnv.equals(DjDomain.CASE_LOWER)) return Messages.getString("DomainTableModel.CaseLower");
224       return Messages.getString("DomainTableModel.CaseNone");
225     }
226     else if (nCol == IDX_SUPER)
227     {
228       DjDomain dom = c.getSuperDomain();
229       if (dom == null) return _noneDomain;
230       return dom;
231     }
232     else if (nCol == IDX_TYPE) return c.getNativeType();
233     else if (nCol == IDX_LENGTH)
234     {
235       if (c.getLength() <= 0) return "";
236       return "" + c.getLength();
237     }
238     else if (nCol == IDX_WIDTH)
239     {
240       if (c.getDisplayWidth() <= 0) return "";
241       return "" + c.getDisplayWidth();
242     }
243     else if (nCol == IDX_HEIGHT)
244     {
245       if (c.getDisplayHeight() <= 0) return "";
246       return "" + c.getDisplayHeight();
247     }
248     else if (nCol == IDX_DECIMALS)
249     {
250       if (c.getDecimals() <= 0) return "";
251       return "" + c.getDecimals();
252     }
253     else if (nCol == IDX_ENFORCE) return new Boolean JavaDoc(c.isEnforced());
254     else if (nCol == IDX_FORMAT_MASK) return c.getFormatMask();
255     else if (nCol == IDX_RE_MASK) return c.getReValidationMask();
256     else if (nCol == IDX_RE_FAIL_MSG) return c.getReFailureMessage();
257     else if (nCol == IDX_COMPONENT) return c.getComponentTypeName();
258     else if (nCol == IDX_PACKAGE)
259     {
260       DjPackage p = c.getPackage();
261       if (p == null) return _defaultPackage;
262       else return p;
263     }
264     else if (nCol == IDX_DESCRIPTION) return c.getDescription();
265     else return Messages.getString("global.InvalidColumn", String.valueOf(nCol));
266   }
267
268   public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex)
269   {
270     if (rowIndex < 0 || rowIndex >= getRowCount()) return;
271     if (aValue == null) return;
272     notifyModified();
273
274     try
275     {
276       DjDomain c = (DjDomain) _data.get(rowIndex);
277       if (columnIndex == IDX_NAME)
278       {
279         c.setName(aValue.toString());
280         _mgr.updateDomainHashes();
281       }
282       else if (columnIndex == IDX_CASE_CONVERSION)
283       {
284         String JavaDoc cnv = aValue.toString();
285         if (cnv.equals(Messages.getString("DomainTableModel.CaseUpper"))) c.setCaseConversion(DjDomain.CASE_UPPER);
286         else if (cnv.equals(Messages.getString("DomainTableModel.CaseLower"))) c.setCaseConversion(DjDomain.CASE_LOWER);
287         else c.setCaseConversion(DjDomain.CASE_NONE);
288       }
289       else if (columnIndex == IDX_SUPER)
290       {
291         if (aValue == _noneDomain) c.setSuperDomain(null);
292         else c.setSuperDomain((DjDomain) aValue);
293       }
294       else if (columnIndex == IDX_TYPE) c.setNativeType(aValue.toString());
295       else if (columnIndex == IDX_LENGTH) c.setLength(convert2Int(aValue.toString()));
296       else if (columnIndex == IDX_WIDTH) c.setDisplayWidth(convert2Int(aValue.toString()));
297       else if (columnIndex == IDX_HEIGHT) c.setDisplayHeight(convert2Int(aValue.toString()));
298       else if (columnIndex == IDX_DECIMALS) c.setDecimals(convert2Int(aValue.toString()));
299       else if (columnIndex == IDX_ENFORCE) c.setEnforced(((Boolean JavaDoc) aValue).booleanValue());
300       else if (columnIndex == IDX_FORMAT_MASK) c.setFormatMask(aValue.toString());
301       else if (columnIndex == IDX_RE_MASK) c.setReValidationMask(aValue.toString());
302       else if (columnIndex == IDX_RE_FAIL_MSG) c.setReFailureMessage(aValue.toString());
303       else if (columnIndex == IDX_COMPONENT) c.setComponentType(aValue.toString());
304       else if (columnIndex == IDX_PACKAGE)
305       {
306         DjPackage p = (DjPackage) aValue;
307         if (p == _defaultPackage) p = null;
308         c.setPackage(p);
309       }
310       else if (columnIndex == IDX_DESCRIPTION) c.setDescription(aValue.toString());
311       setStatusMessage(ModelEditor.OK_MSG);
312     }
313     catch (Exception JavaDoc x)
314     {
315       setStatusMessage(x);
316     }
317   }
318
319   public boolean isCellEditable(int row, int column)
320   {
321     if (!isEditable()) return false;
322
323     if ((column == IDX_FORMAT_MASK) || (column == IDX_PACKAGE) || (column == IDX_COMPONENT) || (column == IDX_WIDTH)
324         || (column == IDX_HEIGHT)) return true;
325
326     DjDomain c = (DjDomain) _data.get(row);
327     if (c.getSuperDomain() != null && column == IDX_TYPE) return false;
328     return !c.isBuiltIn();
329   }
330
331   int counter = 1;
332
333   public int insertRow(int atIdx)
334   {
335     if (!isEditable()) return -1;
336     try
337     {
338       DjDomain c = new DjDomain(Messages.getString("global.newSomething") + counter++, "String", 0, 0, "", Messages
339           .getString("DomainTableModel.ANewDomain"), true);
340
341       if (_filterPackage != _defaultPackage && _filterPackage != _allPackages) c.setPackage(_filterPackage);
342       _mgr.addDomain(c);
343       _data.add(atIdx, c);
344       notifyModified();
345       setStatusMessage(ModelEditor.OK_MSG);
346       return atIdx;
347     }
348     catch (Exception JavaDoc x)
349     {
350       setStatusMessage(x.getMessage(), false);
351     }
352     return -1;
353   }
354
355   public boolean deleteRow(int atIdx)
356   {
357     if (!isEditable()) return false;
358     try
359     {
360       DjDomain c = (DjDomain) _data.get(atIdx);
361       if (c.isBuiltIn())
362       {
363         setStatusMessage(Messages.getString("DomainTableModel.CanNotDeleteBuiltIn"), false);
364         return false;
365       }
366       if (_mgr.isDomainUsed(c))
367       {
368         setStatusMessage(Messages.getString("DomainTableModel.CanNotDeleteUsed"), false);
369         return false;
370       }
371       _mgr.removeDomain(c);
372       _data.remove(c);
373       notifyModified();
374       setStatusMessage(ModelEditor.OK_MSG);
375       return true;
376     }
377     catch (Exception JavaDoc x)
378     {
379       setStatusMessage(x.getMessage(), false);
380     }
381     return false;
382   }
383
384   public MouseAdapter JavaDoc getHeaderClickListener(DjTable table)
385   {
386     return new ColumnListener(table);
387   }
388
389   public void setFilter(DjPackage filterPackage)
390   {
391     _filterPackage = filterPackage;
392     _data = new ArrayList JavaDoc();
393     _data.addAll(Arrays.asList(_mgr.getDomains()));
394     if (_filterPackage != _allPackages)
395     {
396       if (_filterPackage == _defaultPackage) filterPackages(_data, null);
397       else filterPackages(_data, _filterPackage);
398     }
399   }
400
401   private void filterPackages(ArrayList JavaDoc filtered, DjPackage pack)
402   {
403     int i = 0;
404     while (i < filtered.size())
405     {
406       DjDomain dom = (DjDomain) filtered.get(i);
407       if (dom.getPackage() != pack) filtered.remove(i);
408       else i++;
409     }
410   }
411
412   public DjPackage getAllPackagesPackage()
413   {
414     return _allPackages;
415   }
416
417   public DjPackage getDefaultPackage()
418   {
419     return _defaultPackage;
420   }
421
422   class ColumnListener extends MouseAdapter JavaDoc
423   {
424     protected DjTable _table;
425     boolean _sortAsc = true;
426     int _sortCol = -1;
427
428     public ColumnListener(DjTable table)
429     {
430       _table = table;
431     }
432
433     public void mouseClicked(MouseEvent JavaDoc e)
434     {
435       TableColumnModel JavaDoc colModel = _table.getColumnModel();
436       int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
437       int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();
438       if (modelIndex < 0) return;
439       if (_sortCol == modelIndex) _sortAsc = !_sortAsc;
440       else _sortCol = modelIndex;
441       for (int i = 0; i < getColumnCount(); i++)
442       {
443         TableColumn JavaDoc column = colModel.getColumn(i);
444         column.setHeaderValue(getColumnName(column.getModelIndex()));
445       }
446       _table.getTableHeader().repaint();
447       Collections.sort(_data, new DomainComparator(modelIndex, _sortAsc));
448       _table.tableChanged(new TableModelEvent JavaDoc(DomainTableModel.this));
449       _table.repaint();
450     }
451   }
452
453   public DjDomain getDomain(int idx)
454   {
455     return (DjDomain) _data.get(idx);
456   }
457
458   public class DomainComparator implements Comparator JavaDoc
459   {
460     int _cellIdx;
461     boolean _sortAsc;
462
463     public DomainComparator(int cellIdx, boolean sortAsc)
464     {
465       _cellIdx = cellIdx;
466       _sortAsc = sortAsc;
467     }
468
469     public int compare(Object JavaDoc o1, Object JavaDoc o2)
470     {
471       DjDomain f1 = (DjDomain) o1;
472       DjDomain f2 = (DjDomain) o2;
473
474       int result = 0;
475
476       if (_cellIdx == IDX_CASE_CONVERSION) result = DjString
477           .safeCompare(f1.getCaseConversion(), f2.getCaseConversion());
478       if (_cellIdx == IDX_COMPONENT) result = DjString
479           .safeCompare(f1.getComponentTypeName(), f2.getComponentTypeName());
480       if (_cellIdx == IDX_DECIMALS) result = f1.getDecimals() - f2.getDecimals();
481       if (_cellIdx == IDX_DESCRIPTION) result = DjString.safeCompare(f1.getDescription(), f2.getDescription());
482       if (_cellIdx == IDX_ENFORCE) result = (f1.isEnforced() ? 1 : 0) - (f2.isEnforced() ? 1 : 0);
483       if (_cellIdx == IDX_FORMAT_MASK) result = DjString.safeCompare(f1.getFormatMask(), f2.getFormatMask());
484       if (_cellIdx == IDX_HEIGHT) result = f1.getDisplayHeight() - f2.getDisplayHeight();
485       if (_cellIdx == IDX_LENGTH) result = f1.getLength() - f2.getLength();
486       if (_cellIdx == IDX_NAME) result = DjString.safeCompare(f1.getName(), f2.getName());
487       if (_cellIdx == IDX_PACKAGE) result = DjString.safeCompare(f1.getPackage(), f2.getPackage());
488       if (_cellIdx == IDX_RE_FAIL_MSG) result = DjString
489           .safeCompare(f1.getReFailureMessage(), f2.getReFailureMessage());
490       if (_cellIdx == IDX_SUPER) result = DjString.safeCompare(f1.getSuperDomain(), f2.getSuperDomain());
491       if (_cellIdx == IDX_TYPE) result = DjString.safeCompare(f1.getNativeType(), f2.getNativeType());
492       if (_cellIdx == IDX_WIDTH) result = f1.getDisplayWidth() - f2.getDisplayWidth();
493
494       if (!_sortAsc) result = -result;
495       return result;
496     }
497   }
498
499 }
Popular Tags