Skip to content

Commit

Permalink
create comlex object for image selection
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Nov 21, 2024
1 parent 861a640 commit 48b8fa8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
63 changes: 60 additions & 3 deletions src/main/java/ai/nets/samj/gui/ImageSelection.java
Original file line number Diff line number Diff line change
@@ -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<ComboBoxItem> implements PopupMenuListener {

public class ImageSelection extends ComboBoxButtonComp<ComboBoxItem> {
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<ComboBoxItem>());
this.cmbBox.setModel(null);
this.consumerUtils = consumerUtils;
this.listener = listener;
List<ComboBoxItem> 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<ComboBoxItem> openSeqs = consumerUtils.getListOfOpenImages();
ComboBoxItem[] objects = new ComboBoxItem[openSeqs.size()];
for (int i = 0; i < objects.length; i ++) objects[i] = openSeqs.get(i);
DefaultComboBoxModel<ComboBoxItem> comboBoxModel = new DefaultComboBoxModel<ComboBoxItem>(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();
}

}
5 changes: 5 additions & 0 deletions src/main/java/ai/nets/samj/gui/ModelSelection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,6 +34,10 @@ private ModelSelection(List<Model> models, ModelSelectionListener listener) {
protected ModelSelection create(List<Model> models, ModelSelectionListener listener) {
return new ModelSelection(models, listener);
}

protected JButton getButton() {
return this.btn;
}

@Override
/**
Expand Down

0 comments on commit 48b8fa8

Please sign in to comment.