Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add an entry to eclipse Package Explorer context menu to start a TextEditor?

Tags:

eclipse

I am trying to add an entry to the Package Explorer pop-up context menu that opens a modified version of a file in an editor when the entry is selected.

I am using Eclipse Indigo on a Fedora 15 laptop.

Here's a very specific question that may be adequately well-focused, and might provide the help I need to tackle the general problem I have.

Eclipse provides a sample plugin named org.eclipse.ui.examples.readmetool, which can be set up from the Help->Welcome page.

This plugin defines an editor named ReadmeEditor.java. It also defines an extension point "org.eclipse.ui.popupMenus" in plugin.xml. When you use this action from the eclipse Project Explorer view on a file with the suffix ".readme", you just get a little pop-up window.

How can I change the readmetool plugin so that when you select the above-defined Project Explorer menu item on a foo.readme file, you instead get a new ReadmeEditor in eclipse, with foo.readme in it??

The more specific project need I am struggling with is as follows:

  • From the Package Explorer, navigate to a file.

  • Right-click the file to get a pop-up context menu

  • See a new entry "Version view"

  • On selecting that entry, find the file, run it through an external program to create a temporary file, and display the contents of that temporary file in a new TextEditor window. (And, don't leave an orphan temporary file around after it is no longer needed.)

I have some of the above pieces in place; I augmented the MultiPageEditor example so that it does the work of running the external program, creating the temporary file, and opening the temporary file in a text editor.

However, the only way I can currently access that functionality is to select the "Open With" option in the pop-up context window for the file in the Project Explorer, and go to "Other" within that window and select my new editor option.

So, where I am stuck at the moment is how to add the right magic to plugin.xml so that I get a new option on the Project Manager pop-up context window, and connect that with a new TextEditor window.

This seems very much like what the vanilla "Open" option would do, and it seems like this should be a pretty basic thing to do within eclipse. Any examples and/or guidance would be greatly appreciated.

Greg

like image 782
user691307 Avatar asked Jan 16 '23 05:01

user691307


1 Answers

Here is a terse but complete example of setting up and using a project. (Scouring the net, there was much advice about eclipse, but it tended to be rather splintered and piecemeal, and when I was struggling to solve my problem it would have been helpful to have a complete, self-contained HOWTO. So, this is that..)

This recipe assumes you are using Eclipse Indigo or Juno with:

  • Eclipse IDE for Java Developers
  • Eclipse Plug-in Development Environment.

To confirm the above from within eclipse, see

Help -> About Eclipse -> Installation Details -> Installed Software
  1. start up Ecipse Indigo, with a fresh, empty workspace:

    you, before starting eclipse:
              move or delete ~/eclipse.d/popup_workspace.d
              mkdir -p ~/eclipse.d/popup_workspace.d
    start eclipse:  eclipse
    eclipse:  "Select a workspace"
    you:      "Browse", navigate to above-created popup_workspace.d; "OK" "OK"
    eclipse:  "Welcome to Eclipse IDE for Java Developers"
    you:      select "Workbench"
    
  2. create a new project, using the Plug-in Project as a handy starting place:

    you:      File -> New -> Project
    eclipse:  "Select a wizard:
    you:      Plug-in Development -> Plug-in Project;  Next>
    eclipse:  "Plug-in Project"
    you:      Project name:  org.mypopup.popup_editor  Next>
    eclipse:  "Content"
    you:      Next>
    eclipse:  "Templates"
    you:      Plug-in with a popup menu                Finish
    eclipse:  "This kind of project is associated with the
              Plug-in Development perspective..."
    you:      Yes
    
  3. in "Package Explorer, navigate to NewActions.java and double-click to open:

    org.mypopup.popup_editor
        -> src
            -> org.mypopup.popup_editor.popup.actions
                -> NewAction.java
    
    delete entire contents and replace with:
    
    package org.mypopup.popup_editor.popup.actions;
    
    import org.eclipse.jface.viewers.TreeSelection;
    import org.eclipse.jface.viewers.TreePath;
    import org.eclipse.core.resources.IFile;
    import org.eclipse.core.filesystem.EFS;
    import org.eclipse.core.runtime.IPath;
    import org.eclipse.jface.action.IAction;
    import org.eclipse.jface.viewers.ISelection;
    import org.eclipse.ui.IObjectActionDelegate;
    import org.eclipse.ui.IWorkbenchPage;
    import org.eclipse.ui.IWorkbenchPart;
    import org.eclipse.ui.IWorkbenchWindow;
    import org.eclipse.ui.PlatformUI;
    import org.eclipse.ui.ide.IDE;
    
    public class NewAction implements IObjectActionDelegate {
        public NewAction() {
            super();
        }
    
        public void run(IAction action) {
            try {
                IWorkbenchWindow window =
                        PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    
                ISelection selection = window.getSelectionService()
                        .getSelection("org.eclipse.ui.navigator.ProjectExplorer");
    
                TreePath[] paths = ((TreeSelection) selection).getPaths();
                TreePath p = paths[0];
                Object last = p.getLastSegment();
    
                if (last instanceof IFile) {
                    IPath ipath = ((IFile) last).getLocation();
    
                    IWorkbenchPage page = window.getActivePage();
    
                    IDE.openEditorOnFileStore(page,
                            EFS.getLocalFileSystem().getStore(ipath));
                }
            } catch (Exception blah) {
            }
        }
    
        public void setActivePart(IAction action, IWorkbenchPart targetPart) { }
    
        public void selectionChanged(IAction action, ISelection selection) { }
    }
    
  4. add dependencies to the project to be able to resolve imports:

    in Package Explorer, navigate to and open META-INF -> MANIFEST.MF
    
    Select "Dependencies" tab along the bottom of the resulting
    "org.mypopup.plugin_editor" window
    
    "Add..."; "Select a Plug-in:" enter org.eclipse.core.filesystem; "OK"
    "Add..."; "Select a Plug-in:" enter org.eclipse.ui.ide; "OK"
    
    File -> Save my.popup.popup_editor
    
  5. run the new project, with a fresh empty target directory:

    outside of eclipse, browse to the directory containing popup_workspace.d,
    in this case ~/eclipse.d, and if a folder "runtime-EclipseApplication"
    exists, move it or delete it.
    
    right-click META-INF -> MANIFEST.MF, select Run As -> Eclipse Application
    
    in new eclipse window:
    eclipse:  "Welcome to Eclipse"
    you:      select "Workbench"
    you:      File -> New -> Project
    eclipse:  "Select a wizard"
    you:      General -> Project              Next>
    eclipse:  "Project"
    you:      project name popup_test         Finish
    you:      in Project Explorer, right-click popup_test, select New -> File
    eclipse:  "File"
    you:      File name:  test_file1          Finish
    you:      enter a little text in test_file1, File -> Save; File -> Close
    
  6. the big moment: open the file with your new pop-up menu entry:

    in Project Explorer, right-click popup_test -> test_file1
    select New Submenu -> New Action
    
like image 141
user691307 Avatar answered Jan 31 '23 01:01

user691307