Skip to content

Commit

Permalink
FIX: ModelSelection listens to Item changes (not _only_ to Popup chan…
Browse files Browse the repository at this point in the history
…ges)
  • Loading branch information
xulman committed Dec 18, 2024
1 parent 7a86afb commit bce3441
Showing 1 changed file with 18 additions and 32 deletions.
50 changes: 18 additions & 32 deletions src/main/java/ai/nets/samj/gui/ModelSelection.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package ai.nets.samj.gui;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.event.PopupMenuEvent;
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<String> implements PopupMenuListener {
public class ModelSelection extends ComboBoxButtonComp<String> implements ItemListener {

private SAMModel selected;

Expand All @@ -33,7 +32,7 @@ private ModelSelection(List<SAMModel> models, ModelSelectionListener listener) {
for (SAMModel model : models) {
this.cmbBox.addItem(model.getName());
}
cmbBox.addPopupMenuListener(this);
cmbBox.addItemListener(this);
selected = models.get(cmbBox.getSelectedIndex());
}

Expand Down Expand Up @@ -65,37 +64,24 @@ protected void unLoadModel() {
}

@Override
/**
* Nothing
*/
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
}

@Override
/**
* Check if the image selected has been changed once the combobox pop up is closed
*/
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
try {
SAMModel nSelectedModel = models.get(cmbBox.getSelectedIndex());
if (nSelectedModel != selected) {
unLoadModel();
listener.changeGUI();
selected = nSelectedModel;
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
try {
SAMModel nSelectedModel = models.get(cmbBox.getSelectedIndex());
// checks if indeed a different model is selected (from what was selected before)
if (nSelectedModel != selected) {
unLoadModel();
listener.changeGUI();
selected = nSelectedModel;
}
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
listener.changeDrawerPanel();
}
listener.changeDrawerPanel();
}

@Override
/**
* Nothing
*/
public void popupMenuCanceled(PopupMenuEvent e) {
}


public interface ModelSelectionListener {

void changeDrawerPanel();
Expand Down

0 comments on commit bce3441

Please sign in to comment.