KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > actions > ScenarioAction


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.actions;
20
21 import javax.swing.JComboBox JavaDoc;
22
23 import java.awt.Dialog JavaDoc;
24 import javax.swing.ComboBoxModel JavaDoc;
25 import javax.swing.event.AncestorEvent JavaDoc;
26 import javax.swing.event.AncestorListener JavaDoc;
27 import org.openide.nodes.Node;
28 import org.openide.util.HelpCtx;
29 import org.openide.util.actions.CookieAction;
30 import org.openide.loaders.DataObject;
31
32 import javax.swing.event.ListDataListener JavaDoc;
33 import org.netbeans.modules.xml.api.cookies.ScenarioCookie;
34 import org.openide.windows.TopComponent;
35 import org.netbeans.modules.xml.core.actions.CollectXMLAction;
36
37 /**
38  * This is the action which is used to customize scenarios and select the active
39  * scenario. In a toolbar it is represented as ComboBox where the user can
40  * select the active scenario. The last item in the ComboBox will cause the
41  * ScenarioAction to be performed. Performing of this action will call
42  * customizeScenarios() on the ScenarioCookie of the selected node and it
43  * will display the Scenario customization dialog.
44  *
45  * @author asgeir@dimonsoftware.com
46  */

47 public class ScenarioAction extends CookieAction implements CollectXMLAction.XMLAction {
48     /** Serial Version UID */
49     private static final long serialVersionUID = -640535981015880507L;
50     
51     /** @link dependency */
52     /*# ScenarioCookie lnkScenarioCookie; */
53     
54     protected Class JavaDoc[] cookieClasses() {
55         return new Class JavaDoc[] {ScenarioCookie.class};
56     }
57     
58     protected int mode() {
59         return MODE_EXACTLY_ONE;
60     }
61     
62     protected void performAction(Node[] nodes) {
63         if (nodes.length > 0) {
64             ScenarioCookie scenarioCookie = (ScenarioCookie)nodes[0].getCookie(ScenarioCookie.class);
65             if (scenarioCookie != null) {
66                 scenarioCookie.customizeScenarios();
67             }
68         }
69     }
70     
71     public String JavaDoc getName() {
72         return Util.THIS.getString("LBL_Scenario_Action");
73     }
74     
75     protected String JavaDoc iconResource() {
76         return "org/netbeans/modules/xml/core/resources/scenarioAction.gif"; // NOI18N
77
}
78     
79     public java.awt.Component JavaDoc getToolbarPresenter() {
80         return new ToolbarPresenter();
81     }
82     
83     public HelpCtx getHelpCtx() {
84         return new HelpCtx (ScenarioAction.class);
85     }
86     
87     private class ToolbarPresenter extends JComboBox JavaDoc {
88         private static final int FIXED_WIDTH = 150;
89         
90         
91         public ToolbarPresenter() {
92             super();
93             addAncestorListener(new AncestorListener JavaDoc() {
94                 public void ancestorAdded(AncestorEvent JavaDoc event) {
95                     java.awt.Component JavaDoc parent = event.getAncestor();
96                     while (parent != null) {
97                         if (parent instanceof TopComponent) {
98                             Node[] nodes = ((TopComponent)parent).getActivatedNodes();
99                             if (nodes != null && nodes.length > 0) {
100                                 DataObject dataObj = (DataObject)nodes[0].getCookie(DataObject.class);
101                                 if (dataObj != null) {
102                                     ScenarioCookie scenarioCookie = (ScenarioCookie)dataObj.getCookie(ScenarioCookie.class);
103                                     if (scenarioCookie != null) {
104                                         ExScenarioModel model = new ExScenarioModel(scenarioCookie);
105                                         setModel(model);
106                                         setEnabled(true);
107                                         if (model.getSize() == 1) {
108                                             setSelectedIndex(-1);
109                                         }
110                                         model.setAllowCustomizing(true);
111                                         return;
112                                     }
113                                 }
114                             }
115                             setEnabled(false);
116                             break;
117                         }
118                         parent = parent.getParent();
119                     }
120                 }
121                 public void ancestorMoved(AncestorEvent JavaDoc event) {}
122                 public void ancestorRemoved(AncestorEvent JavaDoc event) {}
123             });
124         }
125         
126         public java.awt.Dimension JavaDoc getMinimumSize() {
127             // minimum width to prevent excessive horizontal shrinking
128
return new java.awt.Dimension JavaDoc(FIXED_WIDTH, getPreferredSize().height);
129         }
130         
131         public java.awt.Dimension JavaDoc getMaximumSize() {
132             // minimum width to prevent moving of the remaining contents of the toolbar
133
return new java.awt.Dimension JavaDoc(FIXED_WIDTH, getPreferredSize().height);
134         }
135     }
136     
137     
138     /**
139      * This class adds one item to the end of the ComboBoxModel which performs
140      * the scenario action when selected.
141      */

142     private class ExScenarioModel implements ComboBoxModel JavaDoc {
143         
144         private ScenarioCookie scenarioCookie;
145         
146         private ComboBoxModel JavaDoc scenarioModel;
147         
148         private String JavaDoc customizeValue;
149         
150         private boolean allowCustomizing = false;
151         
152         public ExScenarioModel(ScenarioCookie scenarioCookie) {
153             this.scenarioCookie = scenarioCookie;
154             this.scenarioModel = scenarioCookie.getModel();
155             this.customizeValue = "[ " + Util.THIS.getString("LBL_Scenario_Action") + "]";
156         }
157         
158         /** Specifies whether scenario customizer should be started when the customizeValue
159          * is selected. The default value is false */

160         public void setAllowCustomizing(boolean allow) {
161             allowCustomizing = allow;
162         }
163         
164         public Object JavaDoc getElementAt(int index) {
165             if (index == scenarioModel.getSize()) {
166                 return customizeValue;
167             } else {
168                 return scenarioModel.getElementAt(index);
169             }
170         }
171         
172         public void setSelectedItem(Object JavaDoc anObject) {
173             if (anObject == customizeValue && allowCustomizing) {
174                 scenarioCookie.customizeScenarios();
175             } else {
176                 scenarioModel.setSelectedItem(anObject);
177             }
178         }
179         
180         public int getSize() {
181             return scenarioModel.getSize() + 1;
182         }
183         
184         public void addListDataListener(ListDataListener JavaDoc l) {
185             scenarioModel.addListDataListener(l);
186         }
187         
188         public Object JavaDoc getSelectedItem() {
189             return scenarioModel.getSelectedItem();
190         }
191         
192         public void removeListDataListener(ListDataListener JavaDoc l) {
193             scenarioModel.removeListDataListener(l);
194         }
195     }
196 }
197
Popular Tags