KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > administrator > editors > ModelViewTableModel


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.administrator.editors;
31
32 import java.awt.Component JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import javax.swing.DefaultComboBoxModel JavaDoc;
37 import javax.swing.JComboBox JavaDoc;
38 import javax.swing.JTable JavaDoc;
39 import javax.swing.table.TableCellEditor JavaDoc;
40
41 import com.genimen.djeneric.language.Messages;
42 import com.genimen.djeneric.repository.DjModelView;
43 import com.genimen.djeneric.repository.DjPersistenceManager;
44 import com.genimen.djeneric.repository.DjUser;
45 import com.genimen.djeneric.tools.modeler.ModelEditor;
46 import com.genimen.djeneric.ui.DjCellEditor;
47 import com.genimen.djeneric.ui.DjModelColumn;
48 import com.genimen.djeneric.ui.DjTableModel;
49 import com.genimen.djeneric.util.DjLogger;
50
51 public class ModelViewTableModel extends DjTableModel
52 {
53   private static final long serialVersionUID = 1L;
54   private final int _colCount = 1;
55
56   private DjPersistenceManager _mgr;
57
58   ArrayList JavaDoc _allViews;
59
60   private final int IDX_VIEW = 0;
61
62   public ModelViewTableModel(DjPersistenceManager mgr, ArrayList JavaDoc allViews, DjUser user)
63   {
64     _mgr = mgr;
65     _allViews = allViews;
66     _columns = new DjModelColumn[_colCount];
67
68     _columns[IDX_VIEW] = new DjModelColumn(Messages.getString("global.View"), 200, Messages
69         .getString("ModelViewTableModel.ViewsUserHasAccess2"));
70   }
71
72   public TableCellEditor JavaDoc getColumnEditor(int column)
73   {
74     if (column == IDX_VIEW)
75     {
76       return new DjCellEditor(new JComboBox JavaDoc())
77       {
78         private static final long serialVersionUID = 1L;
79
80         public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row, int column)
81         {
82           JComboBox JavaDoc cbb = (JComboBox JavaDoc) editorComponent;
83           try
84           {
85             DjModelView[] views = _mgr.getViews();
86             Vector JavaDoc v = new Vector JavaDoc();
87             DjModelView c = (DjModelView) _allViews.get(row);
88             if (c != null) v.add(c);
89
90             for (int i = 0; i < views.length; i++)
91             {
92               boolean canAdd = true;
93               for (int j = 0; j < _allViews.size(); j++)
94               {
95                 c = (DjModelView) _allViews.get(j);
96                 if (c != null && c.equals(views[i])) canAdd = false;
97               }
98               if (canAdd) v.add(views[i]);
99             }
100             cbb.setModel(new DefaultComboBoxModel JavaDoc(v));
101           }
102           catch (Exception JavaDoc x)
103           {
104             DjLogger.log(x);
105           }
106           delegate.setValue(value);
107           return editorComponent;
108         }
109       };
110     }
111     return super.getColumnEditor(column);
112   }
113
114   public int getRowCount()
115   {
116     if (_allViews == null) return 0;
117
118     return _allViews.size();
119   }
120
121   public int getColumnCount()
122   {
123     return _colCount;
124   }
125
126   public Object JavaDoc getValueAt(int nRow, int nCol)
127   {
128     if (nRow < 0 || nRow >= getRowCount()) return "";
129
130     DjModelView c = (DjModelView) _allViews.get(nRow);
131     if (nCol == IDX_VIEW) return c;
132     else return Messages.getString("global.InvalidColumn", String.valueOf(nCol));
133   }
134
135   public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex)
136   {
137     if (rowIndex < 0 || rowIndex >= getRowCount()) return;
138     if (aValue == null) return;
139     notifyModified();
140
141     try
142     {
143       if (columnIndex == IDX_VIEW) _allViews.set(rowIndex, aValue);
144       setStatusMessage(ModelEditor.OK_MSG);
145     }
146     catch (Exception JavaDoc x)
147     {
148       setStatusMessage(x);
149     }
150
151   }
152
153   int counter = 1;
154
155   public int insertRow(int atIdx)
156   {
157     try
158     {
159       _allViews.add(atIdx, null);
160       notifyModified();
161       setStatusMessage(ModelEditor.OK_MSG);
162       return atIdx;
163     }
164     catch (Exception JavaDoc x)
165     {
166       setStatusMessage(x.getMessage(), false);
167     }
168     return -1;
169   }
170
171   public boolean deleteRow(int atIdx)
172   {
173     try
174     {
175       _allViews.remove(atIdx);
176       notifyModified();
177       setStatusMessage(ModelEditor.OK_MSG);
178       return true;
179     }
180     catch (Exception JavaDoc x)
181     {
182       setStatusMessage(x.getMessage(), false);
183     }
184     return false;
185   }
186
187 }
Popular Tags