Commits

sk  committed 0fb2c4b

- Added action to associate project with running lisp.
- Added decoration to show which lisp a project is associated.

  • Participants
  • Parent commits f368864

Comments (0)

Files changed (5)

File META-INF/MANIFEST.MF

  org.lispdev.editor;bundle-version="0.0.1",
  org.lispdev.preferences;bundle-version="0.0.0",
  org.eclipse.ui.console;bundle-version="3.4.0",
- org.eclipse.ui.ide
+ org.eclipse.ui.ide,
+ org.lispdev.swank;bundle-version="1.0.0",
+ org.eclipse.core.resources,
+ org.lispdev.log;bundle-version="1.0.0"
 Bundle-ActivationPolicy: lazy
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Export-Package: org.lispdev.main
          </selection>
       </wizard>
    </extension>
+   <extension
+         point="org.eclipse.ui.popupMenus">
+      <objectContribution
+            id="org.lispdev.main.AssociateProjectAction.folder"
+            objectClass="org.eclipse.core.resources.IProject">
+         <action
+               class="org.lispdev.main.AssociateProjectAction"
+               enablesFor="1"
+               icon="icons/lisp-nature.gif"
+               id="org.lispdev.main.AssociateProjectAction"
+               label="Associate with ..."
+               menubarPath="group.add"
+               style="pulldown">
+         </action>
+      </objectContribution>
+   </extension>
+   <extension
+         point="org.eclipse.ui.decorators">
+      <decorator
+            adaptable="false"
+            class="org.lispdev.main.LispProjectDecorator"
+            id="org.lispdev.main.projectDecorator"
+            label="Lisp Project Decorator"
+            lightweight="true"
+            location="TOP_LEFT"
+            state="true">
+         <description>
+            Adds label of associated lisp runtime.
+         </description>
+         <enablement>
+            <objectClass
+                  name="org.eclipse.core.resources.IProject">
+            </objectClass>
+         </enablement>
+      </decorator>
+   </extension>
 
 </plugin>

File src/org/lispdev/main/AssociateProjectAction.java

+package org.lispdev.main;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.ActionContributionItem;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.IMenuCreator;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.TreeSelection;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.ui.IObjectActionDelegate;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PlatformUI;
+import org.lispdev.swank.SwankPlugin;
+import org.lispdev.swank.debug.LispProcess;
+
+public class AssociateProjectAction implements IObjectActionDelegate, IMenuCreator
+{
+  private IWorkbenchPart part;
+  private IAction action;
+  private IProject project;
+
+  @Override
+  public void run(IAction action)
+  {
+  }
+
+  @Override
+  public void selectionChanged(IAction action, ISelection selection)
+  {
+    if( action != this.action )
+    {
+      action.setMenuCreator(this);
+      this.action = action;
+    }
+    if( selection instanceof TreeSelection )
+    {
+      Object obj = ((TreeSelection)selection).getFirstElement();
+      try
+      {
+        if(obj instanceof IProject
+            && ((IProject)obj).getNature(LispProjectNature.ID) != null )
+        {
+          project = (IProject)obj;
+          action.setEnabled(true);
+        }
+        else
+        {
+          action.setEnabled(false);
+        }
+      }
+      catch(CoreException e)
+      {
+        action.setEnabled(false);
+      }
+    }
+    else
+    {
+      action.setEnabled(false);
+    }
+  }
+
+  @Override
+  public void dispose()
+  {
+  }
+
+  @Override
+  public Menu getMenu(Control parent)
+  {
+    return null;
+  }
+
+  @Override
+  public Menu getMenu(Menu parent)
+  {
+    Menu menu = new Menu(parent);
+    for( LispProcess p : SwankPlugin.get().getProcesses() )
+    {
+      final String label = p.getLabel();
+      final LispProcess pi = p;
+      final IProject pr = project;
+      Action a = new Action(label){
+        @Override
+        public void run()
+        {
+          LispPlugin.get().setActiveProcess(project, pi);
+          Display.getDefault().asyncExec(new Runnable(){
+
+            @Override
+            public void run()
+            {
+              LispProjectDecorator lp =
+                (LispProjectDecorator)PlatformUI.getWorkbench()
+                .getDecoratorManager()
+                .getBaseLabelProvider(LispProjectDecorator.ID);
+              lp.fireNotification(pr);
+            }
+          });
+        }
+      };
+      a.setDescription("Associate project with " + label);
+      ActionContributionItem item = new ActionContributionItem(a);
+      item.fill(menu, -1);
+    }
+      //new MenuItem(menu, SWT.SEPARATOR);
+
+    return menu;
+  }
+
+  @Override
+  public void setActivePart(IAction action, IWorkbenchPart targetPart)
+  {
+    part = targetPart;
+  }
+
+}

