|
From: <caw...@us...> - 2007-09-18 13:00:27
|
Revision: 3203
http://rubyeclipse.svn.sourceforge.net/rubyeclipse/?rev=3203&view=rev
Author: cawilliams
Date: 2007-09-18 06:00:24 -0700 (Tue, 18 Sep 2007)
Log Message:
-----------
Add Project ActionGroup to Ruby Explorer - so we can close and open projects.
Modified Paths:
--------------
trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/packageview/PackageExplorerActionGroup.java
trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/ActionMessages.java
Added Paths:
-----------
trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/OpenProjectAction.java
trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/ProjectActionGroup.java
Modified: trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/packageview/PackageExplorerActionGroup.java
===================================================================
--- trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/packageview/PackageExplorerActionGroup.java 2007-09-17 20:58:36 UTC (rev 3202)
+++ trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/internal/ui/packageview/PackageExplorerActionGroup.java 2007-09-18 13:00:24 UTC (rev 3203)
@@ -59,6 +59,7 @@
import org.rubypeople.rdt.ui.actions.CCPActionGroup;
import org.rubypeople.rdt.ui.actions.CustomFiltersActionGroup;
import org.rubypeople.rdt.ui.actions.NavigateActionGroup;
+import org.rubypeople.rdt.ui.actions.ProjectActionGroup;
import org.rubypeople.rdt.ui.actions.RubySearchActionGroup;
class PackageExplorerActionGroup extends CompositeActionGroup {
@@ -108,7 +109,7 @@
// new ImportActionGroup(fPart),
// new BuildActionGroup(fPart),
new RubySearchActionGroup(fPart),
-// new ProjectActionGroup(fPart),
+ new ProjectActionGroup(fPart),
fViewActionGroup= new ViewActionGroup(fPart.getRootMode(), workingSetListener, site),
fCustomFiltersActionGroup= new CustomFiltersActionGroup(fPart, viewer),
// new LayoutActionGroup(fPart),
Modified: trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/ActionMessages.java
===================================================================
--- trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/ActionMessages.java 2007-09-17 20:58:36 UTC (rev 3202)
+++ trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/ActionMessages.java 2007-09-18 13:00:24 UTC (rev 3203)
@@ -17,6 +17,9 @@
public static String ShowInPackageViewAction_tooltip;
public static String ShowInPackageViewAction_error_message;
public static String ShowInPackageViewAction_dialog_title;
+ public static String OpenProjectAction_dialog_title;
+ public static String OpenProjectAction_dialog_message;
+ public static String OpenProjectAction_error_message;
static {
NLS.initializeMessages(BUNDLE_NAME, ActionMessages.class);
Added: trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/OpenProjectAction.java
===================================================================
--- trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/OpenProjectAction.java (rev 0)
+++ trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/OpenProjectAction.java 2007-09-18 13:00:24 UTC (rev 3203)
@@ -0,0 +1,211 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.rubypeople.rdt.ui.actions;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResourceChangeEvent;
+import org.eclipse.core.resources.IResourceChangeListener;
+import org.eclipse.core.resources.IResourceDelta;
+import org.eclipse.core.resources.IWorkspaceRunnable;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.MultiStatus;
+import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.window.Window;
+import org.eclipse.ui.IWorkbenchSite;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.actions.OpenResourceAction;
+import org.eclipse.ui.dialogs.ElementListSelectionDialog;
+import org.rubypeople.rdt.internal.ui.IRubyHelpContextIds;
+import org.rubypeople.rdt.internal.ui.RubyPlugin;
+import org.rubypeople.rdt.internal.ui.actions.WorkbenchRunnableAdapter;
+import org.rubypeople.rdt.internal.ui.util.ExceptionHandler;
+import org.rubypeople.rdt.ui.RubyElementLabelProvider;
+
+/**
+ * Action to open a closed project. Action either opens the closed projects
+ * provided by the structured selection or presents a dialog from which the
+ * user can select the projects to be opened.
+ *
+ * <p>
+ * This class may be instantiated; it is not intended to be subclassed.
+ * </p>
+ *
+ * @since 2.0
+ */
+public class OpenProjectAction extends SelectionDispatchAction implements IResourceChangeListener {
+
+ private static final int EMPTY_SELECTION= 1;
+ private static final int ELEMENT_SELECTION= 2;
+
+ private int fMode;
+ private OpenResourceAction fWorkbenchAction;
+
+ /**
+ * Creates a new <code>OpenProjectAction</code>. The action requires
+ * that the selection provided by the site's selection provider is of type <code>
+ * org.eclipse.jface.viewers.IStructuredSelection</code>.
+ *
+ * @param site the site providing context information for this action
+ */
+ public OpenProjectAction(IWorkbenchSite site) {
+ super(site);
+ fWorkbenchAction= new OpenResourceAction(site.getShell());
+ setText(fWorkbenchAction.getText());
+ setToolTipText(fWorkbenchAction.getToolTipText());
+ PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IRubyHelpContextIds.OPEN_PROJECT_ACTION);
+ }
+
+ /*
+ * @see IResourceChangeListener#resourceChanged(IResourceChangeEvent)
+ */
+ public void resourceChanged(IResourceChangeEvent event) {
+ fWorkbenchAction.resourceChanged(event);
+ switch (fMode) {
+ case ELEMENT_SELECTION:
+ setEnabled(fWorkbenchAction.isEnabled());
+ break;
+ case EMPTY_SELECTION:
+ internalResourceChanged(event);
+ break;
+ }
+ }
+
+ private void internalResourceChanged(IResourceChangeEvent event) {
+ IResourceDelta delta = event.getDelta();
+ if (delta != null) {
+ IResourceDelta[] projDeltas = delta.getAffectedChildren(IResourceDelta.CHANGED);
+ for (int i = 0; i < projDeltas.length; ++i) {
+ IResourceDelta projDelta = projDeltas[i];
+ if ((projDelta.getFlags() & IResourceDelta.OPEN) != 0) {
+ setEnabled(hasCloseProjects());
+ return;
+ }
+ }
+ }
+ }
+
+ //---- normal selection -------------------------------------
+
+ /* (non-Rubydoc)
+ * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.ISelection)
+ */
+ public void selectionChanged(ISelection selection) {
+ setEnabled(hasCloseProjects());
+ fMode= EMPTY_SELECTION;
+ }
+
+ /* (non-Rubydoc)
+ * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.ISelection)
+ */
+ public void run(ISelection selection) {
+ internalRun();
+ }
+
+ //---- structured selection ---------------------------------------
+
+ /* (non-Rubydoc)
+ * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
+ */
+ public void selectionChanged(IStructuredSelection selection) {
+ if (selection.isEmpty()) {
+ setEnabled(hasCloseProjects());
+ fMode= EMPTY_SELECTION;
+ return;
+ }
+ fWorkbenchAction.selectionChanged(selection);
+ setEnabled(fWorkbenchAction.isEnabled());
+ fMode= ELEMENT_SELECTION;
+ }
+
+
+ /* (non-Rubydoc)
+ * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
+ */
+ public void run(IStructuredSelection selection) {
+ if (selection.isEmpty()) {
+ internalRun();
+ return;
+ }
+ fWorkbenchAction.run();
+ }
+
+ private void internalRun() {
+ ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new RubyElementLabelProvider());
+ dialog.setTitle(ActionMessages.OpenProjectAction_dialog_title);
+ dialog.setMessage(ActionMessages.OpenProjectAction_dialog_message);
+ dialog.setElements(getClosedProjects());
+ dialog.setMultipleSelection(true);
+ int result= dialog.open();
+ if (result != Window.OK)
+ return;
+ final Object[] projects= dialog.getResult();
+ IWorkspaceRunnable runnable= createRunnable(projects);
+ try {
+ PlatformUI.getWorkbench().getProgressService().run(true, true, new WorkbenchRunnableAdapter(runnable));
+ } catch (InvocationTargetException e) {
+ ExceptionHandler.handle(e, getShell(),
+ ActionMessages.OpenProjectAction_dialog_title,
+ ActionMessages.OpenProjectAction_error_message);
+ } catch (InterruptedException e) {
+ }
+ }
+
+ private IWorkspaceRunnable createRunnable(final Object[] projects) {
+ return new IWorkspaceRunnable() {
+ public void run(IProgressMonitor monitor) throws CoreException {
+ monitor.beginTask("", projects.length); //$NON-NLS-1$
+ MultiStatus errorStatus= null;
+ for (int i = 0; i < projects.length; i++) {
+ IProject project= (IProject)projects[i];
+ try {
+ project.open(new SubProgressMonitor(monitor, 1));
+ } catch (CoreException e) {
+ if (errorStatus == null)
+ errorStatus = new MultiStatus(RubyPlugin.getPluginId(), IStatus.ERROR, ActionMessages.OpenProjectAction_error_message, e);
+ errorStatus.merge(e.getStatus());
+ }
+ }
+ monitor.done();
+ if (errorStatus != null)
+ throw new CoreException(errorStatus);
+ }
+ };
+ }
+
+ private Object[] getClosedProjects() {
+ IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
+ List result= new ArrayList(5);
+ for (int i = 0; i < projects.length; i++) {
+ IProject project= projects[i];
+ if (!project.isOpen())
+ result.add(project);
+ }
+ return result.toArray();
+ }
+
+ private boolean hasCloseProjects() {
+ IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
+ for (int i = 0; i < projects.length; i++) {
+ if (!projects[i].isOpen())
+ return true;
+ }
+ return false;
+ }
+}
Property changes on: trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/OpenProjectAction.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/ProjectActionGroup.java
===================================================================
--- trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/ProjectActionGroup.java (rev 0)
+++ trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/ProjectActionGroup.java 2007-09-18 13:00:24 UTC (rev 3203)
@@ -0,0 +1,141 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.rubypeople.rdt.ui.actions;
+
+import java.util.Iterator;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IActionBars;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbenchSite;
+import org.eclipse.ui.actions.ActionGroup;
+import org.eclipse.ui.actions.CloseResourceAction;
+import org.eclipse.ui.ide.IDEActionFactory;
+import org.eclipse.ui.internal.ide.actions.CloseUnrelatedProjectsAction;
+import org.rubypeople.rdt.ui.IContextMenuConstants;
+
+/**
+ * Adds actions to open and close a project to the global menu bar.
+ *
+ * <p>
+ * This class may be instantiated; it is not intended to be subclassed.
+ * </p>
+ *
+ * @since 2.0
+ */
+public class ProjectActionGroup extends ActionGroup {
+
+ private IWorkbenchSite fSite;
+
+ private OpenProjectAction fOpenAction;
+ private CloseResourceAction fCloseAction;
+ private CloseResourceAction fCloseUnrelatedAction;
+
+ /**
+ * Creates a new <code>ProjectActionGroup</code>. The group requires
+ * that the selection provided by the site's selection provider is of type <code>
+ * org.eclipse.jface.viewers.IStructuredSelection</code>.
+ *
+ * @param part the view part that owns this action group
+ */
+ public ProjectActionGroup(IViewPart part) {
+ fSite = part.getSite();
+ Shell shell= fSite.getShell();
+ ISelectionProvider provider= fSite.getSelectionProvider();
+ ISelection selection= provider.getSelection();
+
+ fCloseAction= new CloseResourceAction(shell);
+ fCloseAction.setActionDefinitionId("org.eclipse.ui.project.closeProject"); //$NON-NLS-1$
+
+ /*
+ * XXX: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=128386
+ */
+ fCloseUnrelatedAction= new CloseUnrelatedProjectsAction(shell);
+ fCloseUnrelatedAction.setActionDefinitionId("org.eclipse.ui.project.closeUnrelatedProjects"); //$NON-NLS-1$
+
+ fOpenAction= new OpenProjectAction(fSite);
+ fOpenAction.setActionDefinitionId("org.eclipse.ui.project.openProject"); //$NON-NLS-1$
+ if (selection instanceof IStructuredSelection) {
+ IStructuredSelection s= (IStructuredSelection)selection;
+ fOpenAction.selectionChanged(s);
+ fCloseAction.selectionChanged(s);
+ fCloseUnrelatedAction.selectionChanged(s);
+ }
+ provider.addSelectionChangedListener(fOpenAction);
+ provider.addSelectionChangedListener(fCloseAction);
+ provider.addSelectionChangedListener(fCloseUnrelatedAction);
+ IWorkspace workspace= ResourcesPlugin.getWorkspace();
+ workspace.addResourceChangeListener(fOpenAction);
+ workspace.addResourceChangeListener(fCloseAction);
+ workspace.addResourceChangeListener(fCloseUnrelatedAction);
+ }
+
+ /* (non-Javadoc)
+ * Method declared in ActionGroup
+ */
+ public void fillActionBars(IActionBars actionBars) {
+ super.fillActionBars(actionBars);
+ actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), fCloseAction);
+ actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_UNRELATED_PROJECTS.getId(), fCloseUnrelatedAction);
+ actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), fOpenAction);
+ }
+
+ /* (non-Javadoc)
+ * Method declared in ActionGroup
+ */
+ public void fillContextMenu(IMenuManager menu) {
+ super.fillContextMenu(menu);
+ if (fOpenAction.isEnabled())
+ menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fOpenAction);
+ if (fCloseAction.isEnabled())
+ menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fCloseAction);
+ if (fCloseUnrelatedAction.isEnabled() && areOnlyProjectsSelected(fCloseUnrelatedAction.getStructuredSelection()))
+ menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fCloseUnrelatedAction);
+ }
+
+ private boolean areOnlyProjectsSelected(IStructuredSelection selection) {
+ if (selection.isEmpty())
+ return false;
+
+ Iterator iter= selection.iterator();
+ while (iter.hasNext()) {
+ Object obj= iter.next();
+ if (obj instanceof IAdaptable) {
+ if (((IAdaptable)obj).getAdapter(IProject.class) == null)
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /*
+ * @see ActionGroup#dispose()
+ */
+ public void dispose() {
+ ISelectionProvider provider= fSite.getSelectionProvider();
+ provider.removeSelectionChangedListener(fOpenAction);
+ provider.removeSelectionChangedListener(fCloseAction);
+ provider.removeSelectionChangedListener(fCloseUnrelatedAction);
+ IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ workspace.removeResourceChangeListener(fOpenAction);
+ workspace.removeResourceChangeListener(fCloseAction);
+ workspace.removeResourceChangeListener(fCloseUnrelatedAction);
+ super.dispose();
+ }
+}
Property changes on: trunk/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/actions/ProjectActionGroup.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|