001: package com.calipso.reportgenerator.usermanager;
002:
003: import com.calipso.reportgenerator.common.ShowExceptionMessageDialog;
004: import com.calipso.reportgenerator.common.*;
005: import javax.swing.*;
006: import java.awt.event.*;
007: import java.awt.*;
008: import java.util.*;
009: import com.calipso.reportgenerator.common.InfoException;
010:
011: /**
012: * frame que se invoca para agregar o modificar un usuario
013: */
014: public class UserManagerModifyFrame extends JDialog {
015:
016: private javax.swing.JButton accept;
017: private javax.swing.JButton cancel;
018: private javax.swing.JLabel User;
019: private javax.swing.JLabel Name;
020: private javax.swing.JLabel Company;
021: private javax.swing.JButton add;
022: private javax.swing.JButton del;
023: private javax.swing.JLabel jLabel2;
024: private javax.swing.JPanel jPanel1;
025: private javax.swing.JTextField jTextFieldUser;
026: private javax.swing.JTextField jTextFieldName;
027: private javax.swing.JTextField jTextFieldCompany;
028: private JList listRols;
029: private DefaultListModel listModel;
030: private UserManager userManager;
031: private User user;
032:
033: /**
034: * Construnctor de usuario nuevo
035: * @param parent
036: * @param userManager
037: */
038: public UserManagerModifyFrame(JFrame parent, UserManager userManager) {
039: super (parent, true);
040: setResizable(false);
041: this .userManager = userManager;
042: initComponents();
043:
044: listModel = new DefaultListModel();
045:
046: listRols = new JList(listModel);
047:
048: listRols.setVisible(true);
049: JScrollPane scrollpane = new JScrollPane(listRols);
050:
051: jPanel1.add(scrollpane, new AbsoluteConstraints(80, 85, 210,
052: 180));
053:
054: accept.addActionListener(new ButtonListener());
055: add.addActionListener(new ButtonListener());
056: del.addActionListener(new ButtonListener());
057: }
058:
059: /**
060: * Constructor cuando voy a modificar datos de un usuario
061: * @param parent
062: * @param userManager
063: * @param user
064: */
065: public UserManagerModifyFrame(JFrame parent,
066: UserManager userManager, User user) {
067: super (parent, true);
068: setResizable(false);
069: initComponents();
070: this .user = user;
071: this .userManager = userManager;
072: jTextFieldName.setText(user.getUserName());
073: jTextFieldUser.setText(user.getId());
074: jTextFieldUser.setEditable(false);
075: if (user.getCompany().equals("null")) {
076: jTextFieldCompany.setText("");
077: } else {
078: jTextFieldCompany.setText(user.getCompany());
079: }
080:
081: getRols();
082:
083: listRols.setVisible(true);
084: JScrollPane scrollpane = new JScrollPane(listRols);
085:
086: jPanel1.add(scrollpane, new AbsoluteConstraints(80, 85, 210,
087: 180));
088:
089: add.addActionListener(new ButtonListener());
090: del.addActionListener(new ButtonListener());
091: accept.addActionListener(new ButtonListener());
092: }
093:
094: /**
095: * obtiene los roles cuando se hace show del formulario
096: */
097: private void getRols() {
098: java.util.List list;
099: listModel = new DefaultListModel();
100: try {
101: list = (java.util.List) userManager.getRolsByUser(user);
102: if (!list.isEmpty()) {
103: for (int i = 0; i < list.size(); i++) {
104: listModel.addElement(list.get(i));
105: }
106: }
107: } catch (Exception e) {
108: new ShowExceptionMessageDialog(LanguageTraslator
109: .traslate("439"), e);
110: }
111: listRols = new JList(listModel);
112: listRols = new JList(listModel);
113:
114: }
115:
116: /**
117: * checkea los roles de la lista y obtiene los diponibles que se pueden agregar
118: */
119: private ArrayList getRolesAvailable() {
120: ArrayList list = new ArrayList();
121: boolean flag = false;
122:
123: try {
124:
125: if (listModel.size() == 0) {
126: list = (ArrayList) userManager.getRols();
127: return list;
128: }
129:
130: for (int i = 0; i < userManager.getRols().size(); i++) {
131: flag = false;
132:
133: for (int j = 0; j < listModel.size(); j++) {
134: if (((Rol) userManager.getRols().get(i))
135: .compareTo(listModel.get(j)) == 0) {
136: flag = true;
137: }
138: }
139:
140: if (flag != true) {
141: list.add(userManager.getRols().get(i));
142: }
143: }
144: } catch (Exception e) {
145: new ShowExceptionMessageDialog("434", e);
146: }
147: return list;
148: }
149:
150: public void setTitulo(String titulo) {
151: setTitle(titulo);
152: }
153:
154: private void initComponents() {
155: jPanel1 = new JPanel();
156: jTextFieldUser = new JTextField();
157: User = new JLabel();
158: jTextFieldName = new JTextField();
159: Name = new JLabel();
160: jTextFieldCompany = new JTextField();
161: Company = new JLabel();
162: jLabel2 = new JLabel();
163:
164: add = new JButton();
165: del = new JButton();
166: accept = new JButton();
167: cancel = new JButton();
168:
169: getContentPane().setLayout(new AbsoluteLayout());
170:
171: setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
172: jPanel1.setLayout(new AbsoluteLayout());
173:
174: jPanel1.add(jTextFieldUser, new AbsoluteConstraints(80, 10,
175: 210, -1));
176:
177: User.setText(LanguageTraslator.traslate("141"));
178: jPanel1.add(User, new AbsoluteConstraints(10, 10, 80, 20));
179:
180: jPanel1.add(jTextFieldName, new AbsoluteConstraints(80, 35,
181: 210, -1));
182:
183: Name.setText(LanguageTraslator.traslate("232"));
184: jPanel1.add(Name, new AbsoluteConstraints(10, 35, 80, 20));
185:
186: jPanel1.add(jTextFieldCompany, new AbsoluteConstraints(80, 60,
187: 210, -1));
188:
189: Company.setText(LanguageTraslator.traslate("420"));
190: jPanel1.add(Company, new AbsoluteConstraints(10, 60, 80, 20));
191:
192: jLabel2.setText(LanguageTraslator.traslate("426"));
193: jPanel1.add(jLabel2, new AbsoluteConstraints(10, 80, 80, 20));
194:
195: add.setText(LanguageTraslator.traslate("130"));
196:
197: jPanel1.add(add, new AbsoluteConstraints(300, 85, 80, 25));
198:
199: del.setText(LanguageTraslator.traslate("421"));
200: jPanel1.add(del, new AbsoluteConstraints(300, 115, 80, 25));
201:
202: accept.setText(LanguageTraslator.traslate("112"));
203: jPanel1.add(accept, new AbsoluteConstraints(80, 275, 85, 25));
204:
205: cancel.setText(LanguageTraslator.traslate("113"));
206: jPanel1.add(cancel, new AbsoluteConstraints(205, 275, 85, 25));
207:
208: getContentPane().add(jPanel1,
209: new AbsoluteConstraints(0, 0, 390, 310));
210:
211: Dimension scrn = getToolkit().getScreenSize();
212:
213: this .setLocation((scrn.width - getWidth()) / 3 + 80,
214: (scrn.height - getHeight()) / 3 + 80);
215:
216: cancel.addActionListener(new ButtonListener());
217:
218: pack();
219: }
220:
221: /**agrega o elimina roles de la lista al userManager
222: *
223: */
224: private void addRolesToUser() throws InfoException {
225: java.util.List list;
226: ArrayList repositoryRolAdd = new ArrayList();
227: ArrayList repositoyRolRemove = new ArrayList();
228:
229: try {
230:
231: list = userManager.getRolsByUser(user);
232: } catch (Exception e) {
233: throw new InfoException(LanguageTraslator.traslate("439"),
234: e);
235: }
236:
237: for (int i = 0; i < listModel.size(); i++) {
238: if (!list.contains(listModel.getElementAt(i))) {
239: repositoryRolAdd.add((Rol) listModel.getElementAt(i));
240: }
241: }
242:
243: for (int j = 0; j < list.size(); j++) {
244: if (!listModel.contains(list.get(j))) {
245: //el elemento del userManager no está en la lista, entonces lo agrego a la lista a eliminar
246: repositoyRolRemove.add((Rol) list.get(j));
247: }
248: }
249:
250: for (int i = 0; i < repositoryRolAdd.size(); i++) {
251: try {
252: userManager.addUsersToCollectionRol(
253: (Rol) repositoryRolAdd.get(i), user);
254: } catch (Exception e) {
255: throw new InfoException(LanguageTraslator
256: .traslate("444")
257: + " " + (Rol) repositoryRolAdd.get(i), e);
258: }
259: }
260:
261: //elimina roles al userManager del usuario
262: for (int i = 0; i < repositoyRolRemove.size(); i++) {
263: try {
264: userManager.removeRolToUser((Rol) repositoyRolRemove
265: .get(i), user);
266: } catch (Exception e) {
267: throw new InfoException(LanguageTraslator
268: .traslate("437")
269: + " " + (Rol) repositoyRolRemove.get(i), e);
270: }
271: }
272:
273: }
274:
275: /**
276: * verifica campos vacios
277: * @param jTextField
278: * @param jLabel
279: * @return
280: */
281: private boolean emptyTextField(JTextField jTextField, JLabel jLabel) {
282: if (jTextField.getText().length() == 0) {
283: JOptionPane.showMessageDialog(null, LanguageTraslator
284: .traslate("422")
285: + " " + jLabel.getText());
286:
287: return true;
288: }
289: return false;
290: }
291:
292: private void acceptNewUser() {
293:
294: if (jTextFieldCompany.getText().length() == 0) {
295: user = new User(jTextFieldUser.getText(), jTextFieldName
296: .getText());
297: } else {
298: user = new User(jTextFieldUser.getText(), jTextFieldName
299: .getText(), jTextFieldCompany.getText());
300: }
301:
302: try {
303: userManager.addUser(user);
304: } catch (InfoException e) {
305: new ShowExceptionMessageDialog(LanguageTraslator
306: .traslate("423"), e);
307: user = null;
308: return;
309: }
310: try {
311: addRolsToNewUser();
312: setVisible(false);
313: } catch (InfoException e) {
314: new ShowExceptionMessageDialog(LanguageTraslator
315: .traslate("446"), e);
316: }
317:
318: }
319:
320: /**
321: * agrega roles a la lista del nuevo usuario
322: * @throws InfoException
323: */
324: private void addRolsToNewUser() throws InfoException {
325: try {
326: for (int i = 0; i < listModel.size(); i++) {
327: userManager.addUsersToCollectionRol((Rol) listModel
328: .getElementAt(i), user);
329: }
330: } catch (Exception e) {
331: throw new InfoException(LanguageTraslator.traslate("444"),
332: e);
333: }
334: }
335:
336: class ButtonListener implements ActionListener {
337:
338: public void actionPerformed(ActionEvent e) {
339: if (e.getSource() == cancel) {
340: setVisible(false);
341:
342: //ACEPTA AGREGAR USUARIO NUEVO
343: } else if (e.getSource() == accept && user == null) {
344:
345: if (emptyTextField(jTextFieldUser, User)) {
346: return;
347: }
348:
349: if (emptyTextField(jTextFieldName, Name)) {
350: return;
351: }
352: acceptNewUser();
353:
354: //ACEPTA MODIFICAR USUARIO MODIFICAR
355: } else if (e.getSource() == accept && user != null) {
356: try {
357: acceptModifyUser();
358: } catch (InfoException el) {
359: new ShowExceptionMessageDialog(LanguageTraslator
360: .traslate("446"), el);
361: }
362:
363: //AGREGO UN ROL A LA LISTA DEL USUARIO
364: } else if (e.getSource() == add) {
365: addRoltoUserList();
366: }
367:
368: //eliminar roles a un usuario
369: else if (e.getSource() == del
370: && listRols.getSelectedValues().length > 0) {
371: delRolToUser();
372:
373: }
374: }
375:
376: }
377:
378: /**
379: * elimina roles de la lista del usuario
380: */
381: private void delRolToUser() {
382: while (listRols.getSelectedValues().length > 0) {
383: Rol rol = (Rol) listRols.getSelectedValues()[0];
384: listModel.removeElement(rol);
385: }
386: }
387:
388: /**
389: * agrega roles a la lista del usuario
390: */
391: private void addRoltoUserList() {
392: if (getRolesAvailable().size() > 0) {
393:
394: AddRoltoUserFrame dialog = new AddRoltoUserFrame(this ,
395: getRolesAvailable());
396: dialog.setTitle(LanguageTraslator.traslate("425"));
397: dialog.setVisible(true);
398:
399: if (dialog.getRols() != null) {
400: for (int i = 0; i < dialog.getRols().length; i++) {
401:
402: listModel.addElement((Rol) dialog.getRols()[i]);
403: }
404: }
405: } else {
406: JOptionPane.showMessageDialog(null, LanguageTraslator
407: .traslate("449"));
408: }
409:
410: }
411:
412: /**
413: * acepta las modificaciones
414: * @throws InfoException
415: */
416: private void acceptModifyUser() throws InfoException {
417: user.setName(jTextFieldName.getText());
418: if (jTextFieldCompany.getText().length() != 0) {
419: user.setCompany(jTextFieldCompany.getText());
420: }
421:
422: if (emptyTextField(jTextFieldUser, User)) {
423: return;
424: }
425:
426: if (emptyTextField(jTextFieldName, Name)) {
427: return;
428: }
429:
430: try {
431: userManager.modifyUser(user);
432: } catch (Exception e) {
433: new ShowExceptionMessageDialog(LanguageTraslator
434: .traslate("440"), e);
435: }
436: addRolesToUser();
437: setVisible(false);
438: }
439:
440: }
|