KickJava   Java API By Example, From Geeks To Geeks.

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


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  * UIUtilities.java
22  *
23  * Created on June 23, 2006, 3:24 PM
24  *
25  */

26
27 package org.netbeans.modules.xml.schema.ui.basic;
28
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collections JavaDoc;
36 import java.util.List JavaDoc;
37 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
38 import org.netbeans.modules.xml.schema.model.Schema;
39 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
40 import org.openide.nodes.Node;
41 import org.netbeans.modules.xml.schema.model.SchemaComponent;
42 import org.netbeans.modules.xml.schema.model.SchemaModel;
43 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
44 import org.netbeans.modules.xml.schema.model.SchemaModelReference;
45 import org.netbeans.modules.xml.schema.ui.nodes.SchemaComponentNode;
46 import org.netbeans.modules.xml.schema.ui.nodes.categorized.CategoryNode;
47 import org.netbeans.modules.xml.schema.ui.nodes.categorized.PrimitiveSimpleTypesNode;
48 import org.netbeans.modules.xml.xam.ui.customizer.Customizer;
49 import org.openide.DialogDescriptor;
50 import org.openide.util.NbBundle;
51
52 /**
53  *
54  * @author Ajit Bhate
55  */

56 public class UIUtilities {
57     
58     /** Creates a new instance of UIUtilities */
59     private UIUtilities() {
60     }
61     
62     /**
63      * finds path of given sceham component from root node representing its schema
64      * example
65      * 1. Schema->GlobalElement->ComplexType->Sequence
66      * results in SchemaNode->Elements Category Node->GlobalElement Node ->
67      * Complex Type Node -> Sequence Node
68      * 2. Schema->GlobalElement->ComplexType->Complex Content->Extension
69      * results in SchemaNode->Elements Category Node->GlobalElement Node ->
70      * Complex Type Node
71      * (as complex content and extension nodes are not shown)
72      */

73     public static List JavaDoc<Node> findPathFromRoot(Node root, SchemaComponent sc) {
74         if(sc.getModel()==null) return Collections.emptyList();
75         ArrayList JavaDoc<SchemaComponent> path = new ArrayList JavaDoc<SchemaComponent>();
76         SchemaComponent parent = sc;
77         while(parent!=null) {
78             path.add(0,parent);
79             parent = parent.getParent();
80         }
81         if(path.get(0) !=sc.getModel().getSchema())
82             return Collections.emptyList();
83         SchemaComponentNode rootSCN = (SchemaComponentNode)root.
84                 getCookie(SchemaComponentNode.class);
85         if(rootSCN==null||rootSCN.getReference().get()!=path.get(0))
86             return Collections.emptyList();
87         ArrayList JavaDoc<Node> selectionPath = new ArrayList JavaDoc<Node>();
88         selectionPath.add(root);
89         Class JavaDoc<? extends SchemaComponent> categoryType = null;
90         if(path.size()>1) categoryType = path.get(1).getComponentType();
91         Node parentNode = root;
92         for(int i=0;i<path.size();i++) {
93             SchemaComponent comp = path.get(i);
94             List JavaDoc<Node> subPath = null;
95             Node[] children = parentNode.getChildren().getNodes();
96             if (children == null || children.length<0) {
97                 return selectionPath;
98             }
99             if (subPath==null) {
100                 for (Node n : children) {
101                     CategoryNode cNode = (CategoryNode) n.getCookie(CategoryNode.class);
102                     if (cNode != null && cNode.getChildType()==categoryType) {
103                         Node[] nChildren = n.getChildren().getNodes();
104                         if (nChildren != null && nChildren.length > 0) {
105                             for (Node nChild : nChildren) {
106                                 SchemaComponentNode node = (SchemaComponentNode) nChild.
107                                         getCookie(SchemaComponentNode.class);
108                                 if (node != null) {
109                                     SchemaComponent scomp = node.getReference().get();
110                                     int idx = path.indexOf(scomp);
111                                     if (idx>=i) {
112                                         subPath = new ArrayList JavaDoc<Node>();
113                                         subPath.add(n);
114                                         subPath.add(nChild);
115                                         parentNode = nChild;
116                                         i=idx;
117                                         if(i!=0&&i<path.size()) categoryType =
118                                                 path.get(i).getComponentType();
119                                         break;
120                                     }
121                                 }
122                             }
123                         }
124                         break;
125                     }
126                 }
127             }
128             if (subPath==null) {
129                 for (Node n : children) {
130                     SchemaComponentNode node = (SchemaComponentNode) n.getCookie(
131                             SchemaComponentNode.class);
132                     if (node != null) {
133                         SchemaComponent scomp = node.getReference().get();
134                         int idx = path.indexOf(scomp);
135                         if (idx>=i) {
136                             subPath = Collections.singletonList(n);
137                             parentNode = n;
138                             i=idx;
139                             if(i!=0&&i<path.size()) categoryType =
140                                     path.get(i).getComponentType();
141                             break;
142                         }
143                     }
144                 }
145             }
146             if (subPath==null) break;
147             selectionPath.addAll(subPath);
148         }
149         return selectionPath;
150     }
151     
152     /**
153      * Searches for node representing given schema component, under given node.
154      * Search is recusrive.
155      */

156     public static Node findNode(Node parent, ReferenceableSchemaComponent sc, SchemaModel currentModel) {
157         if (parent == null) return null;
158         Node[] children = parent.getChildren().getNodes();
159         if(children == null || children.length<0) return null;
160         boolean primitive = sc.getModel()==
161                 SchemaModelFactory.getDefault().getPrimitiveTypesModel();
162         boolean externalRef = !primitive&&sc.getModel()!=currentModel;
163         for (Node n:children) {
164             SchemaComponentNode node =(SchemaComponentNode)n.
165                     getCookie(SchemaComponentNode.class);
166             SchemaComponent scomp = node==null?null:node.getReference().get();
167             if (scomp == sc) {
168                 return n;
169             }
170             if(primitive) {
171                 if (n instanceof PrimitiveSimpleTypesNode) {
172                     return findNode(n,sc,currentModel);
173                 }
174             }
175             if(externalRef) {
176                 if(scomp instanceof Schema && scomp.getModel() == sc.getModel()) {
177                     Node result = findNode(n,sc,sc.getModel());
178                     if (result != null) return result;
179                 }
180                 try {
181                     if(scomp instanceof SchemaModelReference &&
182                             ((SchemaModelReference)scomp).
183                             resolveReferencedModel() == sc.getModel()) {
184                         Node result = findNode(n,sc,sc.getModel());
185                         if (result != null) return result;
186                     }
187                 } catch(CatalogModelException cme) {
188                     // nothing
189
}
190             }
191             if(scomp!=null) continue;
192             CategoryNode cNode = (CategoryNode)n.getCookie(
193                     CategoryNode.class);
194             if(cNode==null) continue;
195             if(!externalRef && cNode.getChildType().
196                     isAssignableFrom(sc.getComponentType())) {
197                 return findNode(n,sc,currentModel);
198             } else if(externalRef&& SchemaModelReference.class.
199                     isAssignableFrom(cNode.getChildType())) {
200                 return findNode(n,sc,currentModel);
201             }
202         }
203         return null;
204     }
205     
206     public static DialogDescriptor getCustomizerDialog(
207             final Customizer customizer, final String JavaDoc title, final boolean editable) {
208         java.awt.Component JavaDoc component = customizer.getComponent();
209         final DialogDescriptor descriptor = new DialogDescriptor(component,
210                 NbBundle.getMessage(UIUtilities.class,
211                 "TITLE_SchemaComponentNode_Customizer", title),
212                 true, null);
213         descriptor.setHelpCtx(customizer.getHelpCtx());
214         if(editable) {
215             // customizer's property change listener to enable/disable OK
216
final PropertyChangeListener JavaDoc pcl = new PropertyChangeListener JavaDoc() {
217                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
218                     if(evt.getSource()==customizer && evt.getPropertyName().
219                             equals(Customizer.PROP_ACTION_APPLY)) {
220                         descriptor.setValid(((Boolean JavaDoc) evt.getNewValue()).booleanValue());
221                     }
222                 }
223             };
224             customizer.addPropertyChangeListener(pcl);
225             // dialog's action listener
226
ActionListener JavaDoc al = new ActionListener JavaDoc() {
227                 public void actionPerformed(ActionEvent JavaDoc evt) {
228                     if (evt.getSource().equals(DialogDescriptor.OK_OPTION) ||
229                             evt.getSource().equals(DialogDescriptor.CANCEL_OPTION) ||
230                             evt.getSource().equals(DialogDescriptor.CLOSED_OPTION)) {
231                         customizer.removePropertyChangeListener(pcl);
232                     }
233                     if (evt.getSource().equals(DialogDescriptor.OK_OPTION)) {
234                         try {
235                             customizer.apply();
236                         } catch (IOException JavaDoc ioe) {
237                         }
238                     }
239                 }
240             };
241             descriptor.setButtonListener(al);
242         }
243         descriptor.setValid(customizer.canApply());
244         return descriptor;
245     }
246 }
247
Popular Tags