KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > scenario > NewScenarioPanel


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.core.scenario;
20
21 import java.awt.Dialog JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import org.openide.filesystems.FileObject;
24 import org.openide.filesystems.FileSystem;
25 import org.openide.filesystems.Repository;
26 import org.openide.loaders.FolderLookup;
27 import org.openide.util.Lookup;
28 import org.openide.loaders.DataObject;
29 import org.openide.loaders.DataObjectNotFoundException;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Vector JavaDoc;
33 import javax.swing.DefaultComboBoxModel JavaDoc;
34 import org.openide.DialogDescriptor;
35 import org.openide.NotifyDescriptor;
36 import org.openide.DialogDisplayer;
37
38 import org.openide.util.HelpCtx;
39 import org.netbeans.modules.xml.api.scenario.*;
40
41 /**
42  * A UI panel which allows the user to select a name and scenario type from
43  * a list of ScenarioFactories allowed for the requesting DataObject.
44  * Allowed ScenarioFactories are all registered ScenarioFactory objects
45  * where the isEnabled() method returns true.
46  * <p>
47  * Recommended entry point is (@link #addScenario} method.
48  *
49  * @author asgeir@dimonsoftware.com
50  */

51 public class NewScenarioPanel extends javax.swing.JPanel JavaDoc {
52     
53     private static final String JavaDoc FOLDER = "Plugins/XML/ScenarioFactories";// NOI18N
54

55     /** Creates new form NewScenarioPanel */
56     public NewScenarioPanel(String JavaDoc defaultName, DataObject dataObject) throws NoFactoriesException {
57         initComponents();
58         
59         nameField.setText(defaultName);
60         nameField.setSelectionStart(0);
61         nameField.setSelectionEnd(defaultName.length());
62        
63         try {
64             // Load the ScenarioFactories from the XML layer
65
FileSystem fs = Repository.getDefault().getDefaultFileSystem();
66             FileObject fo = fs.findResource(FOLDER);
67             if (fo == null) throw new NoFactoriesException();
68             DataObject df = DataObject.find(fo);
69             if (df instanceof DataObject.Container) {
70                 FolderLookup lookup =
71                     new FolderLookup((DataObject.Container) df);
72                 Lookup.Template template =
73                     new Lookup.Template(ScenarioFactory.class);
74                 Lookup.Result registrations = lookup.getLookup().lookup(template);
75                 
76                 // Only display enabled factories for this DataObject
77
Collection JavaDoc allFactories = registrations.allInstances();
78                 Vector JavaDoc allowedFactories = new Vector JavaDoc();
79                 Iterator JavaDoc iter = allFactories.iterator();
80                 while(iter.hasNext()) {
81                     ScenarioFactory factory = (ScenarioFactory)iter.next();
82                     if (factory.isEnabled(dataObject)) {
83                         allowedFactories.add(factory);
84                     }
85                 }
86                 
87                 if (allowedFactories.size() == 0) {
88                     throw new NoFactoriesException();
89                 }
90                 
91                 typeCombo.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(allowedFactories.toArray()));
92             } else {
93                 throw new NoFactoriesException();
94             }
95         } catch (DataObjectNotFoundException ex) {
96             throw new NoFactoriesException();
97         }
98     }
99     
100     /**
101      * Adds a new scenario to the scenario model (pupups UI).
102      * @param dataObject transformation for which is the scerio created
103      * @param scenarioModel output model
104      * @return Scenarion if a new scenario was added, else return <code>null</code>.
105      */

