1 28 package com.genimen.djeneric.tools.administrator.editors; 29 30 import java.awt.BorderLayout ; 31 import java.awt.Dimension ; 32 import java.awt.event.ActionEvent ; 33 import java.awt.event.MouseEvent ; 34 import java.util.ArrayList ; 35 36 import javax.swing.JButton ; 37 import javax.swing.JLabel ; 38 import javax.swing.JPanel ; 39 import javax.swing.JScrollPane ; 40 import javax.swing.JTable ; 41 import javax.swing.JTextField ; 42 43 import com.genimen.djeneric.language.Messages; 44 import com.genimen.djeneric.repository.DjContext; 45 import com.genimen.djeneric.repository.DjSession; 46 import com.genimen.djeneric.repository.DjUserContextAssociation; 47 import com.genimen.djeneric.tools.administrator.Administrator; 48 import com.genimen.djeneric.tools.administrator.helpers.AdminPanel; 49 import com.genimen.djeneric.tools.administrator.nodes.AdministratorTreeNode; 50 import com.genimen.djeneric.ui.DjTable; 51 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 52 import com.genimen.djeneric.ui.Util; 53 import com.genimen.djeneric.util.DjLogger; 54 55 public class ContextEditor extends AdminEditor 56 { 57 private static final long serialVersionUID = 1L; 58 59 BorderLayout borderLayout1 = new BorderLayout (); 60 JPanel jPanel3 = new JPanel (); 61 BorderLayout borderLayout2 = new BorderLayout (); 62 JPanel jPanel4 = new JPanel (); 63 JButton _butOk = new JButton (); 64 JButton _butCancel = new JButton (); 65 AdministratorTreeNode _theNode; 66 DjContext _ctxt; 67 JPanel jPanel5 = new JPanel (); 68 JTextField _edtName = new JTextField (); 69 JPanel jPanel2 = new JPanel (); 70 JPanel jPanel1 = new JPanel (); 71 JTextField _edtCode = new JTextField (); 72 DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout(); 73 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 74 JLabel jLabel2 = new JLabel (); 75 JLabel jLabel1 = new JLabel (); 76 BorderLayout borderLayout3 = new BorderLayout (); 77 JScrollPane jScrollPane1 = new JScrollPane (); 78 JLabel jLabel3 = new JLabel (); 79 DjTable _grdAccess = new DjTable(); 80 AccessTableModel _model; 81 ArrayList _allAssocs = new ArrayList (); 82 JButton _butDown = new JButton (); 83 JButton _butDelete = new JButton (); 84 JButton _butBottom = new JButton (); 85 JButton _butFirst = new JButton (); 86 JPanel jPanel6 = new JPanel (); 87 JButton _butCreate = new JButton (); 88 JButton _butUp = new JButton (); 89 90 public ContextEditor(AdminPanel adminPanel, AdministratorTreeNode theNode, DjContext ctxt) 91 { 92 super(adminPanel); 93 _ctxt = ctxt; 94 _theNode = theNode; 95 96 try 97 { 98 jbInit(); 99 _edtCode.setText(ctxt.getCode()); 100 _edtName.setText(ctxt.getName()); 101 102 DjSession session = adminPanel.getPersistenceManager().createSession(); 103 try 104 { 105 DjUserContextAssociation[] dca = ctxt.getUsers(session); 106 for (int i = 0; i < dca.length; i++) 107 { 108 _allAssocs.add(dca[i]); 109 } 110 _model = new AccessTableModel(adminPanel.getPersistenceManager().getContextManager(), _allAssocs, ctxt); 111 _grdAccess.setModel(_model); 112 tableFirst(_grdAccess); 113 } 114 finally 115 { 116 session.close(); 117 } 118 } 119 catch (Exception ex) 120 { 121 DjLogger.log(ex); 122 } 123 } 124 125 void jbInit() throws Exception 126 { 127 this.setLayout(borderLayout1); 128 jPanel3.setLayout(borderLayout2); 129 _butOk.setText(Messages.getString("global.Ok")); 130 _butOk.addActionListener(new java.awt.event.ActionListener () 131 { 132 public void actionPerformed(ActionEvent e) 133 { 134 _butOk_actionPerformed(e); 135 } 136 }); 137 _butCancel.setText(Messages.getString("global.Cancel")); 138 _butCancel.addActionListener(new java.awt.event.ActionListener () 139 { 140 public void actionPerformed(ActionEvent e) 141 { 142 _butCancel_actionPerformed(e); 143 } 144 }); 145 jPanel2.setLayout(verticalFlowLayout2); 146 jPanel1.setLayout(verticalFlowLayout1); 147 jLabel2.setText(Messages.getString("global.Name")); 148 jLabel1.setPreferredSize(new Dimension (41, 21)); 149 jLabel1.setText(Messages.getString("global.Code")); 150 jPanel5.setLayout(borderLayout3); 151 jLabel3.setText(Messages.getString("ContextEditor.Users")); 152 jScrollPane1.addMouseListener(new java.awt.event.MouseAdapter () 153 { 154 public void mouseClicked(MouseEvent e) 155 { 156 jScrollPane1_mouseClicked(e); 157 } 158 }); 159 _butDown.setPreferredSize(new Dimension (24, 24)); 160 _butDown.setIcon(Administrator.getImageIcon("down.gif")); 161 _butDown.setToolTipText(Messages.getString("global.NextInList")); 162 _butDown.setRequestFocusEnabled(true); 163 _butDown.addActionListener(new java.awt.event.ActionListener () 164 { 165 public void actionPerformed(ActionEvent e) 166 { 167 _butDown_actionPerformed(e); 168 } 169 }); 170 _butDelete.setPreferredSize(new Dimension (24, 24)); 171 _butDelete.setIcon(Administrator.getImageIcon("delete.gif")); 172 _butDelete.setToolTipText(Messages.getString("global.DeleteCtrl")); 173 _butDelete.setRequestFocusEnabled(true); 174 _butDelete.addActionListener(new java.awt.event.ActionListener () 175 { 176 public void actionPerformed(ActionEvent e) 177 { 178 _butDelete_actionPerformed(e); 179 } 180 }); 181 _butBottom.addActionListener(new java.awt.event.ActionListener () 182 { 183 public void actionPerformed(ActionEvent e) 184 { 185 _butBottom_actionPerformed(e); 186 } 187 }); 188 _butBottom.setRequestFocusEnabled(true); 189 _butBottom.setToolTipText(Messages.getString("global.LastInList")); 190 _butBottom.setIcon(Administrator.getImageIcon("bottom.gif")); 191 _butBottom.setPreferredSize(new Dimension (24, 24)); 192 _butFirst.addActionListener(new java.awt.event.ActionListener () 193 { 194 public void actionPerformed(ActionEvent e) 195 { 196 _butFirst_actionPerformed(e); 197 } 198 }); 199 _butFirst.setRequestFocusEnabled(true); 200 _butFirst.setToolTipText(Messages.getString("global.FirstInList")); 201 _butFirst.setIcon(Administrator.getImageIcon("top.gif")); 202 _butFirst.setPreferredSize(new Dimension (24, 24)); 203 _butCreate.setPreferredSize(new Dimension (24, 24)); 204 _butCreate.setIcon(Administrator.getImageIcon("new.gif")); 205 _butCreate.setToolTipText(Messages.getString("global.CreateCtrl")); 206 _butCreate.setRequestFocusEnabled(true); 207 _butCreate.addActionListener(new java.awt.event.ActionListener () 208 { 209 public void actionPerformed(ActionEvent e) 210 { 211 _butCreate_actionPerformed(e); 212 } 213 }); 214 _butUp.addActionListener(new java.awt.event.ActionListener () 215 { 216 public void actionPerformed(ActionEvent e) 217 { 218 _butUp_actionPerformed(e); 219 } 220 }); 221 _butUp.setRequestFocusEnabled(true); 222 _butUp.setToolTipText(Messages.getString("global.PreviousInList")); 223 _butUp.setIcon(Administrator.getImageIcon("up.gif")); 224 _butUp.setPreferredSize(new Dimension (24, 24)); 225 this.add(jPanel3, BorderLayout.SOUTH); 226 jPanel3.add(jPanel4, BorderLayout.EAST); 227 jPanel4.add(_butCancel, null); 228 jPanel4.add(_butOk, null); 229 jPanel3.add(jPanel6, BorderLayout.WEST); 230 jPanel6.add(_butUp, null); 231 jPanel6.add(_butDown, null); 232 jPanel6.add(_butFirst, null); 233 jPanel6.add(_butBottom, null); 234 jPanel6.add(_butCreate, null); 235 jPanel6.add(_butDelete, null); 236 this.add(jPanel5, BorderLayout.NORTH); 237 jPanel2.add(_edtCode, null); 238 jPanel2.add(_edtName, null); 239 this.add(jScrollPane1, BorderLayout.CENTER); 240 jScrollPane1.getViewport().add(_grdAccess, null); 241 jPanel5.add(jPanel1, BorderLayout.WEST); 242 jPanel1.add(jLabel1, null); 243 jPanel1.add(jLabel2, null); 244 jPanel1.add(jLabel3, null); 245 jPanel5.add(jPanel2, BorderLayout.CENTER); 246 247 Util.sizeButtons(new JButton []{_butOk, _butCancel}); 248 } 249 250 void _butCancel_actionPerformed(ActionEvent e) 251 { 252 _adminPanel.closeEditor(this); 253 setStatusMessage(Messages.getString("global.Ok")); 254 } 255 256 public void setStatusMessage(String msg, boolean isInformative) 257 { 258 _adminPanel.setStatusMessage(msg, isInformative); 259 } 260 261 public void requestFocus() 262 { 263 _edtCode.requestFocus(); 264 } 265 266 void _butOk_actionPerformed(ActionEvent e) 267 { 268 if (_edtCode.getText().trim().length() == 0) 269 { 270 setStatusMessage(Messages.getString("global.CodeRequired"), false); 271 return; 272 } 273 if (_edtName.getText().trim().length() == 0) 274 { 275 setStatusMessage(Messages.getString("global.NameRequired"), false); 276 return; 277 } 278 279 _ctxt.setCode(_edtCode.getText()); 280 _ctxt.setName(_edtName.getText()); 281 282 try 283 { 284 DjSession session = _adminPanel.getPersistenceManager().createSession(); 285 try 286 { 287 _ctxt.persist(session); 288 289 for (int i = 0; i < _allAssocs.size(); i++) 290 { 291 DjUserContextAssociation dca = (DjUserContextAssociation) _allAssocs.get(i); 292 if (dca.getContext() == null) 293 { 294 _grdAccess.setRowSelectionInterval(i, i); 295 throw new Exception (Messages.getString("global.ContextRequired")); 296 } 297 if (dca.getUser() == null) 298 { 299 _grdAccess.setRowSelectionInterval(i, i); 300 throw new Exception (Messages.getString("global.UserIsRequired")); 301 } 302 } 303 _ctxt.removeAllUserAssociations(session); 304 for (int i = 0; i < _allAssocs.size(); i++) 305 { 306 DjUserContextAssociation dca = (DjUserContextAssociation) _allAssocs.get(i); 307 dca.persist(session); 308 } 309 session.commit(); 310 setStatusMessage(Messages.getString("global.Ok")); 311 } 312 finally 313 { 314 session.close(); 315 } 316 _adminPanel.closeEditor(this); 317 if (_theNode != null) 318 { 319 _theNode.reload(); 320 } 321 } 322 catch (Exception x) 323 { 324 setStatusMessage(x); 325 } 326 } 327 328 public String getID() 329 { 330 if (_ctxt.getId() == -1) return null; 331 return "C" + _ctxt.getId(); 332 } 333 334 public void doubleClick(MouseEvent e, DjTable table) 335 { 336 table.requestFocus(); 337 if (e.getClickCount() < 2) return; 338 339 try 340 { 341 table.insertRow(); 342 } 343 catch (Exception x) 344 { 345 setStatusMessage(x); 346 } 347 } 348 349 void jScrollPane1_mouseClicked(MouseEvent e) 350 { 351 doubleClick(e, _grdAccess); 352 } 353 354 private void tableFirst(JTable table) 355 { 356 if (table.getRowCount() <= 0) return; 357 358 table.setRowSelectionInterval(0, 0); 359 } 360 361 private void tableBottom(JTable table) 362 { 363 int row = table.getRowCount() - 1; 364 if (row != -1) table.setRowSelectionInterval(row, row); 365 } 366 367 private void tableUp(JTable table) 368 { 369 int row = table.getSelectedRow(); 370 if (row > 0) table.setRowSelectionInterval(row - 1, row - 1); 371 } 372 373 private void tableDown(JTable table) 374 { 375 int row = table.getSelectedRow(); 376 if (row < table.getRowCount() - 1) table.setRowSelectionInterval(row + 1, row + 1); 377 } 378 379 void _butDown_actionPerformed(ActionEvent e) 380 { 381 tableDown(_grdAccess); 382 } 383 384 void _butDelete_actionPerformed(ActionEvent e) 385 { 386 try 387 { 388 _grdAccess.deleteRow(); 389 } 390 catch (Exception x) 391 { 392 setStatusMessage(x.getMessage(), false); 393 } 394 } 395 396 void _butBottom_actionPerformed(ActionEvent e) 397 { 398 tableBottom(_grdAccess); 399 } 400 401 void _butFirst_actionPerformed(ActionEvent e) 402 { 403 tableFirst(_grdAccess); 404 } 405 406 void _butCreate_actionPerformed(ActionEvent e) 407 { 408 try 409 { 410 _grdAccess.insertRow(); 411 } 412 catch (Exception x) 413 { 414 setStatusMessage(x.getMessage(), false); 415 } 416 } 417 418 void _butUp_actionPerformed(ActionEvent e) 419 { 420 tableUp(_grdAccess); 421 } 422 423 } | Popular Tags |