Auto size ListChooser dialogs to fit items if possible

This commit is contained in:
drdev
2014-01-10 04:30:23 +00:00
parent 753c0731a3
commit feaec2eb6d
3 changed files with 47 additions and 5 deletions

View File

@@ -19,6 +19,7 @@
package forge.gui;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
@@ -97,12 +98,19 @@ public class ListChooser<T> {
if (maxChoices == 1 || minChoices == -1) {
this.lstChoices.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
if (display != null) {
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);
if (minChoices != -1) {
@@ -278,11 +286,11 @@ public class ListChooser<T> {
ListChooser.this.optionPane.setButtonEnabled(0, (num >= ListChooser.this.minChoices) && (num <= ListChooser.this.maxChoices));
}
}
private class TransformedCellRenderer implements ListCellRenderer<T> {
public final Function<T, String> transformer;
public final DefaultListCellRenderer defRenderer;
/**
* TODO: Write javadoc for Constructor.
*/

View File

@@ -1,6 +1,7 @@
package forge.gui.toolbox;
import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
@@ -11,6 +12,7 @@ import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.border.EmptyBorder;
import forge.Singletons;
import forge.gui.toolbox.FSkin.SkinnedList;
/**
@@ -19,6 +21,8 @@ import forge.gui.toolbox.FSkin.SkinnedList;
*/
@SuppressWarnings("serial")
public class FList<E> extends SkinnedList<E> {
private static final EmptyBorder itemBorder = new EmptyBorder(4, 3, 4, 3);
public FList() {
super();
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));
}
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 DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
@@ -79,7 +107,7 @@ public class FList<E> extends SkinnedList<E> {
JLabel lblItem = (JLabel) defaultRenderer.getListCellRendererComponent(
lst0, val0, i0, isSelected, cellHasFocus);
lblItem.setBorder(new EmptyBorder(4, 3, 4, 3));
lblItem.setBorder(itemBorder);
return lblItem;
}
}

View File

@@ -22,6 +22,7 @@ import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.GradientPaint;
import java.awt.Graphics;
@@ -1260,6 +1261,10 @@ public enum FSkin {
return g.getFontMetrics(this.font).stringWidth(text);
}
public FontMetrics getFontMetrics() {
return Singletons.getView().getFrame().getGraphics().getFontMetrics(this.font);
}
private void updateFont() {
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 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 resetFont() { this.font = null; }