Sunday, September 28, 2008

Template Tools Eclipse plug-in

UPDATE:
Template Tools Eclipse plug-in enhances the functionality of code templates:
  • ${clipboard} template variable - replaced by the contents of the clipboard.
  • ${prompt(input|file|directory|enumeration, value [, value]*|color|font)}
    • input - the user is prompted to enter a value
    • file - the user is prompted to select a file
    • directory - the user is prompted to select a directory
    • enumeration - the user is prompted to select a value from the list of values
    • color - the user is prompted to select a color using a color dialog. The color value can be formatted using java.util.Formatter strings. The parameters passed to the format are red, green or blue color component values. If no format is specified the value is formatted as the org.eclipse.swt.graphics.RGB.toString() value of the color.
    • font - the user is prompted to select a font using a FontDialog. The returned value is the org.eclipse.swt.graphics.FontData.toString() value returned by the font dialog.

This plug-in also supports extension-point promptProviders. Here is an example:

  <extension
point="TemplateTools.promptProviders">
<promptProvider
class="templatetools.InputPrompt"
type="input">
</promptProvider>
:
:
</extension>

Here is the implementation:


package templatetools;

import java.util.Formatter;
import java.util.List;

import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.ui.PlatformUI;

public class ColorPrompt implements IPrompt {

@SuppressWarnings("unchecked")
public String getValue(String name, List params) {
ColorDialog colorDilaog = new ColorDialog(PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getShell());
RGB color = colorDilaog.open();
if (color != null) {
if (params.size() > 0) {
Object format = params.get(0);
if (format instanceof String) {
StringBuffer formatBuffer = new StringBuffer();
new Formatter(formatBuffer).format((String) format,
color.red, color.green, color.blue);
return formatBuffer.toString();
}
} else {
return color.toString();
}
}
return null;
}

}

You can download the plug-in here.

DISCLAIMER: This plug-in is experimental. So no guarantees. Use the plug-in at your own risk.


1 comment:

joker said...

Could you give one example of usage your prompt:enumaration template?