KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > cookies > ScenarioSupport


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.cookies;
20
21 import java.awt.Dialog JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import java.util.Vector JavaDoc;
24 import javax.swing.DefaultComboBoxModel JavaDoc;
25 import javax.swing.event.ListDataEvent JavaDoc;
26 import javax.swing.event.ListDataListener JavaDoc;
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 /**
39  * Implements the ScenarioCookie.
40  * This class manages the scenarios for a particular DataObject.
41  * The Scenarios are stored persistently in attribute of the primary FileObject
42  * of this data object.
43  *
44  * @author asgeir@dimonsoftware.com
45  */

46 public class ScenarioSupport implements ScenarioCookie {
47     
48 // public static final String OLD_SCENARIO_DATA_ATTRIBUTE = "org.netbeans.modules.xsl.scenario.ScenarioSupport.Data";
49
public static final String JavaDoc SCENARIO_DATA_ATTRIBUTE = "org.netbeans.modules.xml.core.cookies.ScenarioData";
50     
51     /** Owning dataObject */
52     private DataObject dataObject;
53     
54     /** The ComboBoxModel model which describes the available scenarios and the active scenario */
55     private DefaultComboBoxModel JavaDoc scenarioModel;
56     
57     /** The ScenarioData which will be stored persistently
58      * @link aggregation*/

59     private ScenarioData data;
60     
61     /** @link dependency */
62     /*# ScenarioPanel lnkScenarioPanel; */
63     
64     /** @link dependency */
65     /*# Scenario lnkScenario; */
66     
67     /** Creates a new instance of ScenarioSupport */
68     public ScenarioSupport(DataObject dataObject) {
69         this.dataObject = dataObject;
70     }
71     
72     /**
73      * Displays the Customize scenarios dialog
74      * @return true if the Customize dialog was pressed with the OK button,
75      * but false if it was closed with the Cancel button.
76      */

77     public boolean customizeScenarios() {
78         
79         DefaultComboBoxModel JavaDoc 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 JavaDoc[] { DialogDescriptor.OK_OPTION, DialogDescriptor.CANCEL_OPTION });
88         
89         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(scenariosDD);
90         
91         // inlined ScenarioPanel.addNotify() content
92
if (model.getSize() == 0) {
93             // Displays the add-scenario dialog is no scenarios are found.
94
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             // cancel all changes made
110
scenarioPanel.rollBackAddedAndRemovedScenarios();
111         }
112         
113         return false;
114     }
115     
116     /**
117      * Returns the ComboBoxModel that represents the a list of scenarios.
118      * The active scenario is the selected item in that ComboBoxModel
119      * @return The ComboBoxModel that represents the a list of scenarios.
120      * The active scenario is the selected item in that ComboBoxModel.
121      */

122     public DefaultComboBoxModel JavaDoc getModel() {
123         if (scenarioModel == null) {
124             // Create a new DefaultComboBoxModel which listens to selection changes
125
// and saves the data when selection is changed.
126
scenarioModel = new DefaultComboBoxModel JavaDoc(getData().getScenarios()) {
127                 public void setSelectedItem(Object JavaDoc anItem) {
128                     super.setSelectedItem(anItem);
129                     saveData();
130                 }
131             };
132             
133             // Set the initial selected item
134
scenarioModel.setSelectedItem(scenarioModel.getElementAt(getData().getActiveScenarioIndex()));
135             
136             // Listener to change events in the scenarioModel and save the
137
// data when the model changes.
138
scenarioModel.addListDataListener(new ListDataListener JavaDoc() {
139                 public void contentsChanged(ListDataEvent JavaDoc e) {
140                     saveData();
141                 }
142                 public void intervalAdded(ListDataEvent JavaDoc e) {
143                     saveData();
144                 }
145                 public void intervalRemoved(ListDataEvent JavaDoc e) {
146                     saveData();
147                 }
148             });
149         }
150         return scenarioModel;
151     }
152     
153     /**
154      * Executes an object using the active scenario.
155      * If no scenario is active, the Customize scenarios dialog is displayed
156      * before execution.
157      */

158     public void executeActiveScenario() {
159         DefaultComboBoxModel JavaDoc model = getModel();
160         Scenario activeScenario = (Scenario)model.getSelectedItem();
161         
162         if (activeScenario == null) {
163             // No scenario active -> let's display the customize dialog
164
if (customizeScenarios() == true) {
165                 activeScenario = (Scenario)model.getSelectedItem();
166             }
167         }
168         
169         if (activeScenario != null) {
170             activeScenario.execute(dataObject);
171         }
172     }
173     
174     /**
175      * Serialize the scenario model to the an attribute of the primary FileObject
176      * of the DataObject.
177      */

178     private void saveData() {
179         if (scenarioModel == null || data == null) {
180             return; // Nothing to save
181
}
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 JavaDoc e) {
191             String JavaDoc 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     /**
198      * Retuns the ScenarioData object which describes the scenarios for this
199      * DataObject. If it has not been loaded yet, it is loaded from an
200      * attribute of the primary FileObject.
201      * @return the ScenarioData object, never null.
202      */

203     private ScenarioData getData() {
204         if (data == null) {
205             FileObject fileObject = dataObject.getPrimaryFile();
206             if (fileObject != null) {
207                 Object JavaDoc 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