KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  Redistribution and use in source and binary forms, with or without modification, is permitted
4  provided that the following conditions are met:
5  - Redistributions of source code must retain the above copyright notice, this list of conditions
6  and the following disclaimer.
7  - Redistributions in binary form must reproduce the above copyright notice, this list of
8  conditions and the following disclaimer in the documentation and/or other materials
9  provided with the distribution.
10  - All advertising materials mentioning features or use of this software must display the
11  following acknowledgment: "This product includes Djeneric."
12  - Products derived from this software may not be called "Djeneric" nor may
13  "Djeneric" appear in their names without prior written permission of Genimen BV.
14  - Redistributions of any form whatsoever must retain the following acknowledgment: "This
15  product includes Djeneric."
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
17  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
20  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */

28 package com.genimen.djeneric.tools.administrator.editors;
29
30 import java.awt.BorderLayout JavaDoc;
31 import java.awt.Dimension JavaDoc;
32 import java.awt.event.ActionEvent JavaDoc;
33 import java.awt.event.MouseEvent JavaDoc;
34 import java.util.ArrayList JavaDoc;
35
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.JScrollPane JavaDoc;
40 import javax.swing.JTable JavaDoc;
41 import javax.swing.JTextField JavaDoc;
42
43 import com.genimen.djeneric.language.Messages;
44 import com.genimen.djeneric.repository.DjContext;
45 import com.genimen.djeneric.repository.DjSession;
46 import com.genimen.djeneric.repository.DjUserContextAssociation;
47 import com.genimen.djeneric.tools.administrator.Administrator;
48 import com.genimen.djeneric.tools.administrator.helpers.AdminPanel;
49 import com.genimen.djeneric.tools.administrator.nodes.AdministratorTreeNode;
50 import com.genimen.djeneric.ui.DjTable;
51 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
52 import com.genimen.djeneric.ui.Util;
53 import com.genimen.djeneric.util.DjLogger;
54
55 public class ContextEditor extends AdminEditor
56 {
57   private static final long serialVersionUID = 1L;
58
59   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
60   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
61   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
62   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
63   JButton JavaDoc _butOk = new JButton JavaDoc();
64   JButton JavaDoc _butCancel = new JButton JavaDoc();
65   AdministratorTreeNode _theNode;
66   DjContext _ctxt;
67   JPanel JavaDoc jPanel5 = new JPanel JavaDoc();
68   JTextField JavaDoc _edtName = new JTextField JavaDoc();
69   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
70   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
71   JTextField JavaDoc _edtCode = new JTextField JavaDoc();
72   DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout();
73   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
74   JLabel JavaDoc jLabel2 = new JLabel JavaDoc();
75   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
76   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
77   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
78   JLabel JavaDoc jLabel3 = new JLabel JavaDoc();
79   DjTable _grdAccess = new DjTable();
80   AccessTableModel _model;
81   ArrayList JavaDoc _allAssocs = new ArrayList JavaDoc();
82   JButton JavaDoc _butDown = new JButton JavaDoc();
83   JButton JavaDoc _butDelete = new JButton JavaDoc();
84   JButton JavaDoc _butBottom = new JButton JavaDoc();
85   JButton JavaDoc _butFirst = new JButton JavaDoc();
86   JPanel JavaDoc jPanel6 = new JPanel JavaDoc();
87   JButton JavaDoc _butCreate = new JButton JavaDoc();
88   JButton JavaDoc _butUp = new JButton JavaDoc();
89
90   public ContextEditor(AdminPanel adminPanel, AdministratorTreeNode theNode, DjContext ctxt)
91   {
92     super(adminPanel);
93     _ctxt = ctxt;
94     _theNode = theNode;
95
96     try
97     {
98       jbInit();
99       _edtCode.setText(ctxt.getCode());
100       _edtName.setText(ctxt.getName());
101
102       DjSession session = adminPanel.getPersistenceManager().createSession();
103       try
104       {
105         DjUserContextAssociation[] dca = ctxt.getUsers(session);
106         for (int i = 0; i < dca.length; i++)
107         {
108           _allAssocs.add(dca[i]);
109         }
110         _model = new AccessTableModel(adminPanel.getPersistenceManager().getContextManager(), _allAssocs, ctxt);
111         _grdAccess.setModel(_model);
112         tableFirst(_grdAccess);
113       }
114       finally
115       {
116         session.close();
117       }
118     }
119     catch (Exception JavaDoc ex)
120     {
121       DjLogger.log(ex);
122     }
123   }
124
125   void jbInit() throws Exception JavaDoc
126   {
127     this.setLayout(borderLayout1);
128     jPanel3.setLayout(borderLayout2);
129     _butOk.setText(Messages.getString("global.Ok"));
130     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
131     {
132       public void actionPerformed(ActionEvent JavaDoc e)
133       {
134         _butOk_actionPerformed(e);
135       }
136     });
137     _butCancel.setText(Messages.getString("global.Cancel"));
138     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
139     {
140       public void actionPerformed(ActionEvent JavaDoc e)
141       {
142         _butCancel_actionPerformed(e);
143       }
144     });
145     jPanel2.setLayout(verticalFlowLayout2);
146     jPanel1.setLayout(verticalFlowLayout1);
147     jLabel2.setText(Messages.getString("global.Name"));
148     jLabel1.setPreferredSize(new Dimension JavaDoc(41, 21));
149     jLabel1.setText(Messages.getString("global.Code"));
150     jPanel5.setLayout(borderLayout3);
151     jLabel3.setText(Messages.getString("ContextEditor.Users"));
152     jScrollPane1.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
153     {
154       public void mouseClicked(MouseEvent JavaDoc e)
155       {
156         jScrollPane1_mouseClicked(e);
157       }
158     });
159     _butDown.setPreferredSize(new Dimension JavaDoc(24, 24));
160     _butDown.setIcon(Administrator.getImageIcon("down.gif"));
161     _butDown.setToolTipText(Messages.getString("global.NextInList"));
162     _butDown.setRequestFocusEnabled(true);
163     _butDown.addActionListener(new java.awt.event.ActionListener JavaDoc()
164     {
165       public void actionPerformed(ActionEvent JavaDoc e)
166       {
167         _butDown_actionPerformed(e);
168       }
169     });
170     _butDelete.setPreferredSize(new Dimension JavaDoc(24, 24));
171     _butDelete.setIcon(Administrator.getImageIcon("delete.gif"));
172     _butDelete.setToolTipText(Messages.getString("global.DeleteCtrl"));
173     _butDelete.setRequestFocusEnabled(true);
174     _butDelete.addActionListener(new java.awt.event.ActionListener JavaDoc()
175     {
176       public void actionPerformed(ActionEvent JavaDoc e)
177       {
178         _butDelete_actionPerformed(e);
179       }
180     });
181     _butBottom.addActionListener(new java.awt.event.ActionListener JavaDoc()
182     {
183       public void actionPerformed(ActionEvent JavaDoc e)
184       {
185         _butBottom_actionPerformed(e);
186       }
187     });
188     _butBottom.setRequestFocusEnabled(true);
189     _butBottom.setToolTipText(Messages.getString("global.LastInList"));
190     _butBottom.setIcon(Administrator.getImageIcon("bottom.gif"));
191     _butBottom.setPreferredSize(new Dimension JavaDoc(24, 24));
192     _butFirst.addActionListener(new java.awt.event.ActionListener JavaDoc()
193     {
194       public void actionPerformed(ActionEvent JavaDoc e)
195       {
196         _butFirst_actionPerformed(e);
197       }
198     });
199     _butFirst.setRequestFocusEnabled(true);
200     _butFirst.setToolTipText(Messages.getString("global.FirstInList"));
201     _butFirst.setIcon(Administrator.getImageIcon("top.gif"));
202     _butFirst.setPreferredSize(new Dimension JavaDoc(24, 24));
203     _butCreate.setPreferredSize(new Dimension JavaDoc(24, 24));
204     _butCreate.setIcon(Administrator.getImageIcon("new.gif"));
205     _butCreate.setToolTipText(Messages.getString("global.CreateCtrl"));
206     _butCreate.setRequestFocusEnabled(true);
207     _butCreate.addActionListener(new java.awt.event.ActionListener JavaDoc()
208     {
209       public void actionPerformed(ActionEvent JavaDoc e)
210       {
211         _butCreate_actionPerformed(e);
212       }
213     });
214     _butUp.addActionListener(new java.awt.event.ActionListener JavaDoc()
215     {
216       public void actionPerformed(ActionEvent JavaDoc e)
217       {
218         _butUp_actionPerformed(e);
219       }
220     });
221     _butUp.setRequestFocusEnabled(true);
222     _butUp.setToolTipText(Messages.getString("global.PreviousInList"));
223     _butUp.setIcon(Administrator.getImageIcon("up.gif"));
224     _butUp.setPreferredSize(new Dimension JavaDoc(24, 24));
225     this.add(jPanel3, BorderLayout.SOUTH);
226     jPanel3.add(jPanel4, BorderLayout.EAST);
227     jPanel4.add(_butCancel, null);
228     jPanel4.add(_butOk, null);
229     jPanel3.add(jPanel6, BorderLayout.WEST);
230     jPanel6.add(_butUp, null);
231     jPanel6.add(_butDown, null);
232     jPanel6.add(_butFirst, null);
233     jPanel6.add(_butBottom, null);
234     jPanel6.add(_butCreate, null);
235     jPanel6.add(_butDelete, null);
236     this.add(jPanel5, BorderLayout.NORTH);
237     jPanel2.add(_edtCode, null);
238     jPanel2.add(_edtName, null);
239     this.add(jScrollPane1, BorderLayout.CENTER);
240     jScrollPane1.getViewport().add(_grdAccess, null);
241     jPanel5.add(jPanel1, BorderLayout.WEST);
242     jPanel1.add(jLabel1, null);
243     jPanel1.add(jLabel2, null);
244     jPanel1.add(jLabel3, null);
245     jPanel5.add(jPanel2, BorderLayout.CENTER);
246
247     Util.sizeButtons(new JButton JavaDoc[]{_butOk, _butCancel});
248   }
249
250   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
251   {
252     _adminPanel.closeEditor(this);
253     setStatusMessage(Messages.getString("global.Ok"));
254   }
255
256   public void setStatusMessage(String JavaDoc msg, boolean isInformative)
257   {
258     _adminPanel.setStatusMessage(msg, isInformative);
259   }
260
261   public void requestFocus()
262   {
263     _edtCode.requestFocus();
264   }
265
266   void _butOk_actionPerformed(ActionEvent JavaDoc e)
267   {
268     if (_edtCode.getText().trim().length() == 0)
269     {
270       setStatusMessage(Messages.getString("global.CodeRequired"), false);
271       return;
272     }
273     if (_edtName.getText().trim().length() == 0)
274     {
275       setStatusMessage(Messages.getString("global.NameRequired"), false);
276       return;
277     }
278
279     _ctxt.setCode(_edtCode.getText());
280     _ctxt.setName(_edtName.getText());
281
282     try
283     {
284       DjSession session = _adminPanel.getPersistenceManager().createSession();
285       try
286       {
287         _ctxt.persist(session);
288
289         for (int i = 0; i < _allAssocs.size(); i++)
290         {
291           DjUserContextAssociation dca = (DjUserContextAssociation) _allAssocs.get(i);
292           if (dca.getContext() == null)
293           {
294             _grdAccess.setRowSelectionInterval(i, i);
295             throw new Exception JavaDoc(Messages.getString("global.ContextRequired"));
296           }
297           if (dca.getUser() == null)
298           {
299             _grdAccess.setRowSelectionInterval(i, i);
300             throw new Exception JavaDoc(Messages.getString("global.UserIsRequired"));
301           }
302         }
303         _ctxt.removeAllUserAssociations(session);
304         for (int i = 0; i < _allAssocs.size(); i++)
305         {
306           DjUserContextAssociation dca = (DjUserContextAssociation) _allAssocs.get(i);
307           dca.persist(session);
308         }
309         session.commit();
310         setStatusMessage(Messages.getString("global.Ok"));
311       }
312       finally
313       {
314         session.close();
315       }
316       _adminPanel.closeEditor(this);
317       if (_theNode != null)
318       {
319         _theNode.reload();
320       }
321     }
322     catch (Exception JavaDoc x)
323     {
324       setStatusMessage(x);
325     }
326   }
327
328   public String JavaDoc getID()
329   {
330     if (_ctxt.getId() == -1) return null;
331     return "C" + _ctxt.getId();
332   }
333
334   public void doubleClick(MouseEvent JavaDoc e, DjTable table)
335   {
336     table.requestFocus();
337     if (e.getClickCount() < 2) return;
338
339     try
340     {
341       table.insertRow();
342     }
343     catch (Exception JavaDoc x)
344     {
345       setStatusMessage(x);
346     }
347   }
348
349   void jScrollPane1_mouseClicked(MouseEvent JavaDoc e)
350   {
351     doubleClick(e, _grdAccess);
352   }
353
354   private void tableFirst(JTable JavaDoc table)
355   {
356     if (table.getRowCount() <= 0) return;
357
358     table.setRowSelectionInterval(0, 0);
359   }
360
361   private void tableBottom(JTable JavaDoc table)
362   {
363     int row = table.getRowCount() - 1;
364     if (row != -1) table.setRowSelectionInterval(row, row);
365   }
366
367   private void tableUp(JTable JavaDoc table)
368   {
369     int row = table.getSelectedRow();
370     if (row > 0) table.setRowSelectionInterval(row - 1, row - 1);
371   }
372
373   private void tableDown(JTable JavaDoc table)
374   {
375     int row = table.getSelectedRow();
376     if (row < table.getRowCount() - 1) table.setRowSelectionInterval(row + 1, row + 1);
377   }
378
379   void _butDown_actionPerformed(ActionEvent JavaDoc e)
380   {
381     tableDown(_grdAccess);
382   }
383
384   void _butDelete_actionPerformed(ActionEvent JavaDoc e)
385   {
386     try
387     {
388       _grdAccess.deleteRow();
389     }
390     catch (Exception JavaDoc x)
391     {
392       setStatusMessage(x.getMessage(), false);
393     }
394   }
395
396   void _butBottom_actionPerformed(ActionEvent JavaDoc e)
397   {
398     tableBottom(_grdAccess);
399   }
400
401   void _butFirst_actionPerformed(ActionEvent JavaDoc e)
402   {
403     tableFirst(_grdAccess);
404   }
405
406   void _butCreate_actionPerformed(ActionEvent JavaDoc e)
407   {
408     try
409     {
410       _grdAccess.insertRow();
411     }
412     catch (Exception JavaDoc x)
413     {
414       setStatusMessage(x.getMessage(), false);
415     }
416   }
417
418   void _butUp_actionPerformed(ActionEvent JavaDoc e)
419   {
420     tableUp(_grdAccess);
421   }
422
423 }
Popular Tags