From 89d38191dde8fba8388d514575022655be97680e Mon Sep 17 00:00:00 2001 From: carlosuc3m <100329787@alumnos.uc3m.es> Date: Fri, 22 Nov 2024 13:52:26 +0100 Subject: [PATCH] keep improving robustness --- .../java/ai/nets/samj/gui/ImageSelection.java | 37 ++++++++++++------- .../java/ai/nets/samj/gui/ModelSelection.java | 12 ++++-- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/main/java/ai/nets/samj/gui/ImageSelection.java b/src/main/java/ai/nets/samj/gui/ImageSelection.java index 148c6ca..29b25a9 100644 --- a/src/main/java/ai/nets/samj/gui/ImageSelection.java +++ b/src/main/java/ai/nets/samj/gui/ImageSelection.java @@ -31,6 +31,7 @@ private ImageSelection(ConsumerInterface consumer, ImageSelectionListener listen List listImages = this.consumer.getListOfOpenImages(); for(ComboBoxItem item : listImages) this.cmbBox.addItem(item); + cmbBox.addPopupMenuListener(this); } protected static ImageSelection create(ConsumerInterface consumer, ImageSelectionListener listener) { @@ -47,24 +48,32 @@ protected & NativeType> RandomAccessibleInterval ge @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) { - Object item = this.cmbBox.getSelectedItem(); - List openSeqs = consumer.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) - ); + try { + Object item = this.cmbBox.getSelectedItem(); + List openSeqs = consumer.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) + ); + } catch (Exception ex) { + ex.printStackTrace(); + } } @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { - ComboBoxItem item = (ComboBoxItem) this.cmbBox.getSelectedItem(); - if (selected != item) { - listener.modelActionsOnImageChanged(); - listener.imageActionsOnImageChanged(); + try { + ComboBoxItem item = (ComboBoxItem) this.cmbBox.getSelectedItem(); + if (selected != item) { + listener.modelActionsOnImageChanged(); + listener.imageActionsOnImageChanged(); + } + } catch (Exception ex) { + ex.printStackTrace(); } } diff --git a/src/main/java/ai/nets/samj/gui/ModelSelection.java b/src/main/java/ai/nets/samj/gui/ModelSelection.java index 408772d..2d868e3 100644 --- a/src/main/java/ai/nets/samj/gui/ModelSelection.java +++ b/src/main/java/ai/nets/samj/gui/ModelSelection.java @@ -66,10 +66,14 @@ public void popupMenuWillBecomeVisible(PopupMenuEvent e) { * Check if the image selected has been changed once the combobox pop up is closed */ public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { - SAMModel nSelectedModel = models.get(cmbBox.getSelectedIndex()); - if (nSelectedModel != selected) { - unLoadModel(); - selected = nSelectedModel; + try { + SAMModel nSelectedModel = models.get(cmbBox.getSelectedIndex()); + if (nSelectedModel != selected) { + unLoadModel(); + selected = nSelectedModel; + } + } catch (Exception ex) { + ex.printStackTrace(); } }