File src/org/lispdev/main/LispPlugin.java

 package org.lispdev.main;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.core.resources.IProject;
 import org.eclipse.ui.plugin.AbstractUIPlugin;
+import org.lispdev.swank.debug.LispProcess;
 import org.osgi.framework.BundleContext;
 
 /**
     return plugin;
   }
 
+  private static class Processes
+  {
+    private LispProcess active;
+    private final List<LispProcess> processes = new ArrayList<LispProcess>();
+
+    public void setActive(LispProcess p)
+    {
+      active = p;
+    }
+
+    public LispProcess getActive()
+    {
+      return active;
+    }
+
+    public void addProcess(LispProcess p)
+    {
+      processes.add(p);
+    }
+
+    public void removeProcess(LispProcess p)
+    {
+      if( p.equals(active) )
+      {
+        active = null;
+      }
+      processes.remove(p);
+    }
+
+    public List<LispProcess> getProcesses()
+    {
+      return processes;
+    }
+  }
+
+  /**
+   * holds mapping between lisp projects and their associated
+   * lisp processes
+   */
+  private final Map<IProject, Processes> project2process =
+    new HashMap<IProject, Processes>();
+
+  public LispProcess getActiveProcess(IProject p)
+  {
+    Processes ps = project2process.get(p);
+    return (ps == null ? null : ps.getActive());
+  }
+
+  public List<LispProcess> getProcesses(IProject p)
+  {
+    Processes ps = project2process.get(p);
+    return (ps == null ? new ArrayList<LispProcess>(0) : ps.getProcesses());
+  }
+
+  public void addProcess(IProject pj, LispProcess pc)
+  {
+    Processes ps = project2process.get(pj);
+    if( ps == null )
+    {
+      ps = new Processes();
+      project2process.put(pj, ps);
+    }
+    ps.addProcess(pc);
+  }
+
+  public void setActiveProcess(IProject p, LispProcess pc)
+  {
+    Processes ps = project2process.get(p);
+    if( ps == null )
+    {
+      ps = new Processes();
+      project2process.put(p, ps);
+    }
+    ps.setActive(pc);
+  }
+
+  public void removeProcess(IProject p, LispProcess pc)
+  {
+    Processes ps = project2process.get(p);
+    if( ps == null )
+    {
+      return;
+    }
+    ps.removeProcess(pc);
+  }
+
+  public void removeProject(IProject p)
+  {
+    project2process.remove(p);
+  }
 }

File src/org/lispdev/main/LispProjectDecorator.java

+package org.lispdev.main;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jface.viewers.IDecoration;
+import org.eclipse.jface.viewers.ILightweightLabelDecorator;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.LabelProviderChangedEvent;
+import org.lispdev.swank.debug.LispProcess;
+
+
+public class LispProjectDecorator extends LabelProvider implements
+    ILightweightLabelDecorator
+{
+  public static final String ID = "org.lispdev.main.projectDecorator";
+
+  @Override
+  public void decorate(Object element, IDecoration decoration)
+  {
+    IProject p = (IProject)element;
+    try
+    {
+      if( p.getNature(LispProjectNature.ID) != null )
+      {
+        LispProcess pr = LispPlugin.get().getActiveProcess(p);
+        decoration.addSuffix(" ["+(pr == null ? "" : pr.getLabel())+"]");
+      }
+    }
+    catch(CoreException e)
+    {
+      e.printStackTrace();
+    }
+  }
+
+  public void fireNotification(IProject notification)
+  {
+    LabelProviderChangedEvent event =
+        new LabelProviderChangedEvent(this, new Object[] {notification});
+    fireLabelProviderChanged(event);
+  }
+}