-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create comlex object for image selection
- Loading branch information
1 parent
861a640
commit 48b8fa8
Showing
2 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters