1 19 20 package org.netbeans.modules.web.struts.dialogs; 21 22 import java.awt.Color ; 23 import java.awt.event.ItemListener ; 24 import javax.swing.AbstractButton ; 25 import javax.swing.JButton ; 26 import javax.swing.JPanel ; 27 import javax.swing.text.JTextComponent ; 28 import org.openide.DialogDescriptor; 29 import org.openide.util.HelpCtx; 30 import org.openide.util.NbBundle; 31 32 37 public class AddDialog extends DialogDescriptor { 38 private static Color errorLabelColor = javax.swing.UIManager.getDefaults().getColor("ToolBar.dockingForeground"); public static final JButton ADD_OPTION = new JButton (NbBundle.getMessage(AddDialog.class,"LBL_Add")); 40 private JPanel panel; 42 43 44 public AddDialog(ValidatingPanel panel, String title, HelpCtx helpCtx) { 45 super (new InnerPanel((JPanel )panel),getTitle(title),true, 46 new Object []{ADD_OPTION, DialogDescriptor.CANCEL_OPTION}, 48 DialogDescriptor.OK_OPTION, 49 DialogDescriptor.BOTTOM_ALIGN, 50 helpCtx, 51 null); 52 this.panel=(JPanel )panel; 53 AbstractButton [] comp = panel.getStateChangeComponents(); 54 if (comp!=null && comp.length>0) { 55 StateChangeListener list = new StateChangeListener(this); 56 for (int i=0;i<comp.length;i++) { 57 comp[i].addItemListener(list); 58 } 59 } 60 JTextComponent [] textComp = panel.getDocumentChangeComponents(); 61 if (textComp!=null && textComp.length>0) { 62 DocListener list = new DocListener(this); 63 for (int i=0;i<textComp.length;i++) { 64 textComp[i].getDocument().addDocumentListener(list); 65 } 66 } 67 checkValues(); 68 } 69 70 private static String getTitle(String title) { 71 return NbBundle.getMessage(AddDialog.class,"TTL_ADD",title); 72 } 73 74 public void disableAdd() { 75 ((JButton )getOptions()[0]).setEnabled(false); 76 } 77 78 public void enableAdd() { 79 ((JButton )getOptions()[0]).setEnabled(true); 80 } 81 82 85 public final javax.swing.JPanel getDialogPanel() { 86 return panel; 87 } 88 89 92 protected final void checkValues() { 93 String errorMessage = validate(); 94 if (errorMessage==null) { 95 enableAdd(); 96 } else { 97 disableAdd(); 98 } 99 javax.swing.JLabel errorLabel = ((InnerPanel)getMessage()).getErrorLabel(); 100 errorLabel.setText(errorMessage==null?" ":errorMessage); 101 } 102 103 104 protected String validate() { 105 return ((ValidatingPanel)panel).validatePanel(); 106 } 107 108 private static class InnerPanel extends javax.swing.JPanel { 109 javax.swing.JLabel errorLabel; 110 InnerPanel(JPanel panel) { 111 super(new java.awt.BorderLayout ()); 112 errorLabel = new javax.swing.JLabel (" "); 113 errorLabel.setBorder(new javax.swing.border.EmptyBorder (12,12,0,0)); 114 errorLabel.setForeground(errorLabelColor); 115 add(panel, java.awt.BorderLayout.CENTER); 116 add(errorLabel, java.awt.BorderLayout.SOUTH); 117 this.getAccessibleContext().setAccessibleDescription(panel.getAccessibleContext().getAccessibleDescription()); 118 this.getAccessibleContext().setAccessibleName(panel.getAccessibleContext().getAccessibleName()); 119 } 120 121 void setErrorMessage(String message) { 122 errorLabel.setText(message); 123 } 124 125 javax.swing.JLabel getErrorLabel() { 126 return errorLabel; 127 } 128 } 129 130 131 private class DocListener implements javax.swing.event.DocumentListener { 132 AddDialog dialog; 133 134 DocListener(AddDialog dialog) { 135 this.dialog=dialog; 136 } 137 140 public void changedUpdate(javax.swing.event.DocumentEvent evt) { 141 dialog.checkValues(); 142 } 143 144 147 public void insertUpdate(javax.swing.event.DocumentEvent evt) { 148 dialog.checkValues(); 149 } 150 151 154 public void removeUpdate(javax.swing.event.DocumentEvent evt) { 155 dialog.checkValues(); 156 } 157 } 158 159 private class StateChangeListener implements java.awt.event.ItemListener { 160 AddDialog dialog; 161 StateChangeListener (AddDialog dialog) { 162 this.dialog=dialog; 163 } 164 public void itemStateChanged(java.awt.event.ItemEvent e) { 165 dialog.checkValues(); 166 } 167 } 168 } 169 | Popular Tags |