1 19 package org.netbeans.modules.xml.core.scenario; 20 21 import java.awt.Dialog ; 22 import java.util.Iterator ; 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 ; 31 import java.util.Collection ; 32 import java.util.Vector ; 33 import javax.swing.DefaultComboBoxModel ; 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 51 public class NewScenarioPanel extends javax.swing.JPanel { 52 53 private static final String FOLDER = "Plugins/XML/ScenarioFactories"; 55 56 public NewScenarioPanel(String 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 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 Collection allFactories = registrations.allInstances(); 78 Vector allowedFactories = new Vector (); 79 Iterator 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 (allowedFactories.toArray())); 92 } else { 93 throw new NoFactoriesException(); 94 } 95 } catch (DataObjectNotFoundException ex) { 96 throw new NoFactoriesException(); 97 } 98 } 99 100 106 public static Scenario createScenario(DataObject dataObject, DefaultComboBoxModel scenarioModel) { 107 108 String 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 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 [] { DialogDescriptor.OK_OPTION, DialogDescriptor.CANCEL_OPTION }); 137 Dialog 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 154 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 156 157 jLabel1 = new javax.swing.JLabel (); 158 nameField = new javax.swing.JTextField (); 159 jLabel2 = new javax.swing.JLabel (); 160 typeCombo = new javax.swing.JComboBox (); 161 162 setLayout(new java.awt.GridBagLayout ()); 163 164 jLabel1.setText("Name:"); 165 gridBagConstraints = new java.awt.GridBagConstraints (); 166 gridBagConstraints.insets = new java.awt.Insets (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 (); 172 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 173 gridBagConstraints.insets = new java.awt.Insets (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 (); 180 gridBagConstraints.gridy = 1; 181 gridBagConstraints.insets = new java.awt.Insets (0, 10, 0, 10); 182 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 183 add(jLabel2, gridBagConstraints); 184 185 gridBagConstraints = new java.awt.GridBagConstraints (); 186 gridBagConstraints.gridy = 1; 187 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 188 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 10); 189 gridBagConstraints.weightx = 1.0; 190 add(typeCombo, gridBagConstraints); 191 192 } 194 195 private javax.swing.JComboBox typeCombo; 197 private javax.swing.JTextField nameField; 198 private javax.swing.JLabel jLabel2; 199 private javax.swing.JLabel jLabel1; 200 201 203 204 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 { 218 public NoFactoriesException() { 219 super(Util.THIS.getString("MSG_no_factories")); 220 } 221 } 222 } 223 | Popular Tags |