1 package com.calipso.reportgenerator.usermanager; 2 3 import com.calipso.reportgenerator.common.ShowExceptionMessageDialog; 4 import com.calipso.reportgenerator.common.*; 5 import javax.swing.*; 6 import java.awt.event.*; 7 import java.awt.*; 8 import java.util.*; 9 import com.calipso.reportgenerator.common.InfoException; 10 11 14 public class UserManagerModifyFrame extends JDialog { 15 16 private javax.swing.JButton accept; 17 private javax.swing.JButton cancel; 18 private javax.swing.JLabel User; 19 private javax.swing.JLabel Name; 20 private javax.swing.JLabel Company; 21 private javax.swing.JButton add; 22 private javax.swing.JButton del; 23 private javax.swing.JLabel jLabel2; 24 private javax.swing.JPanel jPanel1; 25 private javax.swing.JTextField jTextFieldUser; 26 private javax.swing.JTextField jTextFieldName; 27 private javax.swing.JTextField jTextFieldCompany; 28 private JList listRols; 29 private DefaultListModel listModel; 30 private UserManager userManager; 31 private User user; 32 33 38 public UserManagerModifyFrame(JFrame parent, UserManager userManager) { 39 super(parent,true); 40 setResizable(false); 41 this.userManager=userManager; 42 initComponents(); 43 44 listModel = new DefaultListModel(); 45 46 listRols = new JList(listModel); 47 48 listRols.setVisible(true); 49 JScrollPane scrollpane = new JScrollPane(listRols); 50 51 jPanel1.add(scrollpane,new AbsoluteConstraints(80, 85, 210, 180)); 52 53 accept.addActionListener(new ButtonListener()); 54 add.addActionListener(new ButtonListener()); 55 del.addActionListener(new ButtonListener()); 56 } 57 58 64 public UserManagerModifyFrame(JFrame parent, UserManager userManager, User user) { 65 super(parent,true); 66 setResizable(false); 67 initComponents(); 68 this.user = user; 69 this.userManager = userManager; 70 jTextFieldName.setText(user.getUserName()); 71 jTextFieldUser.setText(user.getId()); 72 jTextFieldUser.setEditable(false); 73 if(user.getCompany().equals("null")) { 74 jTextFieldCompany.setText(""); 75 } else { 76 jTextFieldCompany.setText(user.getCompany()); 77 } 78 79 getRols(); 80 81 listRols.setVisible(true); 82 JScrollPane scrollpane = new JScrollPane(listRols); 83 84 jPanel1.add(scrollpane,new AbsoluteConstraints(80, 85, 210, 180)); 85 86 add.addActionListener(new ButtonListener()); 87 del.addActionListener(new ButtonListener()); 88 accept.addActionListener(new ButtonListener()); 89 } 90 91 94 private void getRols() { 95 java.util.List list; 96 listModel = new DefaultListModel(); 97 try { 98 list = (java.util.List ) userManager.getRolsByUser(user); 99 if( !list.isEmpty()) { 100 for (int i = 0; i < list.size(); i++) { 101 listModel.addElement(list.get(i)); 102 } 103 } 104 } 105 catch(Exception e) { 106 new ShowExceptionMessageDialog(LanguageTraslator.traslate("439"),e); 107 } 108 listRols = new JList(listModel); 109 listRols = new JList(listModel); 110 111 } 112 113 116 private ArrayList getRolesAvailable() { 117 ArrayList list = new ArrayList(); 118 boolean flag=false; 119 120 try { 121 122 if(listModel.size() ==0) { 123 list = (ArrayList) userManager.getRols(); 124 return list; 125 } 126 127 for (int i = 0; i < userManager.getRols().size() ; i++) { 128 flag=false; 129 130 for (int j = 0; j < listModel.size(); j++) { 131 if( ((Rol)userManager.getRols().get(i)).compareTo(listModel.get(j))==0 ) { 132 flag=true; 133 } 134 } 135 136 if( flag != true) { 137 list.add(userManager.getRols().get(i)); 138 } 139 } 140 } 141 catch(Exception e) { 142 new ShowExceptionMessageDialog("434",e); 143 } 144 return list; 145 } 146 147 public void setTitulo(String titulo) { 148 setTitle(titulo); 149 } 150 151 private void initComponents() { 152 jPanel1 = new JPanel(); 153 jTextFieldUser = new JTextField(); 154 User = new JLabel(); 155 jTextFieldName = new JTextField(); 156 Name = new JLabel(); 157 jTextFieldCompany = new JTextField(); 158 Company = new JLabel(); 159 jLabel2 = new JLabel(); 160 161 add = new JButton(); 162 del = new JButton(); 163 accept = new JButton(); 164 cancel = new JButton(); 165 166 getContentPane().setLayout(new AbsoluteLayout()); 167 168 setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE); 169 jPanel1.setLayout(new AbsoluteLayout()); 170 171 jPanel1.add(jTextFieldUser, new AbsoluteConstraints(80, 10, 210, -1)); 172 173 User.setText(LanguageTraslator.traslate("141")); 174 jPanel1.add(User, new AbsoluteConstraints(10, 10, 80, 20)); 175 176 jPanel1.add(jTextFieldName, new AbsoluteConstraints(80, 35, 210, -1)); 177 178 Name.setText(LanguageTraslator.traslate("232")); 179 jPanel1.add(Name, new AbsoluteConstraints(10, 35, 80, 20)); 180 181 jPanel1.add(jTextFieldCompany, new AbsoluteConstraints(80, 60, 210, -1)); 182 183 Company.setText(LanguageTraslator.traslate("420")); 184 jPanel1.add(Company, new AbsoluteConstraints(10, 60, 80, 20)); 185 186 jLabel2.setText(LanguageTraslator.traslate("426")); 187 jPanel1.add(jLabel2, new AbsoluteConstraints(10, 80, 80, 20)); 188 189 add.setText(LanguageTraslator.traslate("130")); 190 191 jPanel1.add(add, new AbsoluteConstraints(300, 85, 80, 25)); 192 193 del.setText(LanguageTraslator.traslate("421")); 194 jPanel1.add(del, new AbsoluteConstraints(300, 115, 80, 25)); 195 196 accept.setText(LanguageTraslator.traslate("112")); 197 jPanel1.add(accept, new AbsoluteConstraints(80, 275, 85, 25)); 198 199 cancel.setText(LanguageTraslator.traslate("113")); 200 jPanel1.add(cancel, new AbsoluteConstraints(205, 275, 85, 25)); 201 202 getContentPane().add(jPanel1, new AbsoluteConstraints(0,0, 390, 310)); 203 204 Dimension scrn = getToolkit().getScreenSize(); 205 206 this.setLocation((scrn.width-getWidth())/3+80,(scrn.height-getHeight())/3+80); 207 208 cancel.addActionListener(new ButtonListener()); 209 210 pack(); 211 } 212 213 216 private void addRolesToUser() throws InfoException { 217 java.util.List list; 218 ArrayList repositoryRolAdd=new ArrayList(); 219 ArrayList repositoyRolRemove= new ArrayList(); 220 221 try { 222 223 list = userManager.getRolsByUser(user); 224 } 225 catch(Exception e) { 226 throw new InfoException(LanguageTraslator.traslate("439"),e); 227 } 228 229 for (int i = 0; i < listModel.size(); i++) { 230 if(!list.contains(listModel.getElementAt(i)) ) { 231 repositoryRolAdd.add((Rol)listModel.getElementAt(i)); 232 } 233 } 234 235 for (int j = 0; j < list.size(); j++) { 236 if(!listModel.contains(list.get(j))) { 237 repositoyRolRemove.add((Rol)list.get(j)); 239 } 240 } 241 242 for (int i = 0; i < repositoryRolAdd.size(); i++) { 243 try { 244 userManager.addUsersToCollectionRol((Rol)repositoryRolAdd.get(i), user); 245 } 246 catch(Exception e) { 247 throw new InfoException(LanguageTraslator.traslate("444")+" "+(Rol)repositoryRolAdd.get(i),e); 248 } 249 } 250 251 for (int i = 0; i < repositoyRolRemove.size(); i++) { 253 try { 254 userManager.removeRolToUser((Rol)repositoyRolRemove.get(i),user); 255 } 256 catch(Exception e) { 257 throw new InfoException(LanguageTraslator.traslate("437")+" "+(Rol)repositoyRolRemove.get(i),e); 258 } 259 } 260 261 } 262 263 269 private boolean emptyTextField(JTextField jTextField, JLabel jLabel) { 270 if(jTextField.getText().length()==0 ) { 271 JOptionPane.showMessageDialog(null,LanguageTraslator.traslate("422")+" " + jLabel.getText()); 272 273 return true; 274 } 275 return false; 276 } 277 278 private void acceptNewUser() { 279 280 if(jTextFieldCompany.getText().length() == 0 ) { 281 user = new User(jTextFieldUser.getText(),jTextFieldName.getText()); 282 } 283 else { 284 user = new User(jTextFieldUser.getText(),jTextFieldName.getText(),jTextFieldCompany.getText()); 285 } 286 287 try { 288 userManager.addUser(user); 289 } catch (InfoException e) { 290 new ShowExceptionMessageDialog(LanguageTraslator.traslate("423"),e); 291 user=null; 292 return; 293 } 294 try { 295 addRolsToNewUser(); 296 setVisible(false); 297 } 298 catch(InfoException e) { 299 new ShowExceptionMessageDialog(LanguageTraslator.traslate("446"),e); 300 } 301 302 } 303 304 308 private void addRolsToNewUser() throws InfoException { 309 try { 310 for (int i = 0; i < listModel.size(); i++) { 311 userManager.addUsersToCollectionRol((Rol)listModel.getElementAt(i),user); 312 } 313 } 314 catch(Exception e) { 315 throw new InfoException(LanguageTraslator.traslate("444"),e); 316 } 317 } 318 319 class ButtonListener implements ActionListener { 320 321 public void actionPerformed(ActionEvent e) { 322 if( e.getSource() == cancel) { 323 setVisible(false); 324 325 } else if ( e.getSource() == accept && user==null) { 327 328 if(emptyTextField(jTextFieldUser,User) ) { 329 return; 330 } 331 332 if(emptyTextField(jTextFieldName,Name)) { 333 return; 334 } 335 acceptNewUser(); 336 337 } else if ( e.getSource() == accept && user!=null) { 339 try { 340 acceptModifyUser(); 341 } catch (InfoException el) { 342 new ShowExceptionMessageDialog(LanguageTraslator.traslate("446"),el); 343 } 344 345 } else if ( e.getSource() == add ) { 347 addRoltoUserList(); 348 } 349 350 else if ( e.getSource() == del && listRols.getSelectedValues().length > 0 ) { 352 delRolToUser(); 353 354 } 355 } 356 357 } 358 359 362 private void delRolToUser() { 363 while(listRols.getSelectedValues().length > 0 ) { 364 Rol rol = (Rol) listRols.getSelectedValues()[0]; 365 listModel.removeElement(rol); 366 } 367 } 368 369 372 private void addRoltoUserList() { 373 if(getRolesAvailable().size() > 0 ) { 374 375 AddRoltoUserFrame dialog = new AddRoltoUserFrame(this,getRolesAvailable()); 376 dialog.setTitle(LanguageTraslator.traslate("425")); 377 dialog.setVisible(true); 378 379 if (dialog.getRols() != null ) { 380 for( int i=0; i<dialog.getRols().length; i++ ) { 381 382 listModel.addElement((Rol)dialog.getRols()[i]); 383 } 384 } 385 } else { 386 JOptionPane.showMessageDialog(null,LanguageTraslator.traslate("449")); 387 } 388 389 } 390 394 private void acceptModifyUser() throws InfoException { 395 user.setName(jTextFieldName.getText()); 396 if(jTextFieldCompany.getText().length()!=0 ) { 397 user.setCompany(jTextFieldCompany.getText()); 398 } 399 400 if( emptyTextField(jTextFieldUser,User) ) { 401 return; 402 } 403 404 if( emptyTextField(jTextFieldName,Name) ) { 405 return; 406 } 407 408 try { 409 userManager.modifyUser(user); 410 } 411 catch(Exception e) { 412 new ShowExceptionMessageDialog(LanguageTraslator.traslate("440"),e); 413 } 414 addRolesToUser(); 415 setVisible(false); 416 } 417 418 } 419 | Popular Tags |