1 19 24 25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common; 26 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.ResourceBundle ; 33 34 import java.awt.BorderLayout ; 36 import java.awt.Component ; 37 import java.awt.Dimension ; 38 import java.awt.Frame ; 39 import java.awt.GridBagLayout ; 40 import java.awt.GridBagConstraints ; 41 import java.awt.Insets ; 42 import java.awt.event.ActionEvent ; 43 import java.awt.event.ActionListener ; 44 import java.awt.event.WindowAdapter ; 45 import java.awt.event.WindowEvent ; 46 47 import javax.swing.JButton ; 49 import javax.swing.JDialog ; 50 import javax.swing.JLabel ; 51 import javax.swing.JPanel ; 52 53 import org.openide.util.HelpCtx; 55 56 import org.netbeans.modules.j2ee.sun.share.configbean.Utils; 57 58 67 68 public abstract class InputDialog extends JDialog implements HelpCtx.Provider { 69 70 72 public static final int CANCEL_OPTION = 0; 73 74 76 public static final int OK_OPTION = 1; 77 78 80 public static final int HELP_OPTION = 2; 81 82 private static final ResourceBundle bundle = ResourceBundle.getBundle( 83 "org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.Bundle"); 85 86 private int chosenOption; 87 private JPanel buttonPanel; 88 private JButton okButton; 89 private JButton helpButton; 90 private JPanel messagePanel; 91 private List errorList; 92 private List warningList; 93 94 98 public InputDialog(JPanel panel, String title) { 99 this(panel, title, false, false); 100 } 101 102 108 public InputDialog(JPanel panel, String title, boolean showRequiredNote) { 109 this(panel, title, showRequiredNote, false); 110 } 111 112 121 public InputDialog(JPanel panel, String title, boolean showRequiredNote, boolean resizeMsg) { 122 super(getFrame(panel), title, true); 123 124 addWindowListener(new WindowAdapter (){ 125 public void windowClosing(WindowEvent e) { 126 chosenOption = CANCEL_OPTION; 127 } 128 }); 129 130 Object buttonPanelConstraints; 131 132 if(!resizeMsg) { 133 getContentPane().setLayout(new BorderLayout ()); 135 buttonPanelConstraints = BorderLayout.SOUTH; 136 } else { 137 getContentPane().setLayout(new GridBagLayout ()); 139 GridBagConstraints constraints = new GridBagConstraints (); 140 constraints.anchor = GridBagConstraints.SOUTH; 141 constraints.gridwidth = GridBagConstraints.REMAINDER; 142 constraints.fill = GridBagConstraints.HORIZONTAL; 143 constraints.weightx = 1.0; 144 buttonPanelConstraints = constraints; 145 } 146 147 buttonPanel = new JPanel (); 151 buttonPanel.setLayout(new GridBagLayout ()); 152 153 messagePanel = new JPanel (); 154 messagePanel.setLayout(new GridBagLayout ()); 155 messagePanel.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_ErrorTextArea")); messagePanel.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ErrorTextArea")); 158 GridBagConstraints gridBagConstraints = new GridBagConstraints (); 159 gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER; 160 gridBagConstraints.fill = GridBagConstraints.BOTH; 161 gridBagConstraints.insets = new Insets (6,12,5,11); 162 gridBagConstraints.weightx = 1.0; 163 buttonPanel.add(messagePanel, gridBagConstraints); 164 165 if(showRequiredNote) { 166 JLabel requiredNote = new JLabel (); 167 requiredNote.setText(bundle.getString("LBL_RequiredMessage")); gridBagConstraints = new GridBagConstraints (); 169 gridBagConstraints.insets = new Insets (6,12,11,5); 170 gridBagConstraints.anchor = GridBagConstraints.SOUTHWEST; 171 buttonPanel.add(requiredNote, gridBagConstraints); 172 } 173 174 okButton = new JButton (bundle.getString("LBL_OK")); gridBagConstraints = new GridBagConstraints (); 176 gridBagConstraints.insets = new Insets (6,6,11,5); 177 gridBagConstraints.anchor = GridBagConstraints.EAST; 178 gridBagConstraints.weightx = 1.0; 179 okButton.addActionListener(new ActionListener (){ 180 public void actionPerformed(ActionEvent e){ 181 chosenOption = OK_OPTION; 182 actionOk(); 183 } 184 }); 185 okButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_OK")); okButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_OK")); buttonPanel.add(okButton, gridBagConstraints); 188 189 JButton cancelButton = new JButton (bundle.getString("LBL_Cancel")); gridBagConstraints = new GridBagConstraints (); 191 gridBagConstraints.insets = new Insets (6,0,11,5); 192 gridBagConstraints.anchor = GridBagConstraints.EAST; 193 cancelButton.addActionListener(new ActionListener (){ 194 public void actionPerformed(ActionEvent e){ 195 chosenOption = CANCEL_OPTION; 196 actionCancel(); 197 } 198 }); 199 cancelButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_Cancel")); cancelButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Cancel")); buttonPanel.add(cancelButton, gridBagConstraints); 202 203 helpButton = new JButton (bundle.getString("LBL_Help")); helpButton.setMnemonic(bundle.getString("MNE_Help").charAt(0)); gridBagConstraints = new GridBagConstraints (); 206 gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER; 207 gridBagConstraints.insets = new Insets (6,0,11,11); 208 gridBagConstraints.anchor = GridBagConstraints.EAST; 209 helpButton.addActionListener(new ActionListener (){ 210 public void actionPerformed(ActionEvent e){ 211 chosenOption = HELP_OPTION; 212 actionHelp(); 213 } 214 }); 215 helpButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_Help")); helpButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Help")); buttonPanel.add(helpButton, gridBagConstraints); 218 219 getContentPane().add(buttonPanel, buttonPanelConstraints); 220 getRootPane().setDefaultButton(okButton); 221 } 222 223 224 227 public int display() { 228 this.setVisible(true); 229 return chosenOption; 230 } 231 232 protected void actionOk() { 233 super.dispose(); 234 } 235 236 protected void actionCancel() { 237 super.dispose(); 238 } 239 240 protected void actionHelp() { 241 Utils.invokeHelp(getHelpId()); 242 } 243 244 protected abstract String getHelpId(); 245 246 248 public static Frame getFrame(Component component) { 249 while (!(component instanceof Frame )){ 250 component = component.getParent(); 251 } 252 return ((Frame ) component); 253 } 254 255 256 259 protected void setLocationInside(JPanel panel) { 260 java.awt.Rectangle rect = this.getBounds(); 261 int width = rect.width; 262 int height = rect.height; 263 java.awt.Rectangle panelRect = panel.getBounds(); 264 if (width > panelRect.width || height > panelRect.height) { 265 setLocationRelativeTo(panel); 266 } else { 267 java.awt.Point location = panel.getLocationOnScreen(); 268 setLocation(location.x + (panelRect.width - width) / 2, 269 location.y + (panelRect.height - height) / 2); 270 } 271 } 272 273 277 protected void setOkEnabled(boolean flag) { 278 okButton.setEnabled(flag); 279 } 280 281 284 public void showErrors() { 285 boolean hasErrors = false; 286 boolean hasWarnings = false; 287 288 messagePanel.removeAll(); 290 291 if(warningList != null & warningList.size() > 0) { 292 hasWarnings = true; 293 294 for(Iterator iter = warningList.iterator(); iter.hasNext();) { 295 String message = iter.next().toString(); 296 297 JLabel label = new JLabel (); 299 label.setIcon(BaseCustomizer.warningMessageIcon); 300 label.setText("<html>" + message + "</html>"); label.getAccessibleContext().setAccessibleName(bundle.getString("ASCN_WarningMessage")); label.getAccessibleContext().setAccessibleDescription(message); 303 label.setForeground(BaseCustomizer.getWarningForegroundColor()); 304 305 GridBagConstraints constraints = new GridBagConstraints (); 306 constraints.gridwidth = GridBagConstraints.REMAINDER; 307 constraints.fill = GridBagConstraints.HORIZONTAL; 308 constraints.weightx = 1.0; 309 messagePanel.add(label, constraints); 310 } 311 } 312 313 if(errorList != null & errorList.size() > 0) { 314 hasErrors = true; 315 for(Iterator iter = errorList.iterator(); iter.hasNext();) { 316 String message = iter.next().toString(); 317 318 JLabel label = new JLabel (); 320 label.setIcon(BaseCustomizer.errorMessageIcon); 321 label.setText("<html>" + message + "</html>"); label.getAccessibleContext().setAccessibleName(bundle.getString("ASCN_ErrorMessage")); label.getAccessibleContext().setAccessibleDescription(message); 324 label.setForeground(BaseCustomizer.getErrorForegroundColor()); 325 326 GridBagConstraints constraints = new GridBagConstraints (); 327 constraints.gridwidth = GridBagConstraints.REMAINDER; 328 constraints.fill = GridBagConstraints.HORIZONTAL; 329 constraints.weightx = 1.0; 330 messagePanel.add(label, constraints); 331 } 332 } 333 334 pack(); 337 338 setOkEnabled(!hasErrors); 340 } 341 342 protected void setErrors(Collection errors, Collection warnings) { 343 warningList = new ArrayList (warnings); 344 errorList = new ArrayList (errors); 345 showErrors(); 346 } 347 348 351 protected void setErrors(Collection errors) { 352 setErrors(errors, Collections.EMPTY_LIST); 353 } 354 355 358 public void addWarning(String warning) { 359 if(warningList == null) { 360 warningList = new ArrayList (); 361 } 362 363 warningList.add(warning); 364 showErrors(); 365 } 366 367 370 protected void addWarnings(Collection warnings) { 371 if(warningList == null) { 372 warningList = new ArrayList (warnings); 373 } else { 374 warningList.addAll(warnings); 375 } 376 377 showErrors(); 378 } 379 380 383 public void addError(String error) { 384 if(errorList == null) { 385 errorList = new ArrayList (); 386 } 387 388 errorList.add(error); 389 showErrors(); 390 } 391 392 395 protected void addErrors(Collection errors) { 396 if(errorList == null) { 397 errorList = new ArrayList (errors); 398 } else { 399 errorList.addAll(errors); 400 } 401 402 showErrors(); 403 } 404 405 406 408 protected void clearErrors() { 409 warningList = null; 410 errorList = null; 411 showErrors(); 412 } 413 414 415 418 public boolean hasErrors() { 419 boolean result = false; 420 421 if(errorList != null && errorList.size() > 0) { 422 result = true; 423 } 424 425 return result; 426 } 427 428 431 public HelpCtx getHelpCtx() { 432 return new HelpCtx(getHelpId()); 433 } 434 435 436 protected void setButtonPanelPreferredSize(Dimension dimension){ 437 buttonPanel.setMinimumSize(dimension); 438 buttonPanel.setPreferredSize(dimension); 439 } 440 } 441 | Popular Tags |