Search in sources :

Example 16 with AstroImageJ

use of ij.astro.AstroImageJ in project astroimagej by keastrid.

the class GenericDialog method addSlider.

private void addSlider(String label, double minValue, double maxValue, double defaultValue, double scale, int digits) {
    @AstroImageJ(reason = "Value text box on Mac is two characters too narrow.", modified = true) int columns = 4 + digits + (IJ.isMacOSX() ? 0 : -2);
    if (columns < 4)
        columns = 4;
    if (minValue < 0.0)
        columns++;
    String mv = IJ.d2s(maxValue, 0);
    if (mv.length() > 4 && digits == 0)
        columns += mv.length() - 4;
    String label2 = label;
    if (label2.indexOf('_') != -1)
        label2 = label2.replace('_', ' ');
    Label fieldLabel = makeLabel(label2);
    this.lastLabelAdded = fieldLabel;
    if (addToSameRow) {
        c.gridx = GridBagConstraints.RELATIVE;
        c.insets.bottom += 3;
        addToSameRow = false;
    } else {
        c.gridx = 0;
        c.gridy++;
        // top, left, bottom, right
        c.insets = getInsets(0, 0, 3, 0);
    }
    c.anchor = GridBagConstraints.EAST;
    c.gridwidth = 1;
    add(fieldLabel, c);
    if (slider == null) {
        slider = new Vector(5);
        sliderIndexes = new Vector(5);
        sliderScales = new Vector(5);
        sliderDigits = new Vector(5);
    }
    Scrollbar s = new Scrollbar(Scrollbar.HORIZONTAL, (int) defaultValue, 1, (int) minValue, (int) maxValue + 1);
    GUI.fixScrollbar(s);
    slider.addElement(s);
    s.addAdjustmentListener(this);
    s.setUnitIncrement(1);
    if (IJ.isMacOSX())
        s.addKeyListener(this);
    if (numberField == null) {
        numberField = new Vector(5);
        defaultValues = new Vector(5);
        defaultText = new Vector(5);
    }
    if (IJ.isWindows())
        columns -= 2;
    if (columns < 1)
        columns = 1;
    // IJ.log("scale=" + scale + ", columns=" + columns + ", digits=" + digits);
    TextField tf = new TextField(IJ.d2s(defaultValue / scale, digits), columns);
    if (IJ.isLinux())
        tf.setBackground(Color.white);
    tf.addActionListener(this);
    tf.addTextListener(this);
    tf.addFocusListener(this);
    tf.addKeyListener(this);
    numberField.addElement(tf);
    sliderIndexes.add(Integer.valueOf(numberField.size() - 1));
    sliderScales.add(Double.valueOf(scale));
    sliderDigits.add(Integer.valueOf(digits));
    defaultValues.addElement(Double.valueOf(defaultValue / scale));
    defaultText.addElement(tf.getText());
    tf.setEditable(true);
    firstSlider = false;
    Panel panel = new Panel();
    GridBagLayout pgrid = new GridBagLayout();
    GridBagConstraints pc = new GridBagConstraints();
    panel.setLayout(pgrid);
    pc.gridx = 0;
    pc.gridy = 0;
    pc.gridwidth = 1;
    pc.ipadx = 85;
    pc.anchor = GridBagConstraints.WEST;
    panel.add(s, pc);
    // reset
    pc.ipadx = 0;
    // text field
    pc.gridx = 1;
    pc.insets = new Insets(5, 5, 0, 0);
    pc.anchor = GridBagConstraints.EAST;
    panel.add(tf, pc);
    c.gridx = GridBagConstraints.RELATIVE;
    c.gridwidth = 1;
    c.anchor = GridBagConstraints.WEST;
    c.insets.left = 0;
    c.insets.bottom -= 3;
    add(panel, c);
    if (Recorder.record || macro)
        saveLabel(tf, label);
}
Also used : AstroImageJ(ij.astro.AstroImageJ) Vector(java.util.Vector)

