Skip to content

Commit

Permalink
keep improving the code and GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Nov 22, 2024
1 parent bbadb77 commit 9902900
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/main/java/ai/nets/samj/gui/MainGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ai.nets.samj.communication.model.SAMModel;
import ai.nets.samj.gui.ImageSelection.ImageSelectionListener;
import ai.nets.samj.gui.components.ModelDrawerPanel;
import ai.nets.samj.gui.components.ModelDrawerPanel.ModelDrawerPanelListener;
import ai.nets.samj.ui.ConsumerInterface;
import ai.nets.samj.utils.Constants;
import net.imglib2.util.Cast;
Expand All @@ -29,6 +30,7 @@ public class MainGUI extends JFrame {
private boolean isDrawerOpen = false;
private final List<SAMModel> modelList;
private ImageSelectionListener imageListener;
private ModelDrawerPanelListener modelDrawerListener;
private ConsumerInterface consumer;

private JCheckBox chkRoiManager = new JCheckBox("Add to RoiManager", true);
Expand Down Expand Up @@ -392,7 +394,7 @@ private void toggleDrawer() {
setSize(getWidth() - drawerPanel.getPreferredSize().width, getHeight());
} else {
drawerPanel.setVisible(true);
drawerPanel.setTitle(cmbModels.getSelectedModel().getName());
drawerPanel.setSelectedModel(this.cmbModels.getSelectedModel());
this.cmbModels.getButton().setText("◀");
setSize(getWidth() + drawerPanel.getPreferredSize().width, getHeight());
}
Expand All @@ -413,6 +415,14 @@ public void imageActionsOnImageChanged() {
// TODO Auto-generated method stub
}
};
modelDrawerListener = new ModelDrawerPanelListener() {

@Override
public void setGUIEnabled(boolean enabled) {
// TODO Auto-generated method stub

}
};
}

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

public class ImageDrawerPanel extends JPanel {

private JLabel drawerTitle = new JLabel();
private static final long serialVersionUID = -8070874774405110221L;
private JLabel drawerTitle = new JLabel();
private JButton install = new JButton("Install");
private JButton uninstall = new JButton("Uninstall");

Expand Down Expand Up @@ -82,7 +83,6 @@ public void setTitle(String title) {

@Override
public void setVisible(boolean visible) {
this.isOpen = visible;
super.setVisible(visible);
}

Expand Down
76 changes: 70 additions & 6 deletions src/main/java/ai/nets/samj/gui/components/ModelDrawerPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,46 @@
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URISyntaxException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import org.apache.commons.compress.archivers.ArchiveException;

import ai.nets.samj.communication.model.SAMModel;
import ai.nets.samj.gui.HTMLPane;
import io.bioimage.modelrunner.apposed.appose.MambaInstallException;

public class ModelDrawerPanel extends JPanel {
public class ModelDrawerPanel extends JPanel implements ActionListener {

private JLabel drawerTitle = new JLabel();
private JButton install = new JButton("Install");
private JButton uninstall = new JButton("Uninstall");


private SAMModel model;
private ModelDrawerPanelListener listener;
private int hSize;
private Thread modelInstallThread;

private static final String MODEL_TITLE = "<html><div style='text-align: center; font-size: 15px;'>%s</html>";


private ModelDrawerPanel(int hSize) {
private ModelDrawerPanel(int hSize, ModelDrawerPanelListener listener) {
this.hSize = hSize;
this.listener = listener;
createDrawerPanel();
}

public static ModelDrawerPanel create(int hSize) {
return new ModelDrawerPanel(hSize);
public static ModelDrawerPanel create(int hSize, ModelDrawerPanelListener listener) {
return new ModelDrawerPanel(hSize, listener);
}

private void createDrawerPanel() {
Expand Down Expand Up @@ -77,14 +89,66 @@ private JPanel createInstallModelComponent() {
return thirdComponent;
}

public void setSelectedModel(SAMModel model) {
this.model = model;
setTitle(model.getName());
}

public void setTitle(String title) {
drawerTitle.setText(String.format(MODEL_TITLE, title));
}

@Override
public void setVisible(boolean visible) {
this.isOpen = visible;
super.setVisible(visible);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.install) {
installModel();
} else if (e.getSource() == this.uninstall) {
uninstallModel();
}
}

private void installModel() {
SwingUtilities.invokeLater(() -> listener.setGUIEnabled(false));
modelInstallThread = new Thread(() ->{
try {
this.model.getInstallationManger().installEverything();
SwingUtilities.invokeLater(() -> {
listener.setGUIEnabled(true);
listener.setGUIModelInstalled(true);
});
} catch (IOException | InterruptedException | ArchiveException | URISyntaxException
| MambaInstallException e) {
e.printStackTrace();
SwingUtilities.invokeLater(() -> {
listener.setGUIEnabled(true);
listener.setGUIModelInstalled(false);
});
}
});

}

private void uninstallModel() {
SwingUtilities.invokeLater(() -> listener.setGUIEnabled(false));
modelInstallThread = new Thread(() ->{
this.model.getInstallationManger().uninstall();
SwingUtilities.invokeLater(() -> {
listener.setGUIEnabled(true);
listener.setGUIModelInstalled(false);
});
});
}

public interface ModelDrawerPanelListener {

void setGUIEnabled(boolean enabled);

void setGUIModelInstalled(boolean installed);
}

}

0 comments on commit 9902900

Please sign in to comment.