1 19 20 25 26 package org.netbeans.modules.xml.schema.ui.basic.editors; 27 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.util.HashSet ; 31 import java.util.Set ; 32 import org.netbeans.modules.xml.schema.model.Derivation; 33 34 38 public class DerivationTypeForm extends javax.swing.JPanel implements ActionListener { 39 40 static final long serialVersionUID = 1L; 41 private String property = "final"; 42 private Set <Derivation.Type> currentSelection; 43 private Set <Derivation.Type> supportedTypes; 44 private boolean isValid = true; 45 46 49 50 public DerivationTypeForm(String property, 51 Set <Derivation.Type> initialSelection, 52 Set <Derivation.Type> supportedTypes) { 53 this.currentSelection = initialSelection==null? 54 new HashSet <Derivation.Type>():initialSelection; 55 this.property = property; 56 this.supportedTypes = supportedTypes; 57 initComponents(); 58 initialize(); 59 } 60 61 66 private void initComponents() { 68 radioButtonGroup = new javax.swing.ButtonGroup (); 69 useDefaultRadioButton = new javax.swing.JRadioButton (); 70 useDefaultLabel = new javax.swing.JLabel (); 71 allowAllRadioButton = new javax.swing.JRadioButton (); 72 preventTypeRadioButton = new javax.swing.JRadioButton (); 73 extensionCheckBox = new javax.swing.JCheckBox (); 74 restrictionCheckBox = new javax.swing.JCheckBox (); 75 preventAllRadioButton = new javax.swing.JRadioButton (); 76 listCheckBox = new javax.swing.JCheckBox (); 77 unionCheckBox = new javax.swing.JCheckBox (); 78 substitutionCheckBox = new javax.swing.JCheckBox (); 79 dummyLabel1 = new javax.swing.JLabel (); 80 dummyLabel2 = new javax.swing.JLabel (); 81 82 org.openide.awt.Mnemonics.setLocalizedText(useDefaultRadioButton, org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("LBL_Use_Schema_Default_"+property)); 83 useDefaultRadioButton.setToolTipText(org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("HINT_Use_Schema_Default_"+property)); 84 useDefaultRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 85 useDefaultRadioButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 86 useDefaultRadioButton.addActionListener(this); 87 radioButtonGroup.add(useDefaultRadioButton); 88 89 org.openide.awt.Mnemonics.setLocalizedText(useDefaultLabel, org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("LBL_Default_Value_"+property)); 90 useDefaultLabel.setToolTipText(org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("HINT_Default_Value_"+property)); 91 92 org.openide.awt.Mnemonics.setLocalizedText(allowAllRadioButton, org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("LBL_Allow_All_"+property)); 93 allowAllRadioButton.setToolTipText(org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("HINT_Allow_All_"+property)); 94 allowAllRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 95 allowAllRadioButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 96 allowAllRadioButton.addActionListener(this); 97 radioButtonGroup.add(allowAllRadioButton); 98 99 org.openide.awt.Mnemonics.setLocalizedText(preventTypeRadioButton, org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("LBL_Prevent_Derivations_"+property)); 100 preventTypeRadioButton.setToolTipText(org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("HINT_Prevent_Derivations_"+property)); 101 preventTypeRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 102 preventTypeRadioButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 103 preventTypeRadioButton.addActionListener(this); 104 radioButtonGroup.add(preventTypeRadioButton); 105 106 org.openide.awt.Mnemonics.setLocalizedText(extensionCheckBox, org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("LBL_Extension")); 107 extensionCheckBox.setToolTipText(org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("HINT_Extension")); 108 extensionCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 109 extensionCheckBox.setMargin(new java.awt.Insets (0, 0, 0, 0)); 110 extensionCheckBox.addActionListener(this); 111 112 org.openide.awt.Mnemonics.setLocalizedText(restrictionCheckBox, org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("LBL_Restriction")); 113 restrictionCheckBox.setToolTipText(org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("HINT_Restriction")); 114 restrictionCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 115 restrictionCheckBox.setMargin(new java.awt.Insets (0, 0, 0, 0)); 116 restrictionCheckBox.addActionListener(this); 117 118 org.openide.awt.Mnemonics.setLocalizedText(preventAllRadioButton, org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("LBL_Prevent_All_"+property)); 119 preventAllRadioButton.setToolTipText(org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("HINT_Prevent_All_"+property)); 120 preventAllRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 121 preventAllRadioButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 122 preventAllRadioButton.addActionListener(this); 123 radioButtonGroup.add(preventAllRadioButton); 124 125 org.openide.awt.Mnemonics.setLocalizedText(listCheckBox, org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("LBL_List")); 126 listCheckBox.setToolTipText(org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("HINT_List")); 127 listCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 128 listCheckBox.setMargin(new java.awt.Insets (0, 0, 0, 0)); 129 listCheckBox.addActionListener(this); 130 131 org.openide.awt.Mnemonics.setLocalizedText(unionCheckBox, org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("LBL_Union")); 132 unionCheckBox.setToolTipText(org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("HINT_Union")); 133 unionCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 134 unionCheckBox.setMargin(new java.awt.Insets (0, 0, 0, 0)); 135 unionCheckBox.addActionListener(this); 136 137 org.openide.awt.Mnemonics.setLocalizedText(substitutionCheckBox, org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("LBL_Substitution")); 138 substitutionCheckBox.setToolTipText(org.openide.util.NbBundle.getBundle(DerivationTypeForm.class).getString("HINT_Substitution")); 139 substitutionCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 140 substitutionCheckBox.setMargin(new java.awt.Insets (0, 0, 0, 0)); 141 substitutionCheckBox.addActionListener(this); 142 143 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 144 this.setLayout(layout); 145 layout.setHorizontalGroup( 146 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 147 .add(layout.createSequentialGroup() 148 .addContainerGap() 149 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 150 .add(layout.createSequentialGroup() 151 .add(17, 17, 17) 152 .add(useDefaultLabel)) 153 .add(useDefaultRadioButton) 154 .add(allowAllRadioButton) 155 .add(preventAllRadioButton) 156 .add(preventTypeRadioButton) 157 .add(layout.createSequentialGroup() 158 .add(17, 17, 17) 159 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 160 .add(extensionCheckBox) 161 .add(restrictionCheckBox) 162 .add(substitutionCheckBox) 163 .add(listCheckBox) 164 .add(unionCheckBox) 165 .add(dummyLabel2) 166 .add(dummyLabel1)))) 167 .addContainerGap(227, Short.MAX_VALUE)) 168 ); 169 layout.setVerticalGroup( 170 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 171 .add(layout.createSequentialGroup() 172 .addContainerGap() 173 .add(useDefaultRadioButton) 174 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 175 .add(useDefaultLabel) 176 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 177 .add(allowAllRadioButton) 178 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 179 .add(preventAllRadioButton) 180 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 181 .add(preventTypeRadioButton) 182 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 183 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 184 .add(dummyLabel1) 185 .add(layout.createSequentialGroup() 186 .add(extensionCheckBox) 187 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 188 .add(restrictionCheckBox) 189 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 190 .add(substitutionCheckBox) 191 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 192 .add(dummyLabel2) 193 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 194 .add(listCheckBox) 195 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 196 .add(unionCheckBox))) 197 .addContainerGap(102, Short.MAX_VALUE)) 198 ); 199 } 201 private void initialize() { 202 if(!supportedTypes.contains(Derivation.Type.EXTENSION)) { 203 extensionCheckBox.setVisible(false); 204 } 205 if(!supportedTypes.contains(Derivation.Type.RESTRICTION)) { 206 restrictionCheckBox.setVisible(false); 207 } 208 if(!supportedTypes.contains(Derivation.Type.LIST)) { 209 listCheckBox.setVisible(false); 210 } 211 if(!supportedTypes.contains(Derivation.Type.UNION)) { 212 unionCheckBox.setVisible(false); 213 } 214 if (!supportedTypes.contains(Derivation.Type.SUBSTITUTION)) { 215 substitutionCheckBox.setVisible(false); 216 } 217 218 if (currentSelection.isEmpty()) { 219 useDefaultRadioButton.setSelected(true); 220 } else if (currentSelection.contains(Derivation.Type.EMPTY)){ 221 allowAllRadioButton.setSelected(true); 222 } else if (currentSelection.contains(Derivation.Type.ALL)){ 223 preventAllRadioButton.setSelected(true); 224 } else { 225 for (Derivation.Type type:currentSelection) { 226 if(type == Derivation.Type.EXTENSION) { 227 extensionCheckBox.setSelected(true); 228 } else if(type == Derivation.Type.RESTRICTION) { 229 restrictionCheckBox.setSelected(true); 230 } else if(type == Derivation.Type.LIST) { 231 listCheckBox.setSelected(true); 232 } else if(type == Derivation.Type.UNION) { 233 unionCheckBox.setSelected(true); 234 } else if(type == Derivation.Type.SUBSTITUTION) { 235 substitutionCheckBox.setSelected(true); 236 } 237 } 238 if(checkBoxSelected()) preventTypeRadioButton.setSelected(true); 239 } 240 } 241 242 public void actionPerformed(ActionEvent ae) { 243 Object source = ae.getSource(); 244 if(source instanceof javax.swing.JRadioButton ) { 245 if (source.equals(useDefaultRadioButton)) { 246 currentSelection.clear(); 247 setCheckBoxesSelected(false); 248 } else if (source.equals(allowAllRadioButton)) { 249 currentSelection.clear(); 250 currentSelection.add(Derivation.Type.EMPTY); 251 setCheckBoxesSelected(false); 252 } else if (source.equals(preventAllRadioButton)) { 253 currentSelection.clear(); 254 currentSelection.add(Derivation.Type.ALL); 255 setCheckBoxesSelected(false); 256 } else if (source.equals(preventTypeRadioButton)) { 257 if (!checkBoxSelected()) currentSelection.clear(); 258 } 259 } else if(source instanceof javax.swing.JCheckBox ) { 260 if(((javax.swing.JCheckBox )source).isSelected() && 261 !preventTypeRadioButton.isSelected()) { 262 preventTypeRadioButton.setSelected(true); 263 currentSelection.clear(); 264 } 265 if (source.equals(extensionCheckBox)) { 266 if(extensionCheckBox.isSelected()) { 267 currentSelection.add(Derivation.Type.EXTENSION); 268 } else { 269 currentSelection.remove(Derivation.Type.EXTENSION); 270 } 271 } else if (source.equals(restrictionCheckBox)) { 272 if(restrictionCheckBox.isSelected()) { 273 currentSelection.add(Derivation.Type.RESTRICTION); 274 } else { 275 currentSelection.remove(Derivation.Type.RESTRICTION); 276 } 277 } else if (source.equals(listCheckBox)) { 278 if(listCheckBox.isSelected()) { 279 currentSelection.add(Derivation.Type.LIST); 280 } else { 281 currentSelection.remove(Derivation.Type.LIST); 282 } 283 } else if (source.equals(unionCheckBox)) { 284 if(unionCheckBox.isSelected()) { 285 currentSelection.add(Derivation.Type.UNION); 286 } else { 287 currentSelection.remove(Derivation.Type.UNION); 288 } 289 } else if (source.equals(substitutionCheckBox)) { 290 if(substitutionCheckBox.isSelected()) { 291 currentSelection.add(Derivation.Type.SUBSTITUTION); 292 } else { 293 currentSelection.remove(Derivation.Type.SUBSTITUTION); 294 } 295 } 296 } 297 firePropertyChange("valid", isValid, !currentSelection.isEmpty() || 299 !preventTypeRadioButton.isSelected()); 300 isValid = !currentSelection.isEmpty() || 301 !preventTypeRadioButton.isSelected(); 302 } 303 304 public Set <Derivation.Type> getCurrentSelection() { 305 return currentSelection; 306 } 307 308 private boolean checkBoxSelected() { 309 return (extensionCheckBox.isSelected() || 310 restrictionCheckBox.isSelected() || 311 listCheckBox.isSelected() || 312 unionCheckBox.isSelected() || 313 substitutionCheckBox.isSelected()); 314 } 315 316 private void setCheckBoxesSelected(boolean flag) { 317 extensionCheckBox.setSelected(flag); 318 restrictionCheckBox.setSelected(flag); 319 listCheckBox.setSelected(flag); 320 unionCheckBox.setSelected(flag); 321 substitutionCheckBox.setSelected(flag); 322 } 323 324 private javax.swing.JRadioButton allowAllRadioButton; 326 private javax.swing.JLabel dummyLabel1; 327 private javax.swing.JLabel dummyLabel2; 328 private javax.swing.JCheckBox extensionCheckBox; 329 private javax.swing.JCheckBox listCheckBox; 330 private javax.swing.JRadioButton preventAllRadioButton; 331 private javax.swing.JRadioButton preventTypeRadioButton; 332 private javax.swing.ButtonGroup radioButtonGroup; 333 private javax.swing.JCheckBox restrictionCheckBox; 334 private javax.swing.JCheckBox substitutionCheckBox; 335 private javax.swing.JCheckBox unionCheckBox; 336 private javax.swing.JLabel useDefaultLabel; 337 private javax.swing.JRadioButton useDefaultRadioButton; 338 340 } 341 | Popular Tags |