KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AttributeGroupRefPanel.java
22  *
23  * Created on January 4, 2006, 1:42 PM
24  */

25
26 package org.netbeans.modules.xml.schema.ui.basic.editors;
27
28 import java.awt.BorderLayout JavaDoc;
29 import java.awt.EventQueue JavaDoc;
30 import java.beans.PropertyChangeEvent JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32 import java.beans.PropertyVetoException JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.Collections JavaDoc;
36 import javax.swing.JPanel JavaDoc;
37 import javax.swing.plaf.basic.BasicBorders JavaDoc;
38 import javax.swing.tree.TreeSelectionModel JavaDoc;
39
40 import org.openide.explorer.ExplorerManager;
41 import org.openide.explorer.view.BeanTreeView;
42 import org.openide.nodes.Node;
43 import org.openide.util.Lookup;
44 import org.openide.util.NbBundle;
45
46 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
47 import org.netbeans.modules.xml.schema.model.SchemaComponent;
48 import org.netbeans.modules.xml.schema.model.SchemaModel;
49 import org.netbeans.modules.xml.schema.model.SchemaModelReference;
50 import org.netbeans.modules.xml.schema.ui.basic.UIUtilities;
51 import org.netbeans.modules.xml.schema.ui.nodes.SchemaComponentNode;
52 import org.netbeans.modules.xml.schema.ui.nodes.categorized.CategorizedSchemaNodeFactory;
53 import org.netbeans.modules.xml.schema.ui.nodes.categorized.PrimitiveSimpleType;
54
55 /**
56  *
57  * @author Jeri Lockhart
58  */

