KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > userperspective > PropertyUsageTableModel


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.userperspective;
31
32 import java.awt.Component JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator 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.PopupMenuEvent JavaDoc;
42 import javax.swing.event.PopupMenuListener JavaDoc;
43 import javax.swing.table.TableCellEditor JavaDoc;
44 import javax.swing.text.JTextComponent 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.DjProperty;
51 import com.genimen.djeneric.repository.exceptions.DjenericException;
52 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
53 import com.genimen.djeneric.structure.ExtentUsage;
54 import com.genimen.djeneric.structure.PropertyUsage;
55 import com.genimen.djeneric.structure.RelationDescriptor;
56 import com.genimen.djeneric.tools.modeler.ModelEditor;
57 import com.genimen.djeneric.ui.DjCellEditor;
58 import com.genimen.djeneric.ui.DjCheckCellRenderer;
59 import com.genimen.djeneric.ui.DjModelColumn;
60 import com.genimen.djeneric.ui.DjPropertyPathCompleter;
61 import com.genimen.djeneric.ui.DjTableModel;
62
63 public class PropertyUsageTableModel extends DjTableModel
64 {
65   private static final long serialVersionUID = 1L;
66   private ExtentUsage _extentUsage;
67
68   private final int IDX_SEQ = 0;
69   private final int IDX_COLUMN = 1;
70   private final int IDX_PROMPT = 2;
71   private final int IDX_DESCRIPTION = 3;
72   private final int IDX_REQUIRED = 4;
73   private final int IDX_UPDATEABLE = 5;
74   private final int IDX_QUERYABLE = 6;
75   private final int IDX_DISPLAYED = 7;
76   private final int IDX_DEFAULT_VALUE = 8;
77   private final int IDX_COMPONENT = 9;
78   private final int IDX_WIDTH = 10;
79   private final int IDX_HEIGHT = 11;
80   private final int IDX_SORT_ORDER = 12;
81   private final int IDX_RELATIVE_PATH = 13;
82   private final int IDX_CUSTOM_EDITOR = 14;
83
84   private final int _colCount = 15;
85
86   public PropertyUsageTableModel(ExtentUsage extentUsage)
87   {
88     _extentUsage = extentUsage;
89
90     _columns = new DjModelColumn[_colCount];
91
92     _columns[IDX_SEQ] = new DjModelColumn(Messages.getString("global.Seq"), 30, Messages
93         .getString("PropertyUsageTableModel.SequentialOrder"));
94     _columns[IDX_COLUMN] = new DjModelColumn(Messages.getString("global.Property"), 150, Messages
95         .getString("PropertyUsageTableModel.UnderlyingProp"));
96     _columns[IDX_PROMPT] = new DjModelColumn(Messages.getString("global.Prompt"), 150, Messages
97         .getString("PropertyUsageTableModel.PromptForProp"));
98     _columns[IDX_DESCRIPTION] = new DjModelColumn(Messages.getString("PropertyUsageTableModel.Tooltip"), 150, Messages
99         .getString("PropertyUsageTableModel.Tooltip"));
100     _columns[IDX_REQUIRED] = new DjModelColumn(Messages.getString("global.Req"), 30,
101         Messages.getString("global.IsReq"), new DjCheckCellRenderer());
102     _columns[IDX_UPDATEABLE] = new DjModelColumn(Messages.getString("PropertyUsageTableModel.Upd"), 30, Messages
103         .getString("PropertyUsageTableModel.IsUpd"), new DjCheckCellRenderer());
104     _columns[IDX_QUERYABLE] = new DjModelColumn(Messages.getString("global.Qry"), 30, Messages
105         .getString("global.IsQuery"), new DjCheckCellRenderer());
106     _columns[IDX_DISPLAYED] = new DjModelColumn(Messages.getString("PropertyUsageTableModel.Dsp"), 30, Messages
107         .getString("PropertyUsageTableModel.IsDsp"), new DjCheckCellRenderer());
108     _columns[IDX_DEFAULT_VALUE] = new DjModelColumn(Messages.getString("global.Default"), 80, Messages
109         .getString("PropertyUsageTableModel.DefaultValue"));
110     _columns[IDX_COMPONENT] = new DjModelColumn(Messages.getString("global.Component"), 80, Messages
111         .getString("PropertyUsageTableModel.HowDsp"));
112     _columns[IDX_WIDTH] = new DjModelColumn(Messages.getString("global.Width"), 50, Messages
113         .getString("PropertyUsageTableModel.WidthInPxs"));
114     _columns[IDX_HEIGHT] = new DjModelColumn(Messages.getString("global.Height"), 50, Messages
115         .getString("PropertyUsageTableModel.HeightInPxs"));
116     _columns[IDX_SORT_ORDER] = new DjModelColumn(Messages.getString("PropertyUsageTableModel.SortOrder"), 35, Messages
117         .getString("PropertyUsageTableModel.HowToSort"));
118     _columns[IDX_RELATIVE_PATH] = new DjModelColumn(Messages.getString("PropertyUsageTableModel.RelativePath"), 150,
119         Messages.getString("PropertyUsageTableModel.RelativePathDescr"));
120     _columns[IDX_CUSTOM_EDITOR] = new DjModelColumn(Messages.getString("global.CustomEditor"), 150, Messages
121         .getString("PropertyUsageTableModel.CustomEditor"));
122   }
123
124   public DjExtent getExtent()
125   {
126     return _extentUsage.getExtent();
127   }
128
129   public DjProperty[] getAvailableProperties(DjProperty selectedProperty)
130   {
131     ArrayList JavaDoc result = new ArrayList JavaDoc();
132     result.add(selectedProperty);
133     DjExtent extent = _extentUsage.getExtent();
134     for (int i = 0; i < extent.getPropertyCount(); i++)
135     {
136       boolean found = false;
137       for (int j = 0; j < _extentUsage.getPropertyUsageCount(); j++)
138       {
139         if (_extentUsage.getPropertyUsage(j).getBaseProperty().getInternalId() == extent.getProperty(i).getInternalId())
140         {
141           found = true;
142           break;
143         }
144       }
145       if (!found) result.add(extent.getProperty(i));
146     }
147     return (DjProperty[]) result.toArray(new DjProperty[0]);
148   }
149
150   public TableCellEditor JavaDoc getColumnEditor(int column)
151   {
152     if (column == IDX_COLUMN)
153     {
154       return new DjCellEditor(new JComboBox JavaDoc())
155       {
156         private static final long serialVersionUID = 1L;
157
158         public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row, int column)
159         {
160           JComboBox JavaDoc cbb = (JComboBox JavaDoc) editorComponent;
161           cbb.setModel(new DefaultComboBoxModel JavaDoc(getAvailableProperties(_extentUsage.getPropertyUsage(row)
162               .getBaseProperty())));
163           delegate.setValue(value);
164           return editorComponent;
165         }
166       };
167     }
168     else if (column == IDX_RELATIVE_PATH)
169     {
170       return new CompletionCellEditor(new JComboBox JavaDoc());
171     }
172     else if (column == IDX_DEFAULT_VALUE)
173     {
174       return new DefaultValueCellEditor(new JComboBox JavaDoc());
175     }
176     else if (column == IDX_COMPONENT)
177     {
178       return new DjCellEditor(new JComboBox JavaDoc(DjDomain.getComponentTypeNames()));
179     }
180     else if (column == IDX_REQUIRED || column == IDX_UPDATEABLE || column == IDX_QUERYABLE || column == IDX_DISPLAYED)
181     {
182       return new DjCellEditor(new JCheckBox JavaDoc());
183     }
184
185     return _columns[column].getCellEditor();
186   }
187
188   public int getRowCount()
189   {
190     if (_extentUsage == null) return 0;
191
192     return _extentUsage.getPropertyUsageCount();
193   }
194
195   public int getPropertyCount()
196   {
197     return _colCount;
198   }
199
200   public Object JavaDoc getValueAt(int nRow, int nCol)
201   {
202     if (nRow < 0 || nRow >= getRowCount()) return "";
203
204     PropertyUsage c = _extentUsage.getPropertyUsage(nRow);
205     if (nCol == IDX_SEQ) return new Integer JavaDoc(c.getSeq());
206     else if (nCol == IDX_COLUMN) return c.getBaseProperty();
207     else if (nCol == IDX_PROMPT) return c.getPrompt();
208     else if (nCol == IDX_DESCRIPTION) return c.getDescription();
209     else if (nCol == IDX_DEFAULT_VALUE) return c.getDefaultValue();
210     else if (nCol == IDX_COMPONENT) return c.getComponentTypeName();
211     else if (nCol == IDX_WIDTH)
212     {
213       if (c.getDisplayWidth() <= 0) return "";
214       return new Integer JavaDoc(c.getDisplayWidth());
215     }
216     else if (nCol == IDX_HEIGHT)
217     {
218       if (c.getDisplayHeight() <= 0) return "";
219       return new Integer JavaDoc(c.getDisplayHeight());
220     }
221     else if (nCol == IDX_SORT_ORDER)
222     {
223       if (c.getSortOrder() == 0) return "";
224       return new Integer JavaDoc(c.getSortOrder());
225     }
226     else if (nCol == IDX_REQUIRED) return new Boolean JavaDoc(c.isRequired());
227     else if (nCol == IDX_QUERYABLE) return new Boolean JavaDoc(c.isQueryable());
228     else if (nCol == IDX_DISPLAYED) return new Boolean JavaDoc(c.isDisplayed());
229     else if (nCol == IDX_UPDATEABLE) return new Boolean JavaDoc(c.isUpdateable());
230     else if (nCol == IDX_RELATIVE_PATH) return c.getRelativePath();
231     else if (nCol == IDX_CUSTOM_EDITOR)
232     {
233       if (c.isCustomComponent())
234       {
235         return c.getCustomEditorClass();
236       }
237       else return "";
238     }
239     else return Messages.getString("PropertyUsageTableModel.InvalidCell", String.valueOf(nCol));
240   }
241
242   public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex)
243   {
244     if (rowIndex < 0 || rowIndex >= getRowCount()) return;
245     if (aValue == null) return;
246     notifyModified();
247
248     boolean setMsgToOk = true;
249
250     try
251     {
252       PropertyUsage c = _extentUsage.getPropertyUsage(rowIndex);
253       if (columnIndex == IDX_SEQ)
254       {
255         c.setSeq(convert2Int(aValue.toString()));
256       }
257       else if (columnIndex == IDX_WIDTH)
258       {
259         c.setDisplayWidth(convert2Int(aValue.toString()));
260       }
261       else if (columnIndex == IDX_HEIGHT)
262       {
263         c.setDisplayHeight(convert2Int(aValue.toString()));
264       }
265       else if (columnIndex == IDX_SORT_ORDER)
266       {
267         c.setSortOrder(convert2Int(aValue.toString()));
268       }
269       else if (columnIndex == IDX_COLUMN)
270       {
271         if (c.getBaseProperty().getInternalId() == ((DjProperty) aValue).getInternalId())
272         {
273           // nothing changed
274
return;
275         }
276         c.setProperty((DjProperty) aValue);
277         c.setPrompt(((DjProperty) aValue).getPrompt());
278         c.setRequired(((DjProperty) aValue).isRequired());
279         c.setComponentType(((DjProperty) aValue).getType().getComponentType());
280         fireTableRowsUpdated(rowIndex, rowIndex);
281       }
282       else if (columnIndex == IDX_PROMPT) c.setPrompt(aValue.toString());
283       else if (columnIndex == IDX_DESCRIPTION) c.setDescription(aValue.toString());
284       else if (columnIndex == IDX_DEFAULT_VALUE) c.setDefaultValue(aValue.toString());
285       else if (columnIndex == IDX_COMPONENT) c.setComponentType(aValue.toString());
286       else if (columnIndex == IDX_REQUIRED) c.setRequired(((Boolean JavaDoc) aValue).booleanValue());
287       else if (columnIndex == IDX_QUERYABLE) c.setQueryable(((Boolean JavaDoc) aValue).booleanValue());
288       else if (columnIndex == IDX_DISPLAYED) c.setDisplayed(((Boolean JavaDoc) aValue).booleanValue());
289       else if (columnIndex == IDX_UPDATEABLE) c.setUpdateable(((Boolean JavaDoc) aValue).booleanValue());
290       else if (columnIndex == IDX_RELATIVE_PATH)
291       {
292         c.setRelativePath(aValue.toString());
293         if (!c.hasValidRelativePath())
294         {
295           setStatusMessage(Messages.getString("PropertyUsageTableModel.InvalidPath", aValue.toString()), false);
296           setMsgToOk = false;
297         }
298       }
299       else if (columnIndex == IDX_CUSTOM_EDITOR) c.setCustomEditorClass(aValue.toString());
300       if (setMsgToOk) setStatusMessage(ModelEditor.OK_MSG);
301     }
302     catch (Exception JavaDoc x)
303     {
304       setStatusMessage(x);
305     }
306
307   }
308
309   DjProperty getNextProperty() throws DjenericException
310   {
311     DjExtent extent = _extentUsage.getExtent();
312
313     RelationDescriptor via = _extentUsage.getVia();
314     String JavaDoc detailProp = "";
315     if (via != null) detailProp = via.getDetailPropertyName();
316
317     for (int i = 0; i < extent.getPropertyCount(); i++)
318     {
319       boolean found = false;
320       DjProperty mightAdd = extent.getProperty(i);
321
322       // Skip the object_id column and
323
// skip the property that is in fact the relation to the parent
324
if (mightAdd.getName().equals(extent.getIdProperty().getName()) || mightAdd.getName().equals(detailProp)) continue;
325
326       for (int j = 0; j < _extentUsage.getPropertyUsageCount(); j++)
327       {
328         DjProperty prop = _extentUsage.getPropertyUsage(j).getBaseProperty();
329         if (prop.getInternalId() == mightAdd.getInternalId()
330             && _extentUsage.getPropertyUsage(j).getRelativePath() == null)
331         {
332           found = true;
333           break;
334         }
335       }
336       if (!found) return mightAdd;
337     }
338
339     return null;
340   }
341
342   int counter = 1;
343
344   public int insertRow(int atIdx)
345   {
346     if (_extentUsage == null) return -1;
347     try
348     {
349       DjProperty nextCol = getNextProperty();
350       if (nextCol == null)
351       {
352         setStatusMessage(Messages.getString("PropertyUsageTableModel.AllPropsAdded"));
353         return -1;
354       }
355
356       PropertyUsage c = new PropertyUsage(nextCol);
357       c.setComponentType(nextCol.getType().getComponentType());
358       c.setSortOrder(nextCol.getSortOrder());
359       c.setQueryable(nextCol.isQueryable());
360       _extentUsage.addPropertyUsage(atIdx, c);
361       notifyModified();
362       setStatusMessage(ModelEditor.OK_MSG);
363       return atIdx;
364     }
365     catch (Exception JavaDoc x)
366     {
367       setStatusMessage(x.getMessage(), false);
368     }
369     return -1;
370   }
371
372   public boolean deleteRow(int atIdx)
373   {
374     try
375     {
376       _extentUsage.removePropertyUsage(atIdx);
377       notifyModified();
378       setStatusMessage(ModelEditor.OK_MSG);
379       return true;
380     }
381     catch (Exception JavaDoc x)
382     {
383       setStatusMessage(x.getMessage(), false);
384     }
385     return false;
386   }
387
388   public boolean isCellEditable(int row, int column)
389   {
390     if (column == IDX_CUSTOM_EDITOR)
391     {
392       PropertyUsage c = _extentUsage.getPropertyUsage(row);
393       return c.isCustomComponent();
394     }
395     else if (column == IDX_RELATIVE_PATH)
396     {
397       PropertyUsage c = _extentUsage.getPropertyUsage(row);
398       return (c.getBaseProperty().getType() instanceof DjExtent);
399     }
400
401     return super.isCellEditable(row, column);
402   }
403
404   private final class DefaultValueCellEditor extends DjCellEditor
405   {
406     private static final long serialVersionUID = 1L;
407
408     private DefaultValueCellEditor(JComboBox JavaDoc comboBox)
409     {
410       super(comboBox);
411     }
412
413     public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row, int column)
414     {
415       ArrayList JavaDoc possibles = getValidDefaults(row);
416
417       JComboBox JavaDoc cbb = (JComboBox JavaDoc) editorComponent;
418       cbb.setEditable(true);
419       cbb.setModel(new DefaultComboBoxModel JavaDoc(possibles.toArray()));
420       delegate.setValue(value);
421       return editorComponent;
422     }
423
424     private ArrayList JavaDoc getValidDefaults(int row)
425     {
426       HashMap JavaDoc params;
427       if (_extentUsage.getParent() != null)
428       {
429         params = _extentUsage.getParent().getParameters();
430       }
431       else
432       {
433         params = new HashMap JavaDoc();
434       }
435
436       ArrayList JavaDoc possibles = new ArrayList JavaDoc();
437
438       DjProperty prop = _extentUsage.getPropertyUsage(row).getBaseProperty();
439       if (prop != null && prop.getType() instanceof DjDomain)
440       {
441         DjDomain domain = (DjDomain) prop.getType();
442         DjDomainValue[] values = domain.getValidValues();
443         for (int i = 0; i < values.length; i++)
444           possibles.add(values[i].getValue());
445       }
446
447       Iterator JavaDoc it = params.keySet().iterator();
448       while (it.hasNext())
449         possibles.add("{" + it.next().toString() + "}");
450
451       possibles.add("{$timestamp}");
452       possibles.add("{$user}");
453       possibles.add("{$username}");
454       possibles.add("{$count}");
455       possibles.add("{$seq}");
456       possibles.add("{$unique}");
457       possibles.add("{:<parameter>}");
458       possibles.add("{:<parameter>.<property>.<...>}");
459       return possibles;
460     }
461   }
462
463   class CompletionCellEditor extends DjCellEditor implements PopupMenuListener JavaDoc
464   {
465     private static final long serialVersionUID = 1L;
466     JComboBox JavaDoc _cbb;
467     DjPropertyPathCompleter _ppc;
468     DjExtent _rootExtent;
469
470     public CompletionCellEditor(JComboBox JavaDoc cbb)
471     {
472       super(cbb);
473       cbb.setEditable(true);
474       _cbb = cbb;
475       _ppc = new DjPropertyPathCompleter();
476
477       Object JavaDoc o = cbb.getEditor().getEditorComponent();
478       if (o instanceof JTextComponent JavaDoc) _ppc.setEditor((JTextComponent JavaDoc) o);
479       cbb.addPopupMenuListener(this);
480
481     }
482
483     public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row, int column)
484     {
485       _rootExtent = (DjExtent) _extentUsage.getPropertyUsage(row).getBaseProperty().getType();
486       delegate.setValue(value);
487
488       return editorComponent;
489     }
490
491     public void popupMenuCanceled(PopupMenuEvent JavaDoc e)
492     {
493
494     }
495
496     public void popupMenuWillBecomeInvisible(PopupMenuEvent JavaDoc e)
497     {
498       ArrayList JavaDoc lst = new ArrayList JavaDoc();
499       for (int i = 0; i < 10; i++)
500       {
501         lst.add(_ppc.getEditor().getText());
502       }
503       _cbb.setModel(new DefaultComboBoxModel JavaDoc(lst.toArray()));
504     }
505
506     public void popupMenuWillBecomeVisible(PopupMenuEvent JavaDoc e)
507     {
508       DjExtent rootExtent = _rootExtent;
509       _ppc.setRootExtent(rootExtent);
510
511       ArrayList JavaDoc withPrefix = new ArrayList JavaDoc();
512       String JavaDoc currentValue = _ppc.getEditor().getText();
513
514       try
515       {
516         String JavaDoc path = _ppc.getEditor().getText();
517         int idx = path.lastIndexOf(".");
518         if (idx != -1)
519         {
520           path = path.substring(0, idx).trim();
521           DjProperty prop = _rootExtent.getProperty(path);
522           if (prop.getType() instanceof DjExtent)
523           {
524             rootExtent = (DjExtent) prop.getType();
525           }
526         }
527
528         ArrayList JavaDoc lst = _ppc.getCompletions();
529         String JavaDoc prefix = path;
530         if (rootExtent == _rootExtent) prefix = "";
531
532         if (prefix.trim().length() > 0)
533         {
534           withPrefix.add(prefix);
535           prefix += ".";
536         }
537
538         Iterator JavaDoc it = lst.iterator();
539         while (it.hasNext())
540         {
541           withPrefix.add(prefix + it.next().toString());
542         }
543       }
544       catch (ObjectNotDefinedException x)
545       {
546         setStatusMessage(x.getMessage());
547       }
548       withPrefix.remove(currentValue);
549       withPrefix.add(0, currentValue);
550
551       JComboBox JavaDoc cbb = (JComboBox JavaDoc) editorComponent;
552       cbb.setModel(new DefaultComboBoxModel JavaDoc(withPrefix.toArray()));
553     }
554   }
555 }
Popular Tags