-
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.
- Loading branch information
1 parent
8b2282f
commit 2f9b85b
Showing
4 changed files
with
289 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 |
---|---|---|
@@ -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
84
src/main/java/ai/nets/samj/gui/ImageSelectionOnlyComboBox.java
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 |
---|---|---|
@@ -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) { | ||
|
||
} | ||
|
||
} |
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
63 changes: 63 additions & 0 deletions
63
src/main/java/ai/nets/samj/gui/components/ComboBoxComp.java
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 |
---|---|---|
@@ -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); | ||
}); | ||
} | ||
} |