KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > usermanager > UserManagerFrame


1 package com.calipso.reportgenerator.usermanager;
2
3 import com.calipso.reportgenerator.common.LanguageTraslator;
4 import com.calipso.reportgenerator.common.*;
5 import com.calipso.reportgenerator.reportmanager.UsersRepository;
6
7 import javax.swing.*;
8 import java.awt.event.*;
9 import java.awt.*;
10
11 import com.calipso.reportgenerator.common.InfoException;
12
13
14 /**
15  * Esta clase es una interfaz grafica, que utiliza userManager
16  * esta formada por un jTabbedPane con dos Jlist que poseen Usuarios Y Roles respectivamente
17  */

18 public class UserManagerFrame extends javax.swing.JFrame JavaDoc implements UserManagerListener {
19
20     private javax.swing.JButton JavaDoc exit;
21     private javax.swing.JButton JavaDoc add;
22     private javax.swing.JButton JavaDoc del;
23     private javax.swing.JButton JavaDoc modify;
24     private javax.swing.JPanel JavaDoc jPanel1;
25     private javax.swing.JPanel JavaDoc jPanel2;
26     private javax.swing.JTabbedPane JavaDoc jTabbedPane1;
27     private JList listUsers;
28     private JList listRols;
29     private UserManager userManager;
30     private DefaultListModel listModelUsers;
31     private DefaultListModel listModelRols;
32     private boolean isEmbedded;
33     private ReportGeneratorConfiguration reportGeneratorConfiguration;
34
35   public UserManagerFrame(boolean isEmbedded, ReportGeneratorConfiguration reportGeneratorConfiguration) throws InfoException {
36     this.isEmbedded = isEmbedded;
37     String JavaDoc propertiesPath = "";
38     this.reportGeneratorConfiguration = reportGeneratorConfiguration;
39     if (this.reportGeneratorConfiguration == null){
40      this.reportGeneratorConfiguration = new ReportGeneratorConfiguration(ReportGeneratorConfiguration.getConfiguration(propertiesPath));
41     }
42     LanguageTraslator.newLocaleFrom(this.reportGeneratorConfiguration.getLocaleLanguage(),this.reportGeneratorConfiguration.getCountry(),this.reportGeneratorConfiguration.getLanguagesPath());
43
44
45
46     initComponents(this.reportGeneratorConfiguration);
47     try{
48       userManager = new UserManager(this.reportGeneratorConfiguration);
49     }
50     catch(InfoException e){
51       new ShowExceptionMessageDialog(LanguageTraslator.traslate("458"),e);
52       System.exit(0);
53     }
54
55     if (!isEmbedded()){
56       if(!login(this.reportGeneratorConfiguration) ) {
57         System.exit(0);
58       }
59     }
60     userManager.addListener(this);
61     //this.addWindowListener(this);
62

63     jTabbedPane1.addTab(LanguageTraslator.traslate("418"),null);
64     jTabbedPane1.addTab(LanguageTraslator.traslate("419"),null);
65     userManagerChange(userManager);
66
67
68     exit.addActionListener(new ButtonListener());
69     modify.addActionListener(new ButtonListener());
70     add.addActionListener(new ButtonListener());
71     del.addActionListener(new ButtonListener());
72   }
73
74     /**
75      * metodo privado para login
76      * @param reportGeneratorConfiguration
77      * @return
78      * @throws InfoException
79      */

80     private boolean login(ReportGeneratorConfiguration reportGeneratorConfiguration) throws InfoException {
81       UsersRepository repository = new UsersRepository(reportGeneratorConfiguration.getUsersRepositoryPath());
82       UserManagerLoginFrame userManagerLoginFrame = new UserManagerLoginFrame(this,"Login",true,"root",reportGeneratorConfiguration,userManager);
83       if(userManagerLoginFrame.login()) {
84         return true;
85       }
86       return false;
87     }
88
89     /**
90      *carga el list de usuarios con los usuarios de user manager
91      */

92     private void getUsers() {
93       Object JavaDoc[] list;
94       listModelUsers = new DefaultListModel();
95       try {
96         list = userManager.getUsers().toArray();
97         for(int i =0; i<list.length;i++) {
98           listModelUsers.addElement(list[i]);
99         }
100       }
101       catch(Exception JavaDoc e) {
102         new ShowExceptionMessageDialog(LanguageTraslator.traslate("433"),e);
103       }
104
105       listUsers = new JList(listModelUsers);
106       JScrollPane scrollpane = new JScrollPane(listUsers);
107
108       jTabbedPane1.setComponentAt(0,scrollpane);
109     }
110
111     /**
112      *carga el list de roles con los roles de user manager
113      */

114     private void getRols() {
115       java.util.List JavaDoc list;
116       listModelRols = new DefaultListModel();
117
118       try {
119         list = (java.util.List JavaDoc) userManager.getRols();
120         for(int i =0; i<list.size();i++) {
121           listModelRols.addElement(list.get(i));
122         }
123       }
124       catch(Exception JavaDoc e) {
125           new ShowExceptionMessageDialog(LanguageTraslator.traslate("434"),e);
126       }
127
128       listRols = new JList(listModelRols);
129       JScrollPane scrollpane = new JScrollPane(listRols);
130       jTabbedPane1.setComponentAt(1,scrollpane);
131     }
132
133     /**
134      * inicializa el frame
135      */

136     private void initComponents(ReportGeneratorConfiguration reportGeneratorConfiguration) {
137         jPanel1 = new javax.swing.JPanel JavaDoc();
138         jPanel2 = new javax.swing.JPanel JavaDoc();
139         jTabbedPane1 = new javax.swing.JTabbedPane JavaDoc();
140         add = new javax.swing.JButton JavaDoc();
141         del = new javax.swing.JButton JavaDoc();
142         modify = new javax.swing.JButton JavaDoc();
143         exit = new javax.swing.JButton JavaDoc();
144         if(isEmbedded()){
145           setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
146         }else{
147           setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
148         }
149         jPanel1.setLayout(new AbsoluteLayout());
150
151         jPanel2.setLayout(new AbsoluteLayout());
152
153         jPanel2.add(jTabbedPane1, new AbsoluteConstraints(0, 0, 290, 370));
154
155         add.setText(LanguageTraslator.traslate("130"));
156         jPanel2.add(add, new AbsoluteConstraints(310, 40, 90, 25));
157
158         del.setText(LanguageTraslator.traslate("421"));
159         jPanel2.add(del, new AbsoluteConstraints(310, 100, 90, 25));
160
161         modify.setText(LanguageTraslator.traslate("428"));
162         jPanel2.add(modify, new AbsoluteConstraints(310, 70, 90, 25));
163
164         exit.setText(LanguageTraslator.traslate("144"));
165
166         jPanel2.add(exit, new AbsoluteConstraints(310, 340, 90, 25));
167
168         jPanel1.add(jPanel2, new AbsoluteConstraints(0, 10, 410, 370));
169
170         getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
171
172         setTitle("User Manager");
173
174         setIconImage(reportGeneratorConfiguration.getImage("ICON"));
175
176
177         Dimension scrn = getToolkit().getScreenSize();
178
179         setLocation((scrn.width-getWidth())/3,(scrn.height-getHeight())/3);
180         setResizable(false);
181         pack();
182     }
183
184     /**
185      * metodo privado que invoca UserManagerModifyFrame, frame para agregar nuevo usuario
186      */

187     private void addUser() {
188       UserManagerModifyFrame dialog = new UserManagerModifyFrame(this, userManager);
189       dialog.setTitle(LanguageTraslator.traslate("130") + " "+ LanguageTraslator.traslate("141"));
190       dialog.setVisible(true);
191       userManagerChange(userManager);
192     }
193
194    /**
195     *metodo privado que invoca UserManagerModifyFrame que es un frame para modificar datos de los usuarios
196     */

197     private void modifyUser() {
198       UserManagerModifyFrame dialog = new UserManagerModifyFrame(this, userManager, (User)listUsers.getSelectedValue());
199       dialog.setTitle(LanguageTraslator.traslate("432"));
200       dialog.setVisible(true);
201       userManagerChange(userManager);
202     }
203
204   /**
205    * metodo privado que se invoca con button para eliminar los usuarios seleccionados de la lista
206    */

207     private void delUser() {
208     int res;
209       res = JOptionPane.showConfirmDialog(this,LanguageTraslator.traslate("430"),
210          LanguageTraslator.traslate("435"),JOptionPane.YES_NO_OPTION );
211       if( res == JOptionPane.YES_OPTION ) {
212         try {
213           for (int i = 0; i < listUsers.getSelectedValues().length; i++) {
214             userManager.removeUser((User)listUsers.getSelectedValues()[i]);
215           }
216           userManagerChange(userManager);
217         }
218         catch(Exception JavaDoc e) {
219            new ShowExceptionMessageDialog(LanguageTraslator.traslate("436"),e);
220         }
221       }
222     }
223
224     /**
225      * metodo privado que invoca RolManagerModifyFrame para modificar datos de los roles
226      *
227      */

228     private void modifyRol() {
229       RolManagerModifyFrame dialog = new RolManagerModifyFrame(this, userManager, (Rol)listRols.getSelectedValue());
230       dialog.setTitle(LanguageTraslator.traslate("429"));
231       dialog.setVisible(true);
232       userManagerChange(userManager);
233     }
234
235     /**
236      * metodo privado que invoca RolManagerModifyFrame, frame para agregar nuevo roles
237      */

238     private void addRol() throws InfoException {
239       RolManagerModifyFrame dialog = new RolManagerModifyFrame(this, userManager);
240       dialog.setTitle(LanguageTraslator.traslate("130") + " "+LanguageTraslator.traslate("426"));
241       dialog.setVisible(true);
242       userManagerChange(userManager);
243     }
244
245    /***
246    *metodo privado que se invoca con un button y sirve para eliminar los roles seleccionados
247    */

248     private void delRol() {
249       int res;
250       res = JOptionPane.showConfirmDialog(this,LanguageTraslator.traslate("431"),
251          LanguageTraslator.traslate("438"),JOptionPane.YES_NO_OPTION );
252
253       if( res == JOptionPane.YES_OPTION ) {
254         try {
255           for (int i = 0; i < listRols.getSelectedValues().length; i++) {
256             userManager.removeRol((Rol)listRols.getSelectedValues()[i]);
257            }
258           userManagerChange(userManager);
259         }
260         catch(Exception JavaDoc e) {
261           new ShowExceptionMessageDialog(LanguageTraslator.traslate("443"),e);
262         }
263       }
264     }
265
266   /**
267    * este metodo se invoca para ver si cambio userManager
268    * @param userManager
269    */

270   public void userManagerChange(UserManager userManager) {
271     getUsers();
272     getRols();
273   }
274
275   class ButtonListener implements ActionListener
276     {
277         public void actionPerformed(ActionEvent evento) {
278           //AGREGAR USUARIO
279
if( evento.getSource()== add && jTabbedPane1.getSelectedIndex() == 0 ) {
280             addUser();
281           }
282
283           //MODIFICAR USUARIO
284
else if ( evento.getSource()== modify && jTabbedPane1.getSelectedIndex() == 0 && listUsers.getSelectedIndices().length == 1 ) {
285             modifyUser();
286           }
287
288           //ELIMINAR USUARIO
289
else if ( evento.getSource()== del && jTabbedPane1.getSelectedIndex() == 0 && listUsers.getSelectedValues().length > 0 ) {
290             delUser();
291           }
292
293           //AGREGAR UN ROL
294
else if ( evento.getSource()== add && jTabbedPane1.getSelectedIndex() == 1 ) {
295             try {
296               addRol();
297             } catch (InfoException e) {
298               new ShowExceptionMessageDialog(LanguageTraslator.traslate("434"),e);
299             }
300           }
301
302           //MODIFICAR UN ROL
303
else if ( evento.getSource()== modify && jTabbedPane1.getSelectedIndex() == 1 && listRols.getSelectedValues().length == 1 ) {
304             try {
305               modifyRol();
306             } catch (Exception JavaDoc e) {
307               new ShowExceptionMessageDialog(LanguageTraslator.traslate("445"),e);
308             }
309           }
310
311           //ELIMINAR UN ROL
312
else if ( evento.getSource()== del && jTabbedPane1.getSelectedIndex() == 1 && listRols.getSelectedValues().length > 0 ) {
313             delRol();
314           }
315
316           //SALIR
317
else if (evento.getSource() == exit ) {
318             if (isEmbedded()){
319               setVisible(false);
320             }else{
321               System.exit(0);
322             }
323
324           }
325
326         }
327     }
328
329   public boolean isEmbedded() {
330     return isEmbedded;
331   }
332 }
333
334
Popular Tags