Skip to content

Commit

Permalink
keep improving GUI reactivity
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Nov 18, 2024
1 parent 79be4bf commit 97b7a4d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/deepimagej/gui/ContentPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,14 @@ protected void update(ModelDescriptor modelDescriptor, URL path, int logoWidth,
revalidate();
repaint();
});
ModelSelectionPanel.ICONS_DISPLAYED.put("main", path);
ModelInfoWorker worker = new ModelInfoWorker(modelDescriptor, this);
worker.execute();
ImageLoader.loadImageIconFromURL(path, logoWidth, logoHeight, new ImageLoadCallback() {
@Override
public void onImageLoaded(ImageIcon icon) {
if (ModelSelectionPanel.ICONS_DISPLAYED.get("main") != path)
return;
setIcon(icon);
revalidate();
repaint();
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/deepimagej/gui/ModelCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.awt.Dimension;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;

import javax.swing.BorderFactory;
Expand All @@ -27,6 +26,7 @@ public class ModelCard extends JPanel {

private long cardWidth;
private long cardHeight;
private String id;
private final double scale;


Expand Down Expand Up @@ -58,6 +58,15 @@ private ModelCard(long cardWidth, long cardHeight, double scale) {
this.add(this.imageLabel, BorderLayout.CENTER);
this.add(this.nicknameLabel, BorderLayout.SOUTH);
}

/**
* Set an optional id
* @param id
* the identifier of the card
*/
public void setOptionalID(String id) {
this.id = id;
}

protected static ModelCard createModelCard(long cardWidth, long cardHeight, double scale) {
ModelCard modelCardPanel = new ModelCard(cardWidth, cardHeight, scale);
Expand All @@ -79,6 +88,8 @@ protected void updateCard(String name, String nickname, URL imagePath) {
ImageLoader.loadImageIconFromURL(imagePath, iconW, iconH, new ImageLoadCallback() {
@Override
public void onImageLoaded(ImageIcon icon) {
if (ModelSelectionPanel.ICONS_DISPLAYED.get(id) != imagePath)
return;
imageLabel.setIcon(icon);
ModelCard.this.revalidate();
ModelCard.this.repaint();
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/deepimagej/gui/ModelSelectionPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import javax.swing.BorderFactory;
Expand Down Expand Up @@ -42,6 +44,12 @@ public class ModelSelectionPanel extends JPanel {
private List<String> modelNicknames;
private List<URL> modelImagePaths;
private List<ModelDescriptor> models;

protected static Map<String, URL> ICONS_DISPLAYED = new ConcurrentHashMap<>();

private static final String PREV_ICON = "prev";
private static final String MAIN_ICON = "main";
private static final String NEXT_ICON = "next";

private static final double CARD_VRATIO = 0.8;
private static final double CARD_HRATIO = 0.33;
Expand Down Expand Up @@ -69,8 +77,11 @@ protected ModelSelectionPanel(int parentWidth, int parentHeight) {
int cardHeight = (int) (this.parentHeight * SELECTION_PANE_VRATIO * CARD_VRATIO);
int cardWidth = (int) (parentWidth * CARD_HRATIO);
prevModelPanel = ModelCard.createModelCard(cardWidth, cardHeight, SECOND_CARD_RT);
prevModelPanel.setOptionalID(PREV_ICON);
selectedModelPanel = ModelCard.createModelCard(cardWidth, cardHeight, MAIN_CARD_RT);
selectedModelPanel.setOptionalID(MAIN_ICON);
nextModelPanel = ModelCard.createModelCard(cardWidth, cardHeight, SECOND_CARD_RT);
nextModelPanel.setOptionalID(NEXT_ICON);

modelCarouselPanel.add(prevModelPanel);
modelCarouselPanel.add(selectedModelPanel);
Expand Down Expand Up @@ -182,12 +193,15 @@ else if (model.getModelPath() == null) {
}

protected void redrawModelCards(int currentIndex) {
ICONS_DISPLAYED.put(PREV_ICON, modelImagePaths.get(getWrappedIndex(currentIndex - 1)));
prevModelPanel.updateCard(modelNames.get(getWrappedIndex(currentIndex - 1)),
modelNicknames.get(getWrappedIndex(currentIndex - 1)),
modelImagePaths.get(getWrappedIndex(currentIndex - 1)));
ICONS_DISPLAYED.put(MAIN_ICON, modelImagePaths.get(getWrappedIndex(currentIndex)));
selectedModelPanel.updateCard(modelNames.get(currentIndex),
modelNicknames.get(currentIndex),
modelImagePaths.get(currentIndex));
ICONS_DISPLAYED.put(NEXT_ICON, modelImagePaths.get(getWrappedIndex(currentIndex + 1)));
nextModelPanel.updateCard(modelNames.get(getWrappedIndex(currentIndex + 1)),
modelNicknames.get(getWrappedIndex(currentIndex + 1)),
modelImagePaths.get(getWrappedIndex(currentIndex + 1)));
Expand Down

0 comments on commit 97b7a4d

Please sign in to comment.