1 19 package org.netbeans.modules.xml.core.actions; 20 21 import javax.swing.JComboBox ; 22 23 import java.awt.Dialog ; 24 import javax.swing.ComboBoxModel ; 25 import javax.swing.event.AncestorEvent ; 26 import javax.swing.event.AncestorListener ; 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 ; 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 47 public class ScenarioAction extends CookieAction implements CollectXMLAction.XMLAction { 48 49 private static final long serialVersionUID = -640535981015880507L; 50 51 52 53 54 protected Class [] cookieClasses() { 55 return new Class [] {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 getName() { 72 return Util.THIS.getString("LBL_Scenario_Action"); 73 } 74 75 protected String iconResource() { 76 return "org/netbeans/modules/xml/core/resources/scenarioAction.gif"; } 78 79 public java.awt.Component 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 { 88 private static final int FIXED_WIDTH = 150; 89 90 91 public ToolbarPresenter() { 92 super(); 93 addAncestorListener(new AncestorListener () { 94 public void ancestorAdded(AncestorEvent event) { 95 java.awt.Component 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 event) {} 122 public void ancestorRemoved(AncestorEvent event) {} 123 }); 124 } 125 126 public java.awt.Dimension getMinimumSize() { 127 return new java.awt.Dimension (FIXED_WIDTH, getPreferredSize().height); 129 } 130 131 public java.awt.Dimension getMaximumSize() { 132 return new java.awt.Dimension (FIXED_WIDTH, getPreferredSize().height); 134 } 135 } 136 137 138 142 private class ExScenarioModel implements ComboBoxModel { 143 144 private ScenarioCookie scenarioCookie; 145 146 private ComboBoxModel scenarioModel; 147 148 private String 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 160 public void setAllowCustomizing(boolean allow) { 161 allowCustomizing = allow; 162 } 163 164 public Object 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 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 l) { 185 scenarioModel.addListDataListener(l); 186 } 187 188 public Object getSelectedItem() { 189 return scenarioModel.getSelectedItem(); 190 } 191 192 public void removeListDataListener(ListDataListener l) { 193 scenarioModel.removeListDataListener(l); 194 } 195 } 196 } 197 | Popular Tags |