1 26 27 package org.objectweb.util.browser.gui.lib; 28 29 30 import java.awt.Dimension ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Vector ; 34 35 import javax.swing.AbstractAction ; 36 import javax.swing.Action ; 37 import javax.swing.Box ; 38 import javax.swing.JButton ; 39 import javax.swing.JDialog ; 40 import javax.swing.JOptionPane ; 41 import javax.swing.WindowConstants ; 42 43 import org.objectweb.util.browser.gui.api.DialogAction; 44 import org.objectweb.util.browser.gui.api.DialogBox; 45 import org.objectweb.util.browser.gui.api.ElementBox; 46 import org.objectweb.util.browser.gui.api.ValidateReport; 47 48 55 public class DefaultDialogBox 56 extends JDialog 57 implements DialogBox{ 58 59 65 66 protected boolean firstShowInvocation_ = true; 67 68 69 protected Action validateAction_, cancelAction_; 70 71 72 protected DialogAction validateDialogAction_, cancelDialogAction_; 73 74 75 protected Box compositeBox_; 76 77 78 protected List elements_; 79 80 86 89 public DefaultDialogBox(){ 90 this(""); 91 } 92 93 97 public DefaultDialogBox(String title){ 98 super(); 99 100 elements_ = new Vector (); 101 102 setModal(true); 103 setTitle(title); 104 105 getContentPane().add(createMainBox()); 106 } 107 108 114 117 protected Box createCompositeBox(){ 118 Box b = Box.createHorizontalBox(); 119 b.add(Box.createHorizontalStrut(10)); 120 compositeBox_ = Box.createVerticalBox(); 121 compositeBox_.add(Box.createVerticalStrut(10)); 122 b.add(compositeBox_); 123 b.add(Box.createHorizontalStrut(10)); 124 return b; 125 } 126 127 130 protected Box createMainBox(){ 131 Box mainBox = Box.createVerticalBox(); 132 mainBox.add(createCompositeBox()); 133 134 Box buttonBox = Box.createHorizontalBox(); 135 buttonBox.add(Box.createHorizontalGlue()); 136 validateAction_ = new DefaultOKAction(); 137 buttonBox.add(new JButton (validateAction_)); 138 buttonBox.add(Box.createHorizontalStrut(10)); 139 cancelAction_ = new DefaultCancelAction(); 140 buttonBox.add(new JButton (cancelAction_)); 141 buttonBox.add(Box.createHorizontalGlue()); 142 143 mainBox.add(buttonBox); 144 mainBox.add(Box.createVerticalStrut(10)); 145 146 return mainBox; 147 } 148 149 155 159 public void addElementBox(ElementBox box){ 160 elements_.add(box); 161 compositeBox_.add(box.getBox()); 162 compositeBox_.add(Box.createVerticalStrut(10)); 163 } 164 165 169 public JDialog getJDialog(){ 170 return this; 171 } 172 173 176 public void show(){ 177 Iterator it = elements_.iterator(); 179 while (it.hasNext()) { 180 ElementBox element = (ElementBox) it.next(); 181 element.preInitialize(); 182 } 183 pack(); 185 if(firstShowInvocation_){ 187 firstShowInvocation_ = false; 188 setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); 189 setResizable(false); 190 pack(); 191 192 Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 193 setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2); 194 } 195 super.show(); 197 } 198 199 202 public void hide(){ 203 Iterator it = elements_.iterator(); 205 while (it.hasNext()) { 206 ElementBox element = (ElementBox) it.next(); 207 element.postInitialize(); 208 } 209 super.hide(); 211 } 212 213 217 public void setValidateAction(DialogAction action){ 218 validateDialogAction_ = action; 219 } 220 221 225 public void setValidateLabel(String label){ 226 validateAction_.putValue(Action.NAME, label); 227 } 228 229 233 public void setCancelAction(DialogAction action){ 234 cancelDialogAction_ = action; 235 } 236 237 241 public void setCancelLabel(String label){ 242 cancelAction_.putValue(Action.NAME, label); 243 } 244 245 249 public ValidateReport validateDialog(){ 250 Iterator it = elements_.iterator(); 251 while (it.hasNext()) { 252 ElementBox element = (ElementBox) it.next(); 253 ValidateReport report = element.validateBox(); 254 if(!report.getResult()){ 255 return report; 256 } 257 } 258 return new DefaultValidateReport(); 259 } 260 261 267 270 protected class DefaultOKAction extends AbstractAction { 271 272 275 protected DefaultOKAction() { 276 super("OK", null); 277 } 278 279 282 public void actionPerformed(java.awt.event.ActionEvent ae) { 283 ValidateReport report = validateDialog(); 284 if(report.getResult()){ 285 boolean exception = false; 286 if(DefaultDialogBox.this.validateDialogAction_!=null){ 287 try{ 288 DefaultDialogBox.this.validateDialogAction_.executeAction(); 289 }catch(Exception e){ 290 exception = true; 291 JOptionPane.showMessageDialog(null, e.getClass().getName() + ":\n" + e.getMessage(), "Exception (" + ae.getActionCommand() + ")", JOptionPane.ERROR_MESSAGE); 292 } 293 } 294 if(!exception) 295 hide(); 297 }else 298 JOptionPane.showMessageDialog(null, report.getMessage(), "Params error !", JOptionPane.ERROR_MESSAGE); 299 } 300 301 } 302 303 306 protected class DefaultCancelAction extends AbstractAction { 307 308 311 protected DefaultCancelAction() { 312 super("Cancel", null); 313 } 314 315 318 public void actionPerformed(java.awt.event.ActionEvent ae) { 319 boolean exception = false; 320 if(DefaultDialogBox.this.cancelDialogAction_!=null){ 321 try{ 322 DefaultDialogBox.this.cancelDialogAction_.executeAction(); 323 }catch(Exception e){ 324 exception = true; 325 JOptionPane.showMessageDialog(null, e.getMessage(), ae.getActionCommand(), JOptionPane.ERROR_MESSAGE); 326 } 327 } else 328 hide(); 330 } 331 332 } 333 334 } | Popular Tags |