1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
package ij.plugin;
import java.awt.*;
import ij.*;
import ij.gui.*;
import ij.plugin.frame.Editor;
import ij.text.TextWindow;
import ij.io.SaveDialog;
/** This class creates a new macro or the Java source for a new plugin. */
public class NewPlugin implements PlugIn {
public static final int MACRO=0, JAVASCRIPT=1, PLUGIN=2, PLUGIN_FILTER=3, PLUGIN_FRAME=4, TEXT_FILE=5, TABLE=6;
private static int rows = 16;
private static int columns = 60;
private static int tableWidth = 350;
private static int tableHeight = 250;
private int type = MACRO;
private String name = "Macro.txt";
private boolean monospaced;
private boolean menuBar = true;
private Editor ed;
public void run(String arg) {
type = -1;
if (arg.startsWith("text")||arg.equals("")) {
type = TEXT_FILE;
name = "Untitled.txt";
} else if (arg.equals("macro")) {
type = MACRO;
name = "Macro.txt";
} else if (arg.equals("javascript")) {
type = JAVASCRIPT;
name = "Script.js";
} else if (arg.equals("plugin")) {
type = PLUGIN;
name = "My_Plugin.java";
} else if (arg.equals("frame")) {
type = PLUGIN_FRAME;
name = "Plugin_Frame.java";
} else if (arg.equals("filter")) {
type = PLUGIN_FILTER;
name = "Filter_Plugin.java";
} else if (arg.equals("table")) {
String options = Macro.getOptions();
if (IJ.isMacro() && options!=null && options.indexOf("[Text File]")!=-1) {
type = TEXT_FILE;
name = "Untitled.txt";
arg = "text+dialog";
} else {
type = TABLE;
name = "Table";
}
}
menuBar = true;
if (arg.equals("text+dialog") || type==TABLE) {
if (!showDialog()) return;
}
if (type==-1)
createPlugin("Converted_Macro.java", PLUGIN, arg);
else if (type==MACRO || type==TEXT_FILE || type==JAVASCRIPT) {
if (type==TEXT_FILE && name.equals("Macro"))
name = "Untitled.txt";
createMacro(name);
} else if (type==TABLE)
createTable();
else
createPlugin(name, type, arg);
}
public void createMacro(String name) {
int options = (monospaced?Editor.MONOSPACED:0)+(menuBar?Editor.MENU_BAR:0);
ed = new Editor(rows, columns, 0, options);
if (type==MACRO && !name.endsWith(".txt"))
name = SaveDialog.setExtension(name, ".txt");
else if (type==JAVASCRIPT && !name.endsWith(".js")) {
if (name.equals("Macro")) name = "script";
name = SaveDialog.setExtension(name, ".js");
}
ed.create(name, "");
}
void createTable() {
new TextWindow(name, "", tableWidth, tableHeight);
}
public void createPlugin(String name, int type, String methods) {
ed = (Editor)IJ.runPlugIn("ij.plugin.frame.Editor", "");
if (ed==null) return;
String pluginName = name;
if (!(name.endsWith(".java") || name.endsWith(".JAVA")))
name = SaveDialog.setExtension(name, ".java");
String className = pluginName.substring(0, pluginName.length()-5);
String text = "";
text += "import ij.*;\n";
text += "import ij.process.*;\n";
text += "import ij.gui.*;\n";
text += "import java.awt.*;\n";
switch (type) {
case PLUGIN:
text += "import ij.plugin.*;\n";
text += "import ij.plugin.frame.*;\n";
text += "\n";
text += "public class "+className+" implements PlugIn {\n";
text += "\n";
text += "\tpublic void run(String arg) {\n";
if (methods.equals("plugin"))
text += "\t\tIJ.showMessage(\""+className+"\",\"Hello world!\");\n";
else
text += methods;
text += "\t}\n";
break;
case PLUGIN_FILTER:
text += "import ij.plugin.filter.*;\n";
text += "\n";
text += "public class "+className+" implements PlugInFilter {\n";
text += "\tImagePlus imp;\n";
text += "\n";
text += "\tpublic int setup(String arg, ImagePlus imp) {\n";
text += "\t\tthis.imp = imp;\n";
text += "\t\treturn DOES_ALL;\n";
text += "\t}\n";
text += "\n";
text += "\tpublic void run(ImageProcessor ip) {\n";
text += "\t\tip.invert();\n";
text += "\t\timp.updateAndDraw();\n";
text += "\t\tIJ.wait(500);\n";
text += "\t\tip.invert();\n";
text += "\t\timp.updateAndDraw();\n";
text += "\t}\n";
break;
case PLUGIN_FRAME:
text += "import ij.plugin.frame.*;\n";
text += "\n";
text += "public class "+className+" extends PlugInFrame {\n";
text += "\n";
text += "\tpublic "+className+"() {\n";
text += "\t\tsuper(\""+className+"\");\n";
text += "\t\tTextArea ta = new TextArea(15, 50);\n";
text += "\t\tadd(ta);\n";
text += "\t\tpack();\n";
text += "\t\tGUI.center(this);\n";
text += "\t\tshow();\n";
text += "\t}\n";
break;
}
text += "\n";
text += "}\n";
ed.create(pluginName, text);
}
public boolean showDialog() {
String title;
String widthUnit, heightUnit;
int width, height;
if (type==TABLE) {
title = "New Table";
name = "Table";
width = tableWidth;
height = tableHeight;
widthUnit = "pixels";
heightUnit = "pixels";
} else {
title = "New Text Window";
name = "Untitled";
width = columns;
height = rows;
widthUnit = "characters";
heightUnit = "lines";
}
GenericDialog gd = new GenericDialog(title);
gd.addStringField("Name:", name, 16);
gd.addMessage("");
gd.addNumericField("Width:", width, 0, 3, widthUnit);
gd.addNumericField("Height:", height, 0, 3, heightUnit);
if (type!=TABLE) {
gd.setInsets(5, 30, 0);
gd.addCheckbox("Menu Bar", true);
gd.setInsets(0, 30, 0);
gd.addCheckbox("Monospaced Font", monospaced);
}
gd.showDialog();
if (gd.wasCanceled())
return false;
name = gd.getNextString();
width = (int)gd.getNextNumber();
height = (int)gd.getNextNumber();
if (width<1) width = 1;
if (height<1) height = 1;
if (type!=TABLE) {
menuBar = gd.getNextBoolean();
monospaced = gd.getNextBoolean();
columns = width;
rows = height;
if (rows>100) rows = 100;
if (columns>200) columns = 200;
} else {
tableWidth = width;
tableHeight = height;
if (tableWidth<128) tableWidth = 128;
if (tableHeight<75) tableHeight = 75;
}
return true;
}
/** Returns the Editor the newly created macro or plugin was opened in. */
public Editor getEditor() {
return ed;
}
}
|