Example 17 with AstroImageJ

use of ij.astro.AstroImageJ in project astroimagej by keastrid.

the class ImageCanvas method mouseReleased.

@AstroImageJ(reason = "Update x/yClicked", modified = true)
public void mouseReleased(MouseEvent e) {
    xClicked = e.getX();
    yClicked = e.getY();
    if (pressTimer != null) {
        pressTimer.cancel();
        pressTimer = null;
    }
    int ox = offScreenX(e.getX());
    int oy = offScreenY(e.getY());
    Overlay overlay = imp.getOverlay();
    if ((overlay != null || showAllOverlay != null) && ox == mousePressedX && oy == mousePressedY) {
        boolean cmdDown = IJ.isMacOSX() && e.isMetaDown();
        Roi roi = imp.getRoi();
        if (roi != null && roi.getBounds().width == 0)
            roi = null;
        if ((e.isAltDown() || e.isControlDown() || cmdDown) && roi == null) {
            if (activateOverlayRoi(ox, oy))
                return;
        } else if ((System.currentTimeMillis() - mousePressedTime) > 250L && !drawingTool()) {
            // long press
            if (activateOverlayRoi(ox, oy))
                return;
        }
    }
    PlugInTool tool = Toolbar.getPlugInTool();
    if (tool != null) {
        tool.mouseReleased(imp, e);
        if (e.isConsumed())
            return;
    }
    flags = e.getModifiers();
    // make sure button 1 bit is not set
    flags &= ~InputEvent.BUTTON1_MASK;
    // make sure button 2 bit is not set
    flags &= ~InputEvent.BUTTON2_MASK;
    // make sure button 3 bit is not set
    flags &= ~InputEvent.BUTTON3_MASK;
    Roi roi = imp.getRoi();
    if (roi != null) {
        Rectangle r = roi.getBounds();
        int type = roi.getType();
        if ((r.width == 0 || r.height == 0) && !(type == Roi.POLYGON || type == Roi.POLYLINE || type == Roi.ANGLE || type == Roi.LINE) && !(roi instanceof TextRoi) && roi.getState() == roi.CONSTRUCTING && type != roi.POINT)
            imp.deleteRoi();
        else
            roi.handleMouseUp(e.getX(), e.getY());
    }
}
Also used : PlugInTool(ij.plugin.tool.PlugInTool) AstroImageJ(ij.astro.AstroImageJ)

Example 18 with AstroImageJ

use of ij.astro.AstroImageJ in project astroimagej by keastrid.

the class ResultsTable method saveAs.

@AstroImageJ(reason = "Save table with 16 decimal places, not 6", modified = true)
public void saveAs(String path) throws IOException {
    boolean emptyTable = size() == 0 && lastColumn < 0;
    var oldPrecision = getPrecision();
    setPrecision(16);
    if (path == null || path.equals("")) {
        SaveDialog sd = new SaveDialog("Save Table", "Table", Prefs.defaultResultsExtension());
        String file = sd.getFileName();
        if (file == null)
            return;
        path = sd.getDirectory() + file;
    }
    boolean csv = path.endsWith(".csv") || path.endsWith(".CSV");
    delimiter = csv ? ',' : '\t';
    PrintWriter pw = null;
    FileOutputStream fos = new FileOutputStream(path);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    pw = new PrintWriter(bos);
    boolean saveShowRowNumbers = showRowNumbers;
    if (Prefs.dontSaveRowNumbers)
        showRowNumbers = false;
    if (saveColumnHeaders && !emptyTable) {
        String headings = getColumnHeadings();
        pw.println(headings);
    }
    quoteCommas = csv ? true : false;
    for (int i = 0; i < size(); i++) pw.println(getRowAsString(i));
    quoteCommas = false;
    showRowNumbers = saveShowRowNumbers;
    pw.close();
    delimiter = '\t';
    if (renameWhenSaving) {
        File f = new File(path);
        title = f.getName();
    }
    setPrecision(oldPrecision);
}
Also used : SaveDialog(ij.io.SaveDialog) AstroImageJ(ij.astro.AstroImageJ)

