KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MemberTypesEditor.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.Dimension JavaDoc;
31 import java.awt.event.ActionEvent JavaDoc;
32 import java.awt.event.ActionListener JavaDoc;
33 import java.beans.FeatureDescriptor JavaDoc;
34 import java.beans.PropertyEditorSupport JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.List JavaDoc;
39 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
40 import org.netbeans.modules.xml.schema.model.SchemaComponent;
41 import org.netbeans.modules.xml.schema.ui.nodes.schema.UnionNode;
42 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
43 import org.netbeans.modules.xml.xam.Named;
44 import org.openide.DialogDescriptor;
45 import org.openide.DialogDisplayer;
46 import org.openide.ErrorManager;
47 import org.openide.explorer.propertysheet.ExPropertyEditor;
48 import org.openide.explorer.propertysheet.PropertyEnv;
49 import org.openide.util.NbBundle;
50
51 /**
52  *
53  * @author Ajit Bhate
54  */

55 public class MemberTypesEditor extends PropertyEditorSupport JavaDoc
56         implements ExPropertyEditor {
57     
58     private MemberTypesPanel panel;
59     private DialogDescriptor descriptor;
60     private SchemaComponent component;
61     
62     /**
63      * Creates a new instance of MemberTypesEditor
64      */

65     public MemberTypesEditor(SchemaComponent component) {
66         this.component = component;
67     }
68     
69     public String JavaDoc getAsText() {
70         Object JavaDoc val = super.getValue();
71         if (val == null){
72             return null;
73         }
74         if(val instanceof Collection JavaDoc) {
75             boolean first = true;
76             String JavaDoc str = "";
77             for(Object JavaDoc elem: (Collection JavaDoc)val) {
78                 if (elem instanceof NamedComponentReference){
79                     Object JavaDoc obj = ((NamedComponentReference)elem).get();
80                     if (obj instanceof Named){
81                         if(first) {
82                             str = str.concat(((Named)obj).getName());
83                             first = false;
84                         } else {
85                             str = str.concat(" ").concat(((Named)obj).getName());
86                         }
87                     }
88                 }
89             }
90             return str;
91         }
92         // TODO how to display invalid values?
93
return val.toString();
94     }
95     
96     @SuppressWarnings JavaDoc("unchecked")
97     public Collection JavaDoc<NamedComponentReference<GlobalSimpleType>> getValue() {
98         return (Collection JavaDoc<NamedComponentReference<GlobalSimpleType>>) super.getValue();
99     }
100
101     private Collection JavaDoc<NamedComponentReference<GlobalSimpleType>> getCurrentSelection(
102             List JavaDoc<GlobalSimpleType> newSelectionList) {
103         if(newSelectionList != null && !newSelectionList.isEmpty()) {
104                 Collection JavaDoc<NamedComponentReference<GlobalSimpleType>> newSelectionRef =
105                         new ArrayList JavaDoc<NamedComponentReference<GlobalSimpleType>>();
106                 HashMap JavaDoc<GlobalSimpleType,NamedComponentReference<GlobalSimpleType>> gstToRefMap =
107                         new HashMap JavaDoc<GlobalSimpleType,NamedComponentReference<GlobalSimpleType>>();
108                 Collection JavaDoc<NamedComponentReference<GlobalSimpleType>> oldSelectionRef = getValue();
109                 if(oldSelectionRef!=null) {
110                     for(NamedComponentReference<GlobalSimpleType> ref:oldSelectionRef) {
111                         GlobalSimpleType gst = ref.get();
112                         gstToRefMap.put(gst,ref);
113                     }
114                 }
115                 for (Object JavaDoc obj:newSelectionList) {
116                     if (gstToRefMap.containsKey(obj)) {
117                         newSelectionRef.add(gstToRefMap.get(obj));
118                     } else {
119                         newSelectionRef.add(component.getModel().getFactory().
120                                 createGlobalReference((GlobalSimpleType)obj,
121                                 GlobalSimpleType.class, component));
122                     }
123                 }
124                 return newSelectionRef;
125         }
126         return null;
127     }
128     
129     public Component JavaDoc getCustomEditor() {
130         Collection JavaDoc<NamedComponentReference<GlobalSimpleType>> currentSelectionRef = getValue();
131         Collection JavaDoc<GlobalSimpleType> currentSelection = null;
132         if(currentSelectionRef!=null) {
133             currentSelection = new ArrayList JavaDoc<GlobalSimpleType>();
134             for(NamedComponentReference ref :currentSelectionRef) {
135                 currentSelection.add(((GlobalSimpleType)ref.get()));
136             }
137         }
138         panel = new MemberTypesPanel(component.getModel(), currentSelection);
139         descriptor = new DialogDescriptor(panel,
140                 NbBundle.getMessage(MemberTypesEditor.class,
141                 "LBL_Custom_Property_Editor_Title",
142                 new Object JavaDoc[] {NbBundle.getMessage(UnionNode.class, "LBL_UnionNode_TypeDisplayName")
143                         , NbBundle.getMessage(UnionNode.class, "PROP_MemberTypes_DisplayName")}),
144                 true,
145                 new ActionListener JavaDoc() {
146             public void actionPerformed(ActionEvent JavaDoc evt) {
147                 if (evt.getSource().equals(DialogDescriptor.OK_OPTION)) {
148                     try {
149                         setValue(getCurrentSelection(panel.getCurrentSelection()));
150                     } catch (IllegalArgumentException JavaDoc iae) {
151                         ErrorManager.getDefault().annotate(iae, ErrorManager.USER,
152                                 iae.getMessage(), iae.getLocalizedMessage(),
153                                 null, new java.util.Date JavaDoc());
154                         throw iae;
155                     }
156                 }
157             }
158         }
159         );
160         
161         Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(descriptor);
162         dlg.setPreferredSize(new Dimension JavaDoc(400,300));
163         return dlg;
164     }
165     
166     
167     public boolean supportsCustomEditor() {
168         return true;
169     }
170     
171     public void attachEnv(PropertyEnv env ) {
172         FeatureDescriptor JavaDoc desc = env.getFeatureDescriptor();
173         // make this is not editable
174
desc.setValue("canEditAsText", Boolean.FALSE); // NOI18N
175
}
176
177 }
178
Popular Tags