59 public class SchemaComponentSelectionPanel<T extends ReferenceableSchemaComponent> extends JPanel JavaDoc
60         implements ExplorerManager.Provider, PropertyChangeListener JavaDoc {
61     
62     static final long serialVersionUID = 1L;
63     public static final String JavaDoc PROPERTY_SELECTION = "selectionChanged";
64     private Class JavaDoc<T> type;
65     private transient SchemaModel model;
66     private transient T currentSelection;
67     private Collection JavaDoc<? extends SchemaComponent> exclude;
68     private BeanTreeView typeView;
69     private boolean includePrimitives;
70     
71     /**
72      * Creates new form SchemaComponentSelectionPanel.
73      *
74      * @param model the schema model.
75      * @param type the type of component to select.
76      * @param initialSelection the initial selection, if any.
77      * @param exclude which components to exclude.
78      * @param primitives if true, include primitive simple types.
79      */

80     public SchemaComponentSelectionPanel(SchemaModel model, Class JavaDoc<T> type,
81             T initialSelection, Collection JavaDoc<? extends SchemaComponent> exclude,
82             boolean primitives) {
83         this.type = type;
84         if(exclude==null) {
85             this.exclude = Collections.emptyList();
86         } else {
87             this.exclude = exclude;
88         }
89         this.includePrimitives = primitives;
90         initComponents();
91         this.model=model;
92         initialize(initialSelection);
93     }
94     
95     /** This method is called from within the constructor to
96      * initialize the form.
97      * WARNING: Do NOT modify this code. The content of this method is
98      * always regenerated by the Form Editor.
99      */

100     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
101
private void initComponents() {
102         typeSelectionPanel = new EmbededPanel(this);
103         cvPanel = new javax.swing.JPanel JavaDoc();
104         selectedLbl = new javax.swing.JLabel JavaDoc();
105         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
106         descriptionPane = new javax.swing.JTextPane JavaDoc();
107
108         cvPanel.setLayout(new java.awt.BorderLayout JavaDoc());
109
110         selectedLbl.setLabelFor(descriptionPane);
111         org.openide.awt.Mnemonics.setLocalizedText(selectedLbl, org.openide.util.NbBundle.getMessage(SchemaComponentSelectionPanel.class, "LBL_Currently_Selected", new Object JavaDoc[] {"", ""}));
112         selectedLbl.setToolTipText(org.openide.util.NbBundle.getMessage(SchemaComponentSelectionPanel.class, "HINT_Currently_Selected"));
113
114         jScrollPane1.setBorder(null);
115         jScrollPane1.setMinimumSize(new java.awt.Dimension JavaDoc(20, 28));
116         descriptionPane.setBorder(null);
117         descriptionPane.setEditable(false);
118         descriptionPane.setBackground(getBackground());
119         jScrollPane1.setViewportView(descriptionPane);
120
121         org.jdesktop.layout.GroupLayout typeSelectionPanelLayout = new org.jdesktop.layout.GroupLayout(typeSelectionPanel);
122         typeSelectionPanel.setLayout(typeSelectionPanelLayout);
123         typeSelectionPanelLayout.setHorizontalGroup(
124             typeSelectionPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
125             .add(typeSelectionPanelLayout.createSequentialGroup()
126                 .add(selectedLbl)
127                 .addContainerGap())
128             .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
129             .add(cvPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
130         );
131         typeSelectionPanelLayout.setVerticalGroup(
132             typeSelectionPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
133             .add(typeSelectionPanelLayout.createSequentialGroup()
134                 .add(cvPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
135                 .add(6, 6, 6)
136                 .add(selectedLbl)
137                 .add(0, 0, 0)
138                 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))
139         );
140
141         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
142         this.setLayout(layout);
143         layout.setHorizontalGroup(
144             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
145             .add(layout.createSequentialGroup()
146                 .addContainerGap()
147                 .add(typeSelectionPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
148                 .addContainerGap())
149         );
150         layout.setVerticalGroup(
151             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
152             .add(layout.createSequentialGroup()
153                 .addContainerGap()
154                 .add(typeSelectionPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
155                 .addContainerGap())
156         );
157     }// </editor-fold>//GEN-END:initComponents
158

159     private void initialize(final T initialSelection) {
160         ArrayList JavaDoc<Class JavaDoc<? extends SchemaComponent>> childTypes =
161                 new ArrayList JavaDoc<Class JavaDoc<? extends SchemaComponent>>();
162         childTypes.add(SchemaModelReference.class);
163         childTypes.add(type);
164         if(includePrimitives&&!type.isAssignableFrom(PrimitiveSimpleType.class))
165             childTypes.add(PrimitiveSimpleType.class);
166         CategorizedSchemaNodeFactory factory = new CategorizedSchemaNodeFactory(
167                 model, childTypes, Lookup.EMPTY);
168         final Node rootNode = factory.createRootNode();
169         // View for selecting a global type.
170
typeView = new BeanTreeView();
171         typeView.setBorder(BasicBorders.getTextFieldBorder());
172         typeView.setPopupAllowed(false);
173         typeView.setDefaultActionAllowed(false);
174         typeView.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
175         typeView.setRootVisible(false);
176         typeView.getAccessibleContext().setAccessibleName(
177                 org.openide.util.NbBundle.getMessage(
178                 SchemaComponentSelectionPanel.class, "LBL_GlobalReferenceTree"));
179         typeView.getAccessibleContext().setAccessibleDescription(
180                 org.openide.util.NbBundle.getMessage(
181                 SchemaComponentSelectionPanel.class, "LBL_GlobalReferenceTree"));
182         cvPanel.add(typeView, BorderLayout.CENTER);
183         getExplorerManager().setRootContext(rootNode);
184         if(initialSelection!=null) {
185             EventQueue.invokeLater(new Runnable JavaDoc() {
186                 public void run() {
187                     setInitialSelection(initialSelection);
188                 }
189             });
190         }
191     }
192     
193     public void setInitialSelection(T initialSelection) {
194         currentSelection = initialSelection;
195         if(currentSelection!=null) {
196             Node node = UIUtilities.findNode(getExplorerManager().
197                     getRootContext(),currentSelection, model);
198             if(node!=null) {
199                 setDescription(node);
200                 try {
201                     getExplorerManager().setSelectedNodes(new Node[]{node});
202                 } catch (PropertyVetoException JavaDoc ex) {
203                 }
204             }
205         }
206     }
207     
208     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
209         if (evt.getPropertyName().equals(ExplorerManager.PROP_SELECTED_NODES)) {
210             Node[] nodes = getExplorerManager().getSelectedNodes();
211             if (nodes.length > 0) {
212                 SchemaComponentNode<T> scn = findReferenceableNode(nodes[0]);
213                 setCurrentSelection(scn==null?null:scn.getReference().get());
214                 setDescription(scn);
215                 return;
216             }
217             setCurrentSelection(null);
218         }
219     }
220     
221     private void setCurrentSelection(T sc) {
222         T oldValue = currentSelection;
223         currentSelection = sc==null||exclude.contains(sc)?null:sc;
224         firePropertyChange(PROPERTY_SELECTION,oldValue,currentSelection);
225     }
226     
227     public T getCurrentSelection() {
228         return currentSelection;
229     }
230     
231     public ExplorerManager getExplorerManager() {
232         return ((EmbededPanel)getTypeSelectionPanel()).getExplorerManager();
233     }
234     
235     public void setEnabled(boolean flag) {
236         super.setEnabled(flag);
237         typeView.setEnabled(flag);
238         selectedLbl.setEnabled(flag);
239         descriptionPane.setEnabled(flag);
240     }
241     
242     public JPanel JavaDoc getTypeSelectionPanel() {
243         return typeSelectionPanel;
244     }
245     
246     @SuppressWarnings JavaDoc("unchecked")
247     private SchemaComponentNode<T> findReferenceableNode(Node node) {
248         Node parent = node;
249         SchemaComponentNode scn = null;
250         while(parent!=null) {
251             scn = (SchemaComponentNode)parent
252                     .getCookie(SchemaComponentNode.class);
253             if(scn!=null && type.isInstance(scn.getReference().get()))
254                 return (SchemaComponentNode<T>)scn;
255             parent=parent.getParentNode();
256         }
257         return null;
258     }
259     
260     private void setDescription(Node selected) {
261         String JavaDoc arg0 = "";
262         String JavaDoc arg1 = "";
263         if(currentSelection!=null) {
264             arg0 = currentSelection.getName();
265             SchemaComponentNode scn = (SchemaComponentNode)selected.
266                     getCookie(SchemaComponentNode.class);
267             arg1 = scn==null?"":"("+scn.getTypeDisplayName()+")";
268         }
269         selectedLbl.setText(NbBundle.getMessage(
270                 SchemaComponentSelectionPanel.class,
271                 "LBL_Currently_Selected", new Object JavaDoc[] {arg0,arg1}));
272         if(selected!=null&&selected.getShortDescription()!=null)
273             descriptionPane.setText(selected.getShortDescription());
274         else
275             descriptionPane.setText("");
276         descriptionPane.setCaretPosition(0);
277     }
278
279     // Variables declaration - do not modify//GEN-BEGIN:variables
280
public javax.swing.JPanel JavaDoc cvPanel;
281     private javax.swing.JTextPane JavaDoc descriptionPane;
282     private javax.swing.JScrollPane JavaDoc jScrollPane1;
283     private javax.swing.JLabel JavaDoc selectedLbl;
284     public javax.swing.JPanel JavaDoc typeSelectionPanel;
285     // End of variables declaration//GEN-END:variables
286

287     private static class EmbededPanel extends JPanel JavaDoc
288             implements ExplorerManager.Provider {
289         private static final long serialVersionUID = 1L;
290         private ExplorerManager explorerManager;
291         private PropertyChangeListener JavaDoc listener;
292         public EmbededPanel(PropertyChangeListener JavaDoc listener) {
293             super();
294             this.listener = listener;
295             explorerManager = new ExplorerManager();
296         }
297         public ExplorerManager getExplorerManager() {
298             return explorerManager;
299         }
300
301         public void removeNotify() {
302             super.removeNotify();
303             getExplorerManager().removePropertyChangeListener(listener);
304         }
305         
306         public void addNotify() {
307             super.addNotify();
308             getExplorerManager().addPropertyChangeListener(listener);
309         }
310     }
311 }
312
Popular Tags