Author: simoneg
Date: Tue Aug 25 12:36:49 2009
New Revision: 807582

URL: http://svn.apache.org/viewvc?rev=807582&view=rev
Log:
LABS-416 : dybamic tooltips on toolbar buttons

Modified:
    
labs/magma/trunk/magma-eclipse/src/org/apache/magma/tools/magmaeclipse/basics/RunMagmaHandler.java

Modified: 
labs/magma/trunk/magma-eclipse/src/org/apache/magma/tools/magmaeclipse/basics/RunMagmaHandler.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/magma-eclipse/src/org/apache/magma/tools/magmaeclipse/basics/RunMagmaHandler.java?rev=807582&r1=807581&r2=807582&view=diff
==============================================================================
--- 
labs/magma/trunk/magma-eclipse/src/org/apache/magma/tools/magmaeclipse/basics/RunMagmaHandler.java
 (original)
+++ 
labs/magma/trunk/magma-eclipse/src/org/apache/magma/tools/magmaeclipse/basics/RunMagmaHandler.java
 Tue Aug 25 12:36:49 2009
@@ -1,12 +1,13 @@
 package org.apache.magma.tools.magmaeclipse.basics;
 
 import java.util.Iterator;
+import java.util.Map;
 
 import org.apache.magma.tools.magmaeclipse.launch.MagmaLaunchShortcut;
 import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.Command;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.IAdaptable;
@@ -14,31 +15,47 @@
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IPartService;
+import org.eclipse.ui.ISelectionListener;
+import org.eclipse.ui.ISelectionService;
 import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.commands.ICommandService;
+import org.eclipse.ui.commands.IElementUpdater;
 import org.eclipse.ui.handlers.HandlerUtil;
-import org.maven.ide.eclipse.actions.ExecutePomAction;
+import org.eclipse.ui.menus.UIElement;
+import org.eclipse.ui.services.IServiceLocator;
 
-public class RunMagmaHandler extends AbstractHandler {
+public class RunMagmaHandler extends AbstractHandler implements 
IElementUpdater {
 
-       public Object execute(ExecutionEvent event) throws ExecutionException {
-               String goals = event.getParameter("goals");
-               String type = event.getParameter("type");
-               if (type == null) type = "run";
-               if (goals == null) goals = "clean install";
-               MagmaLaunchShortcut epa = new MagmaLaunchShortcut();
-               epa.setInitializationData(null, null, goals);
-               IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
+       public static final String 
COMMAND_ID="org.apache.magma.tools.magmaeclipse.runmagmacmd";
+       
+       public RunMagmaHandler() {
+               super();
+               
+               final IWorkbenchWindow window = 
PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+               window.getSelectionService().addPostSelectionListener(new 
ISelectionListener() {
+                               public void selectionChanged(IWorkbenchPart 
part, ISelection selection) {
+                                       IProject project = findProject(part, 
selection);
+                                       if (project == null) return;
+                                       ICommandService commandService = 
(ICommandService) window.getService(ICommandService.class);
+                                       Command command = 
commandService.getCommand(COMMAND_ID);
+                                       
commandService.refreshElements(command.getId(), null);
+                               }
+                       });
+       }
+       
+       protected IProject findProject(IWorkbenchPart activePart, ISelection 
selection) {
                IProject project = null;
                if (activePart instanceof IEditorPart) {
                        IResource res = (IResource) 
((IEditorPart)activePart).getEditorInput().getAdapter(IResource.class);
                        if (res == null) {
-                               epa.launch((IEditorPart) activePart, type);
                                return null;
                        } else {
                                project = res.getProject();
                        }
                } else {
-                       ISelection selection = 
HandlerUtil.getCurrentSelectionChecked(event);
                        if (!(selection instanceof IStructuredSelection)) 
return null;
                        Iterator iterator = 
((IStructuredSelection)selection).iterator();
                        while (iterator.hasNext()) {
@@ -60,15 +77,43 @@
                                        }
                                }
                        }
-                       if (project == null) {
+               }
+               return project;
+       }
+       
+       public Object execute(ExecutionEvent event) throws ExecutionException {
+               String goals = event.getParameter("goals");
+               String type = event.getParameter("type");
+               if (type == null) type = "run";
+               if (goals == null) goals = "clean install";
+               MagmaLaunchShortcut epa = new MagmaLaunchShortcut();
+               epa.setInitializationData(null, null, goals);
+               IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
+               ISelection selection = 
HandlerUtil.getCurrentSelectionChecked(event);
+               IProject project = findProject(activePart, selection);
+               if (project == null) {
+                       if (activePart instanceof IEditorPart) {
+                               epa.launch((IEditorPart) activePart, type);
+                       } else {
                                epa.launch(selection, type);
                        }
-               }
-               if (project != null) {
+               } else {
                        IStructuredSelection sel = new 
StructuredSelection(project);
                        epa.launch(sel, type);
                }
                return null;
        }
 
+       public void updateElement(UIElement element, Map parameters) {
+               IServiceLocator locator = element.getServiceLocator();
+               ISelectionService serv = (ISelectionService) 
locator.getService(ISelectionService.class);
+               ISelection selection = serv.getSelection();
+               IPartService parts = (IPartService) 
locator.getService(IPartService.class);
+               IWorkbenchPart activePart = parts.getActivePart();
+               IProject project = findProject(activePart, selection);
+               if (project != null) {
+                       element.setTooltip("on project " + project.getName());
+               }
+       }
+       
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to