diff --git a/src/main/java/ai/nets/samj/gui/GridPanel.java b/src/main/java/ai/nets/samj/gui/GridPanel.java deleted file mode 100644 index 849d781..0000000 --- a/src/main/java/ai/nets/samj/gui/GridPanel.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * bilib --- Java Bioimaging Library --- - * - * Author: Daniel Sage, Biomedical Imaging Group, EPFL, Lausanne, Switzerland - * - * Conditions of use: You are free to use this software for research or - * educational purposes. In addition, we expect you to include adequate - * citations and acknowledgments whenever you present or publish results that - * are based on it. - */ - -/* - * Copyright 2010-2017 Biomedical Imaging Group at the EPFL. - * - * This file is part of DeconvolutionLab2 (DL2). - * - * DL2 is free software: you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any later - * version. - * - * DL2 is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - * A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with - * DL2. If not, see . - */ -package ai.nets.samj.gui; - - -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.Insets; - -import javax.swing.BorderFactory; -import javax.swing.JComponent; -import javax.swing.JLabel; -import javax.swing.JPanel; - -/** - * This class extends the JPanel to create grid panel given the possibility to - * place Java components in an organized manner in the dialog box. - * - * @author Daniel Sage, Biomedical Imaging Group, EPFL, Lausanne, Switzerland. - * - */ -public class GridPanel extends JPanel { - - private static final long serialVersionUID = 1L; - private GridBagLayout layout = new GridBagLayout(); - private GridBagConstraints constraint = new GridBagConstraints(); - private int defaultSpace = 3; - - /** - * Constructor. - */ - public GridPanel() { - super(); - setLayout(layout); - setBorder(BorderFactory.createEtchedBorder()); - } - - /** - * Constructor. - */ - public GridPanel(int defaultSpace) { - super(); - setLayout(layout); - this.defaultSpace = defaultSpace; - setBorder(BorderFactory.createEtchedBorder()); - } - - /** - * Constructor. - */ - public GridPanel(boolean border) { - super(); - setLayout(layout); - if (border) { - setBorder(BorderFactory.createEtchedBorder()); - } - } - - /** - * Constructor. - */ - public GridPanel(String title) { - super(); - setLayout(layout); - setBorder(BorderFactory.createTitledBorder(title)); - } - - /** - * Constructor. - */ - public GridPanel(boolean border, int defaultSpace) { - super(); - setLayout(layout); - this.defaultSpace = defaultSpace; - if (border) { - setBorder(BorderFactory.createEtchedBorder()); - } - } - - /** - * Constructor. - */ - public GridPanel(String title, int defaultSpace) { - super(); - setLayout(layout); - this.defaultSpace = defaultSpace; - setBorder(BorderFactory.createTitledBorder(title)); - } - - /** - * Specify the defaultSpace. - */ - public void setSpace(int defaultSpace) { - this.defaultSpace = defaultSpace; - } - - /** - * Place a component in the northwest of the cell. - */ - public void place(int row, int col, String label) { - place(row, col, 1, 1, defaultSpace, new JLabel(label)); - } - - /** - * Place a component in the northwest of the cell. - */ - public void place(int row, int col, int space, String label) { - place(row, col, 1, 1, space, new JLabel(label)); - } - - /** - * Place a component in the northwest of the cell. - */ - public void place(int row, int col, int width, int height, String label) { - place(row, col, width, height, defaultSpace, new JLabel(label)); - } - - /** - * Place a component in the northwest of the cell. - */ - public void place(int row, int col, JComponent comp) { - place(row, col, 1, 1, defaultSpace, comp); - } - - /** - * Place a component in the northwest of the cell. - */ - public void place(int row, int col, int space, JComponent comp) { - place(row, col, 1, 1, space, comp); - } - - /** - * Place a component in the northwest of the cell. - */ - public void place(int row, int col, int width, int height, JComponent comp) { - place(row, col, width, height, defaultSpace, comp); - } - - /** - * Place a component in the northwest of the cell. - */ - public void place(int row, int col, int width, int height, int space, JComponent comp) { - if (comp == null) - return; - constraint.gridx = col; - constraint.gridy = row; - constraint.gridwidth = width; - constraint.gridheight = height; - constraint.anchor = GridBagConstraints.NORTHWEST; - constraint.insets = new Insets(space, space, space, space); - constraint.fill = GridBagConstraints.HORIZONTAL; - layout.setConstraints(comp, constraint); - add(comp); - } - - /** - * Place a component in the northwest of the cell. - */ - public void place(int row, int col, int width, int height, int spaceHorizontal, int spaceVertical, JComponent comp) { - if (comp == null) - return; - constraint.gridx = col; - constraint.gridy = row; - constraint.gridwidth = width; - constraint.gridheight = height; - constraint.anchor = GridBagConstraints.NORTHWEST; - constraint.insets = new Insets(spaceVertical, spaceHorizontal, spaceHorizontal, spaceVertical); - constraint.fill = GridBagConstraints.HORIZONTAL; - layout.setConstraints(comp, constraint); - add(comp); - } -} diff --git a/src/main/java/ai/nets/samj/gui/ImageSelection.java b/src/main/java/ai/nets/samj/gui/ImageSelection.java index df10206..71c08f2 100644 --- a/src/main/java/ai/nets/samj/gui/ImageSelection.java +++ b/src/main/java/ai/nets/samj/gui/ImageSelection.java @@ -10,40 +10,47 @@ import ai.nets.samj.gui.components.ComboBoxButtonComp; import ai.nets.samj.gui.components.ComboBoxItem; +import ai.nets.samj.ui.ConsumerInterface; import ai.nets.samj.ui.UtilityMethods; +import net.imglib2.RandomAccessibleInterval; +import net.imglib2.type.NativeType; +import net.imglib2.type.numeric.RealType; public class ImageSelection extends ComboBoxButtonComp implements PopupMenuListener { - private final UtilityMethods consumerUtils; + private final ConsumerInterface consumer; private final ImageSelectionListener listener; private ComboBoxItem selected; private static final long serialVersionUID = 2478618937640492286L; - private ImageSelection(UtilityMethods consumerUtils, ImageSelectionListener listener) { + private ImageSelection(ConsumerInterface consumer, ImageSelectionListener listener) { super(new JComboBox()); - this.consumerUtils = consumerUtils; + this.consumer = consumer; this.listener = listener; - if (consumerUtils == null) - return; - List listImages = this.consumerUtils.getListOfOpenImages(); + List listImages = this.consumer.getListOfOpenImages(); for(ComboBoxItem item : listImages) this.cmbBox.addItem(item); } - protected static ImageSelection create(UtilityMethods consumerUtils, ImageSelectionListener listener) { - return new ImageSelection(consumerUtils, listener); + protected static ImageSelection create(ConsumerInterface consumer, ImageSelectionListener listener) { + return new ImageSelection(consumer, listener); } protected Object getSelectedObject() { return ((ComboBoxItem) this.cmbBox.getSelectedItem()).getValue(); } + + protected & NativeType> RandomAccessibleInterval getSelectedRai() { + //return ((ComboBoxItem) this.cmbBox.getSelectedItem()).getValue(); + return null; + } @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) { Object item = this.cmbBox.getSelectedItem(); - List openSeqs = consumerUtils.getListOfOpenImages(); + List openSeqs = consumer.getListOfOpenImages(); ComboBoxItem[] objects = new ComboBoxItem[openSeqs.size()]; for (int i = 0; i < objects.length; i ++) objects[i] = openSeqs.get(i); DefaultComboBoxModel comboBoxModel = new DefaultComboBoxModel(objects); diff --git a/src/main/java/ai/nets/samj/gui/MainGUI.java b/src/main/java/ai/nets/samj/gui/MainGUI.java index 9debc84..0752678 100644 --- a/src/main/java/ai/nets/samj/gui/MainGUI.java +++ b/src/main/java/ai/nets/samj/gui/MainGUI.java @@ -8,13 +8,18 @@ import ai.nets.samj.communication.model.SAM2Tiny; import ai.nets.samj.communication.model.SAMModel; import ai.nets.samj.gui.ImageSelection.ImageSelectionListener; +import ai.nets.samj.ui.ConsumerInterface; import ai.nets.samj.ui.UtilityMethods; import ai.nets.samj.utils.Constants; +import net.imglib2.util.Cast; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.border.LineBorder; import java.awt.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -26,9 +31,10 @@ public class MainGUI extends JFrame { private boolean isDrawerOpen = false; private final List modelList; private ImageSelectionListener imageListener; + private ConsumerInterface consumer; private JCheckBox chkRoiManager = new JCheckBox("Add to RoiManager", true); - private JCheckBox chkReturnLargest = new JCheckBox("Only return largest ROI", true); + private JCheckBox retunLargest = new JCheckBox("Only return largest ROI", true); private JSwitchButtonNew chkInstant = new JSwitchButtonNew("LIVE", "OFF"); private JButton go = new JButton("Go"); private JButton btnBatchSAMize = new JButton("Batch SAMize"); @@ -62,21 +68,31 @@ public class MainGUI extends JFrame { DEFAULT_MODEL_LIST.add(new EfficientViTSAML2()); } - public MainGUI(UtilityMethods methods) { - this(null, methods); + public MainGUI(ConsumerInterface consumer) { + this(null, consumer); } - public MainGUI(List modelList, UtilityMethods methods) { + public MainGUI(List modelList, ConsumerInterface consumer) { super(Constants.JAR_NAME + "-" + Constants.SAMJ_VERSION); createListeners(); - cmbImages = ImageSelection.create(methods, imageListener); + this.consumer = consumer; + cmbImages = ImageSelection.create(this.consumer, imageListener); if (modelList == null) this.modelList = DEFAULT_MODEL_LIST; else this.modelList = modelList; - cmbModels = ModelSelection.create(modelList); + cmbModels = ModelSelection.create(this.modelList); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + go.addActionListener(e -> loadModel()); + export.addActionListener(e -> consumer.exportImageLabeling()); + chkInstant.addActionListener(e -> setInstantPromptsEnabled(this.chkInstant.isSelected())); + chkRoiManager.addActionListener(e -> consumer.enableAddingToRoiManager(chkRoiManager.isSelected())); + retunLargest.addActionListener(e -> cmbModels.getSelectedModel().setReturnOnlyBiggest(retunLargest.isSelected())); + btnBatchSAMize.addActionListener(e -> consumer.exportImageLabeling()); + close.addActionListener(e -> dispose()); + help.addActionListener(e -> consumer.exportImageLabeling()); // Use BorderLayout for the main frame setLayout(new BorderLayout()); @@ -106,9 +122,51 @@ public MainGUI(List modelList, UtilityMethods methods) { // Set the initial size of the frame setSize(MAIN_HORIZONTAL_SIZE, MAIN_VERTICAL_SIZE); // Width x Height + this.addWindowListener(new WindowAdapter() { + @Override + public void windowClosed(WindowEvent e) { + close(); + } + }); // Make the frame visible setVisible(true); } + + private void setInstantPromptsEnabled(boolean enabled) { + if (enabled) + consumer.activateListeners(); + else + consumer.deactivateListeners(); + } + + private void setTwoThridsEnabled(boolean enabled) { + this.chkInstant.setEnabled(enabled); + this.retunLargest.setEnabled(enabled); + this.chkRoiManager.setEnabled(enabled); + this.btnBatchSAMize.setEnabled(enabled); + this.export.setEnabled(enabled); + } + + private void loadModel() { + SwingUtilities.invokeLater(() -> { + go.setEnabled(false); + setTwoThridsEnabled(false); + }); + new Thread(() -> { + try { + // TODO try removing Cast + cmbModels.loadModel(Cast.unchecked(cmbImages.getSelectedRai())); + consumer.setFocusedImage(cmbImages.getSelectedObject()); + setTwoThridsEnabled(true); + } catch (IOException | RuntimeException | InterruptedException ex) { + ex.printStackTrace(); + } + }).start();; + } + + private void close() { + cmbModels.unLoadModel(); + } // Method to create the title panel private JPanel createTitlePanel() { @@ -314,7 +372,7 @@ private JPanel createThirdComponent() { // Second checkbox gbc.gridy = 1; - thirdComponent.add(this.chkReturnLargest, gbc); + thirdComponent.add(this.retunLargest, gbc); // Button gbc.gridy = 2; diff --git a/src/main/java/ai/nets/samj/gui/ModelSelection.java b/src/main/java/ai/nets/samj/gui/ModelSelection.java index 27f7a5e..408772d 100644 --- a/src/main/java/ai/nets/samj/gui/ModelSelection.java +++ b/src/main/java/ai/nets/samj/gui/ModelSelection.java @@ -1,5 +1,6 @@ package ai.nets.samj.gui; +import java.io.IOException; import java.util.List; import javax.swing.JButton; @@ -8,6 +9,10 @@ import javax.swing.event.PopupMenuListener; import ai.nets.samj.gui.components.ComboBoxButtonComp; +import net.imglib2.RandomAccessibleInterval; +import net.imglib2.type.NativeType; +import net.imglib2.type.numeric.RealType; +import ai.nets.samj.annotation.Mask; import ai.nets.samj.communication.model.SAMModel; public class ModelSelection extends ComboBoxButtonComp implements PopupMenuListener { @@ -33,16 +38,20 @@ protected static ModelSelection create(List models) { return new ModelSelection(models); } + protected SAMModel getSelectedModel() { + return selected; + } + protected JButton getButton() { return this.btn; } - protected void loadModel() { - + protected & NativeType> void loadModel(RandomAccessibleInterval rai) throws IOException, RuntimeException, InterruptedException { + selected.setImage(rai, null); } - protected void unLooadModel() { - + protected void unLoadModel() { + selected.closeProcess(); } @Override @@ -59,9 +68,8 @@ public void popupMenuWillBecomeVisible(PopupMenuEvent e) { public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { SAMModel nSelectedModel = models.get(cmbBox.getSelectedIndex()); if (nSelectedModel != selected) { - unLooadModel(); + unLoadModel(); selected = nSelectedModel; - loadModel(); } } diff --git a/src/main/java/ai/nets/samj/ui/ConsumerInterface.java b/src/main/java/ai/nets/samj/ui/ConsumerInterface.java index e1654ed..9c8ab92 100644 --- a/src/main/java/ai/nets/samj/ui/ConsumerInterface.java +++ b/src/main/java/ai/nets/samj/ui/ConsumerInterface.java @@ -64,6 +64,10 @@ public abstract class ConsumerInterface { * numbered from 1. */ public abstract void exportImageLabeling(); + + public abstract void activateListeners(); + + public abstract void deactivateListeners(); public abstract void setFocusedImage(Object image);