From 7d2b914583912aec6f87dfff56a2cc3ab668a919 Mon Sep 17 00:00:00 2001 From: carlosuc3m <100329787@alumnos.uc3m.es> Date: Fri, 22 Nov 2024 15:05:24 +0100 Subject: [PATCH] Do not resize main panel when displaying teh drawer --- src/main/java/ai/nets/samj/gui/MainGUI.java | 260 ++++++++++---------- 1 file changed, 126 insertions(+), 134 deletions(-) diff --git a/src/main/java/ai/nets/samj/gui/MainGUI.java b/src/main/java/ai/nets/samj/gui/MainGUI.java index fa5d9b5..24d73fa 100644 --- a/src/main/java/ai/nets/samj/gui/MainGUI.java +++ b/src/main/java/ai/nets/samj/gui/MainGUI.java @@ -1,6 +1,5 @@ package ai.nets.samj.gui; - import ai.nets.samj.communication.model.EfficientSAM; import ai.nets.samj.communication.model.EfficientViTSAML2; import ai.nets.samj.communication.model.SAM2Large; @@ -24,68 +23,63 @@ public class MainGUI extends JFrame { - private static final long serialVersionUID = -797293687195076077L; - + private static final long serialVersionUID = -797293687195076077L; 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 retunLargest = new JCheckBox("Only return largest ROI", true); - private JSwitchButtonNew chkInstant = new JSwitchButtonNew("LIVE", "OFF"); - private JButton go = new JButton("Go"); + private JCheckBox chkRoiManager = new JCheckBox("Add to RoiManager", 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"); - private JButton close = new JButton("Close"); - private JButton help = new JButton("Help"); - private JButton export = new JButton("Export..."); - private JButton install = new JButton("Install"); - private JButton uninstall = new JButton("Uninstall"); - private final ModelSelection cmbModels; - private final ImageSelection cmbImages; - private JLabel drawerTitle = new JLabel(); - private JPanel drawerPanel; - - - - private static double HEADER_VERTIACAL_RATIO = 0.1; - - private static int MAIN_VERTICAL_SIZE = 400; - private static int MAIN_HORIZONTAL_SIZE = 250; - - private static String MANUAL_STR = "Manual"; - - private static String PRESET_STR = "Preset prompts"; - - private static String ROIM_STR = "Selection from ROIManager"; - - private static final List DEFAULT_MODEL_LIST = new ArrayList<>(); - static { - DEFAULT_MODEL_LIST.add(new SAM2Tiny()); - DEFAULT_MODEL_LIST.add(new SAM2Small()); - DEFAULT_MODEL_LIST.add(new SAM2Large()); - DEFAULT_MODEL_LIST.add(new EfficientSAM()); - DEFAULT_MODEL_LIST.add(new EfficientViTSAML2()); - } + private JButton close = new JButton("Close"); + private JButton help = new JButton("Help"); + private JButton export = new JButton("Export..."); + private JButton install = new JButton("Install"); + private JButton uninstall = new JButton("Uninstall"); + private final ModelSelection cmbModels; + private final ImageSelection cmbImages; + private JLabel drawerTitle = new JLabel(); + private JPanel drawerPanel; + + private static double HEADER_VERTICAL_RATIO = 0.1; + + private static int MAIN_VERTICAL_SIZE = 400; + private static int MAIN_HORIZONTAL_SIZE = 250; + + private static String MANUAL_STR = "Manual"; + private static String PRESET_STR = "Preset prompts"; + private static String ROIM_STR = "Selection from ROIManager"; + + private static final List DEFAULT_MODEL_LIST = new ArrayList<>(); + static { + DEFAULT_MODEL_LIST.add(new SAM2Tiny()); + DEFAULT_MODEL_LIST.add(new SAM2Small()); + DEFAULT_MODEL_LIST.add(new SAM2Large()); + DEFAULT_MODEL_LIST.add(new EfficientSAM()); + DEFAULT_MODEL_LIST.add(new EfficientViTSAML2()); + } public MainGUI(ConsumerInterface consumer) { - this(null, consumer); + this(null, consumer); } public MainGUI(List modelList, ConsumerInterface consumer) { - super(Constants.JAR_NAME + "-" + Constants.SAMJ_VERSION); - - createListeners(); - this.consumer = consumer; - cmbImages = ImageSelection.create(this.consumer, imageListener); - - if (modelList == null) this.modelList = DEFAULT_MODEL_LIST; - else this.modelList = modelList; - cmbModels = ModelSelection.create(this.modelList); + super(Constants.JAR_NAME + "-" + Constants.SAMJ_VERSION); + + createListeners(); + this.consumer = consumer; + cmbImages = ImageSelection.create(this.consumer, imageListener); + + if (modelList == null) this.modelList = DEFAULT_MODEL_LIST; + else this.modelList = modelList; + cmbModels = ModelSelection.create(this.modelList); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - + cmbModels.getButton().addActionListener(e -> toggleDrawer()); go.addActionListener(e -> loadModel()); export.addActionListener(e -> consumer.exportImageLabeling()); @@ -97,8 +91,9 @@ public MainGUI(List modelList, ConsumerInterface consumer) { help.addActionListener(e -> consumer.exportImageLabeling()); // Use BorderLayout for the main frame - //setLayout(new BorderLayout()); - setLayout(new GridBagLayout()); + setLayout(new BorderLayout()); + + JPanel mainPanel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(0, 0, 0, 0); @@ -109,20 +104,24 @@ public MainGUI(List modelList, ConsumerInterface consumer) { // Add the title panel at the top gbc.gridy = 0; gbc.weighty = 0.1; - add(createTitlePanel(), gbc); + mainPanel.add(createTitlePanel(), gbc); // Add the main center panel gbc.gridy = 2; gbc.weighty = 0.87; - add(createCenterPanel(), gbc); + mainPanel.add(createCenterPanel(), gbc); // Add the bottom panel with buttons gbc.gridy = 3; gbc.weighty = 0.03; - add(createBottomPanel(), gbc); - + mainPanel.add(createBottomPanel(), gbc); + createDrawerPanel(); + // Add the mainPanel and drawerPanel using BorderLayout + add(mainPanel, BorderLayout.CENTER); + add(drawerPanel, BorderLayout.EAST); + // Set the initial size of the frame setSize(MAIN_HORIZONTAL_SIZE, MAIN_VERTICAL_SIZE); // Width x Height @@ -132,56 +131,60 @@ public void windowClosed(WindowEvent e) { close(); } }); + + // Initially hide the drawerPanel + drawerPanel.setVisible(false); + // Make the frame visible setVisible(true); } - + private void setInstantPromptsEnabled(boolean enabled) { - if (enabled) - consumer.activateListeners(); - else - consumer.deactivateListeners(); + if (enabled) + consumer.activateListeners(); + else + consumer.deactivateListeners(); } - + private void setTwoThirdsEnabled(boolean enabled) { - this.chkInstant.setEnabled(enabled); - this.retunLargest.setEnabled(enabled); - this.chkRoiManager.setEnabled(enabled); - this.btnBatchSAMize.setEnabled(enabled); - this.export.setEnabled(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); - setTwoThirdsEnabled(false); - }); - new Thread(() -> { - try { - // TODO try removing Cast - cmbModels.loadModel(Cast.unchecked(cmbImages.getSelectedRai())); - consumer.setFocusedImage(cmbImages.getSelectedObject()); - consumer.setModel(cmbModels.getSelectedModel()); - setInstantPromptsEnabled(this.chkInstant.isSelected()); - setTwoThirdsEnabled(true); - } catch (IOException | RuntimeException | InterruptedException ex) { - ex.printStackTrace(); - } - }).start();; + SwingUtilities.invokeLater(() -> { + go.setEnabled(false); + setTwoThirdsEnabled(false); + }); + new Thread(() -> { + try { + // TODO try removing Cast + cmbModels.loadModel(Cast.unchecked(cmbImages.getSelectedRai())); + consumer.setFocusedImage(cmbImages.getSelectedObject()); + consumer.setModel(cmbModels.getSelectedModel()); + setInstantPromptsEnabled(this.chkInstant.isSelected()); + setTwoThirdsEnabled(true); + } catch (IOException | RuntimeException | InterruptedException ex) { + ex.printStackTrace(); + } + }).start(); } - + private void close() { - cmbModels.unLoadModel(); + cmbModels.unLoadModel(); } // Method to create the title panel private JPanel createTitlePanel() { JPanel titlePanel = new JPanel(); titlePanel.setBackground(Color.LIGHT_GRAY); - int height = (int) (HEADER_VERTIACAL_RATIO * MAIN_VERTICAL_SIZE); + int height = (int) (HEADER_VERTICAL_RATIO * MAIN_VERTICAL_SIZE); titlePanel.setPreferredSize(new Dimension(0, height)); // Fixed height String text = "
" - + "SAM" + "J"; + + "SAM" + "J"; JLabel titleLabel = new JLabel(text, SwingConstants.CENTER); titleLabel.setFont(new Font("Arial", Font.BOLD, 24)); @@ -225,13 +228,12 @@ private JPanel createBottomPanel() { JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new GridBagLayout()); bottomPanel.setBorder(new EmptyBorder(0, 0, 0, 0)); - + GridBagConstraints gbcb = new GridBagConstraints(); gbcb.gridy = 0; gbcb.weightx = 1; gbcb.weighty = 1; gbcb.fill = GridBagConstraints.BOTH; - gbcb.gridx = 0; bottomPanel.add(help, gbcb); @@ -243,7 +245,7 @@ private JPanel createBottomPanel() { // Method to create the first component private JPanel createFirstComponent() { - JPanel firstComponent = new JPanel(); + JPanel firstComponent = new JPanel(); firstComponent.setLayout(new GridBagLayout()); firstComponent.setBorder(new LineBorder(Color.BLACK)); @@ -308,7 +310,6 @@ private JPanel createSecondComponent() { gbc0.weighty = 0.8; gbc0.insets = new Insets(0, 0, 10, 0); card1.add(chkInstant, gbc0); - //chkInstant.setSize(new Dimension(0, (int) (0.1 * MAIN_VERTICAL_SIZE))); JPanel card2 = new JPanel(new GridBagLayout()); gbc0.gridy = 0; @@ -339,7 +340,6 @@ private JPanel createSecondComponent() { cl.show(cardPanel, PRESET_STR); }); - GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(0, 0, 0, 0); gbc.gridx = 0; @@ -387,16 +387,15 @@ private JPanel createThirdComponent() { return thirdComponent; } - + private void createDrawerPanel() { drawerPanel = new JPanel(); drawerPanel.setLayout(new BorderLayout()); - drawerPanel.setBorder(BorderFactory.createEtchedBorder()); + drawerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); drawerTitle.setText("
 "); drawerPanel.add(drawerTitle, BorderLayout.NORTH); drawerPanel.add(createInstallModelComponent(), BorderLayout.SOUTH); HTMLPane html = new HTMLPane("Arial", "#000", "#CCCCCC", 200, 200); - //html.append("