1 19 package org.netbeans.modules.xml.core.scenario; 20 21 import java.awt.Component ; 22 import java.awt.Container ; 23 import org.openide.DialogDescriptor; 24 import org.openide.util.HelpCtx; 25 26 import org.netbeans.modules.xml.api.scenario.*; 27 28 import java.awt.Dialog ; 29 import java.util.HashMap ; 30 import java.util.Vector ; 31 import javax.swing.DefaultComboBoxModel ; 32 import javax.swing.JLabel ; 33 import javax.swing.JPanel ; 34 import javax.swing.JTextField ; 35 import javax.swing.event.ChangeEvent ; 36 import javax.swing.event.ChangeListener ; 37 import org.openide.NotifyDescriptor; 38 import org.openide.DialogDisplayer; 39 import org.openide.loaders.DataObject; 40 41 46 public class ScenarioPanel extends javax.swing.JPanel { 47 48 49 private DefaultComboBoxModel scenarioModel; 50 51 53 private Scenario selectedScenario; 54 55 56 private Scenario selectedScenarioAtStartup; 57 58 59 private DataObject dataObject; 60 61 62 private HashMap uiCompMap = new HashMap (); 63 64 65 public ScenarioPanel(DataObject dataObject, DefaultComboBoxModel scenarioModel) { 66 this.scenarioModel = scenarioModel; 67 this.dataObject = dataObject; 68 69 initComponents(); 70 71 scenarioList.setModel(scenarioModel); 72 selectedScenarioAtStartup = selectedScenario = (Scenario)scenarioModel.getSelectedItem(); 73 scenarioList.setSelectedValue(selectedScenario, true); 74 75 } 76 77 public boolean createScenario() { 78 Scenario s = NewScenarioPanel.createScenario(dataObject, scenarioModel); 79 if (s != null) { 80 scenarioList.setSelectedValue(s, true); 81 enableControls(); 82 m_addedScenarios.add(s); 83 return true; 84 } 85 86 return false; 87 } 88 89 public void rollBackAddedAndRemovedScenarios() { 90 for(int i=0; i<m_deletedScenarios.size(); i++) { 91 Scenario scen = (Scenario)m_deletedScenarios.get(i); 93 scenarioModel.addElement(scen); 94 } 95 m_deletedScenarios.clear(); 96 for(int i=0; i<m_addedScenarios.size(); i++) { 97 Scenario scen = (Scenario)m_addedScenarios.get(i); 99 scenarioModel.removeElement(scen); 100 } 101 m_addedScenarios.clear(); 102 scenarioModel.setSelectedItem(selectedScenarioAtStartup); 103 } 104 105 110 private void initComponents() { jPanel1 = new javax.swing.JPanel (); 112 jScrollPane1 = new javax.swing.JScrollPane (); 113 scenarioList = new javax.swing.JList (); 114 jPanel2 = new javax.swing.JPanel (); 115 addButton = new javax.swing.JButton (); 116 removeButton = new javax.swing.JButton (); 117 renameButton = new javax.swing.JButton (); 118 curScenarioPanel = new javax.swing.JPanel (); 119 120 setLayout(new java.awt.BorderLayout ()); 121 122 jPanel1.setLayout(new java.awt.BorderLayout ()); 123 124 jPanel1.setBorder(new javax.swing.border.TitledBorder ("Scenarios")); 125 jPanel1.setMinimumSize(new java.awt.Dimension (172, 100)); 126 jPanel1.setPreferredSize(new java.awt.Dimension (172, 150)); 127 jScrollPane1.setPreferredSize(new java.awt.Dimension (100, 70)); 128 scenarioList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); 129 scenarioList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 130 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 131 scenarioListValueChanged(evt); 132 } 133 }); 134 135 jScrollPane1.setViewportView(scenarioList); 136 137 jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER); 138 139 jPanel2.setLayout(new java.awt.GridLayout (2, 0)); 140 141 addButton.setText("New"); 142 addButton.setToolTipText("Create new scenario"); 143 addButton.addActionListener(new java.awt.event.ActionListener () { 144 public void actionPerformed(java.awt.event.ActionEvent evt) { 145 addButtonActionPerformed(evt); 146 } 147 }); 148 149 jPanel2.add(addButton); 150 151 removeButton.setText("Delete"); 152 removeButton.setToolTipText("Delete selected scenario"); 153 removeButton.addActionListener(new java.awt.event.ActionListener () { 154 public void actionPerformed(java.awt.event.ActionEvent evt) { 155 removeButtonActionPerformed(evt); 156 } 157 }); 158 159 jPanel2.add(removeButton); 160 161 renameButton.setText("Rename"); 162 renameButton.setToolTipText("Rename selected scenario"); 163 renameButton.addActionListener(new java.awt.event.ActionListener () { 164 public void actionPerformed(java.awt.event.ActionEvent evt) { 165 renameButtonActionPerformed(evt); 166 } 167 }); 168 169 jPanel2.add(renameButton); 170 171 jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH); 172 173 add(jPanel1, java.awt.BorderLayout.WEST); 174 175 curScenarioPanel.setLayout(new java.awt.BorderLayout ()); 176 177 curScenarioPanel.setBorder(new javax.swing.border.TitledBorder ("Options")); 178 curScenarioPanel.setMinimumSize(new java.awt.Dimension (450, 200)); 179 curScenarioPanel.setPreferredSize(new java.awt.Dimension (450, 200)); 180 add(curScenarioPanel, java.awt.BorderLayout.CENTER); 181 182 } 184 private void renameButtonActionPerformed(java.awt.event.ActionEvent evt) { if (selectedScenario != null) { 186 NamePanel namePanel = new NamePanel(); 187 namePanel.setName(selectedScenario.getName()); 188 189 DialogDescriptor dialogDecr = new DialogDescriptor(namePanel, "Enter new name"); 190 Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDecr); 191 dialog.show(); 192 193 if (dialogDecr.getValue() == DialogDescriptor.OK_OPTION) { 194 selectedScenario.setName(namePanel.getName()); 195 scenarioList.revalidate(); 196 scenarioList.repaint(); 197 } 198 199 } 200 } 202 private void scenarioListValueChanged(javax.swing.event.ListSelectionEvent evt) { if (!evt.getValueIsAdjusting()) { 204 curScenarioPanel.removeAll(); 209 210 Scenario scenario = (Scenario)scenarioList.getSelectedValue(); 211 if (scenario != null) { 212 scenarioModel.setSelectedItem(scenario); 213 selectedScenario = scenario; 214 Component comp = (Component )uiCompMap.get(selectedScenario); 215 if (comp == null) { 216 comp = scenario.getCustomizer(dataObject,false); 217 uiCompMap.put(selectedScenario, comp); 218 } 219 curScenarioPanel.add(comp, java.awt.BorderLayout.CENTER); 220 } else { 221 scenarioModel.setSelectedItem(null); 222 selectedScenario = null; 223 } 224 225 revalidate(); 226 repaint(); 227 enableControls(); 228 } 229 } 231 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { 233 int selInd = scenarioModel.getIndexOf(selectedScenario); 234 if (selInd == scenarioModel.getSize() - 1) { 235 selInd--; 236 } 237 238 Object toRemove = selectedScenario; 239 if(!m_addedScenarios.contains(toRemove)) { 240 m_deletedScenarios.add(toRemove); 242 } else { 243 m_addedScenarios.remove(toRemove); 245 } 246 scenarioList.clearSelection(); 247 scenarioModel.removeElement(toRemove); 248 249 if (selInd >= 0) { 250 scenarioList.setSelectedIndex(selInd); 251 } 252 253 enableControls(); 254 255 } 257 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { createScenario(); 259 } 261 262 263 264 private Vector m_deletedScenarios = new Vector (); 265 266 private Vector m_addedScenarios = new Vector (); 267 268 private javax.swing.JScrollPane jScrollPane1; 270 private javax.swing.JPanel curScenarioPanel; 271 private javax.swing.JPanel jPanel2; 272 private javax.swing.JList scenarioList; 273 private javax.swing.JPanel jPanel1; 274 private javax.swing.JButton addButton; 275 private javax.swing.JButton renameButton; 276 private javax.swing.JButton removeButton; 277 279 280 private void enableControls() { 281 removeButton.setEnabled(selectedScenario != null); 282 renameButton.setEnabled(selectedScenario != null); 283 } 284 285 286 private class NamePanel extends JPanel { 287 private JTextField nameField; 288 289 public NamePanel() { 290 add(new JLabel (Util.THIS.getString("LBL_Rename_panel_name"))); 291 292 nameField = new JTextField (30); 293 add(nameField); 294 } 295 296 public void setName(String name) { 297 nameField.setText(name); 298 } 299 300 public String getName() { 301 return nameField.getText(); 302 } 303 } 304 } 305 | Popular Tags |