KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.calipso.reportgenerator.usermanager;
2
3 import com.calipso.reportgenerator.common.ShowExceptionMessageDialog;
4 import com.calipso.reportgenerator.common.*;
5
6 import javax.swing.*;
7 import java.awt.event.ActionListener JavaDoc;
8 import java.awt.event.ActionEvent JavaDoc;
9 import java.awt.*;
10 import java.util.*;
11
12 /**
13  * frame que se invoca para agregar o modificar un rol
14  */

15
16 public class RolManagerModifyFrame extends JDialog {
17
18     private javax.swing.JButton JavaDoc accept;
19     private javax.swing.JButton JavaDoc cancel;
20     private javax.swing.JLabel JavaDoc JLabelRolId;
21     private javax.swing.JButton JavaDoc add;
22     private javax.swing.JButton JavaDoc del;
23     private javax.swing.JLabel JavaDoc jLabelUsers;
24     private javax.swing.JPanel JavaDoc jPanel1;
25     private javax.swing.JTextField JavaDoc jTextFieldRolId;
26     private JList listUsers;
27     private DefaultListModel listModel;
28     private UserManager userManager;
29     private Rol rol;
30
31     /**
32      * Constructor para crear un rol nuevo
33      * @param parent
34      * @param userManager
35      */

36     public RolManagerModifyFrame(JFrame parent, UserManager userManager) {
37         super(parent,true);
38         setResizable(false);
39         this.userManager=userManager;
40         initComponents();
41
42         listModel = new DefaultListModel();
43
44         listUsers = new JList(listModel);
45
46         listUsers.setVisible(true);
47         JScrollPane scrollpane = new JScrollPane(listUsers);
48
49         jPanel1.add(scrollpane,new AbsoluteConstraints(80, 35, 210, 225));
50
51         accept.addActionListener(new ButtonListener());
52         add.addActionListener(new ButtonListener());
53         del.addActionListener(new ButtonListener());
54     }
55
56     /**
57      * Constructor para modificar un rol ya existente
58      * @param parent
59      * @param userManager
60      * @param rol
61      */

62     public RolManagerModifyFrame(JFrame parent, UserManager userManager, Rol rol) {
63         super(parent,true);
64         setResizable(false);
65         initComponents();
66         this.rol = rol;
67         this.userManager = userManager;
68         jTextFieldRolId.setText(rol.getId());
69         jTextFieldRolId.setEditable(false);
70
71         getUsers();
72
73         listUsers.setVisible(true);
74         JScrollPane scrollpane = new JScrollPane(listUsers);
75
76         jPanel1.add(scrollpane,new AbsoluteConstraints(80, 35, 210, 225));
77
78         add.addActionListener(new ButtonListener());
79         del.addActionListener(new ButtonListener());
80         accept.addActionListener(new ButtonListener());
81     }
82
83   /**
84    * obtiene los usuarios del rol
85    */

86
87   private void getUsers() {
88     listModel = new DefaultListModel();
89     java.util.List JavaDoc list;
90     try {
91       list = (java.util.List JavaDoc) userManager.getUsersByRol(rol);
92       for (int i = 0; i < list.size(); i++) {
93         listModel.addElement(list.get(i));
94       }
95       listUsers = new JList(listModel);
96
97     }
98     catch(Exception JavaDoc e) {
99       new ShowExceptionMessageDialog(LanguageTraslator.traslate("441"),e);
100     }
101   }
102
103   /**
104    *obtiene los usuario disponibles para agregarse a un rol
105    * @return ArrayList
106    */

107   private ArrayList getUsersAvailable() {
108     ArrayList list = new ArrayList();
109     Object JavaDoc[] o;
110     boolean flag;
111
112     try {
113       o = userManager.getUsers().toArray();
114       if(listModel.size() == 0) {
115         list.addAll(userManager.getUsers());
116         return list;
117       }
118
119       for (int i = 0; i < userManager.getUsers().size() ; i++) {
120       flag=false;
121
122         for (int j = 0; j < listModel.size(); j++) {
123           if( ((User)o[i]).compareTo((User) listModel.get(j) ) == 0) {
124             flag=true;
125           }
126         }
127         if( flag != true) {
128           list.add(o[i]);
129         }
130       }
131     }
132
133     catch(Exception JavaDoc e) {
134      new ShowExceptionMessageDialog(LanguageTraslator.traslate("433"),e);
135     }
136     return list;
137   }
138
139   public void setTitulo(String JavaDoc titulo) {
140       setTitle(titulo);
141     }
142
143   private void initComponents() {
144         jPanel1 = new JPanel();
145         jTextFieldRolId = new JTextField();
146         JLabelRolId = new JLabel();
147         jLabelUsers = new JLabel();
148
149         add = new JButton();
150         del = new JButton();
151         accept = new JButton();
152         cancel = new JButton();
153
154         getContentPane().setLayout(new AbsoluteLayout());
155
156         setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
157         jPanel1.setLayout(new AbsoluteLayout());
158
159         jPanel1.add(jTextFieldRolId, new AbsoluteConstraints(80, 10, 210, -1));
160
161         JLabelRolId.setText(LanguageTraslator.traslate("426"));
162         jPanel1.add(JLabelRolId, new AbsoluteConstraints(10, 10, 80, 20));
163
164         jLabelUsers.setText(LanguageTraslator.traslate("418"));
165         jPanel1.add(jLabelUsers, new AbsoluteConstraints(10, 35, 80, 20));
166
167         add.setText(LanguageTraslator.traslate("130"));
168
169         jPanel1.add(add, new AbsoluteConstraints(300, 35, 80, 25));
170
171         del.setText(LanguageTraslator.traslate("421"));
172         jPanel1.add(del, new AbsoluteConstraints(300, 65, 80, 25));
173
174         accept.setText(LanguageTraslator.traslate("112"));
175         jPanel1.add(accept, new AbsoluteConstraints(80, 275, 85, 25));
176
177         cancel.setText(LanguageTraslator.traslate("113"));
178         jPanel1.add(cancel, new AbsoluteConstraints(205, 275, 85, 25));
179
180         getContentPane().add(jPanel1, new AbsoluteConstraints(0,0, 390, 310));
181
182         Dimension scrn = getToolkit().getScreenSize();
183
184         this.setLocation((scrn.width-getWidth())/3+80,(scrn.height-getHeight())/3+80);
185
186         cancel.addActionListener(new ButtonListener());
187
188         pack();
189     }
190
191    /**
192     * agrega o elimina los usuarios de la lista al userManager
193     */

194     private void addUsersToRol() throws InfoException {
195       java.util.List JavaDoc list;
196      ArrayList repositoryUserAdd=new ArrayList();
197      ArrayList repositoyUserRemove= new ArrayList();
198
199      try {
200
201       list = userManager.getUsersByRol(rol);
202      }
203      catch(Exception JavaDoc e) {
204       throw new InfoException(LanguageTraslator.traslate("441"),e);
205      }
206
207       for (int i = 0; i < listModel.size(); i++) {
208           if(!list.contains(listModel.getElementAt(i)) ) {
209           //el elemento de la lista, no está en userManager, entonces lo agrego a la lista a agregar
210
repositoryUserAdd.add((User)listModel.getElementAt(i));
211         }
212       }
213
214       for (int j = 0; j < list.size(); j++) {
215           if(!listModel.contains(list.get(j))) {
216             //agrega los elementos usuario que no tiene el rol al userManager
217
repositoyUserRemove.add((User)list.get(j));
218         }
219       }
220
221       for (int i = 0; i < repositoryUserAdd.size(); i++) {
222         try {
223           userManager.addUsersToCollectionRol(rol,(User)repositoryUserAdd.get(i));
224         }
225         catch(Exception JavaDoc e) {
226           new InfoException(LanguageTraslator.traslate("442")+" "+(User)repositoryUserAdd.get(i),e);
227         }
228       }
229
230       for (int i = 0; i < repositoyUserRemove.size(); i++) {
231         try {
232           userManager.removeRolToUser(rol,(User)repositoyUserRemove.get(i));
233         }
234         catch(Exception JavaDoc e) {
235           throw new InfoException(LanguageTraslator.traslate("436")+" "+(User)repositoyUserRemove.get(i),e);
236         }
237       }
238   }
239     /**
240      * verifica los campo vacios
241      * @param jLabel
242      * @param jLabel
243      * @return
244      */

245     private boolean emptyTextField(JTextField jTextField, JLabel jLabel) {
246       if(jTextField.getText().length()==0 ) {
247         JOptionPane.showMessageDialog(null,LanguageTraslator.traslate("422")+" " + jLabel.getText());
248         return true;
249       }
250       return false;
251     }
252
253   class ButtonListener implements ActionListener JavaDoc {
254
255    public ButtonListener() {
256     }
257
258     public void actionPerformed(ActionEvent JavaDoc e) {
259
260       if( e.getSource() == cancel) {
261        setVisible(false);
262
263      //ACEPTA AGREGAR ROL NUEVO
264
} else if ( e.getSource() == accept && rol==null) {
265
266        acceptNewRol();
267
268        //ACEPTA MODIFICAR ROL
269
} else if ( e.getSource() == accept && rol!=null) {
270        try {
271          addUsersToRol();
272        } catch (InfoException el) {
273          new ShowExceptionMessageDialog(LanguageTraslator.traslate("446"),el);
274        }
275         setVisible(false);
276
277      //AGREGO UN USUARIO A LA LISTA DEL ROL
278
} else if ( e.getSource() == add ) {
279        addUserToListRol();
280
281
282      //ELIMINAR UN USUARIO DE LA LISTA DEL ROL
283
} else if ( e.getSource() == del && listUsers.getSelectedValues().length > 0 ) {
284        delUserToListRol();
285
286      }
287     }
288
289   }
290
291   /**
292    * invoca un AddUsertoRolFrame, frame para agregar usuarios a la lista, del rol
293    */

294   private void addUserToListRol() {
295     if(getUsersAvailable().size() > 0 ) {
296
297
298     AddUsertoRolFrame dialog = new AddUsertoRolFrame(this,getUsersAvailable());
299        dialog.setTitle(LanguageTraslator.traslate("427"));
300        dialog.setVisible(true);
301
302        if (dialog.getUsers() != null ) {
303         for( int i=0; i<dialog.getUsers().length; i++ ) {
304           listModel.addElement((User)dialog.getUsers()[i]);
305         }
306        }
307     } else {
308       JOptionPane.showMessageDialog(null,LanguageTraslator.traslate("450"));
309     }
310   }
311
312   /**
313    * elimina usuario de la lista del rol
314    */

315   private void delUserToListRol() {
316     while(listUsers.getSelectedValues().length > 0 ) {
317       listModel.removeElement((User) listUsers.getSelectedValues()[0]);
318     }
319   }
320
321   /**
322    * metodo privado que se invoca cuando el usuario acepta agregar un nuevo rol
323    */

324   private void acceptNewRol() {
325        if(emptyTextField(jTextFieldRolId,JLabelRolId ) ) {
326            return;
327          }
328
329          rol = new Rol(jTextFieldRolId.getText(),jTextFieldRolId.getText());
330
331          try {
332            userManager.addRol(rol);
333          }
334          catch(Exception JavaDoc e) {
335            new ShowExceptionMessageDialog(LanguageTraslator.traslate("444"),e);
336            rol=null;
337            return;
338          }
339       addUsersToNewRol();
340       setVisible(false);
341       }
342   /**
343    * metodo privado para agregar usuarios a la lista del nuevo rol
344    */

345   private void addUsersToNewRol() {
346     int i=0;
347     try {
348       for (i = 0; i < listModel.size(); i++) {
349         userManager.addUsersToCollectionRol(rol,(User)listModel.getElementAt(i));
350       }
351     }
352     catch(Exception JavaDoc e) {
353       new ShowExceptionMessageDialog(LanguageTraslator.traslate("442")+" "+(User)listModel.getElementAt(i),e);
354     }
355   }
356
357 }
358
359
360
Popular Tags