KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > nodes > properties > AXIComponentSelectionPanel


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 package org.netbeans.modules.xml.schema.abe.nodes.properties;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.EventQueue JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyVetoException JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.ListSelectionModel JavaDoc;
32 import org.netbeans.modules.xml.axi.AXIComponent;
33 import org.netbeans.modules.xml.axi.ContentModel;
34 import org.netbeans.modules.xml.axi.datatype.CustomDatatype;
35 import org.netbeans.modules.xml.axi.datatype.Datatype;
36 import org.netbeans.modules.xml.schema.abe.nodes.ABENodeFactory;
37 import org.netbeans.modules.xml.axi.AXIModel;
38 import org.netbeans.modules.xml.axi.AXIType;
39 import org.netbeans.modules.xml.schema.abe.nodes.ABEAbstractNode;
40 import org.netbeans.modules.xml.schema.abe.nodes.ContentModelNode;
41 import org.netbeans.modules.xml.schema.abe.nodes.CustomDatatypeNode;
42 import org.netbeans.modules.xml.schema.abe.nodes.DatatypeNode;
43 import org.netbeans.modules.xml.schema.abe.nodes.GlobalContentModelsNode;
44 import org.netbeans.modules.xml.schema.abe.nodes.PrimitiveSimpleTypesNode;
45 import org.netbeans.modules.xml.schema.abe.nodes.SimpleTypesNode;
46 import org.openide.explorer.ExplorerManager;
47 import org.openide.explorer.view.BeanTreeView;
48 import org.openide.nodes.Node;
49 import org.openide.util.Lookup;
50
51 /**
52  *
53  * @author Jeri Lockhart
54  */

