KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > basic > editors > DerivationTypeEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * DerivationTypeEditor.java
22  *
23  * Created on December 22, 2005, 12:58 PM
24  */

25
26 package org.netbeans.modules.xml.schema.ui.basic.editors;
27
28 import java.awt.Component JavaDoc;
29 import java.awt.Dialog JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32 import java.beans.FeatureDescriptor JavaDoc;
33 import java.beans.PropertyChangeEvent JavaDoc;
34 import java.beans.PropertyChangeListener JavaDoc;
35 import java.beans.PropertyEditorSupport JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.HashSet JavaDoc;
39 import java.util.Set JavaDoc;
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 /**
55  *
56  * @author Ajit Bhate
57  */

58 public class DerivationTypeEditor extends PropertyEditorSupport JavaDoc
59     implements ExPropertyEditor {
60     
61     private String JavaDoc property;
62     private String JavaDoc typeDisplayName;
63     private HashMap JavaDoc<Derivation.Type,? extends Derivation> typeToDerivationMap = null;
64     
65     /**
66      * Creates a new instance of DerivationTypeEditor
67      */

68     public DerivationTypeEditor(SchemaComponent component, String JavaDoc property, String JavaDoc 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 JavaDoc<Derivation.Type,GlobalElement.Final> tmpMap = new HashMap JavaDoc<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 JavaDoc<Derivation.Type,Element.Block> tmpMap = new HashMap JavaDoc<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 JavaDoc<Derivation.Type,GlobalComplexType.Final> tmpMap = new HashMap JavaDoc<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 JavaDoc<Derivation.Type,GlobalComplexType.Block> tmpMap = new HashMap JavaDoc<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 JavaDoc<Derivation.Type,GlobalSimpleType.Final> tmpMap = new HashMap JavaDoc<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 JavaDoc<Derivation.Type,Schema.Final> tmpMap = new HashMap JavaDoc<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 JavaDoc<Derivation.Type,Schema.Block> tmpMap = new HashMap JavaDoc<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 JavaDoc getAsText() {
140         Object JavaDoc val = getValue();
141         if (val == null){
142             return null;
143         }
144         if (val instanceof Set JavaDoc){
145             return ((Set JavaDoc)val).toString();
146         }
147         // TODO how to display invalid values?
148
return val.toString();
149     }
150     
151     private Set JavaDoc<Derivation.Type> convertToDerivationType(
152             Set JavaDoc<Derivation> derivationSet) {
153         Set JavaDoc<Derivation.Type> derivationTypeSet = new HashSet JavaDoc<Derivation.Type>();
154         if (derivationSet != null && !derivationSet.isEmpty()) {
155             Set JavaDoc<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 JavaDoc<? extends Derivation> convertToDerivation(
169             Set JavaDoc<Derivation.Type> derivationTypeSet) {
170         Set JavaDoc<Derivation> derivationSet = new HashSet JavaDoc<Derivation>();
171         for(Derivation.Type type: derivationTypeSet) {
172             derivationSet.add(typeToDerivationMap.get(type));
173         }
174         return derivationSet;
175     }
176
177     @SuppressWarnings JavaDoc("unchecked")
178     public Component JavaDoc getCustomEditor() {
179         Object JavaDoc obj = getValue();
180         Set JavaDoc<Derivation> initialValue = Collections.emptySet();
181         if(obj instanceof Set JavaDoc) initialValue = (Set JavaDoc<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 JavaDoc() {
189             public void actionPerformed(ActionEvent JavaDoc evt) {
190                 if (evt.getSource().equals(DialogDescriptor.OK_OPTION)) {
191                     try {
192                         Set JavaDoc<Derivation.Type> currentSelection =
193                                 panel.getCurrentSelection();
194                         setValue(currentSelection.isEmpty()?null:
195                             convertToDerivation(currentSelection));
196                     } catch (IllegalArgumentException JavaDoc iae) {
197                         ErrorManager.getDefault().annotate(iae, ErrorManager.USER,
198                                 iae.getMessage(), iae.getLocalizedMessage(),
199                                 null, new java.util.Date JavaDoc());
200                         throw iae;
201                     }
202                 }
203             }
204         });
205         
206         // enable/disable the dlg ok button depending selection
207
panel.addPropertyChangeListener( new PropertyChangeListener JavaDoc() {
208             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
209                 if (evt.getPropertyName().equals("valid")) {
210                     descriptor.setValid(((Boolean JavaDoc)evt.getNewValue()).booleanValue());
211                 }
212             }
213         });
214         Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(descriptor);
215         // set size to 400x300
216
//dlg.setPreferredSize(panel.getSize());
217
dlg.setPreferredSize(new java.awt.Dimension JavaDoc(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 JavaDoc desc = env.getFeatureDescriptor();
228         // make this is not editable
229
desc.setValue("canEditAsText", Boolean.FALSE); // NOI18N
230
}
231
232 }
233
Popular Tags