Use FComboBox in place of JComboBox

This commit is contained in:
drdev
2013-10-27 05:54:22 +00:00
parent 4485e46801
commit d0e65fe1bc
4 changed files with 36 additions and 26 deletions

View File

@@ -6,7 +6,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.swing.JCheckBox; import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
@@ -22,6 +21,7 @@ import forge.control.RestartUtil;
import forge.game.ai.AiProfileUtil; import forge.game.ai.AiProfileUtil;
import forge.gui.framework.FScreen; import forge.gui.framework.FScreen;
import forge.gui.framework.ICDoc; import forge.gui.framework.ICDoc;
import forge.gui.toolbox.FComboBox;
import forge.gui.toolbox.FComboBoxPanel; import forge.gui.toolbox.FComboBoxPanel;
import forge.gui.toolbox.FLabel; import forge.gui.toolbox.FLabel;
import forge.properties.ForgePreferences; import forge.properties.ForgePreferences;
@@ -195,14 +195,14 @@ public enum CSubmenuPreferences implements ICDoc {
private void initializeGameLogVerbosityComboBox() { private void initializeGameLogVerbosityComboBox() {
FPref userSetting = FPref.DEV_LOG_ENTRY_TYPE; FPref userSetting = FPref.DEV_LOG_ENTRY_TYPE;
FComboBoxPanel<GameLogEntryType> panel = this.view.getGameLogVerbosityComboBoxPanel(); FComboBoxPanel<GameLogEntryType> panel = this.view.getGameLogVerbosityComboBoxPanel();
JComboBox<GameLogEntryType> comboBox = createComboBox(GameLogEntryType.values(), userSetting); FComboBox<GameLogEntryType> comboBox = createComboBox(GameLogEntryType.values(), userSetting);
GameLogEntryType selectedItem = GameLogEntryType.valueOf(this.prefs.getPref(userSetting)); GameLogEntryType selectedItem = GameLogEntryType.valueOf(this.prefs.getPref(userSetting));
panel.setComboBox(comboBox, selectedItem); panel.setComboBox(comboBox, selectedItem);
} }
private void initializeCloseActionComboBox() { private void initializeCloseActionComboBox() {
final FComboBoxPanel<CloseAction> panel = this.view.getCloseActionComboBoxPanel(); final FComboBoxPanel<CloseAction> panel = this.view.getCloseActionComboBoxPanel();
final JComboBox<CloseAction> comboBox = new JComboBox<CloseAction>(CloseAction.values()); final FComboBox<CloseAction> comboBox = new FComboBox<CloseAction>(CloseAction.values());
comboBox.addItemListener(new ItemListener() { comboBox.addItemListener(new ItemListener() {
@Override @Override
public void itemStateChanged(final ItemEvent e) { public void itemStateChanged(final ItemEvent e) {
@@ -215,18 +215,18 @@ public enum CSubmenuPreferences implements ICDoc {
private void initializeAiProfilesComboBox() { private void initializeAiProfilesComboBox() {
FPref userSetting = FPref.UI_CURRENT_AI_PROFILE; FPref userSetting = FPref.UI_CURRENT_AI_PROFILE;
FComboBoxPanel<String> panel = this.view.getAiProfilesComboBoxPanel(); FComboBoxPanel<String> panel = this.view.getAiProfilesComboBoxPanel();
JComboBox<String> comboBox = createComboBox(AiProfileUtil.getProfilesArray(), userSetting); FComboBox<String> comboBox = createComboBox(AiProfileUtil.getProfilesArray(), userSetting);
String selectedItem = this.prefs.getPref(userSetting); String selectedItem = this.prefs.getPref(userSetting);
panel.setComboBox(comboBox, selectedItem); panel.setComboBox(comboBox, selectedItem);
} }
private <E> JComboBox<E> createComboBox(E[] items, final ForgePreferences.FPref setting) { private <E> FComboBox<E> createComboBox(E[] items, final ForgePreferences.FPref setting) {
final JComboBox<E> comboBox = new JComboBox<E>(items); final FComboBox<E> comboBox = new FComboBox<E>(items);
addComboBoxListener(comboBox, setting); addComboBoxListener(comboBox, setting);
return comboBox; return comboBox;
} }
private <E> void addComboBoxListener(final JComboBox<E> comboBox, final ForgePreferences.FPref setting) { private <E> void addComboBoxListener(final FComboBox<E> comboBox, final ForgePreferences.FPref setting) {
comboBox.addItemListener(new ItemListener() { comboBox.addItemListener(new ItemListener() {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override

View File

@@ -4,6 +4,8 @@ import java.awt.Component;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.LayoutManager; import java.awt.LayoutManager;
import java.util.Vector;
import javax.swing.ComboBoxModel; import javax.swing.ComboBoxModel;
import javax.swing.DefaultListCellRenderer; import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton; import javax.swing.JButton;
@@ -45,6 +47,10 @@ public class FComboBox<E> extends JComboBox<E> {
super(items); super(items);
initialize(); initialize();
} }
public FComboBox(Vector<E> items) {
super(items);
initialize();
}
private void initialize() { private void initialize() {
setUI(new FComboBoxUI()); setUI(new FComboBoxUI());
@@ -64,10 +70,16 @@ public class FComboBox<E> extends JComboBox<E> {
super.paintComponent(g); super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g; Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(getForeground()); g2d.setPaint(getForeground());
int shapeWidth = 10; int shapeWidth = 8;
int shapeHeight = 10; int shapeHeight = 8;
int x = getWidth() - shapeWidth - 8; int x = getWidth() - shapeWidth - 6;
int y = getHeight() / 2 - 2; int y = getHeight() / 2 - 1;
if (getHeight() > 26) { //increase arrow size if taller combo box
shapeWidth += 2;
shapeHeight += 2;
x -= 4;
y--;
}
int[] xPoints = {x, x + shapeWidth, x + (shapeWidth / 2)}; int[] xPoints = {x, x + shapeWidth, x + (shapeWidth / 2)};
int[] yPoints = {y, y, y + (shapeHeight / 2)}; int[] yPoints = {y, y, y + (shapeHeight / 2)};
g2d.fillPolygon(xPoints, yPoints, 3); g2d.fillPolygon(xPoints, yPoints, 3);

View File

@@ -5,7 +5,6 @@ import java.awt.FlowLayout;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.DefaultListCellRenderer; import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.JPanel; import javax.swing.JPanel;
@@ -17,7 +16,7 @@ import forge.gui.toolbox.FSkin.JLabelSkin;
import forge.properties.ForgePreferences.FPref; import forge.properties.ForgePreferences.FPref;
/** /**
* Panel with combo box and caption (either FComboBoxWrapper or FComboBoxPanel should be used instead of JComboBox so skinning works) * Panel with combo box and caption (either FComboBoxWrapper or FComboBoxPanel should be used instead of FComboBox so skinning works)
* *
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
@@ -32,7 +31,7 @@ public class FComboBoxPanel<E> extends JPanel {
} }
private String comboBoxCaption = ""; private String comboBoxCaption = "";
private JComboBox<E> comboBox = null; private FComboBox<E> comboBox = null;
public FComboBoxPanel(String comboBoxCaption) { public FComboBoxPanel(String comboBoxCaption) {
super(); super();
@@ -41,7 +40,7 @@ public class FComboBoxPanel<E> extends JPanel {
allPanels.add(this); allPanels.add(this);
} }
public void setComboBox(JComboBox<E> comboBox, E selectedItem) { public void setComboBox(FComboBox<E> comboBox, E selectedItem) {
removeExistingComboBox(); removeExistingComboBox();
this.comboBox = comboBox; this.comboBox = comboBox;
this.comboBox.setSelectedItem(selectedItem); this.comboBox.setSelectedItem(selectedItem);
@@ -81,7 +80,7 @@ public class FComboBoxPanel<E> extends JPanel {
private void setComboBoxLayout() { private void setComboBoxLayout() {
if (this.comboBox != null) { if (this.comboBox != null) {
if (Singletons.getModel().getPreferences().getPrefBoolean(FPref.UI_THEMED_COMBOBOX)) { if (Singletons.getModel().getPreferences().getPrefBoolean(FPref.UI_THEMED_COMBOBOX)) {
FSkin.JComponentSkin<JComboBox<E>> comboBoxSkin = FSkin.get(this.comboBox); FSkin.JComponentSkin<FComboBox<E>> comboBoxSkin = FSkin.get(this.comboBox);
comboBoxSkin.setBackground(FSkin.getColor(FSkin.Colors.CLR_THEME2)); comboBoxSkin.setBackground(FSkin.getColor(FSkin.Colors.CLR_THEME2));
comboBoxSkin.setForeground(FSkin.getColor(FSkin.Colors.CLR_TEXT)); comboBoxSkin.setForeground(FSkin.getColor(FSkin.Colors.CLR_TEXT));
comboBoxSkin.setFont(FSkin.getFont(12)); comboBoxSkin.setFont(FSkin.getFont(12));

View File

@@ -8,11 +8,10 @@ import java.util.ArrayList;
import java.util.Vector; import java.util.Vector;
import javax.swing.ComboBoxModel; import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.ListCellRenderer; import javax.swing.ListCellRenderer;
/** /**
* Wrapper for combo box with extra logic (either FComboBoxWrapper or FComboBoxPanel should be used instead of JComboBox so skinning works) * Wrapper for combo box with extra logic (either FComboBoxWrapper or FComboBoxPanel should be used instead of FComboBox so skinning works)
* *
*/ */
public class FComboBoxWrapper<E> { public class FComboBoxWrapper<E> {
@@ -25,30 +24,30 @@ public class FComboBoxWrapper<E> {
} }
} }
private JComboBox<E> comboBox; private FComboBox<E> comboBox;
private Object constraints; private Object constraints;
public FComboBoxWrapper() { public FComboBoxWrapper() {
super(); super();
this.comboBox = new JComboBox<E>(); this.comboBox = new FComboBox<E>();
allWrappers.add(this); allWrappers.add(this);
} }
public FComboBoxWrapper(E[] items) { public FComboBoxWrapper(E[] items) {
super(); super();
this.comboBox = new JComboBox<E>(items); this.comboBox = new FComboBox<E>(items);
allWrappers.add(this); allWrappers.add(this);
} }
public FComboBoxWrapper(Vector<E> items) { public FComboBoxWrapper(Vector<E> items) {
super(); super();
this.comboBox = new JComboBox<E>(items); this.comboBox = new FComboBox<E>(items);
allWrappers.add(this); allWrappers.add(this);
} }
public FComboBoxWrapper(ComboBoxModel<E> aModel) { public FComboBoxWrapper(ComboBoxModel<E> aModel) {
super(); super();
this.comboBox = new JComboBox<E>(aModel); this.comboBox = new FComboBox<E>(aModel);
allWrappers.add(this); allWrappers.add(this);
} }
@@ -129,10 +128,10 @@ public class FComboBoxWrapper<E> {
//refresh combo box skin by replacing it with a copy of itself //refresh combo box skin by replacing it with a copy of itself
//TODO: Figure out if there's a better way, as calling updateUI doesn't seem to work //TODO: Figure out if there's a better way, as calling updateUI doesn't seem to work
public static <E> JComboBox<E> refreshComboBoxSkin(JComboBox<E> comboBox) { public static <E> FComboBox<E> refreshComboBoxSkin(FComboBox<E> comboBox) {
return refreshComboBoxSkin(comboBox, null); return refreshComboBoxSkin(comboBox, null);
} }
public static <E> JComboBox<E> refreshComboBoxSkin(JComboBox<E> comboBox, Object constraints) { public static <E> FComboBox<E> refreshComboBoxSkin(FComboBox<E> comboBox, Object constraints) {
//find index of combo box within parent //find index of combo box within parent
Container parent = comboBox.getParent(); Container parent = comboBox.getParent();
if (parent == null) { return comboBox; } if (parent == null) { return comboBox; }
@@ -145,7 +144,7 @@ public class FComboBoxWrapper<E> {
} }
//create copy of combo box //create copy of combo box
JComboBox<E> newComboBox = new JComboBox<E>(); FComboBox<E> newComboBox = new FComboBox<E>();
for (int i = 0; i < comboBox.getItemCount(); i++) { for (int i = 0; i < comboBox.getItemCount(); i++) {
newComboBox.addItem(comboBox.getItemAt(i)); newComboBox.addItem(comboBox.getItemAt(i));
} }