| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
package com.timjohnstondev.unitconverter.view; |
| 12 | |
|
| 13 | |
import java.awt.Color; |
| 14 | |
import java.awt.Component; |
| 15 | |
import java.awt.FlowLayout; |
| 16 | |
import java.awt.Font; |
| 17 | |
import java.awt.Graphics; |
| 18 | |
import java.awt.event.ActionEvent; |
| 19 | |
import java.awt.event.ActionListener; |
| 20 | |
import java.util.ResourceBundle; |
| 21 | |
import javax.swing.Icon; |
| 22 | |
import javax.swing.JButton; |
| 23 | |
import javax.swing.JColorChooser; |
| 24 | |
import javax.swing.JComboBox; |
| 25 | |
import javax.swing.JDialog; |
| 26 | |
import javax.swing.JLabel; |
| 27 | |
import javax.swing.JList; |
| 28 | |
import javax.swing.JPanel; |
| 29 | |
import javax.swing.ListCellRenderer; |
| 30 | |
import com.timjohnstondev.unitconverter.controller.LogicController; |
| 31 | |
import com.timjohnstondev.unitconverter.controller.ModelController; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 0 | public class ConfigurationPanel extends JPanel implements Configuration, ActionListener |
| 38 | |
{ |
| 39 | |
private LogicController logicController; |
| 40 | |
private int iconWidthHeight; |
| 41 | |
private JButton backgroundColorButton; |
| 42 | |
private JButton foregroundColorButton; |
| 43 | |
private JButton defaultColorButton; |
| 44 | |
private Color chosen; |
| 45 | |
private JComboBox fontSizeList; |
| 46 | |
private JComboBox unitSeparatorList; |
| 47 | |
private UnitConverter mainPanel; |
| 48 | |
private ModelController modelController; |
| 49 | |
private ResourceBundle resources; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public ConfigurationPanel(final UnitConverter appPanel, final ModelController control) |
| 59 | 0 | { |
| 60 | 0 | mainPanel = appPanel; |
| 61 | 0 | logicController = new LogicController(); |
| 62 | 0 | modelController = control; |
| 63 | 0 | setLayout(new FlowLayout(FlowLayout.RIGHT, 15, 5)); |
| 64 | |
|
| 65 | 0 | resources = ResourceBundle.getBundle("com.timjohnstondev.unitconverter.view.View"); |
| 66 | 0 | iconWidthHeight = Integer.parseInt(resources.getString("ConfigurationPanel.iconWidthHeight")); |
| 67 | |
|
| 68 | 0 | backgroundColorButton = new JButton(resources.getString("ConfigurationPanel.backgroundColorButtonText")); |
| 69 | 0 | foregroundColorButton = new JButton(resources.getString("ConfigurationPanel.foregroundColorButtonText")); |
| 70 | 0 | defaultColorButton = new JButton(resources.getString("ConfigurationPanel.defaultColorButtonText")); |
| 71 | |
|
| 72 | 0 | backgroundColorButton.setIcon(new ColorIcon(backgroundColorButton.getText())); |
| 73 | 0 | foregroundColorButton.setIcon(new ColorIcon(foregroundColorButton.getText())); |
| 74 | |
|
| 75 | 0 | final JLabel unitSeparatorLabel = new JLabel(resources.getString("ConfigurationPanel.unitSeparatorLabel")); |
| 76 | 0 | unitSeparatorList = new JComboBox(resources.getString("ConfigurationPanel.unitSeparatorList").split(",")); |
| 77 | 0 | unitSeparatorList.setSelectedIndex(getSeparatorPreferenceIndex()); |
| 78 | 0 | unitSeparatorList.setRenderer(new ComboBoxRenderer()); |
| 79 | |
|
| 80 | 0 | final JLabel fontSizeLabel = new JLabel(resources.getString("ConfigurationPanel.defaultColorButtonText")); |
| 81 | 0 | fontSizeList = new JComboBox(resources.getString("ConfigurationPanel.fontSizes").split(",")); |
| 82 | 0 | fontSizeList.setSelectedIndex(getFontPreferenceIndex()); |
| 83 | 0 | fontSizeList.setRenderer(new ComboBoxRenderer()); |
| 84 | |
|
| 85 | 0 | addActionListeners(); |
| 86 | |
|
| 87 | 0 | add(backgroundColorButton); |
| 88 | 0 | add(foregroundColorButton); |
| 89 | 0 | add(defaultColorButton); |
| 90 | 0 | add(unitSeparatorLabel); |
| 91 | 0 | add(unitSeparatorList); |
| 92 | 0 | add(fontSizeLabel); |
| 93 | 0 | add(fontSizeList); |
| 94 | 0 | } |
| 95 | |
|
| 96 | |
private void addActionListeners() |
| 97 | |
{ |
| 98 | 0 | backgroundColorButton.addActionListener(this); |
| 99 | 0 | foregroundColorButton.addActionListener(this); |
| 100 | 0 | defaultColorButton.addActionListener(this); |
| 101 | 0 | unitSeparatorList.addActionListener(this); |
| 102 | 0 | fontSizeList.addActionListener(this); |
| 103 | 0 | } |
| 104 | |
|
| 105 | |
private int getSeparatorPreferenceIndex() |
| 106 | |
{ |
| 107 | 0 | final String unitSeparator = logicController.getUnitSeparatorPreference(); |
| 108 | 0 | modelController.setUnitSeparator(unitSeparator.replaceAll("\"", "")); |
| 109 | 0 | return logicController.getPreferenceIndex(unitSeparatorList.getModel(), unitSeparator); |
| 110 | |
} |
| 111 | |
|
| 112 | |
private int getFontPreferenceIndex() |
| 113 | |
{ |
| 114 | 0 | final int fontSize = logicController.getFontSizePreference(getFont().getSize()); |
| 115 | 0 | return logicController.getPreferenceIndex(fontSizeList.getModel(), fontSize); |
| 116 | |
} |
| 117 | |
|
| 118 | |
private class ColorIcon implements Icon |
| 119 | |
{ |
| 120 | |
private String property; |
| 121 | |
|
| 122 | |
public ColorIcon(final String prop) |
| 123 | 0 | { |
| 124 | 0 | property = prop; |
| 125 | 0 | } |
| 126 | |
|
| 127 | |
@Override |
| 128 | |
public int getIconHeight() |
| 129 | |
{ |
| 130 | 0 | return iconWidthHeight; |
| 131 | |
} |
| 132 | |
|
| 133 | |
@Override |
| 134 | |
public int getIconWidth() |
| 135 | |
{ |
| 136 | 0 | return iconWidthHeight; |
| 137 | |
} |
| 138 | |
|
| 139 | |
@Override |
| 140 | |
public void paintIcon(final Component component, final Graphics graphic, final int x, final int y) |
| 141 | |
{ |
| 142 | 0 | graphic.setColor(Color.black); |
| 143 | 0 | graphic.fillRect(x, y, getIconWidth(), getIconHeight()); |
| 144 | 0 | if (property.equals(backgroundColorButton.getText())) |
| 145 | |
{ |
| 146 | 0 | graphic.setColor(mainPanel.getContentPane().getBackground()); |
| 147 | |
} |
| 148 | 0 | else if (property.equals(foregroundColorButton.getText())) |
| 149 | |
{ |
| 150 | 0 | graphic.setColor(mainPanel.getContentPane().getForeground()); |
| 151 | |
} |
| 152 | 0 | graphic.fillRect(x + 1, y + 1, getIconWidth() - 2, getIconHeight() - 2); |
| 153 | 0 | } |
| 154 | |
} |
| 155 | |
|
| 156 | |
@Override |
| 157 | |
public final void setBackgroundColor(final Color color) |
| 158 | |
{ |
| 159 | 0 | setBackground(color); |
| 160 | 0 | final Component[] children = getComponents(); |
| 161 | 0 | for (Component child : children) |
| 162 | |
{ |
| 163 | 0 | if (child instanceof JComboBox) |
| 164 | |
{ |
| 165 | 0 | final JComboBox box = (JComboBox) child; |
| 166 | 0 | final Color buttonColor = box.getComponent(0).getBackground(); |
| 167 | 0 | box.setBackground(color); |
| 168 | 0 | box.getComponent(0).setBackground(buttonColor); |
| 169 | |
} |
| 170 | |
} |
| 171 | 0 | } |
| 172 | |
|
| 173 | |
@Override |
| 174 | |
public final void setForegroundColor(final Color color) |
| 175 | |
{ |
| 176 | 0 | setForeground(color); |
| 177 | 0 | final Component[] children = getComponents(); |
| 178 | 0 | for (Component child : children) |
| 179 | |
{ |
| 180 | 0 | if (child != defaultColorButton) |
| 181 | |
{ |
| 182 | 0 | child.setForeground(color); |
| 183 | |
} |
| 184 | |
} |
| 185 | 0 | } |
| 186 | |
|
| 187 | |
@Override |
| 188 | |
public final void actionPerformed(final ActionEvent event) |
| 189 | |
{ |
| 190 | 0 | if (event.getSource() == fontSizeList) |
| 191 | |
{ |
| 192 | 0 | final JComboBox list = (JComboBox) event.getSource(); |
| 193 | 0 | final String fontSize = (String) list.getSelectedItem(); |
| 194 | 0 | mainPanel.setFontSize(Float.parseFloat(fontSize)); |
| 195 | 0 | } |
| 196 | 0 | else if (event.getSource() == unitSeparatorList) |
| 197 | |
{ |
| 198 | 0 | final JComboBox list = (JComboBox) event.getSource(); |
| 199 | 0 | final String value = (String) list.getSelectedItem(); |
| 200 | 0 | modelController.setUnitSeparator(value.replaceAll("\"", "")); |
| 201 | 0 | logicController.setUnitSeparatorPreference(value); |
| 202 | 0 | } |
| 203 | 0 | else if (event.getSource() == defaultColorButton) |
| 204 | |
{ |
| 205 | 0 | resetColors(); |
| 206 | |
} |
| 207 | |
else |
| 208 | |
{ |
| 209 | 0 | Color current = mainPanel.getContentPane().getBackground(); |
| 210 | 0 | if (event.getSource() == foregroundColorButton) |
| 211 | |
{ |
| 212 | 0 | current = mainPanel.getContentPane().getForeground(); |
| 213 | |
} |
| 214 | |
|
| 215 | 0 | final JColorChooser chooser = new JColorChooser(current != null ? current : Color.WHITE); |
| 216 | 0 | chooser.setDragEnabled(true); |
| 217 | |
|
| 218 | 0 | chosen = null; |
| 219 | 0 | final ActionListener okListener = new ActionListener() |
| 220 | 0 | { |
| 221 | |
public void actionPerformed(final ActionEvent ae) |
| 222 | |
{ |
| 223 | 0 | chosen = chooser.getColor(); |
| 224 | 0 | } |
| 225 | |
}; |
| 226 | |
|
| 227 | 0 | final JDialog dialog = JColorChooser.createDialog(getParent(), resources |
| 228 | |
.getString("ConfigurationPanel.dialogTitle"), true, chooser, okListener, null); |
| 229 | 0 | dialog.setVisible(true); |
| 230 | |
|
| 231 | 0 | if (event.getSource() == backgroundColorButton && chosen != null) |
| 232 | |
{ |
| 233 | 0 | mainPanel.setBackgroundColor(chosen); |
| 234 | |
} |
| 235 | 0 | else if (event.getSource() == foregroundColorButton && chosen != null) |
| 236 | |
{ |
| 237 | 0 | mainPanel.setForegroundColor(chosen); |
| 238 | |
} |
| 239 | |
} |
| 240 | 0 | } |
| 241 | |
|
| 242 | |
private void resetColors() |
| 243 | |
{ |
| 244 | 0 | mainPanel.setBackgroundColor(new Color(238, 238, 238)); |
| 245 | 0 | mainPanel.setForegroundColor(new Color(51, 51, 51)); |
| 246 | 0 | } |
| 247 | |
|
| 248 | |
@Override |
| 249 | |
public final void setFontSize(final float fontSize) |
| 250 | |
{ |
| 251 | 0 | final Font font = getFont().deriveFont(fontSize); |
| 252 | 0 | setFont(font); |
| 253 | 0 | final Component[] children = getComponents(); |
| 254 | 0 | for (Component child : children) |
| 255 | |
{ |
| 256 | 0 | child.setFont(font); |
| 257 | |
} |
| 258 | 0 | } |
| 259 | |
|
| 260 | |
private class ComboBoxRenderer extends JLabel implements ListCellRenderer |
| 261 | |
{ |
| 262 | |
public ComboBoxRenderer() |
| 263 | 0 | { |
| 264 | 0 | setOpaque(true); |
| 265 | 0 | } |
| 266 | |
|
| 267 | |
@Override |
| 268 | |
public Component getListCellRendererComponent(final JList list, final Object value, final int index, |
| 269 | |
final boolean isSelected, final boolean cellHasFocus) |
| 270 | |
{ |
| 271 | 0 | setText(value.toString()); |
| 272 | |
|
| 273 | 0 | if (isSelected) |
| 274 | |
{ |
| 275 | 0 | setBackground(ConfigurationPanel.this.getBackground().brighter()); |
| 276 | |
} |
| 277 | |
else |
| 278 | |
{ |
| 279 | 0 | setBackground(ConfigurationPanel.this.getBackground()); |
| 280 | |
} |
| 281 | 0 | return this; |
| 282 | |
} |
| 283 | |
} |
| 284 | |
} |