Example 19 with AstroImageJ

use of ij.astro.AstroImageJ in project astroimagej by keastrid.

the class ResultsTable method show.

/**
 * Displays the contents of this ResultsTable in a window with
 *		the specified title, or updates an existing results window. Opens
 *		a new window if there is no open text window with this title.
 *		The title must be "Results" if this table was obtained using
 *		ResultsTable.getResultsTable() or Analyzer.getResultsTable .
 */
@AstroImageJ(reason = "Disable bringing table to the front when it updates", modified = true)
public void show(String windowTitle) {
    if (GraphicsEnvironment.isHeadless())
        // Tables can't be displayed in headless mode
        return;
    if (windowTitle == null)
        windowTitle = "Results";
    title = windowTitle;
    if (!windowTitle.equals("Results") && this == Analyzer.getResultsTable())
        IJ.log("ResultsTable.show(): the system ResultTable should only be displayed in the \"Results\" window.");
    if (windowTitle.equals("Results") && !showRowNumbersSet)
        showRowNumbers(true);
    String tableHeadings = getColumnHeadings();
    TextPanel tp;
    boolean newWindow = false;
    boolean cloneNeeded = false;
    if (windowTitle.equals("Results")) {
        tp = IJ.getTextPanel();
        if (tp == null)
            return;
        newWindow = tp.getLineCount() == 0;
        if (!newWindow && tp.getLineCount() == size() - 1 && ResultsTable.getResultsTable() == this && tp.getColumnHeadings().equals(tableHeadings)) {
            String s = getRowAsString(size() - 1);
            tp.append(s);
            return;
        }
        IJ.setColumnHeadings(tableHeadings);
        if (this != Analyzer.getResultsTable())
            Analyzer.setResultsTable(this);
        if (size() > 0)
            Analyzer.setUnsavedMeasurements(true);
    } else {
        Frame frame = WindowManager.getFrame(windowTitle);
        TextWindow win;
        if (frame != null && frame instanceof TextWindow) {
            win = (TextWindow) frame;
            if (win != null) {
            // win.toFront();
            // WindowManager.setWindow(frame);
            }
        } else {
            int chars = Math.max(size() > 0 ? getRowAsString(0).length() : 15, getColumnHeadings().length());
            int width = 100 + chars * 10;
            if (width < 180)
                width = 180;
            if (width > 700)
                width = 700;
            if (showRowNumbers)
                width += 50;
            int height = 300;
            if (size() > 15)
                height = 400;
            if (size() > 30 && width > 300)
                height = 500;
            win = new TextWindow(windowTitle, "", width, height);
            cloneNeeded = true;
        }
        tp = win.getTextPanel();
        tp.setColumnHeadings(tableHeadings);
        newWindow = tp.getLineCount() == 0;
    }
    tp.setResultsTable(cloneNeeded ? (ResultsTable) this.clone() : this);
    int n = size();
    if (n > 0) {
        if (tp.getLineCount() > 0)
            tp.clear();
        for (int i = 0; i < n; i++) tp.appendWithoutUpdate(getRowAsString(i));
        tp.updateDisplay();
    }
    if (newWindow)
        tp.scrollToTop();
}
Also used : TextWindow(ij.text.TextWindow) TextPanel(ij.text.TextPanel) AstroImageJ(ij.astro.AstroImageJ)

Example 20 with AstroImageJ

use of ij.astro.AstroImageJ in project astroimagej by keastrid.

the class Opener method openZip.

/**
 * Opens a single TIFF or DICOM contained in a ZIP archive,
 *		or a ZIPed collection of ".roi" files created by the ROI manager.
 */
