1 /******************************************************************************* 2 * Copyright (c) 2002 International Business Machines Corp. and others. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Common Public License v0.5 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/cpl-v05.html 7 * 8 * Contributors: 9 * IBM Corporation - initial API and implementation 10 ******************************************************************************/ 11 package net.sourceforge.phpdt.ui.actions; 12 13 import org.eclipse.jface.action.Action; 14 import org.eclipse.jface.text.ITextSelection; 15 import org.eclipse.jface.util.Assert; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.jface.viewers.ISelectionChangedListener; 18 import org.eclipse.jface.viewers.ISelectionProvider; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.jface.viewers.SelectionChangedEvent; 21 import org.eclipse.swt.widgets.Shell; 22 import org.eclipse.ui.IWorkbenchSite; 23 24 /** 25 * Action that dispatches the <code>IAction#run()</code> and the 26 * <code>ISelectionChangedListener#selectionChanged</code> according to the 27 * type of the selection. 28 * 29 * <ul> 30 * <li>if selection is of type <code>ITextSelection</code> then 31 * <code>run(ITextSelection)</code> and 32 * <code>selectionChanged(ITextSelection)</code> is called.</li> 33 * <li>if selection is of type <code>IStructuredSelection</code> then 34 * <code>run(IStructuredSelection)</code> and <code> 35 * selectionChanged(IStructuredSelection)</code> 36 * is called.</li> 37 * <li>default is to call <code>run(ISelection)</code> and <code> 38 * selectionChanged(ISelection)</code>.</li> 39 * </ul> 40 * 41 * <p> 42 * Note: This class is not intended to be subclassed outside the JDT UI plugin. 43 * </p> 44 * 45 * @since 2.0 46 */ 47 public abstract class SelectionDispatchAction extends Action implements 48 ISelectionChangedListener { 49 50 private IWorkbenchSite fSite; 51 52 /** 53 * Creates a new action with no text and no image. 54 * <p> 55 * Configure the action later using the set methods. 56 * </p> 57 * 58 * @param site 59 * the site this action is working on 60 */ SelectionDispatchAction(IWorkbenchSite site)61 protected SelectionDispatchAction(IWorkbenchSite site) { 62 Assert.isNotNull(site); 63 fSite = site; 64 } 65 66 /** 67 * Returns the site owning this action. 68 * 69 * @return the site owning this action 70 */ getSite()71 public IWorkbenchSite getSite() { 72 return fSite; 73 } 74 75 /** 76 * Returns the selection provided by the site owning this action. 77 * 78 * @return the site's selection 79 */ getSelection()80 public ISelection getSelection() { 81 return getSelectionProvider().getSelection(); 82 } 83 84 /** 85 * Returns the shell provided by the site owning this action. 86 * 87 * @return the site's shell 88 */ getShell()89 public Shell getShell() { 90 return fSite.getShell(); 91 } 92 93 /** 94 * Returns the selection provider managed by the site owning this action. 95 * 96 * @return the site's selection provider 97 */ getSelectionProvider()98 public ISelectionProvider getSelectionProvider() { 99 return fSite.getSelectionProvider(); 100 } 101 102 /** 103 * Updates the action's enablement state according to the given selection. 104 * This default implementation calls one of the 105 * <code>selectionChanged</code> methods depending on the type of the 106 * passed selection. 107 * 108 * @param selection 109 * the selection this action is working on 110 */ update(ISelection selection)111 public void update(ISelection selection) { 112 dispatchSelectionChanged(selection); 113 } 114 115 /** 116 * Notifies this action that the given structured selection has changed. 117 * This default implementation calls 118 * <code>selectionChanged(ISelection selection)</code>. 119 * 120 * @param selection 121 * the new selection 122 */ selectionChanged(IStructuredSelection selection)123 protected void selectionChanged(IStructuredSelection selection) { 124 selectionChanged((ISelection) selection); 125 } 126 127 /** 128 * Executes this actions with the given structured selection. This default 129 * implementation calls <code>run(ISelection selection)</code>. 130 */ run(IStructuredSelection selection)131 protected void run(IStructuredSelection selection) { 132 run((ISelection) selection); 133 } 134 135 /** 136 * Notifies this action that the given text selection has changed. This 137 * default implementation calls 138 * <code>selectionChanged(ISelection selection)</code>. 139 * 140 * @param selection 141 * the new selection 142 */ selectionChanged(ITextSelection selection)143 protected void selectionChanged(ITextSelection selection) { 144 selectionChanged((ISelection) selection); 145 } 146 147 /** 148 * Executes this actions with the given text selection. This default 149 * implementation calls <code>run(ISelection selection)</code>. 150 */ run(ITextSelection selection)151 protected void run(ITextSelection selection) { 152 run((ISelection) selection); 153 } 154 155 /** 156 * Notifies this action that the given selection has changed. This default 157 * implementation sets the action's enablement state to <code>false</code>. 158 * 159 * @param selection 160 * the new selection 161 */ selectionChanged(ISelection selection)162 protected void selectionChanged(ISelection selection) { 163 setEnabled(false); 164 } 165 166 /** 167 * Executes this actions with the given selection. This default 168 * implementation does nothing. 169 */ run(ISelection selection)170 protected void run(ISelection selection) { 171 } 172 173 /* 174 * (non-Javadoc) Method declared on IAction. 175 */ run()176 public void run() { 177 dispatchRun(getSelection()); 178 } 179 180 /* 181 * (non-Javadoc) Method declared on ISelectionChangedListener. 182 */ selectionChanged(SelectionChangedEvent event)183 public void selectionChanged(SelectionChangedEvent event) { 184 dispatchSelectionChanged(event.getSelection()); 185 } 186 dispatchSelectionChanged(ISelection selection)187 private void dispatchSelectionChanged(ISelection selection) { 188 if (selection instanceof IStructuredSelection) { 189 selectionChanged((IStructuredSelection) selection); 190 } else if (selection instanceof ITextSelection) { 191 selectionChanged((ITextSelection) selection); 192 } else { 193 selectionChanged(selection); 194 } 195 } 196 dispatchRun(ISelection selection)197 private void dispatchRun(ISelection selection) { 198 if (selection instanceof IStructuredSelection) { 199 run((IStructuredSelection) selection); 200 } else if (selection instanceof ITextSelection) { 201 run((ITextSelection) selection); 202 } else { 203 run(selection); 204 } 205 } 206 }