RSS

Search Engine

Thursday, July 15, 2010

Contribute to existing UI elements

3.1. Overview

This chapter explains how to extend existing UI elements using the example of the package explorer. The context menu is displayed if the user select a file in the package explorer via a right mouse click. We will offer the option to create a HTML page for a Java source file. file.

To contribute to an existing menu or toolbar you need to know the corresponding ID. This ID can be found via the "Menu Spy".

3.2. Using commands

The following uses Eclipse Commands. Eclipse Command to learn how to work with commands.

3.3. Contribute to package explorer

Create a new plugin project "de.vogella.plugin.htmlconverter". Do not use a template. Select the tab "Dependencies" of your "plugin.xml". Add the dependencies to "org.eclipse.jdt.core" "org.eclipse.core.resources".

Add a command with the ID "de.vogella.plugin.htmlconverter.convert" and the default handler "de.vogella.plugin.htmlconverter.handler.Convert" to your plugin. Add this command to the menu via the extension point "org.eclipse.ui.menus" and use as the "locationURI" "popup:org.eclipse.jdt.ui.PackageExplorer". Set the label to "Create HTML" for this contribution. The resulting "plugin.xml" should look like the following.

				




point="org.eclipse.ui.menus">
locationURI="popup:org.eclipse.jdt.ui.PackageExplorer">
commandId="de.vogella.plugin.htmlconverter.convert"
label="Create HTML"
style="push">



point="org.eclipse.ui.commands">
defaultHandler="de.vogella.plugin.htmlconverter.handler.Convert"
id="de.vogella.plugin.htmlconverter.convert"
name="Convert">





Create the class "de.vogella.plugin.htmlconverter.handler.Convert" Eclipse allows to save additional information for each file. You can use the interface "IResource" and the methods "setPersistentProperty()" and "getPersistentProperty()". With these functions you can save Strings on files. We use these functions to save a directory for Java source files which already were exported via HTML.

				

package de.vogella.plugin.htmlconverter.handler;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.ui.handlers.HandlerUtil;

public class Convert extends AbstractHandler {
private QualifiedName path = new QualifiedName("html", "path");

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

IStructuredSelection selection = (IStructuredSelection) HandlerUtil
.getActiveMenuSelection(event);
DirectoryDialog fileDialog = new DirectoryDialog(HandlerUtil
.getActiveShell(event));
String directory = "";
Object firstElement = selection.getFirstElement();
if (firstElement instanceof ICompilationUnit) {
ICompilationUnit cu = (ICompilationUnit) firstElement;
IResource res = cu.getResource();
boolean newDirectory = true;
directory = getPersistentProperty(res, path);

if (directory != null && directory.length() > 0) {
newDirectory = !(MessageDialog.openQuestion(HandlerUtil
.getActiveShell(event), "Question",
"Use the previous output directory?"));
}
if (newDirectory) {
directory = fileDialog.open();

}
if (directory != null && directory.length() > 0) {
analyze(cu);
setPersistentProperty(res, path, directory);
write(directory, cu);
}

} else {
MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
"Information", "Please select a Java source file");
}

// iterator.next();

// }
return null;
}

protected String getPersistentProperty(IResource res, QualifiedName qn) {
try {
return (String) res.getPersistentProperty(qn);
} catch (CoreException e) {
return "";
}
}

// TODO: Include this in the HTML output

private void analyze(ICompilationUnit cu) {
// Cool JDT allows you to analyze the code easily
// I don't see really a use case here but I just wanted to do this here
// as I consider this as cool and
// what to have a place where I can store the data
try {

IType type = null;
IType[] allTypes;
allTypes = cu.getAllTypes();
/**
* Search the public class
*/
for (int t = 0; t < allTypes.length; t++) {
if (Flags.isPublic((allTypes[t].getFlags()))) {
type = allTypes[t];
break;
}
}

String classname = type.getFullyQualifiedName();
IMethod[] methods = type.getMethods();
} catch (JavaModelException e) {
e.printStackTrace();
}

}

protected void setPersistentProperty(IResource res, QualifiedName qn,
String value) {
try {
res.setPersistentProperty(qn, value);
} catch (CoreException e) {
e.printStackTrace();
}
}

private void write(String dir, ICompilationUnit cu) {
try {
cu.getCorrespondingResource().getName();
String test = cu.getCorrespondingResource().getName();
// Need
String[] name = test.split("\\.");
System.out.println(test);
System.out.println(name.length);
String htmlFile = dir + "\\" + name[0] + ".html";

System.out.println(htmlFile);
FileWriter output = new FileWriter(htmlFile);
BufferedWriter writer = new BufferedWriter(output);
writer.write("");
writer.write("");
writer.write("");
writer.write("");
writer.write("
");

writer.write(cu.getSource());
writer.write("
");
writer.write("");
writer.write("");
writer.flush();
} catch (JavaModelException e) {
} catch (IOException e) {
e.printStackTrace();
}

}
}

Finished. If you run your plugin you should be able to create HTML output from a Java source code file.

3.4. Restrict the extension - Visible When

It would be better if the command if only display if a Source file is selected. Add the dependency "org.eclipse.core.expressions" to your plugin. Select your menu contribution. Using the right mouse add the condition to the command that it should only be visible if a file is selected which represents a "org.eclipse.jdt.core.ICompilationUnit". This is a class which can be compiled.

This will result in the following "plugin.xml".

				




point="org.eclipse.ui.menus">
locationURI="popup:org.eclipse.jdt.ui.PackageExplorer">
commandId="de.vogella.plugin.htmlconverter.convert"
label="Create HTML"
style="push">

variable="activeMenuSelection">

type="org.eclipse.jdt.core.ICompilationUnit">






point="org.eclipse.ui.commands">
defaultHandler="de.vogella.plugin.htmlconverter.handler.Convert"
id="de.vogella.plugin.htmlconverter.convert"
name="Convert">





0 comments:

Post a Comment