55 public class AXIComponentSelectionPanel extends JPanel JavaDoc
56         implements ExplorerManager.Provider, PropertyChangeListener JavaDoc {
57     
58     static final long serialVersionUID = 1L;
59     public static final String JavaDoc PROPERTY_SELECTION = "selectionChanged";
60     private transient Object JavaDoc currentSelection;
61     private transient String JavaDoc typeDisplayName;
62     private Collection JavaDoc<? extends AXIComponent> exclude;
63     private BeanTreeView typeView;
64     private ExplorerManager explorerManager;
65     
66     private List JavaDoc<Class JavaDoc> filterTypes;
67     
68     /** Creates new form AXIComponentSelectionPanel */
69     public AXIComponentSelectionPanel(AXIModel model, String JavaDoc typeDisplayName,
70             List JavaDoc<Class JavaDoc> filterTypes, Object JavaDoc initialSelection,
71             Collection JavaDoc<? extends AXIComponent> exclude) {
72         this.currentSelection = initialSelection;
73         this.typeDisplayName = typeDisplayName;
74         this.filterTypes = filterTypes;
75         if(exclude==null) {
76             this.exclude = Collections.emptyList();
77         } else {
78             this.exclude = exclude;
79         }
80         initComponents();
81         initialize(model);
82     }
83     
84     /** This method is called from within the constructor to
85      * initialize the form.
86      * WARNING: Do NOT modify this code. The content of this method is
87      * always regenerated by the Form Editor.
88      */

89     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
90
private void initComponents() {
91         jPanel1 = new javax.swing.JPanel JavaDoc();
92         selectedLbl = new javax.swing.JLabel JavaDoc();
93         nameTxt = new javax.swing.JTextField JavaDoc();
94         cvPanel = new javax.swing.JPanel JavaDoc();
95
96         selectedLbl.setText(org.openide.util.NbBundle.getMessage(AXIComponentSelectionPanel.class, "LBL_Currently_Selected", new Object JavaDoc[] {typeDisplayName}));
97
98         nameTxt.setEditable(false);
99         nameTxt.setText(getCurrentSelectionName());
100
101         org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
102         jPanel1.setLayout(jPanel1Layout);
103         jPanel1Layout.setHorizontalGroup(
104             jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
105             .add(jPanel1Layout.createSequentialGroup()
106                 .add(selectedLbl)
107                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
108                 .add(nameTxt, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE))
109         );
110         jPanel1Layout.setVerticalGroup(
111             jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
112             .add(jPanel1Layout.createSequentialGroup()
113                 .addContainerGap()
114                 .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
115                     .add(nameTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
116                     .add(selectedLbl, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE))
117                 .addContainerGap())
118         );
119
120         cvPanel.setLayout(new java.awt.BorderLayout JavaDoc());
121
122         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
123         this.setLayout(layout);
124         layout.setHorizontalGroup(
125             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
126             .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
127                 .addContainerGap()
128                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
129                     .add(org.jdesktop.layout.GroupLayout.LEADING, cvPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
130                     .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
131                 .addContainerGap())
132         );
133         layout.setVerticalGroup(
134             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
135             .add(layout.createSequentialGroup()
136                 .addContainerGap()
137                 .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
138                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
139                 .add(cvPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
140                 .addContainerGap())
141         );
142     }// </editor-fold>//GEN-END:initComponents
143

144     private void initialize(final AXIModel model) {
145 // ABENodeFactory factory = new ABENodeFactory(
146
// model, childTypes, Lookup.EMPTY);
147
ABENodeFactory factory = new ABENodeFactory(
148                 model, Lookup.EMPTY);
149         final Node rootNode = factory.createRootNode(filterTypes);
150         // View for selecting a global type.
151
typeView = new BeanTreeView();
152         typeView.setPopupAllowed(false);
153         typeView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
154         typeView.setRootVisible(false);
155         cvPanel.add(typeView, BorderLayout.CENTER);
156         explorerManager = new ExplorerManager();
157         explorerManager.setRootContext(rootNode);
158         EventQueue.invokeLater(new Runnable JavaDoc() {
159             public void run() {
160                 selectCurrentSelection(model);
161             }
162         });
163     }
164     
165     private void selectCurrentSelection(final AXIModel model) {
166         if(currentSelection!=null) {
167             Node node = findNode(explorerManager.getRootContext(),
168                     currentSelection, model);
169             if(node!=null) {
170                 try {
171                     getExplorerManager().setSelectedNodes(new Node[]{node});
172                 } catch (PropertyVetoException JavaDoc ex) {
173                 }
174             }
175         }
176         getExplorerManager().addPropertyChangeListener(this);
177     }
178     
179     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
180         if (evt.getPropertyName().equals(ExplorerManager.PROP_SELECTED_NODES)) {
181             Node[] nodes = getExplorerManager().getSelectedNodes();
182             if (nodes.length > 0) {
183                 if(nodes[0] instanceof ABEAbstractNode) {
184                     setCurrentSelection(((ABEAbstractNode)nodes[0]).getAXIComponent());
185                     return;
186                 }
187                 
188                 if(nodes[0] instanceof DatatypeNode) {
189                     setCurrentSelection((Datatype)
190                     ((DatatypeNode)nodes[0]).getType());
191                     return;
192                 }
193                 
194                 if(nodes[0] instanceof PrimitiveSimpleTypesNode.TypeNode) {
195                     setCurrentSelection((Datatype)
196                     ((PrimitiveSimpleTypesNode.TypeNode)nodes[0]).getType());
197                     return;
198                 }
199             }
200             setCurrentSelection(null);
201         }
202     }
203
204     private void setCurrentSelection(Object JavaDoc sc){
205         if(sc instanceof AXIType) {
206             Object JavaDoc oldValue = currentSelection;
207             currentSelection = sc==null||exclude.contains(sc)?null:sc;
208             nameTxt.setText(getCurrentSelectionName()); //NOI18N
209
firePropertyChange(PROPERTY_SELECTION,oldValue,currentSelection);
210         }
211     }
212     
213     public Object JavaDoc getCurrentSelection() {
214         return currentSelection;
215     }
216     
217     public String JavaDoc getCurrentSelectionName() {
218         if(currentSelection instanceof AXIType)
219             return ((AXIType)currentSelection).getName();
220         return "";
221     }
222     
223     public ExplorerManager getExplorerManager() {
224         return explorerManager;
225     }
226     
227     private Node findNode(Node root, Object JavaDoc currentSelection, AXIModel model) {
228         Node[] categories = root.getChildren().getNodes();
229         for(Node category:categories) {
230             if(category instanceof PrimitiveSimpleTypesNode &&
231                     currentSelection instanceof Datatype) {
232                 String JavaDoc name = ((Datatype)currentSelection).getName();
233                 Node[] childs = category.getChildren().getNodes();
234                 for(Node child:childs) {
235                     if(((PrimitiveSimpleTypesNode.TypeNode)child).getName().equals(name))
236                         return child;
237                 }
238             } else if(category instanceof SimpleTypesNode &&
239                     currentSelection instanceof CustomDatatype) {
240                 String JavaDoc name = ((CustomDatatype)currentSelection).getName();
241                 Node[] childs = category.getChildren().getNodes();
242                 for(Node child:childs) {
243                     if(((CustomDatatypeNode)child).getName().equals(name))
244                         return child;
245                 }
246             } else if(category instanceof GlobalContentModelsNode &&
247                     currentSelection instanceof ContentModel) {
248                 String JavaDoc name = ((ContentModel)currentSelection).getName();
249                 Node[] childs = category.getChildren().getNodes();
250                 for(Node child:childs) {
251                     if(((ContentModelNode)child).getName().equals(name))
252                         return child;
253                 }
254             }
255         }
256         return null;
257     }
258     
259     // Variables declaration - do not modify//GEN-BEGIN:variables
260
private javax.swing.JPanel JavaDoc cvPanel;
261     private javax.swing.JPanel JavaDoc jPanel1;
262     private javax.swing.JTextField JavaDoc nameTxt;
263     private javax.swing.JLabel JavaDoc selectedLbl;
264     // End of variables declaration//GEN-END:variables
265

266 }
267
Popular Tags