1 28 29 package org.objectweb.util.explorer.swing.gui.lib; 30 31 32 import java.awt.Dimension ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Vector ; 36 37 import javax.swing.AbstractAction ; 38 import javax.swing.Action ; 39 import javax.swing.Box ; 40 import javax.swing.JButton ; 41 import javax.swing.JDialog ; 42 import javax.swing.JOptionPane ; 43 import javax.swing.WindowConstants ; 44 45 import org.objectweb.util.explorer.swing.gui.api.DialogAction; 46 import org.objectweb.util.explorer.swing.gui.api.DialogBox; 47 import org.objectweb.util.explorer.swing.gui.api.ElementBox; 48 import org.objectweb.util.explorer.swing.gui.api.ValidateReport; 49 50 51 58 public class DefaultDialogBox 59 extends JDialog 60 implements DialogBox 61 { 62 63 69 70 protected boolean firstShowInvocation_ = true; 71 72 73 protected Action validateAction_, cancelAction_; 74 75 76 protected DialogAction validateDialogAction_, cancelDialogAction_; 77 78 79 protected Box compositeBox_; 80 81 82 protected List elements_; 83 84 90 93 public DefaultDialogBox(){ 94 this(""); 95 } 96 97 101 public DefaultDialogBox(String title){ 102 super(); 103 104 elements_ = new Vector (); 105 106 setModal(true); 107 setTitle(title); 108 109 getContentPane().add(createMainBox()); 110 } 111 112 118 122 protected Box createCompositeBox(){ 123 Box b = Box.createHorizontalBox(); 124 b.add(Box.createHorizontalStrut(10)); 125 compositeBox_ = Box.createVerticalBox(); 126 compositeBox_.add(Box.createVerticalStrut(10)); 127 b.add(compositeBox_); 128 b.add(Box.createHorizontalStrut(10)); 129 return b; 130 } 131 132 136 protected Box createMainBox(){ 137 Box mainBox = Box.createVerticalBox(); 138 mainBox.add(createCompositeBox()); 139 140 Box buttonBox = Box.createHorizontalBox(); 141 buttonBox.add(Box.createHorizontalGlue()); 142 validateAction_ = new DefaultOKAction(); 143 buttonBox.add(new JButton (validateAction_)); 144 buttonBox.add(Box.createHorizontalStrut(10)); 145 cancelAction_ = new DefaultCancelAction(); 146 buttonBox.add(new JButton (cancelAction_)); 147 buttonBox.add(Box.createHorizontalGlue()); 148 149 mainBox.add(buttonBox); 150 mainBox.add(Box.createVerticalStrut(10)); 151 152 return mainBox; 153 } 154 155 161 165 public void addElementBox(ElementBox box){ 166 elements_.add(box); 167 compositeBox_.add(box.getBox()); 168 compositeBox_.add(Box.createVerticalStrut(10)); 169 } 170 171 175 public JDialog getJDialog(){ 176 return this; 177 } 178 179 182 public void show(){ 183 Iterator it = elements_.iterator(); 185 while (it.hasNext()) { 186 ElementBox element = (ElementBox) it.next(); 187 element.preInitialize(); 188 } 189 pack(); 191 if(firstShowInvocation_){ 193 firstShowInvocation_ = false; 194 setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); 195 setResizable(false); 196 pack(); 197 198 Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 199 setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2); 200 } 201 super.show(); 203 } 204 205 208 public void hide(){ 209 Iterator it = elements_.iterator(); 211 while (it.hasNext()) { 212 ElementBox element = (ElementBox) it.next(); 213 element.postInitialize(); 214 } 215 super.hide(); 217 } 218 219 223 public void setValidateAction(DialogAction action){ 224 validateDialogAction_ = action; 225 } 226 227 231 public void setValidateLabel(String label){ 232 validateAction_.putValue(Action.NAME, label); 233 } 234 235 239 public void setCancelAction(DialogAction action){ 240 cancelDialogAction_ = action; 241 } 242 243 247 public void setCancelLabel(String label){ 248 cancelAction_.putValue(Action.NAME, label); 249 } 250 251 255 public ValidateReport validateDialog(){ 256 Iterator it = elements_.iterator(); 257 while (it.hasNext()) { 258 ElementBox element = (ElementBox) it.next(); 259 ValidateReport report = element.validateBox(); 260 if(!report.getResult()){ 261 return report; 262 } 263 } 264 return new DefaultValidateReport(); 265 } 266 267 273 276 protected class DefaultOKAction extends AbstractAction { 277 278 281 protected DefaultOKAction() { 282 super("OK", null); 283 } 284 285 288 public void actionPerformed(java.awt.event.ActionEvent ae) { 289 ValidateReport report = validateDialog(); 290 if(report.getResult()){ 291 boolean exception = false; 292 if(DefaultDialogBox.this.validateDialogAction_!=null){ 293 try{ 294 DefaultDialogBox.this.validateDialogAction_.executeAction(); 295 }catch(Exception e){ 296 exception = true; 297 JOptionPane.showMessageDialog(null, e.getClass().getName() + ":\n" + e.getMessage(), "Exception (" + ae.getActionCommand() + ")", JOptionPane.ERROR_MESSAGE); 298 } 299 } 300 if(!exception) 301 hide(); 303 }else 304 JOptionPane.showMessageDialog(null, report.getMessage(), "Params error !", JOptionPane.ERROR_MESSAGE); 305 } 306 307 } 308 309 312 protected class DefaultCancelAction extends AbstractAction { 313 314 317 protected DefaultCancelAction() { 318 super("Cancel", null); 319 } 320 321 324 public void actionPerformed(java.awt.event.ActionEvent ae) { 325 boolean exception = false; 326 if(DefaultDialogBox.this.cancelDialogAction_!=null){ 327 try{ 328 DefaultDialogBox.this.cancelDialogAction_.executeAction(); 329 }catch(Exception e){ 330 exception = true; 331 JOptionPane.showMessageDialog(null, e.getMessage(), ae.getActionCommand(), JOptionPane.ERROR_MESSAGE); 332 } 333 } else 334 hide(); 336 } 337 338 } 339 340 } | Popular Tags |