KickJava   Java API By Example, From Geeks To Geeks.

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


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 modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes 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,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.modeler.domaineditor;
31
32 import java.awt.event.MouseAdapter JavaDoc;
33 import java.awt.event.MouseEvent JavaDoc;
34
35 import javax.swing.event.TableModelEvent JavaDoc;
36 import javax.swing.table.TableColumn JavaDoc;
37 import javax.swing.table.TableColumnModel JavaDoc;
38
39 import com.genimen.djeneric.language.Messages;
40 import com.genimen.djeneric.repository.DjDomain;
41 import com.genimen.djeneric.repository.DjDomainValue;
42 import com.genimen.djeneric.repository.DjPersistenceManager;
43 import com.genimen.djeneric.tools.modeler.ModelEditor;
44 import com.genimen.djeneric.ui.DjModelColumn;
45 import com.genimen.djeneric.ui.DjTable;
46 import com.genimen.djeneric.ui.DjTableModel;
47
48 public class DomainValueTableModel extends DjTableModel
49 {
50   private static final long serialVersionUID = 1L;
51   private final int _colCount = 4;
52   private DjPersistenceManager _mgr;
53   private ModelEditor _editor;
54
55   private DjTable _parentTable = null;
56
57   private final int IDX_SEQ = 0;
58   private final int IDX_VALUE = 1;
59   private final int IDX_HIGH_VALUE = 2;
60   private final int IDX_DESCRIPTION = 3;
61
62   public DomainValueTableModel(ModelEditor editor, DjTable parentTable)
63   {
64     _editor = editor;
65     _mgr = editor.getPersistenceManager();
66     _parentTable = parentTable;
67
68     _columns = new DjModelColumn[_colCount];
69
70     _columns[IDX_SEQ] = new DjModelColumn(Messages.getString("global.Seq"), 40, Messages
71         .getString("DomainValueTableModel.OrderInList"));
72     _columns[IDX_VALUE] = new DjModelColumn(Messages.getString("DomainValueTableModel.ValueLowValue"), 200, Messages
73         .getString("DomainValueTableModel.ValueLowValueDescr"));
74     _columns[IDX_HIGH_VALUE] = new DjModelColumn(Messages.getString("DomainValueTableModel.HighValue"), 200, Messages
75         .getString("DomainValueTableModel.HighValueDescr"));
76     _columns[IDX_DESCRIPTION] = new DjModelColumn(Messages.getString("global.Description"), 300, Messages
77         .getString("DomainValueTableModel.DescriptiveTitle"));
78   }
79
80   protected DjDomain getDomain()
81   {
82     if (_parentTable == null) return null;
83
84     int idx = _parentTable.getSelectedRow();
85     if (idx == -1) return null;
86     DomainTableModel mdl = (DomainTableModel) _parentTable.getModel();
87
88     return mdl.getDomain(idx);
89   }
90
91   public int getRowCount()
92   {
93     if (getDomain() == null || getDomain().isDynamic()) return 0;
94
95     return getDomain().getPersonalDomainValueCount();
96   }
97
98   public int getColumnCount()
99   {
100     return _colCount;
101   }
102
103   public Object JavaDoc getValueAt(int nRow, int nCol)
104   {
105     if (getDomain() == null) return "";
106     if (nRow < 0 || nRow >= getRowCount()) return "";
107
108     DjDomainValue c = getDomain().getPersonalDomainValue(nRow);
109     if (nCol == IDX_SEQ) return new Integer JavaDoc(c.getSeq());
110     else if (nCol == IDX_VALUE)
111     {
112       Object JavaDoc o = c.getValue();
113       if (o == null) return "";
114       return o.toString();
115     }
116     else if (nCol == IDX_HIGH_VALUE)
117     {
118       Object JavaDoc o = c.getHigh();
119       if (o == null) return "";
120       return o.toString();
121     }
122     else if (nCol == IDX_DESCRIPTION) return c.getDescription();
123     else return Messages.getString("global.InvalidColumn", String.valueOf(nCol));
124   }
125
126   public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex)
127   {
128     if (rowIndex < 0 || rowIndex >= getRowCount()) return;
129     DjDomain dom = getDomain();
130     if (dom == null) return;
131
132     if (aValue == null) return;
133
134     try
135     {
136       DjDomainValue c = getDomain().getPersonalDomainValue(rowIndex);
137       if (columnIndex == IDX_SEQ)
138       {
139         c.setSeq(convert2Int(aValue.toString()));
140       }
141       else if (columnIndex == IDX_VALUE) c.setValue(aValue.toString());
142       else if (columnIndex == IDX_HIGH_VALUE) c.setHigh(aValue.toString());
143       else if (columnIndex == IDX_DESCRIPTION) c.setDescription(aValue.toString());
144       setStatusMessage(ModelEditor.OK_MSG);
145     }
146     catch (Exception JavaDoc x)
147     {
148       setStatusMessage(x);
149     }
150
151   }
152
153   public boolean isCellEditable(int row, int column)
154   {
155     if (!isEditable()) return false;
156     return !getDomain().isBuiltIn();
157   }
158
159   int counter = 1;
160
161   public int insertRow(int atIdx)
162   {
163     if (!isEditable()) return -1;
164     try
165     {
166       DjDomain dom = getDomain();
167       if (dom == null) return -1;
168       if (dom.isBuiltIn()) return -1;
169
170       DjDomainValue c = new DjDomainValue(dom, "", "", "");
171       c.setSeq((dom.getPersonalDomainValueCount() + 1) * 10);
172
173       dom.addDomainValue(atIdx, c);
174       setStatusMessage(ModelEditor.OK_MSG);
175       return atIdx;
176     }
177     catch (Exception JavaDoc x)
178     {
179       setStatusMessage(x.getMessage(), false);
180     }
181     return -1;
182   }
183
184   public boolean deleteRow(int atIdx)
185   {
186     if (!isEditable()) return false;
187     try
188     {
189       DjDomain dom = getDomain();
190       if (dom == null) return false;
191       if (dom.isBuiltIn()) return false;
192
193       dom.removeDomainValue(getDomain().getPersonalDomainValue(atIdx));
194       setStatusMessage(ModelEditor.OK_MSG);
195       return true;
196     }
197     catch (Exception JavaDoc x)
198     {
199       setStatusMessage(x.getMessage(), false);
200     }
201     return false;
202   }
203
204   public MouseAdapter JavaDoc getHeaderClickListener(DjTable table)
205   {
206     return new ColumnListener(table);
207   }
208
209   class ColumnListener extends MouseAdapter JavaDoc
210   {
211     protected DjTable _table;
212     boolean _sortAsc = true;
213     int _sortCol = -1;
214
215     public ColumnListener(DjTable table)
216     {
217       _table = table;
218     }
219
220     public void mouseClicked(MouseEvent JavaDoc e)
221     {
222       TableColumnModel JavaDoc colModel = _table.getColumnModel();
223       int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
224       int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();
225
226       if (modelIndex < 0) return;
227       if (_sortCol == modelIndex) _sortAsc = !_sortAsc;
228       else _sortCol = modelIndex;
229
230       for (int i = 0; i < getColumnCount(); i++)
231       {
232         TableColumn JavaDoc column = colModel.getColumn(i);
233         column.setHeaderValue(getColumnName(column.getModelIndex()));
234       }
235       _table.getTableHeader().repaint();
236
237       // Collections.sort(_mgr.getPeople(), new PersonComparator(modelIndex, _sortAsc));
238
_table.tableChanged(new TableModelEvent JavaDoc(DomainValueTableModel.this));
239       _table.repaint();
240     }
241   }
242 }
Popular Tags