From ecdb827a047b9a7bf0fdd90f8b20c82e8b903286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jer=C3=B4me=20Mutterer?= Date: Mon, 20 Dec 2010 17:32:20 +0100 Subject: [PATCH] Add the Action Bar plugin Requested by Martin Bergert. Signed-off-by: Johannes Schindelin --- .gitignore | 1 + Fakefile | 1 + src-plugins/Action_Bar/Action_Bar.java | 435 +++++++++++++++++++++++++ src-plugins/Action_Bar/plugins.config | 5 + 4 files changed, 442 insertions(+) create mode 100644 src-plugins/Action_Bar/Action_Bar.java create mode 100644 src-plugins/Action_Bar/plugins.config diff --git a/.gitignore b/.gitignore index 247da198e3..8bd1442476 100644 --- a/.gitignore +++ b/.gitignore @@ -209,3 +209,4 @@ /jars/mij.jar /plugins/Temporal_Color_Coder.jar /plugins/Samples_.jar +/plugins/Action_Bar.jar diff --git a/Fakefile b/Fakefile index a01e6466bb..9f0956bf8e 100644 --- a/Fakefile +++ b/Fakefile @@ -203,6 +203,7 @@ PLUGIN_TARGETS=plugins/Jython_Interpreter.jar \ plugins/Jython_Scripts.jar \ plugins/Temporal_Color_Coder.jar \ plugins/Samples_.jar \ + plugins/Action_Bar.jar \ jars/mij.jar all <- fiji $SUBMODULE_TARGETS $PLUGIN_TARGETS diff --git a/src-plugins/Action_Bar/Action_Bar.java b/src-plugins/Action_Bar/Action_Bar.java new file mode 100644 index 0000000000..75b17bbed3 --- /dev/null +++ b/src-plugins/Action_Bar/Action_Bar.java @@ -0,0 +1,435 @@ +import ij.*; +import ij.gui.*; +import ij.plugin.*; +import ij.macro.*; +import java.awt.*; +import java.awt.dnd.*; +import java.awt.event.*; +import java.io.*; +import java.net.URL; +import javax.swing.*; + +/** + * @author jerome.mutterer(at)ibmp.fr + * + */ + +public class Action_Bar implements PlugIn, ActionListener { + + // private static final long serialVersionUID = 7436194992984622141L; + String macrodir = IJ.getDirectory("macros"); + String name, title, path; + String startupAction = ""; + String codeLibrary = ""; + String separator = System.getProperty("file.separator"); + JFrame frame = new JFrame(); + Frame frontframe; + int xfw = 0; + int yfw = 0; + int wfw = 0; + int hfw = 0; + JToolBar toolBar = null; + boolean tbOpenned = false; + boolean grid = true; + boolean visible = true; + boolean shouldExit = false; + JButton button = null; + private boolean isPopup = false; + private boolean isSticky = false; + int nButtons = 0; + + public void run(String s) { + // s used if called from another plugin, or from an installed command. + // arg used when called from a run("command", arg) macro function + // if both are empty, we choose to run the assistant "createAB.txt" + + String arg = Macro.getOptions(); + + if (arg == null && s.equals("")) { + try { + File macro = new File(Action_Bar.class.getResource( + "createAB.txt").getFile()); + new MacroRunner(macro); + return; + } catch (Exception e) { + IJ.error("createAB.txt file not found"); + } + + } else if (arg == null) { // call from an installed command + path = IJ.getDirectory("startup") + s; + try { + name = path.substring(path.lastIndexOf("/") + 1); + } catch (Exception e) { + } + } else { // called from a macro by run("Action Bar",arg) + path = IJ.getDirectory("startup") + arg; + try { + path = path.substring(0, path.indexOf(".txt") + 4); + name = path.substring(path.lastIndexOf("/") + 1); + } catch (Exception e) { + } + } + + // title = name.substring(0, name.indexOf(".")); + title = name.substring(0, name.indexOf(".")).replaceAll("_", " ") + .trim(); + frame.setTitle(title); + + if (WindowManager.getFrame(title) != null) { + WindowManager.getFrame(title).toFront(); + return; + } + // this listener will save the bar's position and close it. + frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + rememberXYlocation(); + e.getWindow().dispose(); + WindowManager.removeWindow((Frame) frame); + } + }); + frontframe = WindowManager.getFrontWindow(); + if (frontframe != null){ + xfw = frontframe.getLocation().x; + yfw = frontframe.getLocation().y; + wfw = frontframe.getWidth(); + hfw = frontframe.getHeight(); + } + // toolbars will be added as lines in a n(0) rows 1 column layout + frame.getContentPane().setLayout(new GridLayout(0, 1)); + + // sets the bar's default icon to imagej icon + frame.setIconImage(IJ.getInstance().getIconImage()); + + // read the config file, and add toolbars to the frame + designPanel(); + + // captures the ImageJ KeyListener + frame.setFocusable(true); + frame.addKeyListener(IJ.getInstance()); + + // setup the frame, and display it + frame.setResizable(false); + + if (!isPopup) { + frame.setLocation((int) Prefs + .get("actionbar" + title + ".xloc", 10), (int) Prefs.get( + "actionbar" + title + ".yloc", 10)); + WindowManager.addWindow(frame); + } + + else { + frame.setLocation(MouseInfo.getPointerInfo().getLocation()); + frame.setUndecorated(true); + frame.addKeyListener(new KeyListener() { + public void keyReleased(KeyEvent e) { + } + + public void keyTyped(KeyEvent e) { + } + + public void keyPressed(KeyEvent e) { + int code = e.getKeyCode(); + if (code == KeyEvent.VK_ESCAPE) { + frame.dispose(); + WindowManager.removeWindow(frame); + } + } + }); + } + + if (isSticky) { + frame.setUndecorated(true); + } + frame.pack(); + frame.setVisible(true); + if (startupAction != "") + try { + new MacroRunner(startupAction + "\n" + codeLibrary); + } catch (Exception fe) { + } + + WindowManager.setWindow(frontframe); + + if (isSticky) { + stickToActiveWindow(); + while ((shouldExit==false)&& (frame.getTitle()!="xxxx")){ + try { + + ImageWindow fw = WindowManager.getCurrentWindow(); + if (fw == null) + frame.setVisible(false); + if ((fw != null) && (fw.getLocation().x != xfw) + || (fw.getLocation().y != yfw) + || (fw.getWidth() != wfw) + || (fw.getHeight() != hfw)) { + xfw = fw.getLocation().x; + yfw = fw.getLocation().y; + wfw = fw.getWidth(); + hfw = fw.getHeight(); + stickToActiveWindow(); + } + } catch (Exception e) { + // TODO: handle exception + } + IJ.wait(20); + } + if (frame.getTitle()=="xxxx") closeActionBar(); + if ((shouldExit)) return; + } + + } + + private void stickToActiveWindow() { + ImageWindow fw = WindowManager.getCurrentWindow(); + try { + if (fw != null) { + if (!frame.isVisible()) + frame.setVisible(true); + frame.toFront(); + frame.setLocation(fw.getLocation().x + fw.getWidth(), fw + .getLocation().y); + fw.toFront(); + } + } catch (Exception e) { + // TODO: handle exception + } + + } + + private void closeActionBar() { + frame.dispose(); + WindowManager.removeWindow(frame); + WindowManager.setWindow(frontframe); + shouldExit = true; + } + + private void designPanel() { + try { + File file = new File(path); + if (!file.exists()) + IJ.error("Config File not found"); + BufferedReader r = new BufferedReader(new FileReader(file)); + while (true) { + String s = r.readLine(); + if (s.equals(null)) { + r.close(); + closeToolBar(); + break; + } else if (s.startsWith("
")) { + setABasMain(); + hideIJ(); + } else if (s.startsWith("")) { + isPopup = true; + } else if (s.startsWith("")) { + isSticky = true; + } else if (s.startsWith("")) { + setABDnD(); + } else if (s.startsWith("")) { + setABonTop(); + } else if (s.startsWith("")) { + String code = ""; + while (true) { + String sc = r.readLine(); + if (sc.equals(null)) { + break; + } + if (!sc.startsWith("")) { + code = code + "" + sc; + } else { + startupAction = code; + break; + } + } + } else if (s.startsWith("")) { + String code = ""; + while (true) { + String sc = r.readLine(); + if (sc.equals(null)) { + break; + } + if (!sc.startsWith("")) { + code = code + "" + sc; + } else { + codeLibrary = code; + break; + } + } + } else if (s.startsWith("")) { + String frameiconName = r.readLine().substring(5); + setABIcon(frameiconName); + } else if (s.startsWith("") && tbOpenned == false) { + grid = false; + } else if (s.startsWith("") && tbOpenned == false) { + frame.getContentPane().add(new JLabel(s.substring(6))); + } else if (s.startsWith("") && tbOpenned == false) { + toolBar = new JToolBar(); + nButtons = 0; + tbOpenned = true; + } else if (s.startsWith("") && tbOpenned == true) { + closeToolBar(); + tbOpenned = false; + } else if (s.startsWith("") && tbOpenned == true) { + toolBar.addSeparator(); + } else if (s.startsWith("