1 19 package org.netbeans.modules.xml.core.cookies; 20 21 import java.awt.Dialog ; 22 import java.io.Serializable ; 23 import java.util.Vector ; 24 import javax.swing.DefaultComboBoxModel ; 25 import javax.swing.event.ListDataEvent ; 26 import javax.swing.event.ListDataListener ; 27 28 import org.netbeans.modules.xml.api.scenario.Scenario; 29 import org.netbeans.modules.xml.core.scenario.ScenarioPanel; 30 import org.openide.DialogDescriptor; 31 import org.openide.NotifyDescriptor; 32 import org.openide.DialogDisplayer; 33 import org.openide.filesystems.FileObject; 34 import org.openide.loaders.DataObject; 35 import org.openide.util.HelpCtx; 36 import org.netbeans.modules.xml.api.cookies.ScenarioCookie; 37 38 46 public class ScenarioSupport implements ScenarioCookie { 47 48 public static final String SCENARIO_DATA_ATTRIBUTE = "org.netbeans.modules.xml.core.cookies.ScenarioData"; 50 51 52 private DataObject dataObject; 53 54 55 private DefaultComboBoxModel scenarioModel; 56 57 59 private ScenarioData data; 60 61 62 63 64 65 66 67 68 public ScenarioSupport(DataObject dataObject) { 69 this.dataObject = dataObject; 70 } 71 72 77 public boolean customizeScenarios() { 78 79 DefaultComboBoxModel model = getModel(); 80 ScenarioPanel scenarioPanel = new ScenarioPanel(dataObject, model); 81 DialogDescriptor scenariosDD = new DialogDescriptor 82 (scenarioPanel, 83 Util.THIS.getString("NAME_customize_scenarios_title"), true, 84 DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, 85 DialogDescriptor.BOTTOM_ALIGN, 86 new HelpCtx(ScenarioSupport.class), null); 87 scenariosDD.setClosingOptions(new Object [] { DialogDescriptor.OK_OPTION, DialogDescriptor.CANCEL_OPTION }); 88 89 Dialog dialog = DialogDisplayer.getDefault().createDialog(scenariosDD); 90 91 if (model.getSize() == 0) { 93 if (!scenarioPanel.createScenario()) { 95 return false; 96 } 97 } 98 99 dialog.show(); 100 101 if (scenariosDD.getValue() == DialogDescriptor.OK_OPTION) { 102 for (int ind = 0; ind < model.getSize(); ind++) { 103 Scenario scenario = (Scenario)model.getElementAt(ind); 104 scenario.saveChanges(); 105 } 106 saveData(); 107 return true; 108 } else { 109 scenarioPanel.rollBackAddedAndRemovedScenarios(); 111 } 112 113 return false; 114 } 115 116 122 public DefaultComboBoxModel getModel() { 123 if (scenarioModel == null) { 124 scenarioModel = new DefaultComboBoxModel (getData().getScenarios()) { 127 public void setSelectedItem(Object anItem) { 128 super.setSelectedItem(anItem); 129 saveData(); 130 } 131 }; 132 133 scenarioModel.setSelectedItem(scenarioModel.getElementAt(getData().getActiveScenarioIndex())); 135 136 scenarioModel.addListDataListener(new ListDataListener () { 139 public void contentsChanged(ListDataEvent e) { 140 saveData(); 141 } 142 public void intervalAdded(ListDataEvent e) { 143 saveData(); 144 } 145 public void intervalRemoved(ListDataEvent e) { 146 saveData(); 147 } 148 }); 149 } 150 return scenarioModel; 151 } 152 153 158 public void executeActiveScenario() { 159 DefaultComboBoxModel model = getModel(); 160 Scenario activeScenario = (Scenario)model.getSelectedItem(); 161 162 if (activeScenario == null) { 163 if (customizeScenarios() == true) { 165 activeScenario = (Scenario)model.getSelectedItem(); 166 } 167 } 168 169 if (activeScenario != null) { 170 activeScenario.execute(dataObject); 171 } 172 } 173 174 178 private void saveData() { 179 if (scenarioModel == null || data == null) { 180 return; } 182 183 try { 184 data.setActiveScenarioIndex(scenarioModel.getIndexOf(scenarioModel.getSelectedItem())); 185 186 FileObject fileObject = dataObject.getPrimaryFile(); 187 if (fileObject != null) { 188 fileObject.setAttribute(SCENARIO_DATA_ATTRIBUTE, data); 189 } 190 } catch(java.io.IOException e) { 191 String message = Util.THIS.getString("MSG_Problems_saving_scenarios_persistently") + "\n" + e.getMessage(); 192 NotifyDescriptor nd = new NotifyDescriptor.Message(message, NotifyDescriptor.WARNING_MESSAGE); 193 DialogDisplayer.getDefault().notify(nd); 194 } 195 } 196 197 203 private ScenarioData getData() { 204 if (data == null) { 205 FileObject fileObject = dataObject.getPrimaryFile(); 206 if (fileObject != null) { 207 Object objData = fileObject.getAttribute(SCENARIO_DATA_ATTRIBUTE); 208 if (objData instanceof ScenarioData) { 209 data = (ScenarioData) fileObject.getAttribute(SCENARIO_DATA_ATTRIBUTE); 210 } 211 } 212 if (data == null ) { 213 data = new ScenarioData(); 214 } 215 } 216 return data; 217 } 218 } 219 | Popular Tags |