1 11 package org.eclipse.ui.actions; 12 13 import org.eclipse.core.commands.IHandlerAttributes; 14 import org.eclipse.jface.action.IAction; 15 import org.eclipse.jface.util.IPropertyChangeListener; 16 import org.eclipse.jface.util.PropertyChangeEvent; 17 import org.eclipse.swt.events.HelpEvent; 18 import org.eclipse.swt.events.HelpListener; 19 import org.eclipse.swt.widgets.Event; 20 import org.eclipse.ui.IActionBars; 21 import org.eclipse.ui.IWorkbenchPart; 22 import org.eclipse.ui.IWorkbenchPartSite; 23 import org.eclipse.ui.SubActionBars; 24 import org.eclipse.ui.internal.PartSite; 25 26 46 public class RetargetAction extends PartEventAction implements 47 ActionFactory.IWorkbenchAction { 48 49 52 private HelpListener localHelpListener; 53 54 private boolean enableAccelerator = true; 55 56 private IAction handler; 57 58 private IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() { 59 public void propertyChange(PropertyChangeEvent event) { 60 RetargetAction.this.propagateChange(event); 61 } 62 }; 63 64 70 public RetargetAction(String actionID, String text) { 71 this(actionID, text, IAction.AS_UNSPECIFIED); 72 } 73 74 84 public RetargetAction(String actionID, String text, int style) { 85 super(text, style); 86 setId(actionID); 87 setEnabled(false); 88 super.setHelpListener(new HelpListener() { 89 public void helpRequested(HelpEvent e) { 90 HelpListener listener = null; 91 if (handler != null) { 92 listener = handler.getHelpListener(); 94 if (listener == null) { 95 listener = localHelpListener; 97 } 98 } 99 if (listener != null) { 100 listener.helpRequested(e); 102 } 103 } 104 }); 105 } 106 107 110 public void dispose() { 111 if (handler != null) { 112 handler.removePropertyChangeListener(propertyChangeListener); 113 handler = null; 114 } 115 IWorkbenchPart part = getActivePart(); 116 if (part != null) { 117 IWorkbenchPartSite site = part.getSite(); 118 SubActionBars bars = (SubActionBars) ((PartSite) site).getActionBars(); 119 bars.removePropertyChangeListener(propertyChangeListener); 120 } 121 } 122 123 128 public void enableAccelerator(boolean b) { 129 enableAccelerator = b; 130 } 131 132 136 public int getAccelerator() { 137 if (enableAccelerator) { 138 return super.getAccelerator(); 139 } 140 return 0; 141 } 142 143 149 public void partActivated(IWorkbenchPart part) { 150 super.partActivated(part); 151 IWorkbenchPartSite site = part.getSite(); 152 SubActionBars bars = (SubActionBars) ((PartSite) site).getActionBars(); 153 bars.addPropertyChangeListener(propertyChangeListener); 154 setActionHandler(bars.getGlobalActionHandler(getId())); 155 } 156 157 162 public void partClosed(IWorkbenchPart part) { 163 IWorkbenchPart activePart = part.getSite().getPage().getActivePart(); 164 if (activePart != null) { 165 return; 168 } 169 if (part == getActivePart()) { 170 setActionHandler(null); 171 } 172 super.partClosed(part); 173 } 174 175 180 public void partDeactivated(IWorkbenchPart part) { 181 super.partDeactivated(part); 182 IWorkbenchPartSite site = part.getSite(); 183 SubActionBars bars = (SubActionBars) ((PartSite) site).getActionBars(); 184 bars.removePropertyChangeListener(propertyChangeListener); 185 186 IWorkbenchPart activePart = part.getSite().getPage().getActivePart(); 187 if (activePart != null) { 188 return; 191 } 192 193 setActionHandler(null); 194 } 195 196 200 protected void propagateChange(PropertyChangeEvent event) { 201 if (event.getProperty().equals(IAction.ENABLED)) { 202 Boolean bool = (Boolean ) event.getNewValue(); 203 setEnabled(bool.booleanValue()); 204 } else if (event.getProperty().equals(IAction.CHECKED)) { 205 Boolean bool = (Boolean ) event.getNewValue(); 206 setChecked(bool.booleanValue()); 207 } else if (event.getProperty().equals(SubActionBars.P_ACTION_HANDLERS)) { 208 if (event.getSource() instanceof IActionBars) { 209 IActionBars bars = (IActionBars) event.getSource(); 210 setActionHandler(bars.getGlobalActionHandler(getId())); 211 } 212 } 213 } 214 215 218 public void run() { 219 if (handler != null) { 220 handler.run(); 221 } 222 } 223 224 227 public void runWithEvent(Event event) { 228 if (handler != null) { 229 handler.runWithEvent(event); 230 } 231 } 232 233 240 public IAction getActionHandler() { 241 return handler; 242 } 243 244 public final boolean isHandled() { 245 return (handler != null); 246 } 247 248 251 protected void setActionHandler(IAction newHandler) { 252 if (newHandler == handler) { 254 return; 255 } 256 257 if (handler != null) { 259 handler.removePropertyChangeListener(propertyChangeListener); 260 handler = null; 261 } 262 263 IAction oldHandler = handler; 265 handler = newHandler; 266 if (handler == null) { 267 setEnabled(false); 268 if (getStyle() == AS_CHECK_BOX || getStyle() == AS_RADIO_BUTTON) { 269 setChecked(false); 270 } 271 } else { 272 setEnabled(handler.isEnabled()); 273 if (getStyle() == AS_CHECK_BOX || getStyle() == AS_RADIO_BUTTON) { 274 setChecked(handler.isChecked()); 275 } 276 handler.addPropertyChangeListener(propertyChangeListener); 277 } 278 279 firePropertyChange(IHandlerAttributes.ATTRIBUTE_HANDLED, oldHandler, 281 newHandler); 282 } 283 284 287 public void setChecked(boolean checked) { 288 super.setChecked(checked); 289 if (handler != null) { 293 handler.setChecked(checked); 294 } 295 } 296 297 302 public void setHelpListener(HelpListener listener) { 303 localHelpListener = listener; 304 } 305 306 313 public final String toString() { 314 final StringBuffer buffer = new StringBuffer (); 315 316 buffer.append("RetargetAction("); buffer.append(getId()); 318 buffer.append(')'); 319 320 return buffer.toString(); 321 } 322 } 323 | Popular Tags |