KickJava   Java API By Example, From Geeks To Geeks.

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


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.JCheckBox JavaDoc;
38 import javax.swing.JComboBox JavaDoc;
39 import javax.swing.JTable JavaDoc;
40 import javax.swing.table.TableCellEditor JavaDoc;
41
42 import com.genimen.djeneric.language.Messages;
43 import com.genimen.djeneric.repository.DjContext;
44 import com.genimen.djeneric.repository.DjContextManager;
45 import com.genimen.djeneric.repository.DjUser;
46 import com.genimen.djeneric.repository.DjUserContextAssociation;
47 import com.genimen.djeneric.tools.modeler.ModelEditor;
48 import com.genimen.djeneric.ui.DjCellEditor;
49 import com.genimen.djeneric.ui.DjCheckCellRenderer;
50 import com.genimen.djeneric.ui.DjModelColumn;
51 import com.genimen.djeneric.ui.DjTableModel;
52 import com.genimen.djeneric.util.DjLogger;
53
54 public class AccessTableModel extends DjTableModel
55 {
56   private static final long serialVersionUID = 1L;
57   private final static int _colCount = 5;
58
59   private ArrayList JavaDoc _allAssocs;
60   private DjUser _user = null;
61   private DjContext _context = null;
62   private DjContextManager _cmgr;
63   private final static int IDX_OTHEREND = 0;
64   private final static int IDX_QUERY = 1;
65   private final static int IDX_CREATE = 2;
66   private final static int IDX_MODIFY = 3;
67   private final static int IDX_DELETE = 4;
68
69   public AccessTableModel(DjContextManager cmgr, ArrayList JavaDoc allAssocs, Object JavaDoc parentToAssocs)
70   {
71     _allAssocs = allAssocs;
72     _cmgr = cmgr;
73     if (parentToAssocs instanceof DjUser)
74     {
75       _user = (DjUser) parentToAssocs;
76     }
77     else
78     {
79       _context = (DjContext) parentToAssocs;
80     }
81
82     _columns = new DjModelColumn[_colCount];
83
84     if (_user != null)
85     {
86       _columns[IDX_OTHEREND] = new DjModelColumn(Messages.getString("AccessTableModel.Context"), 200, Messages
87           .getString("AccessTableModel.ContextGranted"));
88     }
89     else
90     {
91       _columns[IDX_OTHEREND] = new DjModelColumn(Messages.getString("AccessTableModel.User"), 200, Messages
92           .getString("AccessTableModel.UserGranted"));
93     }
94
95     _columns[IDX_QUERY] = new DjModelColumn(Messages.getString("global.Query"), 60, Messages
96         .getString("AccessTableModel.CanQueryObjects"), new DjCheckCellRenderer());
97     _columns[IDX_CREATE] = new DjModelColumn(Messages.getString("global.Create"), 60, Messages
98         .getString("AccessTableModel.CanCreateObjects"), new DjCheckCellRenderer());
99     _columns[IDX_MODIFY] = new DjModelColumn(Messages.getString("AccessTableModel.Modify"), 60, Messages
100         .getString("AccessTableModel.CanModifyObjects"), new DjCheckCellRenderer());
101     _columns[IDX_DELETE] = new DjModelColumn(Messages.getString("global.Delete"), 60, Messages
102         .getString("AccessTableModel.CanDeleteObjects"), new DjCheckCellRenderer());
103   }
104
105   public TableCellEditor JavaDoc getColumnEditor(int column)
106   {
107     if (column == IDX_OTHEREND)
108     {
109       return new DjCellEditor(new JComboBox JavaDoc())
110       {
111         private static final long serialVersionUID = 1L;
112
113         public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row, int column)
114         {
115           JComboBox JavaDoc cbb = (JComboBox JavaDoc) editorComponent;
116           try
117           {
118             if (_user != null)
119             {
120               DjContext[] ctxt = _cmgr.getContexts();
121               Vector JavaDoc v = new Vector JavaDoc();
122               DjUserContextAssociation c = (DjUserContextAssociation) _allAssocs.get(row);
123               if (c.getContext() != null) v.add(c.getContext());
124
125               for (int i = 0; i < ctxt.length; i++)
126               {
127                 boolean canAdd = true;
128                 for (int j = 0; j < _allAssocs.size(); j++)
129                 {
130                   c = (DjUserContextAssociation) _allAssocs.get(j);
131                   if (c.getContext() != null && c.getContext().equals(ctxt[i])) canAdd = false;
132                 }
133                 if (canAdd) v.add(ctxt[i]);
134               }
135               cbb.setModel(new DefaultComboBoxModel JavaDoc(v));
136             }
137             else
138             {
139               DjUser[] devs = _cmgr.getUsers();
140               Vector JavaDoc v = new Vector JavaDoc();
141               DjUserContextAssociation c = (DjUserContextAssociation) _allAssocs.get(row);
142               if (c.getUser() != null) v.add(c.getUser());
143               for (int i = 0; i < devs.length; i++)
144               {
145                 boolean canAdd = true;
146                 for (int j = 0; j < _allAssocs.size(); j++)
147                 {
148                   c = (DjUserContextAssociation) _allAssocs.get(j);
149                   if (c.getUser() != null && c.getUser().equals(devs[i])) canAdd = false;
150                 }
151                 if (canAdd) v.add(devs[i]);
152               }
153               cbb.setModel(new DefaultComboBoxModel JavaDoc(v));
154             }
155           }
156           catch (Exception JavaDoc x)
157           {
158             DjLogger.log(x);
159           }
160           delegate.setValue(value);
161           return editorComponent;
162         }
163       };
164     }
165     if (column == IDX_QUERY)
166     {
167       return new DjCellEditor(new JCheckBox JavaDoc());
168     }
169     if (column == IDX_CREATE)
170     {
171       return new DjCellEditor(new JCheckBox JavaDoc());
172     }
173     if (column == IDX_MODIFY)
174     {
175       return new DjCellEditor(new JCheckBox JavaDoc());
176     }
177     if (column == IDX_DELETE)
178     {
179       return new DjCellEditor(new JCheckBox JavaDoc());
180     }
181
182     return super.getColumnEditor(column);
183   }
184
185   public int getRowCount()
186   {
187     if (_allAssocs == null) return 0;
188
189     return _allAssocs.size();
190   }
191
192   public int getColumnCount()
193   {
194     return _colCount;
195   }
196
197   public Object JavaDoc getValueAt(int nRow, int nCol)
198   {
199     if (nRow < 0 || nRow >= getRowCount()) return "";
200
201     DjUserContextAssociation c = (DjUserContextAssociation) _allAssocs.get(nRow);
202     if (nCol == IDX_OTHEREND)
203     {
204       if (_user != null) return c.getContext();
205       return c.getUser();
206     }
207     else if (nCol == IDX_QUERY) return new Boolean JavaDoc(c.isQuery());
208     else if (nCol == IDX_CREATE) return new Boolean JavaDoc(c.isCreate());
209     else if (nCol == IDX_MODIFY) return new Boolean JavaDoc(c.isModify());
210     else if (nCol == IDX_DELETE) return new Boolean JavaDoc(c.isDelete());
211     else return Messages.getString("global.InvalidColumn", String.valueOf(nCol));
212   }
213
214   public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex)
215   {
216     if (rowIndex < 0 || rowIndex >= getRowCount()) return;
217     if (aValue == null) return;
218     notifyModified();
219
220     try
221     {
222       DjUserContextAssociation c = (DjUserContextAssociation) _allAssocs.get(rowIndex);
223       if (columnIndex == IDX_OTHEREND)
224       {
225         if (aValue instanceof DjUser) c.setUser((DjUser) aValue);
226         else c.setContext((DjContext) aValue);
227       }
228       else if (columnIndex == IDX_QUERY) c.setQuery(((Boolean JavaDoc) aValue).booleanValue());
229       else if (columnIndex == IDX_CREATE) c.setCreate(((Boolean JavaDoc) aValue).booleanValue());
230       else if (columnIndex == IDX_MODIFY) c.setModify(((Boolean JavaDoc) aValue).booleanValue());
231       else if (columnIndex == IDX_DELETE) c.setDelete(((Boolean JavaDoc) aValue).booleanValue());
232       setStatusMessage(ModelEditor.OK_MSG);
233     }
234     catch (Exception JavaDoc x)
235     {
236       setStatusMessage(x);
237     }
238
239   }
240
241   int counter = 1;
242
243   public int insertRow(int atIdx)
244   {
245     try
246     {
247       DjUserContextAssociation c = _cmgr.createNewUserContextAssociation();
248       c.setUser(_user);
249       c.setContext(_context);
250       _allAssocs.add(atIdx, c);
251       notifyModified();
252       setStatusMessage(ModelEditor.OK_MSG);
253       return atIdx;
254     }
255     catch (Exception JavaDoc x)
256     {
257       setStatusMessage(x.getMessage(), false);
258     }
259     return -1;
260   }
261
262   public boolean deleteRow(int atIdx)
263   {
264     try
265     {
266       _allAssocs.remove(atIdx);
267       notifyModified();
268       setStatusMessage(ModelEditor.OK_MSG);
269       return true;
270     }
271     catch (Exception JavaDoc x)
272     {
273       setStatusMessage(x.getMessage(), false);
274     }
275     return false;
276   }
277
278 }
Popular Tags