Skip to content

Commit

Permalink
final adjustments to the interface
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Dec 3, 2024
1 parent 8b2282f commit 2f9b85b
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 3 deletions.
114 changes: 114 additions & 0 deletions src/main/java/ai/nets/samj/gui/CustomInsetsJLabel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package ai.nets.samj.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

/**
* A JLabel with custom insets that ensures the icon touches all edges of the border.
*
* @author Carlos Garcia
*/
public class CustomInsetsJLabel extends JLabel {
private static final long serialVersionUID = 177134806911886339L;

private int top;
private int left;
private int bottom;
private int right;

public CustomInsetsJLabel(Icon icon, int top, int left, int bottom, int right) {
super(icon);
this.top = top;
this.left = left;
this.bottom = bottom;
this.right = right;
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
}

@Override
public Insets getInsets() {
return new Insets(top, left, bottom, right);
}

@Override
public Dimension getPreferredSize() {
// Calculate the preferred size based on icon size, insets, and border
int width = 0;
int height = 0;

if (getIcon() != null) {
width = getIcon().getIconWidth();
height = getIcon().getIconHeight();
}

Insets insets = getInsets();
if (insets != null) {
width += insets.left + insets.right;
height += insets.top + insets.bottom;
}

Border border = getBorder();
if (border != null) {
Insets borderInsets = border.getBorderInsets(this);
width += borderInsets.left + borderInsets.right;
height += borderInsets.top + borderInsets.bottom;
}

return new Dimension(width, height);
}

@Override
protected void paintComponent(Graphics g) {
// Ensure the icon is painted precisely
int x = 0;
int y = 0;

Border border = getBorder();
if (border != null) {
Insets borderInsets = border.getBorderInsets(this);
x += borderInsets.left;
y += borderInsets.top;
}

Insets insets = getInsets();
if (insets != null) {
x += insets.left;
y += insets.top;
}

if (getIcon() != null) {
getIcon().paintIcon(this, g, x, y);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Icon icon = UIManager.getIcon("OptionPane.questionIcon");
if (icon == null) {
icon = UIManager.getIcon("OptionPane.errorIcon");
}

CustomInsetsJLabel label = new CustomInsetsJLabel(icon, 4, 2, 0, 0);
label.setBorder(LineBorder.createBlackLineBorder());
label.setToolTipText("Test Tooltip");

JFrame frame = new JFrame("Test CustomInsetsJLabel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(label);
frame.pack();
frame.setVisible(true);
});
}
}
84 changes: 84 additions & 0 deletions src/main/java/ai/nets/samj/gui/ImageSelectionOnlyComboBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ai.nets.samj.gui;

import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import ai.nets.samj.gui.components.ComboBoxComp;
import ai.nets.samj.gui.components.ComboBoxItem;
import ai.nets.samj.ui.ConsumerInterface;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;

public class ImageSelectionOnlyComboBox extends ComboBoxComp<ComboBoxItem> implements PopupMenuListener {

private final ConsumerInterface consumer;
private final ImageSelection.ImageSelectionListener listener;

private ComboBoxItem selected;

private static final long serialVersionUID = 2478618937640492286L;

private ImageSelectionOnlyComboBox(ConsumerInterface consumer, ImageSelection.ImageSelectionListener listener) {
super(new JComboBox<ComboBoxItem>());
this.consumer = consumer;
this.listener = listener;
List<ComboBoxItem> listImages = this.consumer.getListOfOpenImages();
for(ComboBoxItem item : listImages)
this.cmbBox.addItem(item);
cmbBox.addPopupMenuListener(this);
}

protected static ImageSelectionOnlyComboBox create(ConsumerInterface consumer, ImageSelection.ImageSelectionListener listener) {
return new ImageSelectionOnlyComboBox(consumer, listener);
}

protected Object getSelectedObject() {
if (this.cmbBox.getSelectedItem() == null)
return null;
return ((ComboBoxItem) this.cmbBox.getSelectedItem()).getValue();
}

protected <T extends RealType<T> & NativeType<T>> RandomAccessibleInterval<T> getSelectedRai() {
return ((ComboBoxItem) this.cmbBox.getSelectedItem()).getImageAsImgLib2();
}

@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
try {
List<ComboBoxItem> openSeqs = consumer.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);
if (selected != null && objects.length != 0)
comboBoxModel.setSelectedItem(selected);
this.cmbBox.setModel(comboBoxModel);
} catch (Exception ex) {
ex.printStackTrace();
}
}

@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
try {
ComboBoxItem item = (ComboBoxItem) this.cmbBox.getSelectedItem();
if (selected == null || item == null || !selected.getId().equals(item.getId())) {
listener.modelActionsOnImageChanged();
listener.imageActionsOnImageChanged();
}
selected = item;
} catch (Exception ex) {
ex.printStackTrace();
}
}

