1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.runtime.IConfigurationElement; 17 import org.eclipse.jface.action.IAction; 18 import org.eclipse.jface.util.IPropertyChangeListener; 19 import org.eclipse.jface.util.PropertyChangeEvent; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.swt.events.HelpEvent; 22 import org.eclipse.swt.events.HelpListener; 23 import org.eclipse.swt.widgets.Event; 24 import org.eclipse.ui.IActionDelegate; 25 import org.eclipse.ui.IWorkbenchPart; 26 import org.eclipse.ui.IWorkbenchWindow; 27 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 28 import org.eclipse.ui.WorkbenchException; 29 import org.eclipse.ui.actions.LabelRetargetAction; 30 import org.eclipse.ui.actions.RetargetAction; 31 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 32 33 40 public class WWinPluginAction extends PluginAction implements 41 IActionSetContributionItem { 42 45 private HelpListener localHelpListener; 46 47 private IWorkbenchWindow window; 48 49 private String actionSetId; 50 51 private RetargetAction retargetAction; 52 53 private static ArrayList staticActionList = new ArrayList (50); 54 55 63 public WWinPluginAction(IConfigurationElement actionElement, 64 IWorkbenchWindow window, String id, int style) { 65 super(actionElement, id, style); 66 this.window = window; 67 68 String retarget = actionElement 70 .getAttribute(IWorkbenchRegistryConstants.ATT_RETARGET); 71 if (retarget != null && Boolean.valueOf(retarget).booleanValue()) { 72 String allowLabelUpdate = actionElement 74 .getAttribute(IWorkbenchRegistryConstants.ATT_ALLOW_LABEL_UPDATE); 75 String label = actionElement 76 .getAttribute(IWorkbenchRegistryConstants.ATT_LABEL); 77 78 if (allowLabelUpdate != null && Boolean.valueOf(allowLabelUpdate).booleanValue()) { 79 retargetAction = new LabelRetargetAction(id, label, style); 80 } else { 81 retargetAction = new RetargetAction(id, label, style); 82 } 83 retargetAction 84 .addPropertyChangeListener(new IPropertyChangeListener() { 85 public void propertyChange(PropertyChangeEvent event) { 86 if (event.getProperty().equals(IAction.ENABLED)) { 87 Object val = event.getNewValue(); 88 if (val instanceof Boolean ) { 89 setEnabled(((Boolean ) val).booleanValue()); 90 } 91 } else if (event.getProperty().equals( 92 IAction.CHECKED)) { 93 Object val = event.getNewValue(); 94 if (val instanceof Boolean ) { 95 setChecked(((Boolean ) val).booleanValue()); 96 } 97 } else if (event.getProperty().equals(IAction.TEXT)) { 98 Object val = event.getNewValue(); 99 if (val instanceof String ) { 100 setText((String ) val); 101 } 102 } else if (event.getProperty().equals( 103 IAction.TOOL_TIP_TEXT)) { 104 Object val = event.getNewValue(); 105 if (val instanceof String ) { 106 setToolTipText((String ) val); 107 } 108 } 109 } 110 }); 111 retargetAction.setEnabled(false); 112 setEnabled(false); 113 window.getPartService().addPartListener(retargetAction); 114 IWorkbenchPart activePart = window.getPartService().getActivePart(); 115 if (activePart != null) { 116 retargetAction.partActivated(activePart); 117 } 118 } else { 119 window.getSelectionService().addSelectionListener(this); 121 refreshSelection(); 122 } 123 addToActionList(this); 124 125 super.setHelpListener(new HelpListener() { 126 public void helpRequested(HelpEvent e) { 127 HelpListener listener = null; 128 if (retargetAction != null) { 129 listener = retargetAction.getHelpListener(); 130 } 131 if (listener == null) { 132 listener = localHelpListener; 134 } 135 if (listener != null) { 136 listener.helpRequested(e); 138 } 139 } 140 }); 141 } 142 143 146 private static void addToActionList(WWinPluginAction action) { 147 staticActionList.add(action); 148 } 149 150 153 private static void removeFromActionList(WWinPluginAction action) { 154 staticActionList.remove(action); 155 } 156 157 160 public static void refreshActionList() { 161 Iterator iter = staticActionList.iterator(); 162 while (iter.hasNext()) { 163 WWinPluginAction action = (WWinPluginAction) iter.next(); 164 if ((action.getDelegate() == null) && action.isOkToCreateDelegate()) { 165 action.createDelegate(); 166 } 168 } 169 } 170 171 174 protected IActionDelegate validateDelegate(Object obj) 175 throws WorkbenchException { 176 if (obj instanceof IWorkbenchWindowActionDelegate) { 177 return (IWorkbenchWindowActionDelegate) obj; 178 } 179 180 throw new WorkbenchException( 181 "Action must implement IWorkbenchWindowActionDelegate"); } 183 184 187 protected void initDelegate() { 188 super.initDelegate(); 189 ((IWorkbenchWindowActionDelegate) getDelegate()).init(window); 190 } 191 192 195 public void dispose() { 196 removeFromActionList(this); 197 if (retargetAction != null) { 198 window.getPartService().removePartListener(retargetAction); 199 retargetAction.dispose(); 200 retargetAction = null; 201 } 202 window.getSelectionService().removeSelectionListener(this); 203 super.dispose(); 204 } 205 206 209 public String getActionSetId() { 210 return actionSetId; 211 } 212 213 219 public boolean isOkToCreateDelegate() { 220 return super.isOkToCreateDelegate() && window != null 221 && retargetAction == null; 222 } 223 224 227 public void runWithEvent(Event event) { 228 if (retargetAction == null) { 229 super.runWithEvent(event); 230 return; 231 } 232 233 if (event != null) { 234 retargetAction.runWithEvent(event); 235 } else { 236 retargetAction.run(); 237 } 238 } 239 240 243 public void setActionSetId(String newActionSetId) { 244 actionSetId = newActionSetId; 245 } 246 247 253 public void setHelpListener(HelpListener listener) { 254 localHelpListener = listener; 255 } 256 257 260 public void setChecked(boolean checked) { 261 super.setChecked(checked); 262 if (retargetAction != null) { 266 retargetAction.setChecked(checked); 267 } 268 } 269 270 273 protected void refreshSelection() { 274 ISelection selection = window.getSelectionService().getSelection(); 275 selectionChanged(selection); 276 } 277 } 278 | Popular Tags |