@AstroImageJ(reason = "add support for more file types; open tables in AIJ measurements table; " + "open zip as folder", modified = true)
public ImagePlus openZip(String path) {
    ImagePlus imp = null;
    // Open zip as folder iff it contains FITS files
    var fo = new FolderOpener();
    fo.setDirectory(path);
    var hasFits = Arrays.stream(ZipOpenerUtil.getFilesInZip(path)).map(ZipOpenerUtil.InternalZipFile::path).anyMatch(s -> getFileType(s, false) == Opener.FITS || getFileType(s, false) == Opener.PNG);
    if (hasFits) {
        if (fo.showDialog()) {
            return fo.openFolder(path);
        } else {
            return null;
        }
    }
    try {
        ZipInputStream zis = new ZipInputStream(new FileInputStream(path));
        ZipEntry entry = zis.getNextEntry();
        if (entry == null) {
            zis.close();
            return null;
        }
        String name = entry.getName();
        if (name.endsWith(".roi")) {
            zis.close();
            if (!silentMode)
                if (IJ.isMacro() && Interpreter.isBatchMode() && RoiManager.getInstance() == null)
                    IJ.log("Use roiManager(\"Open\", path) instead of open(path)\nto open ROI sets in batch mode macros.");
                else
                    IJ.runMacro("roiManager(\"Open\", getArgument());", path);
            return null;
        }
        if (name.endsWith(".tif")) {
            imp = openTiff(zis, name);
        } else if (name.endsWith(".dcm")) {
            DICOM dcm = new DICOM(zis);
            dcm.run(name);
            imp = dcm;
        } else if (name.endsWith(".fts") || name.endsWith(".fits") || name.endsWith(".fit") || name.endsWith("fts.fz") || name.endsWith(".fits.fz") || name.endsWith(".fit.fz")) {
            imp = (ImagePlus) IJ.runPlugIn("ij.plugin.FITS_Reader", path + '\\' + name.replace('/', '\\'));
            if (imp.getWidth() != 0)
                return imp;
            else
                return null;
        } else {
            zis.close();
            String msg = "This ZIP archive does not appear to contain a \nTIFF (\".tif\") or DICOM (\".dcm\") or FITS file, or ROIs (\".roi\").\n\t" + path;
            if (silentMode)
                IJ.log(msg);
            else
                IJ.error("Opener", msg);
            return null;
        }
        zis.close();
    } catch (Exception e) {
        IJ.error("Opener", "" + e);
        return null;
    }
    File f = new File(path);
    FileInfo fi = imp.getOriginalFileInfo();
    if (fi != null) {
        fi.fileFormat = FileInfo.ZIP_ARCHIVE;
        fi.fileName = f.getName();
        String parent = f.getParent();
        if (parent != null)
            fi.directory = parent + File.separator;
    }
    return imp;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ZipOpenerUtil(ij.astro.util.ZipOpenerUtil) ZipFile(java.util.zip.ZipFile) MalformedURLException(java.net.MalformedURLException) AstroImageJ(ij.astro.AstroImageJ)

Aggregations

AstroImageJ (ij.astro.AstroImageJ)28 File (java.io.File)4 ZipFile (java.util.zip.ZipFile)4 SaveDialog (ij.io.SaveDialog)3 ResultsTable (ij.measure.ResultsTable)3 ImageProcessor (ij.process.ImageProcessor)3 URL (java.net.URL)3 GenericDialog (ij.gui.GenericDialog)2 Opener (ij.io.Opener)2 TextWindow (ij.text.TextWindow)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 ZipInputStream (java.util.zip.ZipInputStream)2 ZipOpenerUtil (ij.astro.util.ZipOpenerUtil)1 ImageWindow (ij.gui.ImageWindow)1 Overlay (ij.gui.Overlay)1 Roi (ij.gui.Roi)1 FileInfo (ij.io.FileInfo)1