home.


Tagged: eclipse


Eclipse plugin: Accessing files bundles with the plugin

If you’ve got a plugin, you often want to access text files, or whatever, bundled with that plugin.

This provides the answer: http://blog.vogella.com/2010/07/06/reading-resources-from-plugin/

  1. Make a /files/ directory in your plugin’s base directory.

  2. Use this method

    public static String getFileFromBundle(String fileName) {
        try {
           URL url = new URL( "platform:/plugin/ITS_SYMBLOIC_NAME/files/"+fileName);
           InputStream inputStream = url.openConnection().getInputStream();
           BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
           String inputLine, total="";
           while ((inputLine = in.readLine()) != null) {
             total = total + inputLine + "\n";
           }
           in.close();
           return total;
        } catch (IOException e) {
           e.printStackTrace();
           return null;
        }
    }
    

The SYMBOLIC-NAME above refers to the values in the META-INF/MANIFEST.MF file:

    ...
    Bundle-SymbolicName: ITS_SYMBLOIC_NAME;singleton:=true
    ...
eclipse eclipse-plugin

Simple bare-bones Eclipse Plugin tutorial

Here’s how to create a plugin for eclipse that adds a wizard new option to your ‘File -> New’ options.

It does nothing. The File -> Plugin Development -> Plugin has more working examples that actually do things.

This is just here to show you the framework.
.
First, create a new plugin project,

File -> New -> Other -> Plugin Development -> Plug-in Project 
-> [Enter project name, accept defaults on this screen] 
-> [Accept default on next screen] 
-> [Unclick "Create a plugin using one of the templates"] 
-> Finish

Now you have a blank plugin project.

You’ll have a src directory with a Activator.java that deals with the lifecycle, that you can leave alone pretty much.

In META-INF/ there’s a MANIFEST.MF file. ‘Build-SymbolicName’ to a singleton for plugin development and change the ‘Require-Bundle’ by adding some useful packages:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: YOURPACKAGE
Bundle-SymbolicName: YOURPACKAGE;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: YOURPACKAGE.Activator
Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime,
 org.eclipse.core.resources,
 org.eclipse.ui.ide
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy

The final file you are given is build.properties. When you add new files or directories, you need to add them to the bin.includes values. Let’s do that now, refering to a plugin.xml file we’ll create below:

source.. = src/
output.. = bin/
bin.includes = META-INF/,\
               .,\
               plugin.xml

You need to add a plugin.xml, at the same level as build.properties, so you can register your plugin:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

   <extension
         point="org.eclipse.ui.newWizards">
      <category
            name="ITSNAME"
            id="ITSNAME">
      </category>
      <wizard
            name="ITSNAME"
            category="ITSNAME"
            class="ITSPACKAGE.Yeah"
            id="ITSNAME.yeah">
      </wizard>
   </extension>

</plugin>

The XML file should be fairly self-explanatory. The ‘class’ property refers to a file, Yeah.java, that we haven’t defined as yet. Let’s do that now.

package ITSPACKAGE;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

public class Yeah extends Wizard implements INewWizard {

    @Override
    public void init(IWorkbench workbench, IStructuredSelection selection) {
    }

    @Override
    public void addPages() {
       addPage(new WizardPage("yeah_page", "Yeah, innit", null) {
         @Override public void createControl(Composite parent) {
          setControl(new Composite(parent, SWT.NULL));
         }
       });
    }

    @Override
    public boolean performFinish() {
       return true;
    }

}

We’re extending Wizard to give us a wizard when we open our plugin.

The init() method gives us a reference to a IStructuredSelection which is useful to get the selected directory, for example.

The adPages() method makes us create a new page, in this case we’ve just added a blank page – we’d extend this obviously to include our UI.

Finally, performFinish() is called when the user presses the ‘Finish’ button in the wizard.

Installation

Now, if you right click on the plugin project, and run as Eclipse Application, you’ll get a new instance of Eclipse with the plugin loaded.

Now create a new Java project. Then on the ‘src’ directory, for example, right click -> New -> Other. Now you can find your plugin.

It does nothing! Glorious!

You can export the plugin by right clicking on the project and exporting as plugin fragment. This will create a .jar file you can put in [eclipse path]/dropins that will become active once you restart eclipse.

eclipse eclipse-plugin

Gradle: Generate your .classpath with sources

One of the nice features of Gradle is that the eclipse plugin will generate your .classpath for you and will fetch the sources for all the dependencies that you have specified.

This comes by default by running ‘gradle eclipse’, but you can also run it separately, thereby not modifying any of your other eclipse project files:

    gradle eclipseClasspath

You obviously have to “apply plugin: ‘eclipse’” at the top of your build.gradle file.

gradle gradle-eclipse

Gradle: Using Eclipse

If you put

    apply plugin:'eclipse'

at the top of your build.gradle file you can run ‘gradle eclipse’ which will generate the Eclipse project files, including the .classpath.

If you want to change the source and target compatibility settings:

    eclipse {
      jdt {
        sourceCompatibility = 1.6
        targetCompatibility = 1.6
      }
    }
gradle gradle-eclipse

Eclipes shortcuts

Shortcuts! I’m using Eclipse Juno in case that matters

Changing file / view / perspective

 Ctrl+Shift+R: Quickly open a file in the workspace with autocomplete
 Ctrl+E: Quickly change to an open file with autocomplete
 Ctrl+F7: Quickly change / activate a view (Editor, package inspector, etc)
 Ctrl+F8: Quickly change perspective
 Alt+(left/right): Go to previous / former position or file.

Navigating source code

 F3: Go to definition of method under the cursor
 Ctrl+O: Show the class overview, with autocomplete
 (pressing it again shows the inherited methods) 

Refactoring:

 Ctrl+1: Open the quick refactoring menu
 Ctrl+Alt+R: Rename a method / variable
 Ctrl+Alt+M: Move the highlighted code out into an extracted method
 Ctrl+Alt+L: Refactor selected text into a local variable
 Ctrl+Alt+O: Organise imports (delete unused etc)

Debugging

 Ctrl+Shift+B: Add a breakpoint to the current line
 F11: Debug last thing debugged
 F5: Step into method (while debugging)
 F6: Step over line (while debugging)
 F8: Resume (while debugging)

Running:

 Ctrl+F11: Run the previous thing which we ran  
 (must set in Preferences -> Run/Debug -> Always launch previously launched activity)
eclipse

Page 1 of 1