| 1 | package be.nikiroo.utils.ui; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.Color; |
| 5 | import java.awt.Dimension; |
| 6 | import java.awt.Graphics2D; |
| 7 | import java.awt.event.ActionEvent; |
| 8 | import java.awt.event.ActionListener; |
| 9 | import java.awt.image.BufferedImage; |
| 10 | import java.io.File; |
| 11 | |
| 12 | import javax.swing.Icon; |
| 13 | import javax.swing.ImageIcon; |
| 14 | import javax.swing.InputVerifier; |
| 15 | import javax.swing.JButton; |
| 16 | import javax.swing.JCheckBox; |
| 17 | import javax.swing.JColorChooser; |
| 18 | import javax.swing.JComboBox; |
| 19 | import javax.swing.JComponent; |
| 20 | import javax.swing.JFileChooser; |
| 21 | import javax.swing.JLabel; |
| 22 | import javax.swing.JOptionPane; |
| 23 | import javax.swing.JPanel; |
| 24 | import javax.swing.JPasswordField; |
| 25 | import javax.swing.JTextField; |
| 26 | import javax.swing.plaf.basic.BasicArrowButton; |
| 27 | |
| 28 | import be.nikiroo.utils.StringUtils; |
| 29 | import be.nikiroo.utils.StringUtils.Alignment; |
| 30 | import be.nikiroo.utils.resources.Bundle; |
| 31 | import be.nikiroo.utils.resources.Meta.Format; |
| 32 | import be.nikiroo.utils.resources.MetaInfo; |
| 33 | |
| 34 | /** |
| 35 | * A graphical item that reflect a configuration option from the given |
| 36 | * {@link Bundle}. |
| 37 | * <p> |
| 38 | * This graphical item can be edited, and the result will be saved back into the |
| 39 | * linked {@link MetaInfo}; you still have to save the {@link MetaInfo} should |
| 40 | * you wish to, of course. |
| 41 | * |
| 42 | * @author niki |
| 43 | * |
| 44 | * @param <E> |
| 45 | * the type of {@link Bundle} to edit |
| 46 | */ |
| 47 | public class ConfigItem<E extends Enum<E>> extends JPanel { |
| 48 | private static final long serialVersionUID = 1L; |
| 49 | |
| 50 | /** |
| 51 | * Create a new {@link ConfigItem} for the given {@link MetaInfo}. |
| 52 | * |
| 53 | * @param info |
| 54 | * the {@link MetaInfo} |
| 55 | */ |
| 56 | public ConfigItem(MetaInfo<E> info) { |
| 57 | this.setLayout(new BorderLayout()); |
| 58 | |
| 59 | // TODO: support arrays |
| 60 | Format fmt = info.getFormat(); |
| 61 | if (info.isArray()) { |
| 62 | fmt = Format.STRING; |
| 63 | } |
| 64 | |
| 65 | switch (fmt) { |
| 66 | case BOOLEAN: |
| 67 | addBooleanField(info); |
| 68 | break; |
| 69 | case COLOR: |
| 70 | addColorField(info); |
| 71 | break; |
| 72 | case FILE: |
| 73 | addBrowseField(info, false); |
| 74 | break; |
| 75 | case DIRECTORY: |
| 76 | addBrowseField(info, true); |
| 77 | break; |
| 78 | case COMBO_LIST: |
| 79 | addComboboxField(info, true); |
| 80 | break; |
| 81 | case FIXED_LIST: |
| 82 | addComboboxField(info, false); |
| 83 | break; |
| 84 | case INT: |
| 85 | addIntField(info); |
| 86 | break; |
| 87 | case PASSWORD: |
| 88 | addPasswordField(info); |
| 89 | break; |
| 90 | case STRING: |
| 91 | case LOCALE: // TODO? |
| 92 | default: |
| 93 | addStringField(info); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private void addStringField(final MetaInfo<E> info) { |
| 99 | final JTextField field = new JTextField(); |
| 100 | field.setToolTipText(info.getDescription()); |
| 101 | field.setText(info.getString()); |
| 102 | |
| 103 | info.addReloadedListener(new Runnable() { |
| 104 | @Override |
| 105 | public void run() { |
| 106 | field.setText(info.getString()); |
| 107 | } |
| 108 | }); |
| 109 | info.addSaveListener(new Runnable() { |
| 110 | @Override |
| 111 | public void run() { |
| 112 | info.setString(field.getText()); |
| 113 | } |
| 114 | }); |
| 115 | |
| 116 | this.add(label(info), BorderLayout.WEST); |
| 117 | this.add(field, BorderLayout.CENTER); |
| 118 | } |
| 119 | |
| 120 | private void addBooleanField(final MetaInfo<E> info) { |
| 121 | final JCheckBox field = new JCheckBox(); |
| 122 | field.setToolTipText(info.getDescription()); |
| 123 | Boolean state = info.getBoolean(); |
| 124 | if (state == null) { |
| 125 | info.getDefaultBoolean(); |
| 126 | } |
| 127 | |
| 128 | // Should not happen! |
| 129 | if (state == null) { |
| 130 | System.err |
| 131 | .println("No default value given for BOOLEAN parameter \"" |
| 132 | + info.getName() + "\", we consider it is FALSE"); |
| 133 | state = false; |
| 134 | } |
| 135 | |
| 136 | field.setSelected(state); |
| 137 | |
| 138 | info.addReloadedListener(new Runnable() { |
| 139 | @Override |
| 140 | public void run() { |
| 141 | Boolean state = info.getBoolean(); |
| 142 | if (state == null) { |
| 143 | info.getDefaultBoolean(); |
| 144 | } |
| 145 | if (state == null) { |
| 146 | state = false; |
| 147 | } |
| 148 | |
| 149 | field.setSelected(state); |
| 150 | } |
| 151 | }); |
| 152 | info.addSaveListener(new Runnable() { |
| 153 | @Override |
| 154 | public void run() { |
| 155 | info.setBoolean(field.isSelected()); |
| 156 | } |
| 157 | }); |
| 158 | |
| 159 | field.setText(info.getName()); |
| 160 | this.add(field, BorderLayout.CENTER); |
| 161 | } |
| 162 | |
| 163 | private void addColorField(final MetaInfo<E> info) { |
| 164 | final JTextField field = new JTextField(); |
| 165 | field.setToolTipText(info.getDescription()); |
| 166 | field.setText(info.getString()); |
| 167 | |
| 168 | info.addReloadedListener(new Runnable() { |
| 169 | @Override |
| 170 | public void run() { |
| 171 | field.setText(info.getString()); |
| 172 | } |
| 173 | }); |
| 174 | info.addSaveListener(new Runnable() { |
| 175 | @Override |
| 176 | public void run() { |
| 177 | info.setString(field.getText()); |
| 178 | } |
| 179 | }); |
| 180 | |
| 181 | this.add(label(info), BorderLayout.WEST); |
| 182 | JPanel pane = new JPanel(new BorderLayout()); |
| 183 | |
| 184 | final JButton colorWheel = new JButton(); |
| 185 | colorWheel.setIcon(getIcon(17, info.getColor())); |
| 186 | colorWheel.addActionListener(new ActionListener() { |
| 187 | @Override |
| 188 | public void actionPerformed(ActionEvent e) { |
| 189 | Color initialColor = new Color(info.getColor(), true); |
| 190 | Color newColor = JColorChooser.showDialog(ConfigItem.this, |
| 191 | info.getName(), initialColor); |
| 192 | if (newColor != null) { |
| 193 | info.setColor(newColor.getRGB()); |
| 194 | field.setText(info.getString()); |
| 195 | colorWheel.setIcon(getIcon(17, info.getColor())); |
| 196 | } |
| 197 | } |
| 198 | }); |
| 199 | pane.add(colorWheel, BorderLayout.WEST); |
| 200 | pane.add(field, BorderLayout.CENTER); |
| 201 | this.add(pane, BorderLayout.CENTER); |
| 202 | } |
| 203 | |
| 204 | private void addBrowseField(final MetaInfo<E> info, final boolean dir) { |
| 205 | final JTextField field = new JTextField(); |
| 206 | field.setToolTipText(info.getDescription()); |
| 207 | field.setText(info.getString()); |
| 208 | |
| 209 | info.addReloadedListener(new Runnable() { |
| 210 | @Override |
| 211 | public void run() { |
| 212 | field.setText(info.getString()); |
| 213 | } |
| 214 | }); |
| 215 | info.addSaveListener(new Runnable() { |
| 216 | @Override |
| 217 | public void run() { |
| 218 | info.setString(field.getText()); |
| 219 | } |
| 220 | }); |
| 221 | |
| 222 | JButton browseButton = new JButton("..."); |
| 223 | browseButton.addActionListener(new ActionListener() { |
| 224 | @Override |
| 225 | public void actionPerformed(ActionEvent e) { |
| 226 | JFileChooser chooser = new JFileChooser(); |
| 227 | chooser.setCurrentDirectory(null); |
| 228 | chooser.setFileSelectionMode(dir ? JFileChooser.DIRECTORIES_ONLY |
| 229 | : JFileChooser.FILES_ONLY); |
| 230 | if (chooser.showOpenDialog(ConfigItem.this) == JFileChooser.APPROVE_OPTION) { |
| 231 | File file = chooser.getSelectedFile(); |
| 232 | if (file != null) { |
| 233 | info.setString(file.getAbsolutePath()); |
| 234 | field.setText(info.getString()); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | }); |
| 239 | |
| 240 | JPanel pane = new JPanel(new BorderLayout()); |
| 241 | this.add(label(info), BorderLayout.WEST); |
| 242 | pane.add(browseButton, BorderLayout.WEST); |
| 243 | pane.add(field, BorderLayout.CENTER); |
| 244 | this.add(pane, BorderLayout.CENTER); |
| 245 | } |
| 246 | |
| 247 | private void addComboboxField(final MetaInfo<E> info, boolean editable) { |
| 248 | // rawtypes for Java 1.6 (and 1.7 ?) support |
| 249 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 250 | final JComboBox field = new JComboBox(info.getAllowedValues()); |
| 251 | field.setEditable(editable); |
| 252 | field.setSelectedItem(info.getString()); |
| 253 | |
| 254 | info.addReloadedListener(new Runnable() { |
| 255 | @Override |
| 256 | public void run() { |
| 257 | field.setSelectedItem(info.getString()); |
| 258 | } |
| 259 | }); |
| 260 | info.addSaveListener(new Runnable() { |
| 261 | @Override |
| 262 | public void run() { |
| 263 | info.setString(field.getSelectedItem().toString()); |
| 264 | } |
| 265 | }); |
| 266 | |
| 267 | this.add(label(info), BorderLayout.WEST); |
| 268 | this.add(field, BorderLayout.CENTER); |
| 269 | } |
| 270 | |
| 271 | private void addPasswordField(final MetaInfo<E> info) { |
| 272 | final JPasswordField field = new JPasswordField(); |
| 273 | field.setToolTipText(info.getDescription()); |
| 274 | field.setText(info.getString()); |
| 275 | |
| 276 | info.addReloadedListener(new Runnable() { |
| 277 | @Override |
| 278 | public void run() { |
| 279 | field.setText(info.getString()); |
| 280 | } |
| 281 | }); |
| 282 | info.addSaveListener(new Runnable() { |
| 283 | @Override |
| 284 | public void run() { |
| 285 | info.setString(new String(field.getPassword())); |
| 286 | } |
| 287 | }); |
| 288 | |
| 289 | this.add(label(info), BorderLayout.WEST); |
| 290 | this.add(field, BorderLayout.CENTER); |
| 291 | } |
| 292 | |
| 293 | private void addIntField(final MetaInfo<E> info) { |
| 294 | final JTextField field = new JTextField(); |
| 295 | field.setToolTipText(info.getDescription()); |
| 296 | field.setText(info.getString()); |
| 297 | field.setInputVerifier(new InputVerifier() { |
| 298 | @Override |
| 299 | public boolean verify(JComponent input) { |
| 300 | String text = field.getText().trim(); |
| 301 | if (text.startsWith("-")) { |
| 302 | text = text.substring(1).trim(); |
| 303 | } |
| 304 | |
| 305 | return text.replaceAll("[0-9]", "").isEmpty(); |
| 306 | } |
| 307 | }); |
| 308 | |
| 309 | info.addReloadedListener(new Runnable() { |
| 310 | @Override |
| 311 | public void run() { |
| 312 | field.setText(info.getString()); |
| 313 | } |
| 314 | }); |
| 315 | info.addSaveListener(new Runnable() { |
| 316 | @Override |
| 317 | public void run() { |
| 318 | info.setString(field.getText()); |
| 319 | Integer value = info.getInteger(); |
| 320 | if (value == null) { |
| 321 | info.setString(""); |
| 322 | } else { |
| 323 | info.setInteger(value); |
| 324 | } |
| 325 | field.setText(info.getString()); |
| 326 | } |
| 327 | }); |
| 328 | |
| 329 | JButton up = new BasicArrowButton(BasicArrowButton.NORTH); |
| 330 | JButton down = new BasicArrowButton(BasicArrowButton.SOUTH); |
| 331 | |
| 332 | up.addActionListener(new ActionListener() { |
| 333 | @Override |
| 334 | public void actionPerformed(ActionEvent ae) { |
| 335 | int value = 0; |
| 336 | try { |
| 337 | value = Integer.parseInt(field.getText()); |
| 338 | } catch (NumberFormatException e) { |
| 339 | } |
| 340 | |
| 341 | field.setText(Integer.toString(value + 1)); |
| 342 | } |
| 343 | }); |
| 344 | |
| 345 | down.addActionListener(new ActionListener() { |
| 346 | @Override |
| 347 | public void actionPerformed(ActionEvent ae) { |
| 348 | int value = 0; |
| 349 | try { |
| 350 | value = Integer.parseInt(field.getText()); |
| 351 | } catch (NumberFormatException e) { |
| 352 | } |
| 353 | |
| 354 | field.setText(Integer.toString(value - 1)); |
| 355 | } |
| 356 | }); |
| 357 | |
| 358 | JPanel upDown = new JPanel(new BorderLayout()); |
| 359 | upDown.add(up, BorderLayout.NORTH); |
| 360 | upDown.add(down, BorderLayout.SOUTH); |
| 361 | |
| 362 | JPanel pane = new JPanel(new BorderLayout()); |
| 363 | pane.add(upDown, BorderLayout.WEST); |
| 364 | pane.add(field, BorderLayout.CENTER); |
| 365 | |
| 366 | this.add(label(info), BorderLayout.WEST); |
| 367 | this.add(pane, BorderLayout.CENTER); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Create a label which width is constrained in lock steps. |
| 372 | * |
| 373 | * @param info |
| 374 | * the {@link MetaInfo} for which we want to add a label |
| 375 | * |
| 376 | * @return the label |
| 377 | */ |
| 378 | private JComponent label(final MetaInfo<E> info) { |
| 379 | final JLabel label = new JLabel(info.getName()); |
| 380 | |
| 381 | Dimension ps = label.getPreferredSize(); |
| 382 | if (ps == null) { |
| 383 | ps = label.getSize(); |
| 384 | } |
| 385 | |
| 386 | int w = ps.width; |
| 387 | int step = 80; |
| 388 | for (int i = 2 * step; i < 10 * step; i += step) { |
| 389 | if (w < i) { |
| 390 | w = i; |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // TODO: image |
| 396 | JButton help = new JButton("?"); |
| 397 | help.addActionListener(new ActionListener() { |
| 398 | @Override |
| 399 | public void actionPerformed(ActionEvent e) { |
| 400 | StringBuilder builder = new StringBuilder(); |
| 401 | String text = info.getDescription().replace("\\n", "\n"); |
| 402 | for (String line : StringUtils.justifyText(text, 80, |
| 403 | Alignment.LEFT)) { |
| 404 | if (builder.length() > 0) { |
| 405 | builder.append("\n"); |
| 406 | } |
| 407 | builder.append(line); |
| 408 | } |
| 409 | text = builder.toString(); |
| 410 | JOptionPane.showMessageDialog(ConfigItem.this, text); |
| 411 | } |
| 412 | }); |
| 413 | |
| 414 | JPanel pane2 = new JPanel(new BorderLayout()); |
| 415 | pane2.add(help, BorderLayout.WEST); |
| 416 | pane2.add(new JLabel(" "), BorderLayout.CENTER); |
| 417 | |
| 418 | JPanel pane = new JPanel(new BorderLayout()); |
| 419 | pane.add(label, BorderLayout.WEST); |
| 420 | pane.add(pane2, BorderLayout.CENTER); |
| 421 | |
| 422 | ps.width = w + 30; // 30 for the (?) sign |
| 423 | pane.setSize(ps); |
| 424 | pane.setPreferredSize(ps); |
| 425 | |
| 426 | return pane; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Return an {@link Icon} to use as a colour badge for the colour field |
| 431 | * controls. |
| 432 | * |
| 433 | * @param size |
| 434 | * the size of the badge |
| 435 | * @param color |
| 436 | * the colour of the badge |
| 437 | * |
| 438 | * @return the badge |
| 439 | */ |
| 440 | private Icon getIcon(int size, int color) { |
| 441 | Color c = new Color(color, true); |
| 442 | int avg = (c.getRed() + c.getGreen() + c.getBlue()) / 3; |
| 443 | Color border = (avg >= 128 ? Color.BLACK : Color.WHITE); |
| 444 | |
| 445 | BufferedImage img = new BufferedImage(size, size, |
| 446 | BufferedImage.TYPE_4BYTE_ABGR); |
| 447 | |
| 448 | Graphics2D g = img.createGraphics(); |
| 449 | try { |
| 450 | g.setColor(c); |
| 451 | g.fillRect(0, 0, img.getWidth(), img.getHeight()); |
| 452 | g.setColor(border); |
| 453 | g.drawRect(0, 0, img.getWidth() - 1, img.getHeight() - 1); |
| 454 | } finally { |
| 455 | g.dispose(); |
| 456 | } |
| 457 | |
| 458 | return new ImageIcon(img); |
| 459 | } |
| 460 | } |