1 19 20 25 26 package org.netbeans.modules.xml.schema.ui.basic.editors; 27 28 import java.awt.Component ; 29 import java.awt.Dialog ; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.ActionListener ; 32 import java.beans.FeatureDescriptor ; 33 import java.beans.PropertyChangeEvent ; 34 import java.beans.PropertyChangeListener ; 35 import java.beans.PropertyEditorSupport ; 36 import java.util.Collections ; 37 import java.util.HashMap ; 38 import java.util.HashSet ; 39 import java.util.Set ; 40 import org.netbeans.modules.xml.schema.model.Element; 41 import org.netbeans.modules.xml.schema.model.Derivation; 42 import org.netbeans.modules.xml.schema.model.GlobalComplexType; 43 import org.netbeans.modules.xml.schema.model.GlobalElement; 44 import org.netbeans.modules.xml.schema.model.GlobalSimpleType; 45 import org.netbeans.modules.xml.schema.model.Schema; 46 import org.netbeans.modules.xml.schema.model.SchemaComponent; 47 import org.openide.DialogDescriptor; 48 import org.openide.DialogDisplayer; 49 import org.openide.ErrorManager; 50 import org.openide.explorer.propertysheet.ExPropertyEditor; 51 import org.openide.explorer.propertysheet.PropertyEnv; 52 import org.openide.util.NbBundle; 53 54 58 public class DerivationTypeEditor extends PropertyEditorSupport 59 implements ExPropertyEditor { 60 61 private String property; 62 private String typeDisplayName; 63 private HashMap <Derivation.Type,? extends Derivation> typeToDerivationMap = null; 64 65 68 public DerivationTypeEditor(SchemaComponent component, String property, String typeDisplayName) { 69 this.property = property; 70 this.typeDisplayName = typeDisplayName; 71 initialize(component); 72 } 73 74 private void initialize(SchemaComponent component) { 75 if(component instanceof Element) { 76 if(component instanceof GlobalElement && GlobalElement.FINAL_PROPERTY.equals(property)) { 77 HashMap <Derivation.Type,GlobalElement.Final> tmpMap = new HashMap <Derivation.Type,GlobalElement.Final>(); 78 tmpMap.put(Derivation.Type.EMPTY,GlobalElement.Final.EMPTY); 79 tmpMap.put(Derivation.Type.ALL,GlobalElement.Final.ALL); 80 tmpMap.put(Derivation.Type.EXTENSION,GlobalElement.Final.EXTENSION); 81 tmpMap.put(Derivation.Type.RESTRICTION,GlobalElement.Final.RESTRICTION); 82 typeToDerivationMap = tmpMap; 83 } else if(Element.BLOCK_PROPERTY.equals(property)) { 84 HashMap <Derivation.Type,Element.Block> tmpMap = new HashMap <Derivation.Type,Element.Block>(); 85 tmpMap.put(Derivation.Type.EMPTY,Element.Block.EMPTY); 86 tmpMap.put(Derivation.Type.ALL,Element.Block.ALL); 87 tmpMap.put(Derivation.Type.EXTENSION,Element.Block.EXTENSION); 88 tmpMap.put(Derivation.Type.RESTRICTION,Element.Block.RESTRICTION); 89 tmpMap.put(Derivation.Type.SUBSTITUTION,Element.Block.SUBSTITUTION); 90 typeToDerivationMap = tmpMap; 91 } 92 } else if(component instanceof GlobalComplexType) { 93 if(GlobalComplexType.FINAL_PROPERTY.equals(property)) { 94 HashMap <Derivation.Type,GlobalComplexType.Final> tmpMap = new HashMap <Derivation.Type,GlobalComplexType.Final>(); 95 tmpMap.put(Derivation.Type.EMPTY,GlobalComplexType.Final.EMPTY); 96 tmpMap.put(Derivation.Type.ALL,GlobalComplexType.Final.ALL); 97 tmpMap.put(Derivation.Type.EXTENSION,GlobalComplexType.Final.EXTENSION); 98 tmpMap.put(Derivation.Type.RESTRICTION,GlobalComplexType.Final.RESTRICTION); 99 typeToDerivationMap = tmpMap; 100 } else if(GlobalComplexType.BLOCK_PROPERTY.equals(property)) { 101 HashMap <Derivation.Type,GlobalComplexType.Block> tmpMap = new HashMap <Derivation.Type,GlobalComplexType.Block>(); 102 tmpMap.put(Derivation.Type.EMPTY,GlobalComplexType.Block.EMPTY); 103 tmpMap.put(Derivation.Type.ALL,GlobalComplexType.Block.ALL); 104 tmpMap.put(Derivation.Type.EXTENSION,GlobalComplexType.Block.EXTENSION); 105 tmpMap.put(Derivation.Type.RESTRICTION,GlobalComplexType.Block.RESTRICTION); 106 typeToDerivationMap = tmpMap; 107 } 108 } else if(component instanceof GlobalSimpleType) { 109 if(GlobalSimpleType.FINAL_PROPERTY.equals(property)) { 110 HashMap <Derivation.Type,GlobalSimpleType.Final> tmpMap = new HashMap <Derivation.Type,GlobalSimpleType.Final>(); 111 tmpMap.put(Derivation.Type.EMPTY,GlobalSimpleType.Final.EMPTY); 112 tmpMap.put(Derivation.Type.ALL,GlobalSimpleType.Final.ALL); 113 tmpMap.put(Derivation.Type.RESTRICTION,GlobalSimpleType.Final.RESTRICTION); 114 tmpMap.put(Derivation.Type.LIST,GlobalSimpleType.Final.LIST); 115 tmpMap.put(Derivation.Type.UNION,GlobalSimpleType.Final.UNION); 116 typeToDerivationMap = tmpMap; 117 } 118 } else if(component instanceof Schema) { 119 if(Schema.FINAL_DEFAULT_PROPERTY.equals(property)) { 120 HashMap <Derivation.Type,Schema.Final> tmpMap = new HashMap <Derivation.Type,Schema.Final>(); 121 tmpMap.put(Derivation.Type.EMPTY,Schema.Final.EMPTY); 122 tmpMap.put(Derivation.Type.ALL,Schema.Final.ALL); 123 tmpMap.put(Derivation.Type.EXTENSION,Schema.Final.EXTENSION); 124 tmpMap.put(Derivation.Type.RESTRICTION,Schema.Final.RESTRICTION); 125 tmpMap.put(Derivation.Type.LIST,Schema.Final.LIST); 126 tmpMap.put(Derivation.Type.UNION,Schema.Final.UNION); 127 typeToDerivationMap = tmpMap; 128 } else if(Schema.BLOCK_DEFAULT_PROPERTY.equals(property)) { 129 HashMap <Derivation.Type,Schema.Block> tmpMap = new HashMap <Derivation.Type,Schema.Block>(); 130 tmpMap.put(Derivation.Type.EMPTY,Schema.Block.EMPTY); 131 tmpMap.put(Derivation.Type.ALL,Schema.Block.ALL); 132 tmpMap.put(Derivation.Type.EXTENSION,Schema.Block.EXTENSION); 133 tmpMap.put(Derivation.Type.RESTRICTION,Schema.Block.RESTRICTION); 134 typeToDerivationMap = tmpMap; 135 } 136 } 137 } 138 139 public String getAsText() { 140 Object val = getValue(); 141 if (val == null){ 142 return null; 143 } 144 if (val instanceof Set ){ 145 return ((Set )val).toString(); 146 } 147 return val.toString(); 149 } 150 151 private Set <Derivation.Type> convertToDerivationType( 152 Set <Derivation> derivationSet) { 153 Set <Derivation.Type> derivationTypeSet = new HashSet <Derivation.Type>(); 154 if (derivationSet != null && !derivationSet.isEmpty()) { 155 Set <Derivation.Type> keys = typeToDerivationMap.keySet(); 156 int i = 0; 157 for(Derivation.Type key:keys) { 158 if (derivationSet.contains(typeToDerivationMap.get(key))) { 159 derivationTypeSet.add(key); 160 i++; 161 if (i==derivationSet.size()) break; 162 } 163 } 164 } 165 return derivationTypeSet; 166 } 167 168 private Set <? extends Derivation> convertToDerivation( 169 Set <Derivation.Type> derivationTypeSet) { 170 Set <Derivation> derivationSet = new HashSet <Derivation>(); 171 for(Derivation.Type type: derivationTypeSet) { 172 derivationSet.add(typeToDerivationMap.get(type)); 173 } 174 return derivationSet; 175 } 176 177 @SuppressWarnings ("unchecked") 178 public Component getCustomEditor() { 179 Object obj = getValue(); 180 Set <Derivation> initialValue = Collections.emptySet(); 181 if(obj instanceof Set ) initialValue = (Set <Derivation>)obj; 182 final DerivationTypeForm panel = new DerivationTypeForm(property, 183 convertToDerivationType(initialValue),typeToDerivationMap.keySet()); 184 final DialogDescriptor descriptor = new DialogDescriptor(panel, 185 NbBundle.getMessage(DerivationTypeEditor.class, "LBL_Derivation_Type_Editor_Title", 186 typeDisplayName, NbBundle.getMessage(DerivationTypeEditor.class, "LBL_"+property)), 187 true, 188 new ActionListener () { 189 public void actionPerformed(ActionEvent evt) { 190 if (evt.getSource().equals(DialogDescriptor.OK_OPTION)) { 191 try { 192 Set <Derivation.Type> currentSelection = 193 panel.getCurrentSelection(); 194 setValue(currentSelection.isEmpty()?null: 195 convertToDerivation(currentSelection)); 196 } catch (IllegalArgumentException iae) { 197 ErrorManager.getDefault().annotate(iae, ErrorManager.USER, 198 iae.getMessage(), iae.getLocalizedMessage(), 199 null, new java.util.Date ()); 200 throw iae; 201 } 202 } 203 } 204 }); 205 206 panel.addPropertyChangeListener( new PropertyChangeListener () { 208 public void propertyChange(PropertyChangeEvent evt) { 209 if (evt.getPropertyName().equals("valid")) { 210 descriptor.setValid(((Boolean )evt.getNewValue()).booleanValue()); 211 } 212 } 213 }); 214 Dialog dlg = DialogDisplayer.getDefault().createDialog(descriptor); 215 dlg.setPreferredSize(new java.awt.Dimension (400,300)); 218 return dlg; 219 } 220 221 222 public boolean supportsCustomEditor() { 223 return true; 224 } 225 226 public void attachEnv(PropertyEnv env ) { 227 FeatureDescriptor desc = env.getFeatureDescriptor(); 228 desc.setValue("canEditAsText", Boolean.FALSE); } 231 232 } 233 | Popular Tags |