001: package com.calipso.reportgenerator.usermanager;
002:
003: import com.calipso.reportgenerator.common.LanguageTraslator;
004: import com.calipso.reportgenerator.common.*;
005: import com.calipso.reportgenerator.reportmanager.UsersRepository;
006:
007: import javax.swing.*;
008: import java.awt.event.*;
009: import java.awt.*;
010:
011: import com.calipso.reportgenerator.common.InfoException;
012:
013: /**
014: * Esta clase es una interfaz grafica, que utiliza userManager
015: * esta formada por un jTabbedPane con dos Jlist que poseen Usuarios Y Roles respectivamente
016: */
017: public class UserManagerFrame extends javax.swing.JFrame implements
018: UserManagerListener {
019:
020: private javax.swing.JButton exit;
021: private javax.swing.JButton add;
022: private javax.swing.JButton del;
023: private javax.swing.JButton modify;
024: private javax.swing.JPanel jPanel1;
025: private javax.swing.JPanel jPanel2;
026: private javax.swing.JTabbedPane jTabbedPane1;
027: private JList listUsers;
028: private JList listRols;
029: private UserManager userManager;
030: private DefaultListModel listModelUsers;
031: private DefaultListModel listModelRols;
032: private boolean isEmbedded;
033: private ReportGeneratorConfiguration reportGeneratorConfiguration;
034:
035: public UserManagerFrame(boolean isEmbedded,
036: ReportGeneratorConfiguration reportGeneratorConfiguration)
037: throws InfoException {
038: this .isEmbedded = isEmbedded;
039: String propertiesPath = "";
040: this .reportGeneratorConfiguration = reportGeneratorConfiguration;
041: if (this .reportGeneratorConfiguration == null) {
042: this .reportGeneratorConfiguration = new ReportGeneratorConfiguration(
043: ReportGeneratorConfiguration
044: .getConfiguration(propertiesPath));
045: }
046: LanguageTraslator.newLocaleFrom(
047: this .reportGeneratorConfiguration.getLocaleLanguage(),
048: this .reportGeneratorConfiguration.getCountry(),
049: this .reportGeneratorConfiguration.getLanguagesPath());
050:
051: initComponents(this .reportGeneratorConfiguration);
052: try {
053: userManager = new UserManager(
054: this .reportGeneratorConfiguration);
055: } catch (InfoException e) {
056: new ShowExceptionMessageDialog(LanguageTraslator
057: .traslate("458"), e);
058: System.exit(0);
059: }
060:
061: if (!isEmbedded()) {
062: if (!login(this .reportGeneratorConfiguration)) {
063: System.exit(0);
064: }
065: }
066: userManager.addListener(this );
067: //this.addWindowListener(this);
068:
069: jTabbedPane1.addTab(LanguageTraslator.traslate("418"), null);
070: jTabbedPane1.addTab(LanguageTraslator.traslate("419"), null);
071: userManagerChange(userManager);
072:
073: exit.addActionListener(new ButtonListener());
074: modify.addActionListener(new ButtonListener());
075: add.addActionListener(new ButtonListener());
076: del.addActionListener(new ButtonListener());
077: }
078:
079: /**
080: * metodo privado para login
081: * @param reportGeneratorConfiguration
082: * @return
083: * @throws InfoException
084: */
085: private boolean login(
086: ReportGeneratorConfiguration reportGeneratorConfiguration)
087: throws InfoException {
088: UsersRepository repository = new UsersRepository(
089: reportGeneratorConfiguration.getUsersRepositoryPath());
090: UserManagerLoginFrame userManagerLoginFrame = new UserManagerLoginFrame(
091: this , "Login", true, "root",
092: reportGeneratorConfiguration, userManager);
093: if (userManagerLoginFrame.login()) {
094: return true;
095: }
096: return false;
097: }
098:
099: /**
100: *carga el list de usuarios con los usuarios de user manager
101: */
102: private void getUsers() {
103: Object[] list;
104: listModelUsers = new DefaultListModel();
105: try {
106: list = userManager.getUsers().toArray();
107: for (int i = 0; i < list.length; i++) {
108: listModelUsers.addElement(list[i]);
109: }
110: } catch (Exception e) {
111: new ShowExceptionMessageDialog(LanguageTraslator
112: .traslate("433"), e);
113: }
114:
115: listUsers = new JList(listModelUsers);
116: JScrollPane scrollpane = new JScrollPane(listUsers);
117:
118: jTabbedPane1.setComponentAt(0, scrollpane);
119: }
120:
121: /**
122: *carga el list de roles con los roles de user manager
123: */
124: private void getRols() {
125: java.util.List list;
126: listModelRols = new DefaultListModel();
127:
128: try {
129: list = (java.util.List) userManager.getRols();
130: for (int i = 0; i < list.size(); i++) {
131: listModelRols.addElement(list.get(i));
132: }
133: } catch (Exception e) {
134: new ShowExceptionMessageDialog(LanguageTraslator
135: .traslate("434"), e);
136: }
137:
138: listRols = new JList(listModelRols);
139: JScrollPane scrollpane = new JScrollPane(listRols);
140: jTabbedPane1.setComponentAt(1, scrollpane);
141: }
142:
143: /**
144: * inicializa el frame
145: */
146: private void initComponents(
147: ReportGeneratorConfiguration reportGeneratorConfiguration) {
148: jPanel1 = new javax.swing.JPanel();
149: jPanel2 = new javax.swing.JPanel();
150: jTabbedPane1 = new javax.swing.JTabbedPane();
151: add = new javax.swing.JButton();
152: del = new javax.swing.JButton();
153: modify = new javax.swing.JButton();
154: exit = new javax.swing.JButton();
155: if (isEmbedded()) {
156: setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
157: } else {
158: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
159: }
160: jPanel1.setLayout(new AbsoluteLayout());
161:
162: jPanel2.setLayout(new AbsoluteLayout());
163:
164: jPanel2.add(jTabbedPane1, new AbsoluteConstraints(0, 0, 290,
165: 370));
166:
167: add.setText(LanguageTraslator.traslate("130"));
168: jPanel2.add(add, new AbsoluteConstraints(310, 40, 90, 25));
169:
170: del.setText(LanguageTraslator.traslate("421"));
171: jPanel2.add(del, new AbsoluteConstraints(310, 100, 90, 25));
172:
173: modify.setText(LanguageTraslator.traslate("428"));
174: jPanel2.add(modify, new AbsoluteConstraints(310, 70, 90, 25));
175:
176: exit.setText(LanguageTraslator.traslate("144"));
177:
178: jPanel2.add(exit, new AbsoluteConstraints(310, 340, 90, 25));
179:
180: jPanel1.add(jPanel2, new AbsoluteConstraints(0, 10, 410, 370));
181:
182: getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
183:
184: setTitle("User Manager");
185:
186: setIconImage(reportGeneratorConfiguration.getImage("ICON"));
187:
188: Dimension scrn = getToolkit().getScreenSize();
189:
190: setLocation((scrn.width - getWidth()) / 3,
191: (scrn.height - getHeight()) / 3);
192: setResizable(false);
193: pack();
194: }
195:
196: /**
197: * metodo privado que invoca UserManagerModifyFrame, frame para agregar nuevo usuario
198: */
199: private void addUser() {
200: UserManagerModifyFrame dialog = new UserManagerModifyFrame(
201: this , userManager);
202: dialog.setTitle(LanguageTraslator.traslate("130") + " "
203: + LanguageTraslator.traslate("141"));
204: dialog.setVisible(true);
205: userManagerChange(userManager);
206: }
207:
208: /**
209: *metodo privado que invoca UserManagerModifyFrame que es un frame para modificar datos de los usuarios
210: */
211: private void modifyUser() {
212: UserManagerModifyFrame dialog = new UserManagerModifyFrame(
213: this , userManager, (User) listUsers.getSelectedValue());
214: dialog.setTitle(LanguageTraslator.traslate("432"));
215: dialog.setVisible(true);
216: userManagerChange(userManager);
217: }
218:
219: /**
220: * metodo privado que se invoca con button para eliminar los usuarios seleccionados de la lista
221: */
222: private void delUser() {
223: int res;
224: res = JOptionPane.showConfirmDialog(this , LanguageTraslator
225: .traslate("430"), LanguageTraslator.traslate("435"),
226: JOptionPane.YES_NO_OPTION);
227: if (res == JOptionPane.YES_OPTION) {
228: try {
229: for (int i = 0; i < listUsers.getSelectedValues().length; i++) {
230: userManager.removeUser((User) listUsers
231: .getSelectedValues()[i]);
232: }
233: userManagerChange(userManager);
234: } catch (Exception e) {
235: new ShowExceptionMessageDialog(LanguageTraslator
236: .traslate("436"), e);
237: }
238: }
239: }
240:
241: /**
242: * metodo privado que invoca RolManagerModifyFrame para modificar datos de los roles
243: *
244: */
245: private void modifyRol() {
246: RolManagerModifyFrame dialog = new RolManagerModifyFrame(this ,
247: userManager, (Rol) listRols.getSelectedValue());
248: dialog.setTitle(LanguageTraslator.traslate("429"));
249: dialog.setVisible(true);
250: userManagerChange(userManager);
251: }
252:
253: /**
254: * metodo privado que invoca RolManagerModifyFrame, frame para agregar nuevo roles
255: */
256: private void addRol() throws InfoException {
257: RolManagerModifyFrame dialog = new RolManagerModifyFrame(this ,
258: userManager);
259: dialog.setTitle(LanguageTraslator.traslate("130") + " "
260: + LanguageTraslator.traslate("426"));
261: dialog.setVisible(true);
262: userManagerChange(userManager);
263: }
264:
265: /***
266: *metodo privado que se invoca con un button y sirve para eliminar los roles seleccionados
267: */
268: private void delRol() {
269: int res;
270: res = JOptionPane.showConfirmDialog(this , LanguageTraslator
271: .traslate("431"), LanguageTraslator.traslate("438"),
272: JOptionPane.YES_NO_OPTION);
273:
274: if (res == JOptionPane.YES_OPTION) {
275: try {
276: for (int i = 0; i < listRols.getSelectedValues().length; i++) {
277: userManager.removeRol((Rol) listRols
278: .getSelectedValues()[i]);
279: }
280: userManagerChange(userManager);
281: } catch (Exception e) {
282: new ShowExceptionMessageDialog(LanguageTraslator
283: .traslate("443"), e);
284: }
285: }
286: }
287:
288: /**
289: * este metodo se invoca para ver si cambio userManager
290: * @param userManager
291: */
292: public void userManagerChange(UserManager userManager) {
293: getUsers();
294: getRols();
295: }
296:
297: class ButtonListener implements ActionListener {
298: public void actionPerformed(ActionEvent evento) {
299: //AGREGAR USUARIO
300: if (evento.getSource() == add
301: && jTabbedPane1.getSelectedIndex() == 0) {
302: addUser();
303: }
304:
305: //MODIFICAR USUARIO
306: else if (evento.getSource() == modify
307: && jTabbedPane1.getSelectedIndex() == 0
308: && listUsers.getSelectedIndices().length == 1) {
309: modifyUser();
310: }
311:
312: //ELIMINAR USUARIO
313: else if (evento.getSource() == del
314: && jTabbedPane1.getSelectedIndex() == 0
315: && listUsers.getSelectedValues().length > 0) {
316: delUser();
317: }
318:
319: //AGREGAR UN ROL
320: else if (evento.getSource() == add
321: && jTabbedPane1.getSelectedIndex() == 1) {
322: try {
323: addRol();
324: } catch (InfoException e) {
325: new ShowExceptionMessageDialog(LanguageTraslator
326: .traslate("434"), e);
327: }
328: }
329:
330: //MODIFICAR UN ROL
331: else if (evento.getSource() == modify
332: && jTabbedPane1.getSelectedIndex() == 1
333: && listRols.getSelectedValues().length == 1) {
334: try {
335: modifyRol();
336: } catch (Exception e) {
337: new ShowExceptionMessageDialog(LanguageTraslator
338: .traslate("445"), e);
339: }
340: }
341:
342: //ELIMINAR UN ROL
343: else if (evento.getSource() == del
344: && jTabbedPane1.getSelectedIndex() == 1
345: && listRols.getSelectedValues().length > 0) {
346: delRol();
347: }
348:
349: //SALIR
350: else if (evento.getSource() == exit) {
351: if (isEmbedded()) {
352: setVisible(false);
353: } else {
354: System.exit(0);
355: }
356:
357: }
358:
359: }
360: }
361:
362: public boolean isEmbedded() {
363: return isEmbedded;
364: }
365: }
|