@Override
public void popupMenuCanceled(PopupMenuEvent e) {

}

}
31 changes: 28 additions & 3 deletions src/main/java/ai/nets/samj/gui/MainGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ public class MainGUI extends JFrame {
private JProgressBar batchProgress = new JProgressBar();
private ResizableButton stopProgressBtn = new ResizableButton("■", 10, 2, 2);
private final ModelSelection cmbModels;
private final ImageSelection cmbImages;
// TODO add information tab to cmbImages, same as cmbModels
// TODO changes to just a combobox on december 2024
// TODO private final ImageSelection cmbImages;
private final ImageSelectionOnlyComboBox cmbImages;
private ModelDrawerPanel drawerPanel;
private JPanel cardPanel;
private JPanel cardPanel1_2;
Expand Down Expand Up @@ -100,7 +103,7 @@ public MainGUI(List<SAMModel> modelList, ConsumerInterface consumer) {
this.consumer = consumer;
this.consumer.setCallback(consumerCallback);
consumerCallback.validPromptChosen(consumer.isValidPromptSelected());
cmbImages = ImageSelection.create(this.consumer, imageListener);
cmbImages = ImageSelectionOnlyComboBox.create(this.consumer, imageListener);

if (modelList == null) this.modelList = DEFAULT_MODEL_LIST;
else this.modelList = modelList;
Expand Down Expand Up @@ -379,9 +382,31 @@ private JPanel createSecondComponent() {
gbc0.anchor = GridBagConstraints.CENTER;
gbc0.fill = GridBagConstraints.BOTH;
gbc0.weighty = 0.8;
card2.add(btnBatchSAMize, gbc0);
JPanel wrapperBtn = new JPanel(new GridBagLayout());
GridBagConstraints gbcBtn = new GridBagConstraints();
gbcBtn.weightx = 0.05;
gbcBtn.weighty = 1;
gbcBtn.fill = GridBagConstraints.NONE;
gbcBtn.insets = new Insets(0, 0, 0, 0);
gbcBtn.gridy = 0;
gbcBtn.gridx = 1;
Icon questionIcon = UIManager.getIcon("OptionPane.questionIcon");
if (questionIcon == null) {
questionIcon = UIManager.getIcon("OptionPane.informationIcon");
}
CustomInsetsJLabel questionMark = new CustomInsetsJLabel(questionIcon, 4, 2, 0, 0);
//questionMark.setBorder(LineBorder.createBlackLineBorder());
questionMark.setToolTipText("Very useful help");
wrapperBtn.add(questionMark, gbcBtn);
gbcBtn.gridx = 0;
gbcBtn.fill = GridBagConstraints.BOTH;
gbcBtn.weightx = 0.95;
wrapperBtn.add(btnBatchSAMize, gbcBtn);
gbc0.insets = new Insets(0, 5, 5, 0);
card2.add(wrapperBtn, gbc0);

gbc0.gridy = 2;
gbc0.insets = new Insets(0, 2, 5, 2);
gbc0.weighty = 0.1;
gbc0.anchor = GridBagConstraints.CENTER;
gbc0.fill = GridBagConstraints.BOTH;
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/ai/nets/samj/gui/components/ComboBoxComp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ai.nets.samj.gui.components;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JPanel;


public class ComboBoxComp<T> extends JPanel {

private static final long serialVersionUID = 2478618937640492286L;

protected final JComboBox<T> cmbBox;

public ComboBoxComp(JComboBox<T> modelCombobox) {
this.cmbBox = modelCombobox;

// Use GridBagLayout instead of null layout
setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 0, 0, 0); // Adjust insets as needed
gbc.fill = GridBagConstraints.BOTH;
gbc.gridy = 0;

// Add the JComboBox with weightx corresponding to RATIO_CBX_BTN
gbc.gridx = 0;
gbc.weightx = 1;
gbc.weighty = 1;
add(cmbBox, gbc);
}

@Override
public void doLayout() {
int inset = 2; // Separation between components and edges
int totalInset = inset * 2; // Separation between components and edges

int width = getWidth();
int height = getHeight();

int availableWidth = width - totalInset;

// Calculate widths based on the ratio

int x = inset;
int y = 0;
int componentHeight = height; // Account for top and bottom insets

// Set bounds for the JComboBox
cmbBox.setBounds(x, y, availableWidth, componentHeight);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
javax.swing.JFrame frame = new javax.swing.JFrame("Model Selection");
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.add(new ComboBoxComp(null));
frame.setSize(400, 100); // Adjust the size as needed
frame.setVisible(true);
});
}
}

0 comments on commit 2f9b85b

Please sign in to comment.