KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > dialogs > RoleEditorPanel


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.dialogs;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.KeyEvent JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 import javax.swing.DefaultComboBoxModel JavaDoc;
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JList JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.JScrollPane JavaDoc;
44 import javax.swing.JTextField JavaDoc;
45 import javax.swing.event.ListSelectionEvent JavaDoc;
46
47 import com.genimen.djeneric.language.Messages;
48 import com.genimen.djeneric.repository.DjPersistenceManager;
49 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
50 import com.genimen.djeneric.ui.Util;
51 import com.genimen.djeneric.util.DjLogger;
52
53 public class RoleEditorPanel extends JPanel JavaDoc
54 {
55   private static final long serialVersionUID = 1L;
56   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
57   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
58   JButton JavaDoc _butCreate = new JButton JavaDoc();
59   JButton JavaDoc _butRemove = new JButton JavaDoc();
60   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
61   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
62   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
63   JList JavaDoc _lstRoles = new JList JavaDoc();
64   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
65   JLabel JavaDoc _lblRole = new JLabel JavaDoc();
66   JTextField JavaDoc _edtName = new JTextField JavaDoc();
67   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
68   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
69   BorderLayout JavaDoc borderLayout4 = new BorderLayout JavaDoc();
70   ArrayList JavaDoc _roles = new ArrayList JavaDoc();
71
72   public RoleEditorPanel(DjPersistenceManager mgr)
73   {
74     try
75     {
76       jbInit();
77       String JavaDoc[] roles = mgr.getRoles();
78       for(int i=0; i<roles.length; i++) _roles.add(roles[i]);
79       updateModel(null);
80
81     }
82     catch (Exception JavaDoc ex)
83     {
84       DjLogger.log(ex);
85     }
86   }
87
88   public String JavaDoc[] getRoles()
89   {
90     return (String JavaDoc[]) _roles.toArray(new String JavaDoc[0]);
91   }
92   private void updateModel(String JavaDoc selectThisOne)
93   {
94     _lstRoles.setModel(new DefaultComboBoxModel JavaDoc(_roles.toArray(new String JavaDoc[0])));
95     if (selectThisOne != null) _lstRoles.setSelectedValue(selectThisOne, true);
96   }
97
98   void jbInit() throws Exception JavaDoc
99   {
100     this.setLayout(borderLayout1);
101     _butCreate.setText(Messages.getString("global.Create"));
102     _butCreate.addActionListener(new RoleEditorPanel__butCreate_actionAdapter(this));
103     _butRemove.setText(Messages.getString("global.Delete"));
104     _butRemove.addActionListener(new RoleEditorPanel__butRemove_actionAdapter(this));
105     jPanel3.setLayout(verticalFlowLayout1);
106     _lblRole.setText(Messages.getString("RoleEditorPanel.RoleName"));
107     jPanel1.setLayout(borderLayout2);
108     _edtName.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 11));
109     _edtName.setPreferredSize(new Dimension JavaDoc(200, 21));
110     _edtName.setText("");
111     _edtName.addKeyListener(new RoleEditorPanel__edtName_keyAdapter(this));
112     _lstRoles.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 11));
113     _lstRoles.addListSelectionListener(new RoleEditorPanel__lstRoles_listSelectionAdapter(this));
114     jPanel2.setLayout(borderLayout4);
115     jPanel3.add(_butCreate, null);
116     jPanel3.add(_butRemove, null);
117     this.add(jPanel2, BorderLayout.CENTER);
118     jPanel2.add(jScrollPane1, BorderLayout.CENTER);
119     jPanel2.add(jPanel1, BorderLayout.SOUTH);
120     jScrollPane1.getViewport().add(_lstRoles, null);
121     this.add(jPanel3, BorderLayout.EAST);
122     jPanel1.add(_lblRole, BorderLayout.NORTH);
123     jPanel1.add(_edtName, BorderLayout.CENTER);
124
125     Util.sizeButtons(jPanel3);
126   }
127
128   void _butCreate_actionPerformed(ActionEvent JavaDoc e)
129   {
130     String JavaDoc newRole = Messages.getString("RoleEditorPanel.NewRole");
131     _roles.add(newRole);
132     updateModel(newRole);
133     _edtName.requestFocus();
134     _edtName.selectAll();
135   }
136
137   void _butRemove_actionPerformed(ActionEvent JavaDoc e)
138   {
139     String JavaDoc sel = (String JavaDoc) _lstRoles.getSelectedValue();
140     if (sel != null) _roles.remove(sel);
141     updateModel(null);
142   }
143
144   void _lstRoles_valueChanged(ListSelectionEvent JavaDoc e)
145   {
146     String JavaDoc sel = (String JavaDoc) _lstRoles.getSelectedValue();
147     if (sel != null) _edtName.setText(sel);
148   }
149
150   void _edtName_keyReleased(KeyEvent JavaDoc e)
151   {
152     int idx = _lstRoles.getSelectedIndex();
153     
154     if (idx != -1 && _edtName.getText().trim().length() > 0)
155     {
156       String JavaDoc rolename = _edtName.getText().trim().toLowerCase();
157       _roles.set(idx, rolename);
158       updateModel(rolename);
159       _lstRoles.repaint();
160     }
161     else _edtName.setText("");
162   }
163
164 }
165
166 class RoleEditorPanel__butCreate_actionAdapter implements java.awt.event.ActionListener JavaDoc
167 {
168   RoleEditorPanel adaptee;
169
170   RoleEditorPanel__butCreate_actionAdapter(RoleEditorPanel adaptee)
171   {
172     this.adaptee = adaptee;
173   }
174
175   public void actionPerformed(ActionEvent JavaDoc e)
176   {
177     adaptee._butCreate_actionPerformed(e);
178   }
179 }
180
181 class RoleEditorPanel__butRemove_actionAdapter implements java.awt.event.ActionListener JavaDoc
182 {
183   RoleEditorPanel adaptee;
184
185   RoleEditorPanel__butRemove_actionAdapter(RoleEditorPanel adaptee)
186   {
187     this.adaptee = adaptee;
188   }
189
190   public void actionPerformed(ActionEvent JavaDoc e)
191   {
192     adaptee._butRemove_actionPerformed(e);
193   }
194 }
195
196 class RoleEditorPanel__lstRoles_listSelectionAdapter implements javax.swing.event.ListSelectionListener JavaDoc
197 {
198   RoleEditorPanel adaptee;
199
200   RoleEditorPanel__lstRoles_listSelectionAdapter(RoleEditorPanel adaptee)
201   {
202     this.adaptee = adaptee;
203   }
204
205   public void valueChanged(ListSelectionEvent JavaDoc e)
206   {
207     adaptee._lstRoles_valueChanged(e);
208   }
209 }
210
211 class RoleEditorPanel__edtName_keyAdapter extends java.awt.event.KeyAdapter JavaDoc
212 {
213   RoleEditorPanel adaptee;
214
215   RoleEditorPanel__edtName_keyAdapter(RoleEditorPanel adaptee)
216   {
217     this.adaptee = adaptee;
218   }
219
220   public void keyReleased(KeyEvent JavaDoc e)
221   {
222     adaptee._edtName_keyReleased(e);
223   }
224 }
Popular Tags