From 48b8fa8ff49ba0ce0951861f7f67300f4e3db7b9 Mon Sep 17 00:00:00 2001 From: carlosuc3m <100329787@alumnos.uc3m.es> Date: Thu, 21 Nov 2024 15:39:35 +0100 Subject: [PATCH] create comlex object for image selection --- .../java/ai/nets/samj/gui/ImageSelection.java | 63 ++++++++++++++++++- .../java/ai/nets/samj/gui/ModelSelection.java | 5 ++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/main/java/ai/nets/samj/gui/ImageSelection.java b/src/main/java/ai/nets/samj/gui/ImageSelection.java index 84a215b..142de59 100644 --- a/src/main/java/ai/nets/samj/gui/ImageSelection.java +++ b/src/main/java/ai/nets/samj/gui/ImageSelection.java @@ -1,19 +1,76 @@ package ai.nets.samj.gui; +import java.util.List; +import java.util.stream.IntStream; + +import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; +import javax.swing.event.PopupMenuEvent; +import javax.swing.event.PopupMenuListener; import ai.nets.samj.gui.components.ComboBoxButtonComp; import ai.nets.samj.gui.components.ComboBoxItem; +import ai.nets.samj.ui.UtilityMethods; + +public class ImageSelection extends ComboBoxButtonComp implements PopupMenuListener { -public class ImageSelection extends ComboBoxButtonComp { + private final UtilityMethods consumerUtils; + private final ImageSelectionListener listener; + private ComboBoxItem selected; private static final long serialVersionUID = 2478618937640492286L; - public ImageSelection() { + private ImageSelection(UtilityMethods consumerUtils, ImageSelectionListener listener) { super(new JComboBox()); - this.cmbBox.setModel(null); + this.consumerUtils = consumerUtils; + this.listener = listener; + List listImages = this.consumerUtils.getListOfOpenImages(); + for(ComboBoxItem item : listImages) + this.cmbBox.addItem(item); + } + + protected ImageSelection create(UtilityMethods consumerUtils, ImageSelectionListener listener) { + return new ImageSelection(consumerUtils, listener); + } + + protected Object getSelectedObject() { + return ((ComboBoxItem) this.cmbBox.getSelectedItem()).getValue(); + } + + @Override + public void popupMenuWillBecomeVisible(PopupMenuEvent e) { + Object item = this.cmbBox.getSelectedItem(); + List openSeqs = consumerUtils.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); + this.cmbBox.setModel(comboBoxModel); + if (item != null && objects.length != 0) + this.cmbBox.setSelectedIndex( + IntStream.range(0, objects.length).filter(i -> objects[i].getId() == ((ComboBoxItem) item).getId()).findFirst().orElse(0) + ); + } + + @Override + public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { + ComboBoxItem item = (ComboBoxItem) this.cmbBox.getSelectedItem(); + if (selected != item) { + listener.modelActionsOnImageChanged(); + listener.imageActionsOnImageChanged(); + } + } + + @Override + public void popupMenuCanceled(PopupMenuEvent e) { + + } + + public interface ImageSelectionListener { + void modelActionsOnImageChanged(); + + void imageActionsOnImageChanged(); } } diff --git a/src/main/java/ai/nets/samj/gui/ModelSelection.java b/src/main/java/ai/nets/samj/gui/ModelSelection.java index a5da219..bb7472a 100644 --- a/src/main/java/ai/nets/samj/gui/ModelSelection.java +++ b/src/main/java/ai/nets/samj/gui/ModelSelection.java @@ -2,6 +2,7 @@ import java.util.List; +import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuListener; @@ -33,6 +34,10 @@ private ModelSelection(List models, ModelSelectionListener listener) { protected ModelSelection create(List models, ModelSelectionListener listener) { return new ModelSelection(models, listener); } + + protected JButton getButton() { + return this.btn; + } @Override /**