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 ; 8 import java.awt.event.ActionEvent ; 9 import java.awt.*; 10 import java.util.*; 11 12 15 16 public class RolManagerModifyFrame extends JDialog { 17 18 private javax.swing.JButton accept; 19 private javax.swing.JButton cancel; 20 private javax.swing.JLabel JLabelRolId; 21 private javax.swing.JButton add; 22 private javax.swing.JButton del; 23 private javax.swing.JLabel jLabelUsers; 24 private javax.swing.JPanel jPanel1; 25 private javax.swing.JTextField jTextFieldRolId; 26 private JList listUsers; 27 private DefaultListModel listModel; 28 private UserManager userManager; 29 private Rol rol; 30 31 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 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 86 87 private void getUsers() { 88 listModel = new DefaultListModel(); 89 java.util.List list; 90 try { 91 list = (java.util.List ) 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 e) { 99 new ShowExceptionMessageDialog(LanguageTraslator.traslate("441"),e); 100 } 101 } 102 103 107 private ArrayList getUsersAvailable() { 108 ArrayList list = new ArrayList(); 109 Object [] 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 e) { 134 new ShowExceptionMessageDialog(LanguageTraslator.traslate("433"),e); 135 } 136 return list; 137 } 138 139 public void setTitulo(String 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 194 private void addUsersToRol() throws InfoException { 195 java.util.List list; 196 ArrayList repositoryUserAdd=new ArrayList(); 197 ArrayList repositoyUserRemove= new ArrayList(); 198 199 try { 200 201 list = userManager.getUsersByRol(rol); 202 } 203 catch(Exception 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 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 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 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 e) { 235 throw new InfoException(LanguageTraslator.traslate("436")+" "+(User)repositoyUserRemove.get(i),e); 236 } 237 } 238 } 239 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 { 254 255 public ButtonListener() { 256 } 257 258 public void actionPerformed(ActionEvent e) { 259 260 if( e.getSource() == cancel) { 261 setVisible(false); 262 263 } else if ( e.getSource() == accept && rol==null) { 265 266 acceptNewRol(); 267 268 } 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 } else if ( e.getSource() == add ) { 279 addUserToListRol(); 280 281 282 } else if ( e.getSource() == del && listUsers.getSelectedValues().length > 0 ) { 284 delUserToListRol(); 285 286 } 287 } 288 289 } 290 291 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 315 private void delUserToListRol() { 316 while(listUsers.getSelectedValues().length > 0 ) { 317 listModel.removeElement((User) listUsers.getSelectedValues()[0]); 318 } 319 } 320 321 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 e) { 335 new ShowExceptionMessageDialog(LanguageTraslator.traslate("444"),e); 336 rol=null; 337 return; 338 } 339 addUsersToNewRol(); 340 setVisible(false); 341 } 342 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 e) { 353 new ShowExceptionMessageDialog(LanguageTraslator.traslate("442")+" "+(User)listModel.getElementAt(i),e); 354 } 355 } 356 357 } 358 359 360 | Popular Tags |