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