mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 19:58:00 +00:00
Auto size ListChooser dialogs to fit items if possible
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
package forge.gui;
|
package forge.gui;
|
||||||
|
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
|
import java.awt.Dimension;
|
||||||
import java.awt.event.KeyAdapter;
|
import java.awt.event.KeyAdapter;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
@@ -97,12 +98,19 @@ public class ListChooser<T> {
|
|||||||
if (maxChoices == 1 || minChoices == -1) {
|
if (maxChoices == 1 || minChoices == -1) {
|
||||||
this.lstChoices.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
this.lstChoices.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (display != null) {
|
if (display != null) {
|
||||||
this.lstChoices.setCellRenderer(new TransformedCellRenderer(display));
|
this.lstChoices.setCellRenderer(new TransformedCellRenderer(display));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.optionPane = new FOptionPane(null, title, null, new FScrollPane(this.lstChoices), options, minChoices < 0 ? 0 : -1);
|
FScrollPane listScroller = new FScrollPane(this.lstChoices);
|
||||||
|
int minWidth = this.lstChoices.getAutoSizeWidth();
|
||||||
|
if (this.lstChoices.getModel().getSize() > this.lstChoices.getVisibleRowCount()) {
|
||||||
|
minWidth += listScroller.getVerticalScrollBar().getPreferredSize().width;
|
||||||
|
}
|
||||||
|
listScroller.setMinimumSize(new Dimension(minWidth, listScroller.getMinimumSize().height));
|
||||||
|
|
||||||
|
this.optionPane = new FOptionPane(null, title, null, listScroller, options, minChoices < 0 ? 0 : -1);
|
||||||
this.optionPane.setButtonEnabled(0, minChoices <= 0);
|
this.optionPane.setButtonEnabled(0, minChoices <= 0);
|
||||||
|
|
||||||
if (minChoices != -1) {
|
if (minChoices != -1) {
|
||||||
@@ -278,11 +286,11 @@ public class ListChooser<T> {
|
|||||||
ListChooser.this.optionPane.setButtonEnabled(0, (num >= ListChooser.this.minChoices) && (num <= ListChooser.this.maxChoices));
|
ListChooser.this.optionPane.setButtonEnabled(0, (num >= ListChooser.this.minChoices) && (num <= ListChooser.this.maxChoices));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TransformedCellRenderer implements ListCellRenderer<T> {
|
private class TransformedCellRenderer implements ListCellRenderer<T> {
|
||||||
public final Function<T, String> transformer;
|
public final Function<T, String> transformer;
|
||||||
public final DefaultListCellRenderer defRenderer;
|
public final DefaultListCellRenderer defRenderer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: Write javadoc for Constructor.
|
* TODO: Write javadoc for Constructor.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package forge.gui.toolbox;
|
package forge.gui.toolbox;
|
||||||
|
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
|
import java.awt.FontMetrics;
|
||||||
import java.awt.event.FocusEvent;
|
import java.awt.event.FocusEvent;
|
||||||
import java.awt.event.FocusListener;
|
import java.awt.event.FocusListener;
|
||||||
|
|
||||||
@@ -11,6 +12,7 @@ import javax.swing.ListCellRenderer;
|
|||||||
import javax.swing.ListModel;
|
import javax.swing.ListModel;
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
|
|
||||||
|
import forge.Singletons;
|
||||||
import forge.gui.toolbox.FSkin.SkinnedList;
|
import forge.gui.toolbox.FSkin.SkinnedList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -19,6 +21,8 @@ import forge.gui.toolbox.FSkin.SkinnedList;
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class FList<E> extends SkinnedList<E> {
|
public class FList<E> extends SkinnedList<E> {
|
||||||
|
private static final EmptyBorder itemBorder = new EmptyBorder(4, 3, 4, 3);
|
||||||
|
|
||||||
public FList() {
|
public FList() {
|
||||||
super();
|
super();
|
||||||
initialize();
|
initialize();
|
||||||
@@ -70,6 +74,30 @@ public class FList<E> extends SkinnedList<E> {
|
|||||||
this.setSelectionBackground(FSkin.getColor(hasFocus() ? FSkin.Colors.CLR_ACTIVE : FSkin.Colors.CLR_INACTIVE));
|
this.setSelectionBackground(FSkin.getColor(hasFocus() ? FSkin.Colors.CLR_ACTIVE : FSkin.Colors.CLR_INACTIVE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getAutoSizeWidth() {
|
||||||
|
FontMetrics metrics = this.getSkin().getFont().getFontMetrics();
|
||||||
|
int width = 0;
|
||||||
|
for (int i = 0; i < this.getModel().getSize(); i++) {
|
||||||
|
int itemWidth = metrics.stringWidth(this.getModel().getElementAt(i).toString());
|
||||||
|
if (itemWidth > width) {
|
||||||
|
width = itemWidth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
width += itemBorder.getBorderInsets().left + itemBorder.getBorderInsets().right; //account for item border insets
|
||||||
|
|
||||||
|
int minWidth = 150;
|
||||||
|
if (width < minWidth) {
|
||||||
|
width = minWidth;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
int maxWidth = Singletons.getView().getFrame().getWidth() / 2;
|
||||||
|
if (width > maxWidth) {
|
||||||
|
width = maxWidth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
private class ComplexCellRenderer<E1> implements ListCellRenderer<E1> {
|
private class ComplexCellRenderer<E1> implements ListCellRenderer<E1> {
|
||||||
private DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
|
private DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
|
||||||
|
|
||||||
@@ -79,7 +107,7 @@ public class FList<E> extends SkinnedList<E> {
|
|||||||
|
|
||||||
JLabel lblItem = (JLabel) defaultRenderer.getListCellRendererComponent(
|
JLabel lblItem = (JLabel) defaultRenderer.getListCellRendererComponent(
|
||||||
lst0, val0, i0, isSelected, cellHasFocus);
|
lst0, val0, i0, isSelected, cellHasFocus);
|
||||||
lblItem.setBorder(new EmptyBorder(4, 3, 4, 3));
|
lblItem.setBorder(itemBorder);
|
||||||
return lblItem;
|
return lblItem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import java.awt.Component;
|
|||||||
import java.awt.Cursor;
|
import java.awt.Cursor;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
|
import java.awt.FontMetrics;
|
||||||
import java.awt.Frame;
|
import java.awt.Frame;
|
||||||
import java.awt.GradientPaint;
|
import java.awt.GradientPaint;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
@@ -1260,6 +1261,10 @@ public enum FSkin {
|
|||||||
return g.getFontMetrics(this.font).stringWidth(text);
|
return g.getFontMetrics(this.font).stringWidth(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FontMetrics getFontMetrics() {
|
||||||
|
return Singletons.getView().getFrame().getGraphics().getFontMetrics(this.font);
|
||||||
|
}
|
||||||
|
|
||||||
private void updateFont() {
|
private void updateFont() {
|
||||||
this.font = baseFont.deriveFont(this.style, this.size);
|
this.font = baseFont.deriveFont(this.style, this.size);
|
||||||
}
|
}
|
||||||
@@ -1903,6 +1908,7 @@ public enum FSkin {
|
|||||||
protected void setBackground(T comp, SkinColor skinColor) { comp.setBackground(skinColor != null ? skinColor.color : null); this.background = skinColor; }
|
protected void setBackground(T comp, SkinColor skinColor) { comp.setBackground(skinColor != null ? skinColor.color : null); this.background = skinColor; }
|
||||||
protected void resetBackground() { this.background = null; }
|
protected void resetBackground() { this.background = null; }
|
||||||
|
|
||||||
|
public SkinFont getFont() { return this.font; }
|
||||||
protected void setFont(T comp, SkinFont skinFont) { comp.setFont(skinFont != null ? skinFont.font : null); this.font = skinFont; }
|
protected void setFont(T comp, SkinFont skinFont) { comp.setFont(skinFont != null ? skinFont.font : null); this.font = skinFont; }
|
||||||
protected void resetFont() { this.font = null; }
|
protected void resetFont() { this.font = null; }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user