1 11 package org.eclipse.jdt.internal.ui.workingsets; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.SubProgressMonitor; 22 23 import org.eclipse.core.resources.IProject; 24 import org.eclipse.core.resources.IResourceChangeEvent; 25 import org.eclipse.core.resources.IResourceChangeListener; 26 import org.eclipse.core.resources.IResourceDelta; 27 import org.eclipse.core.resources.IWorkspaceRunnable; 28 import org.eclipse.core.resources.ResourcesPlugin; 29 30 import org.eclipse.swt.widgets.Shell; 31 32 import org.eclipse.jface.action.IAction; 33 import org.eclipse.jface.viewers.IStructuredSelection; 34 35 import org.eclipse.ui.IActionBars; 36 import org.eclipse.ui.IViewSite; 37 import org.eclipse.ui.IWorkbenchSite; 38 import org.eclipse.ui.IWorkingSet; 39 import org.eclipse.ui.PlatformUI; 40 41 import org.eclipse.ui.ide.IDEActionFactory; 42 43 import org.eclipse.jdt.core.IJavaProject; 44 45 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 46 47 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; 48 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 49 50 public abstract class OpenCloseWorkingSetAction extends SelectionDispatchAction implements IResourceChangeListener { 51 52 private static final class CloseWorkingSetAction extends OpenCloseWorkingSetAction { 53 private IAction fProjectAction; 54 private CloseWorkingSetAction(IWorkbenchSite site, String label) { 55 super(site, label); 56 IActionBars actionBars= getActionBars(); 57 if (actionBars != null) { 58 fProjectAction= actionBars.getGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId()); 59 } 60 } 61 protected boolean validate(IProject project) { 62 return project.isOpen(); 63 } 64 protected void performOperation(IProject project, IProgressMonitor monitor) throws CoreException { 65 project.close(monitor); 66 } 67 protected void connectToActionBar(IActionBars actionBars) { 68 actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), this); 69 actionBars.updateActionBars(); 70 } 71 protected void disconnectFromActionBar(IActionBars actionBars) { 72 actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), fProjectAction); 73 actionBars.updateActionBars(); 74 } 75 protected String getErrorTitle() { 76 return WorkingSetMessages.OpenCloseWorkingSetAction_close_error_title; 77 } 78 protected String getErrorMessage() { 79 return WorkingSetMessages.OpenCloseWorkingSetAction_close_error_message; 80 } 81 } 82 83 private static final class OpenWorkingSetAction extends OpenCloseWorkingSetAction { 84 private IAction fProjectAction; 85 private OpenWorkingSetAction(IWorkbenchSite site, String label) { 86 super(site, label); 87 IActionBars actionBars= getActionBars(); 88 if (actionBars != null) { 89 fProjectAction= actionBars.getGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId()); 90 } 91 } 92 protected boolean validate(IProject project) { 93 return !project.isOpen(); 94 } 95 protected void performOperation(IProject project, IProgressMonitor monitor) throws CoreException { 96 project.open(monitor); 97 } 98 protected void connectToActionBar(IActionBars actionBars) { 99 actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), this); 100 actionBars.updateActionBars(); 101 } 102 protected void disconnectFromActionBar(IActionBars actionBars) { 103 actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), fProjectAction); 104 actionBars.updateActionBars(); 105 } 106 protected String getErrorTitle() { 107 return WorkingSetMessages.OpenCloseWorkingSetAction_open_error_title; 108 } 109 protected String getErrorMessage() { 110 return WorkingSetMessages.OpenCloseWorkingSetAction_open_error_message; 111 } 112 } 113 114 private OpenCloseWorkingSetAction(IWorkbenchSite site, String label) { 115 super(site); 116 setText(label); 117 ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE); 118 } 119 120 public static OpenCloseWorkingSetAction createCloseAction(IWorkbenchSite site) { 121 return new CloseWorkingSetAction(site, WorkingSetMessages.OpenCloseWorkingSetAction_close_label); 122 } 123 124 public static OpenCloseWorkingSetAction createOpenAction(IWorkbenchSite site) { 125 return new OpenWorkingSetAction(site, WorkingSetMessages.OpenCloseWorkingSetAction_open_label); 126 } 127 128 public void dispose() { 129 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); 130 } 131 132 public void selectionChanged(IStructuredSelection selection) { 133 List projects= getProjects(selection); 134 IActionBars actionBars= getActionBars(); 135 if (projects != null && projects.size() > 0) { 136 setEnabled(true); 137 if (actionBars != null) { 138 connectToActionBar(actionBars); 139 } 140 } else { 141 setEnabled(false); 142 if (actionBars != null) { 143 disconnectFromActionBar(actionBars); 144 } 145 } 146 } 147 148 public void run(IStructuredSelection selection) { 149 final List projects= getProjects(selection); 150 if (projects != null && projects.size() > 0) { 151 try { 152 PlatformUI.getWorkbench().getProgressService().busyCursorWhile( 153 new WorkbenchRunnableAdapter(new IWorkspaceRunnable() { 154 public void run(IProgressMonitor monitor) throws CoreException { 155 monitor.beginTask("", projects.size()); for (Iterator iter= projects.iterator(); iter.hasNext();) { 157 IProject project= (IProject)iter.next(); 158 performOperation(project, new SubProgressMonitor(monitor, 1)); 159 } 160 monitor.done(); 161 } 162 })); 163 } catch (InvocationTargetException e) { 164 ExceptionHandler.handle(e, getShell(), getErrorTitle(), getErrorMessage()); 165 } catch (InterruptedException e) { 166 } 168 } 169 } 170 171 protected abstract boolean validate(IProject project); 172 173 protected abstract void performOperation(IProject project, IProgressMonitor monitor) throws CoreException; 174 175 protected abstract void connectToActionBar(IActionBars actionBars); 176 177 protected abstract void disconnectFromActionBar(IActionBars actionBars); 178 179 protected abstract String getErrorTitle(); 180 181 protected abstract String getErrorMessage(); 182 183 private List getProjects(IStructuredSelection selection) { 184 List result= new ArrayList (); 185 List elements= selection.toList(); 186 for (Iterator iter= elements.iterator(); iter.hasNext();) { 187 Object element= iter.next(); 188 if (!(element instanceof IWorkingSet)) 189 return null; 190 List projects= getProjects((IWorkingSet)element); 191 if (projects == null) 192 return null; 193 result.addAll(projects); 194 } 195 return result; 196 } 197 198 private List getProjects(IWorkingSet set) { 199 List result= new ArrayList (); 200 IAdaptable[] elements= set.getElements(); 201 for (int i= 0; i < elements.length; i++) { 202 Object element= elements[i]; 203 IProject project= null; 204 if (element instanceof IProject) { 205 project= (IProject)element; 206 } else if (element instanceof IJavaProject) { 207 project= ((IJavaProject)element).getProject(); 208 } 209 if (project != null && validate(project)) 210 result.add(project); 211 } 212 return result; 213 } 214 215 protected IActionBars getActionBars() { 216 if (getSite() instanceof IViewSite) { 217 return ((IViewSite)getSite()).getActionBars(); 218 } else { 219 return null; 220 } 221 } 222 223 226 public void resourceChanged(IResourceChangeEvent event) { 227 IResourceDelta delta = event.getDelta(); 228 if (delta != null) { 229 IResourceDelta[] projDeltas = delta.getAffectedChildren(IResourceDelta.CHANGED); 230 for (int i = 0; i < projDeltas.length; ++i) { 231 IResourceDelta projDelta = projDeltas[i]; 232 if ((projDelta.getFlags() & IResourceDelta.OPEN) != 0) { 233 Shell shell= getShell(); 234 if (!shell.isDisposed()) { 235 shell.getDisplay().asyncExec(new Runnable () { 236 public void run() { 237 update(getSelection()); 238 } 239 }); 240 } 241 return; 242 } 243 } 244 } 245 } 246 } | Popular Tags |