KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
22 import java.awt.Container JavaDoc;
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 JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Vector JavaDoc;
31 import javax.swing.DefaultComboBoxModel JavaDoc;
32 import javax.swing.JLabel JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.JTextField JavaDoc;
35 import javax.swing.event.ChangeEvent JavaDoc;
36 import javax.swing.event.ChangeListener JavaDoc;
37 import org.openide.NotifyDescriptor;
38 import org.openide.DialogDisplayer;
39 import org.openide.loaders.DataObject;
40
41 /**
42  * This panel allows the user to edit scenarios for a particular DataObject.
43  *
44  * @author asgeir@dimonsoftware.com
45  */

46 public class ScenarioPanel extends javax.swing.JPanel JavaDoc {
47     
48     /** The list model which represents the scenarios */
49     private DefaultComboBoxModel JavaDoc scenarioModel;
50     
51     /** The selected scenario, one of the Scenario objects in scenarioModel
52      * @supplierCardinality 0..**/

53     private Scenario selectedScenario;
54     
55     /** The svenario that was selected at startup */
56     private Scenario selectedScenarioAtStartup;
57     
58     /** The DataObject which owns the scenarios */
59     private DataObject dataObject;
60     
61     /** Mapping from scenario to a UI component for that scenario */
62     private HashMap JavaDoc uiCompMap = new HashMap JavaDoc();
63     
64     /** Creates new form ScenarioPanel */
65     public ScenarioPanel(DataObject dataObject, DefaultComboBoxModel JavaDoc 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             // put deleted scenarios back in
92
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             // remove added scenarios
98
Scenario scen = (Scenario)m_addedScenarios.get(i);
99             scenarioModel.removeElement(scen);
100         }
101         m_addedScenarios.clear();
102         scenarioModel.setSelectedItem(selectedScenarioAtStartup);
103     }
104
105    /** This method is called from within the constructor to
106      * initialize the form.
107      * WARNING: Do NOT modify this code. The content of this method is
108      * always regenerated by the Form Editor.
109      */

110     private void initComponents() {//GEN-BEGIN:initComponents
111
jPanel1 = new javax.swing.JPanel JavaDoc();
112         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
113         scenarioList = new javax.swing.JList JavaDoc();
114         jPanel2 = new javax.swing.JPanel JavaDoc();
115         addButton = new javax.swing.JButton JavaDoc();
116         removeButton = new javax.swing.JButton JavaDoc();
117         renameButton = new javax.swing.JButton JavaDoc();
118         curScenarioPanel = new javax.swing.JPanel JavaDoc();
119
120         setLayout(new java.awt.BorderLayout JavaDoc());
121
122         jPanel1.setLayout(new java.awt.BorderLayout JavaDoc());
123
124         jPanel1.setBorder(new javax.swing.border.TitledBorder JavaDoc("Scenarios"));
125         jPanel1.setMinimumSize(new java.awt.Dimension JavaDoc(172, 100));
126         jPanel1.setPreferredSize(new java.awt.Dimension JavaDoc(172, 150));
127         jScrollPane1.setPreferredSize(new java.awt.Dimension JavaDoc(100, 70));
128         scenarioList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
129         scenarioList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
130             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc 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 JavaDoc(2, 0));
140
141         addButton.setText("New");
142         addButton.setToolTipText("Create new scenario");
143         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
144             public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc() {
154             public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc() {
164             public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc());
176
177         curScenarioPanel.setBorder(new javax.swing.border.TitledBorder JavaDoc("Options"));
178         curScenarioPanel.setMinimumSize(new java.awt.Dimension JavaDoc(450, 200));
179         curScenarioPanel.setPreferredSize(new java.awt.Dimension JavaDoc(450, 200));
180         add(curScenarioPanel, java.awt.BorderLayout.CENTER);
181
182     }//GEN-END:initComponents
183

184     private void renameButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_renameButtonActionPerformed
185
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 JavaDoc 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     }//GEN-LAST:event_renameButtonActionPerformed
201

202     private void scenarioListValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_scenarioListValueChanged
203
if (!evt.getValueIsAdjusting()) {
204             // this event is called twice each time a new item is selected, both when the mouse is
205
// pressed and when it is released, with this check the new panel is only created when
206
// the button is released (the other way around won't work, because then the panel wont show
207
// up at dialog startup) - Swing bug?
208
curScenarioPanel.removeAll();
209             
210             Scenario scenario = (Scenario)scenarioList.getSelectedValue();
211             if (scenario != null) {
212                 scenarioModel.setSelectedItem(scenario);
213                 selectedScenario = scenario;
214                 Component JavaDoc comp = (Component JavaDoc)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     }//GEN-LAST:event_scenarioListValueChanged
230

231     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
232

233         int selInd = scenarioModel.getIndexOf(selectedScenario);
234         if (selInd == scenarioModel.getSize() - 1) {
235             selInd--;
236         }
237         
238         Object JavaDoc toRemove = selectedScenario;
239         if(!m_addedScenarios.contains(toRemove)) {
240             // if item existed befor dialog started up, add to list of deleted items
241
m_deletedScenarios.add(toRemove);
242         } else {
243             // it item was added after dialog startup, remove from list of added items
244
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     }//GEN-LAST:event_removeButtonActionPerformed
256

257     private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
258
createScenario();
259     }//GEN-LAST:event_addButtonActionPerformed
260

261
262     
263     /** A list of Scenarios deleted since dialog startup, needed to rollback actions when "Cancel" pressed */
264     private Vector JavaDoc m_deletedScenarios = new Vector JavaDoc();
265     /** A list of Scenarios added since dialog startup, needed to rollback actions when "Cancel" pressed */
266     private Vector JavaDoc m_addedScenarios = new Vector JavaDoc();
267     
268     // Variables declaration - do not modify//GEN-BEGIN:variables
269
private javax.swing.JScrollPane JavaDoc jScrollPane1;
270     private javax.swing.JPanel JavaDoc curScenarioPanel;
271     private javax.swing.JPanel JavaDoc jPanel2;
272     private javax.swing.JList JavaDoc scenarioList;
273     private javax.swing.JPanel JavaDoc jPanel1;
274     private javax.swing.JButton JavaDoc addButton;
275     private javax.swing.JButton JavaDoc renameButton;
276     private javax.swing.JButton JavaDoc removeButton;
277     // End of variables declaration//GEN-END:variables
278

279     
280     private void enableControls() {
281         removeButton.setEnabled(selectedScenario != null);
282         renameButton.setEnabled(selectedScenario != null);
283     }
284     
285     
286     private class NamePanel extends JPanel JavaDoc {
287         private JTextField JavaDoc nameField;
288         
289         public NamePanel() {
290             add(new JLabel JavaDoc(Util.THIS.getString("LBL_Rename_panel_name")));
291             
292             nameField = new JTextField JavaDoc(30);
293             add(nameField);
294         }
295         
296         public void setName(String JavaDoc name) {
297             nameField.setText(name);
298         }
299         
300         public String JavaDoc getName() {
301             return nameField.getText();
302         }
303     }
304 }
305
Popular Tags