Search in sources :

Example 1 with Program

use of ij.macro.Program in project imagej1 by imagej.

the class Toolbar method installMenu.

void installMenu(int tool) {
    Program pgm = macroInstaller.getProgram();
    Hashtable h = pgm.getMenus();
    if (h == null)
        return;
    String[] commands = (String[]) h.get(names[tool]);
    if (commands == null)
        return;
    if (menus[tool] == null) {
        menus[tool] = new PopupMenu("");
        if (Menus.getFontSize() != 0)
            menus[tool].setFont(Menus.getFont());
        add(menus[tool]);
    } else
        menus[tool].removeAll();
    for (int i = 0; i < commands.length; i++) {
        if (commands[i].equals("-"))
            menus[tool].addSeparator();
        else if (commands[i].startsWith("-"))
            menus[tool].addSeparator();
        else {
            boolean disable = commands[i].startsWith("*");
            String command = commands[i];
            if (disable)
                command = command.substring(1);
            MenuItem mi = new MenuItem(command);
            if (disable)
                mi.setEnabled(false);
            mi.addActionListener(this);
            menus[tool].add(mi);
        }
    }
    if (tool == current)
        setTool(RECTANGLE);
}
Also used : Program(ij.macro.Program) Hashtable(java.util.Hashtable)

Example 2 with Program

use of ij.macro.Program in project astroimagej by keastrid.

the class Toolbar method installMenu.

void installMenu(int tool) {
    Program pgm = macroInstaller.getProgram();
    Hashtable h = pgm.getMenus();
    if (h == null)
        return;
    String[] commands = (String[]) h.get(names[tool]);
    if (commands == null)
        return;
    if (menus[tool] == null) {
        menus[tool] = new PopupMenu("");
        if (Menus.getFontSize() != 0)
            menus[tool].setFont(Menus.getFont());
        add(menus[tool]);
    } else
        menus[tool].removeAll();
    for (int i = 0; i < commands.length; i++) {
        if (commands[i].equals("-"))
            menus[tool].addSeparator();
        else if (commands[i].startsWith("-"))
            menus[tool].addSeparator();
        else {
            boolean disable = commands[i].startsWith("*");
            String command = commands[i];
            if (disable)
                command = command.substring(1);
            MenuItem mi = new MenuItem(command);
            if (disable)
                mi.setEnabled(false);
            mi.addActionListener(this);
            menus[tool].add(mi);
        }
    }
    if (tool == current)
        setTool(RECTANGLE);
}
Also used : Program(ij.macro.Program) Hashtable(java.util.Hashtable)

Example 3 with Program

use of ij.macro.Program in project astroimagej by keastrid.

the class ResultsTable method applyMacro.

/**
 * Applies a macro to each row of the table; the columns are assigned variable names
 *  as given by getHeadingsAsVaribleNames(). New variables starting with an uppercase letter
 *  create a new column with this name.
 *  The variable 'row' (the row index) is pre-defined.
 *  Except for the row label (if existing), currently only supports numeric values, no Strings.
 *  @return false in case of a macro error
 */
public boolean applyMacro(String macro) {
    String[] columnHeadings = getHeadings();
    // same as variable names
    String[] columnNames = getHeadingsAsVariableNames(columnHeadings);
    // corresponding column index; <0 for rowLabels
    int[] columnIndices = new int[columnHeadings.length];
    for (int i = 0; i < columnHeadings.length; i++) columnIndices[i] = getColumnIndex(columnHeadings[i]);
    Program pgm = (new Tokenizer()).tokenize(macro);
    StringBuilder sb = new StringBuilder(1000);
    sb.append("var ");
    for (int i = 0; i < columnNames.length; i++) {
        // create 'var' statement with 'real' data values, so errors are less likely
        sb.append(columnNames[i]);
        sb.append('=');
        if (columnIndices[i] < 0)
            sb.append(rowLabels[0] == null ? "\"\"" : '"' + rowLabels[0] + '"');
        else
            // avoid negative values since minus would be extra token
            sb.append(Math.abs(getValueAsDouble(columnIndices[i], 0)));
        sb.append(',');
    }
    sb.append("row;\n");
    sb.append("function dummy() {}\n");
    sb.append(macro);
    sb.append(";\n");
    String code = sb.toString();
    // 'macro' code starts at this token number
    int PCStart = 9 + 4 * columnNames.length;
    Interpreter interp = new Interpreter();
    interp.setApplyMacroTable(this);
    try {
        // first test run
        interp.run(code, null);
    } catch (Exception e) {
    }
    if (interp.wasError())
        return false;
    boolean[] columnInUse = new boolean[columnNames.length];
    ArrayList<String> newColumnList = new ArrayList<String>();
    String[] variables = interp.getVariableNames();
    for (String variable : variables) {
        // check for variables that make a new Column
        int columnNumber = indexOf(columnNames, variable);
        if (// variable is a know column
        columnNumber >= 0)
            columnInUse[columnNumber] = macro.indexOf(variable) >= 0;
        else if (Character.isUpperCase(variable.charAt(0))) {
            // create new column
            getFreeColumn(variable);
            newColumnList.add(variable);
        }
    }
    String[] newColumns = newColumnList.toArray(new String[0]);
    int[] newColumnIndices = new int[newColumns.length];
    for (int i = 0; i < newColumns.length; i++) newColumnIndices[i] = getColumnIndex(newColumns[i]);
    for (int row = 0; row < counter; row++) {
        // apply macro to each row
        for (int col = 0; col < columnHeadings.length; col++) {
            if (columnInUse[col]) {
                // set variable values for used columns
                if (columnIndices[col] < 0) {
                    String str = rowLabels[row];
                    interp.setVariable(columnNames[col], str);
                } else {
                    double v = getValueAsDouble(columnIndices[col], row);
                    interp.setVariable(columnNames[col], v);
                }
            }
        }
        interp.setVariable("row", row);
        interp.run(PCStart);
        if (interp.wasError())
            return false;
        for (int col = 0; col < columnNames.length; col++) {
            if (columnInUse[col]) {
                // set new values for previous columns
                if (columnIndices[col] < 0) {
                    String str = interp.getVariableAsString(columnNames[col]);
                    rowLabels[row] = str;
                } else {
                    double v = interp.getVariable(columnNames[col]);
                    setValue(columnIndices[col], row, v);
                }
            }
        }
        for (int i = 0; i < newColumns.length; i++) {
            // set new values for newly-created columns
            double v = interp.getVariable(newColumns[i]);
            setValue(newColumnIndices[i], row, v);
        }
    }
    return true;
}
Also used : Program(ij.macro.Program) Interpreter(ij.macro.Interpreter) Tokenizer(ij.macro.Tokenizer)

Aggregations

Program (ij.macro.Program)3 Hashtable (java.util.Hashtable)2 Interpreter (ij.macro.Interpreter)1 Tokenizer (ij.macro.Tokenizer)1