001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * ElementOrTypeChooserPanel.java
044: *
045: * Created on September 1, 2006, 2:37 PM
046: */
047:
048: package org.netbeans.modules.xml.wsdl.ui.view;
049:
050: import java.beans.PropertyChangeEvent;
051: import java.beans.PropertyChangeListener;
052: import java.beans.PropertyVetoException;
053: import java.util.Map;
054:
055: import javax.swing.SwingUtilities;
056:
057: import org.netbeans.api.project.Project;
058: import org.netbeans.modules.xml.schema.model.GlobalElement;
059: import org.netbeans.modules.xml.schema.model.GlobalType;
060: import org.netbeans.modules.xml.schema.model.SchemaComponent;
061: import org.netbeans.modules.xml.schema.model.SchemaComponentReference;
062: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
063: import org.netbeans.modules.xml.wsdl.ui.api.property.ElementOrTypeChooserHelper;
064: import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
065: import org.openide.explorer.ExplorerManager;
066: import org.openide.nodes.AbstractNode;
067: import org.openide.nodes.Children;
068: import org.openide.nodes.Node;
069:
070: /**
071: *
072: * @author skini
073: */
074: public class ElementOrTypeChooserPanel extends javax.swing.JPanel
075: implements ExplorerManager.Provider {
076:
077: private Map<String, String> namespaceToPrefixMap;
078: private Project mProject;
079: private WSDLModel mModel;
080: private SchemaComponent mPreviousSelectedComponent;
081:
082: /** Creates new form ElementOrTypeChooserPanel */
083: public ElementOrTypeChooserPanel(Project project,
084: Map<String, String> namespaceToPrefixMap, WSDLModel model) {
085: this .namespaceToPrefixMap = namespaceToPrefixMap;
086: this .mProject = project;
087: this .mModel = model;
088: initComponents();
089: initGUI();
090: }
091:
092: public ElementOrTypeChooserPanel(Project project,
093: Map<String, String> namespaceToPrefixMap, WSDLModel model,
094: SchemaComponent previousSelectedComponent) {
095: this .namespaceToPrefixMap = namespaceToPrefixMap;
096: this .mProject = project;
097: this .mModel = model;
098: this .mPreviousSelectedComponent = previousSelectedComponent;
099: initComponents();
100: initGUI();
101: }
102:
103: /** This method is called from within the constructor to
104: * initialize the form.
105: * WARNING: Do NOT modify this code. The content of this method is
106: * always regenerated by the Form Editor.
107: */
108: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
109: private void initComponents() {
110:
111: beanTreeView1 = new org.openide.explorer.view.BeanTreeView();
112:
113: setName("Form"); // NOI18N
114:
115: beanTreeView1.setAutoscrolls(true);
116: beanTreeView1.setDefaultActionAllowed(false);
117: beanTreeView1.setDragSource(false);
118: beanTreeView1.setDropTarget(false);
119: beanTreeView1.setFocusable(false);
120: beanTreeView1.setName("beanTreeView1"); // NOI18N
121: beanTreeView1.setPopupAllowed(false);
122: beanTreeView1.setRootVisible(false);
123: beanTreeView1.setSelectionMode(1);
124:
125: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
126: this );
127: this .setLayout(layout);
128: layout.setHorizontalGroup(layout.createParallelGroup(
129: org.jdesktop.layout.GroupLayout.LEADING).add(
130: beanTreeView1,
131: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 410,
132: Short.MAX_VALUE));
133: layout.setVerticalGroup(layout.createParallelGroup(
134: org.jdesktop.layout.GroupLayout.LEADING).add(
135: beanTreeView1,
136: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 411,
137: Short.MAX_VALUE));
138: }// </editor-fold>//GEN-END:initComponents
139:
140: private void initGUI() {
141: manager = new ExplorerManager();
142: manager
143: .addPropertyChangeListener(new ExplorerPropertyChangeListener());
144: Node rootNode = new AbstractNode(new Children.Array());
145: manager.setRootContext(rootNode);
146: populateRootNode(rootNode);
147: Utility.expandNodes(beanTreeView1, 3, rootNode);
148: }
149:
150: private void populateRootNode(Node rootNode) {
151: ElementOrTypeChooserHelper schemaHelper = new ElementOrTypeChooserHelper(
152: mProject, mModel);
153: schemaHelper.populateNodes(rootNode);
154:
155: if (mPreviousSelectedComponent != null) {
156: Node node = schemaHelper
157: .selectNode(mPreviousSelectedComponent);
158: if (node != null) {
159: selectNode(node);
160: }
161: } else {
162: selectNode(rootNode);
163: }
164:
165: }
166:
167: private void selectNode(Node node) {
168: final Node finalNode = node;
169: Runnable run = new Runnable() {
170: public void run() {
171: if (manager != null) {
172: try {
173: manager.setExploredContextAndSelection(
174: finalNode, new Node[] { finalNode });
175: beanTreeView1.expandNode(finalNode);
176: } catch (PropertyVetoException ex) {
177: //ignore this
178: }
179:
180: }
181: }
182: };
183: SwingUtilities.invokeLater(run);
184: }
185:
186: public ExplorerManager getExplorerManager() {
187: return manager;
188: }
189:
190: public static final void main(String[] args) {
191: /* JFrame frame = new JFrame();
192: frame.add(new ElementOrTypeChooserEditorPanel());
193: frame.setVisible(true);
194: frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);*/
195: }
196:
197: public void apply() {
198: if (selectedComponent != null) {
199: if (selectedComponent instanceof GlobalType) {
200: selectedElementOrType = new ElementOrType(
201: (GlobalType) selectedComponent,
202: namespaceToPrefixMap);
203: } else if (selectedComponent instanceof GlobalElement) {
204: selectedElementOrType = new ElementOrType(
205: (GlobalElement) selectedComponent,
206: namespaceToPrefixMap);
207: }
208: }
209: }
210:
211: private ExplorerManager manager;
212: public static String PROP_ACTION_APPLY = "APPLY";
213: private ElementOrType selectedElementOrType;
214: private SchemaComponent selectedComponent;
215:
216: // Variables declaration - do not modify//GEN-BEGIN:variables
217: private org.openide.explorer.view.BeanTreeView beanTreeView1;
218:
219: // End of variables declaration//GEN-END:variables
220: class ExplorerPropertyChangeListener implements
221: PropertyChangeListener {
222:
223: public void propertyChange(PropertyChangeEvent evt) {
224: if (evt.getPropertyName().equals(
225: ExplorerManager.PROP_SELECTED_NODES)) {
226: Node[] nodes = (Node[]) evt.getNewValue();
227: if (nodes.length > 0) {
228: Node node = nodes[0];
229: //set the selected node to null and state as invalid by default
230: firePropertyChange(PROP_ACTION_APPLY, true, false);
231: SchemaComponent sc = null;
232: SchemaComponentReference reference = node
233: .getLookup().lookup(
234: SchemaComponentReference.class);
235: if (reference != null) {
236: sc = reference.get();
237: }
238: if (sc == null) {
239: sc = node.getLookup().lookup(
240: SchemaComponent.class);
241: }
242:
243: if (sc != null
244: && (sc instanceof GlobalType || sc instanceof GlobalElement)) {
245: selectedComponent = sc;
246: firePropertyChange(PROP_ACTION_APPLY, false,
247: true);
248: }
249: }
250: }
251: }
252: }
253:
254: public ElementOrType getSelectedComponent() {
255: return selectedElementOrType;
256: }
257:
258: public SchemaComponent getSelectedSchemaComponent() {
259: return selectedComponent;
260: }
261:
262: public void setSelectedComponent(ElementOrType selectedComponent) {
263: this.selectedElementOrType = selectedComponent;
264: }
265:
266: }
|