106     public static Scenario createScenario(DataObject dataObject, DefaultComboBoxModel JavaDoc scenarioModel) {
107         
108         // Find a unique name for the new scenario
109
String JavaDoc defaultName = null;
110         for (int nameInd = 1; defaultName == null; nameInd++) {
111             defaultName = Util.THIS.getString ("NAME_Scenario_default", Integer.toString(nameInd));
112             for (int ind = 0; ind < scenarioModel.getSize(); ind++) {
113                 if (defaultName.equals(((Scenario)scenarioModel.getElementAt(ind)).getName())) {
114                     defaultName = null;
115                     break;
116                 }
117             }
118         }
119         
120         // Create a new scenario
121
NewScenarioPanel newScenarioPanel;
122         try {
123             newScenarioPanel = new NewScenarioPanel(defaultName, dataObject);
124         } catch (NewScenarioPanel.NoFactoriesException e) {
125             NotifyDescriptor nd = new NotifyDescriptor.Message (e.getMessage(), NotifyDescriptor.INFORMATION_MESSAGE);
126             DialogDisplayer.getDefault().notify (nd);
127             return null;
128         }
129         
130         DialogDescriptor newDD = new DialogDescriptor
131             (newScenarioPanel,
132              Util.THIS.getString("NAME_New_scenario_dialog_title"), true,
133              DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION,
134              DialogDescriptor.BOTTOM_ALIGN,
135              new HelpCtx (NewScenarioPanel.class), null);
136         newDD.setClosingOptions (new Object JavaDoc[] { DialogDescriptor.OK_OPTION, DialogDescriptor.CANCEL_OPTION });
137         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog (newDD);
138         dialog.show();
139         
140         if (newDD.getValue() == DialogDescriptor.OK_OPTION) {
141             Scenario newScenario = newScenarioPanel.createScenario();
142             scenarioModel.addElement(newScenario);
143             return newScenario;
144         }
145         
146         return null;
147     }
148     
149     /** This method is called from within the constructor to
150      * initialize the form.
151      * WARNING: Do NOT modify this code. The content of this method is
152      * always regenerated by the Form Editor.
153      */

154     private void initComponents() {//GEN-BEGIN:initComponents
155
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
156
157         jLabel1 = new javax.swing.JLabel JavaDoc();
158         nameField = new javax.swing.JTextField JavaDoc();
159         jLabel2 = new javax.swing.JLabel JavaDoc();
160         typeCombo = new javax.swing.JComboBox JavaDoc();
161
162         setLayout(new java.awt.GridBagLayout JavaDoc());
163
164         jLabel1.setText("Name:");
165         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
166         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 10, 0, 10);
167         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
168         add(jLabel1, gridBagConstraints);
169
170         nameField.setToolTipText("null");
171         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
172         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
173         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 10);
174         gridBagConstraints.weightx = 1.0;
175         add(nameField, gridBagConstraints);
176
177         jLabel2.setText("Type:");
178         jLabel2.setToolTipText("null");
179         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
180         gridBagConstraints.gridy = 1;
181         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 10, 0, 10);
182         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
183         add(jLabel2, gridBagConstraints);
184
185         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
186         gridBagConstraints.gridy = 1;
187         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
188         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 10);
189         gridBagConstraints.weightx = 1.0;
190         add(typeCombo, gridBagConstraints);
191
192     }//GEN-END:initComponents
193

194     
195     // Variables declaration - do not modify//GEN-BEGIN:variables
196
private javax.swing.JComboBox JavaDoc typeCombo;
197     private javax.swing.JTextField JavaDoc nameField;
198     private javax.swing.JLabel JavaDoc jLabel2;
199     private javax.swing.JLabel JavaDoc jLabel1;
200
201     /** @link dependency
202      * @label looks up*/

203     /*# ScenarioFactory lnkScenarioFactory; */
204     // End of variables declaration//GEN-END:variables
205

206     public Scenario createScenario() {
207         ScenarioFactory factory = (ScenarioFactory)typeCombo.getSelectedItem();
208         if (factory != null) {
209             Scenario scenario = factory.createScenario();
210             scenario.setName(nameField.getText());
211             return scenario;
212         } else {
213             return null;
214         }
215     }
216     
217     public class NoFactoriesException extends Exception JavaDoc {
218         public NoFactoriesException() {
219             super(Util.THIS.getString("MSG_no_factories"));
220         }
221     }
222 }
223
Popular Tags