| 1 | package be.nikiroo.utils.ui; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.Color; |
| 5 | import java.awt.Cursor; |
| 6 | import java.awt.Dimension; |
| 7 | import java.awt.Graphics2D; |
| 8 | import java.awt.event.ActionEvent; |
| 9 | import java.awt.event.ActionListener; |
| 10 | import java.awt.event.MouseAdapter; |
| 11 | import java.awt.event.MouseEvent; |
| 12 | import java.awt.image.BufferedImage; |
| 13 | import java.io.File; |
| 14 | import java.io.IOException; |
| 15 | |
| 16 | import javax.swing.Icon; |
| 17 | import javax.swing.ImageIcon; |
| 18 | import javax.swing.JButton; |
| 19 | import javax.swing.JCheckBox; |
| 20 | import javax.swing.JColorChooser; |
| 21 | import javax.swing.JComboBox; |
| 22 | import javax.swing.JComponent; |
| 23 | import javax.swing.JFileChooser; |
| 24 | import javax.swing.JLabel; |
| 25 | import javax.swing.JOptionPane; |
| 26 | import javax.swing.JPanel; |
| 27 | import javax.swing.JPasswordField; |
| 28 | import javax.swing.JSpinner; |
| 29 | import javax.swing.JTextField; |
| 30 | |
| 31 | import be.nikiroo.utils.Image; |
| 32 | import be.nikiroo.utils.StringUtils; |
| 33 | import be.nikiroo.utils.StringUtils.Alignment; |
| 34 | import be.nikiroo.utils.resources.Bundle; |
| 35 | import be.nikiroo.utils.resources.Meta.Format; |
| 36 | import be.nikiroo.utils.resources.MetaInfo; |
| 37 | |
| 38 | /** |
| 39 | * A graphical item that reflect a configuration option from the given |
| 40 | * {@link Bundle}. |
| 41 | * <p> |
| 42 | * This graphical item can be edited, and the result will be saved back into the |
| 43 | * linked {@link MetaInfo}; you still have to save the {@link MetaInfo} should |
| 44 | * you wish to, of course. |
| 45 | * |
| 46 | * @author niki |
| 47 | * |
| 48 | * @param <E> |
| 49 | * the type of {@link Bundle} to edit |
| 50 | */ |
| 51 | public class ConfigItem<E extends Enum<E>> extends JPanel { |
| 52 | private static final long serialVersionUID = 1L; |
| 53 | |
| 54 | /** A small (?) blue in PNG, base64 encoded. */ |
| 55 | private static String infoImage64 = // |
| 56 | "" |
| 57 | + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI" |
| 58 | + "WXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wURFRg6IrtcdgAAATdJREFUOMvtkj8sQ1EUxr9z/71G" |
| 59 | + "m1RDogYxq7WDDYMYTSajSG4n6YRYzSaSLibWbiaDIGwdiLIYDFKDNJEgKu969xi8UNHy7H7LPcN3" |
| 60 | + "v/Odcy+hG9oOIeIcBCJS9MAvlZtOMtHxsrFrJHGqe0RVGnHAHpcIbPlng8BS3HmKBJYzabGUzcrJ" |
| 61 | + "XK+ckIrqANYR2JEv2nYDEVck0WKGfHzyq82Go+btxoX3XAcAIqTj8wPqOH6mtMeM4bGCLhyfhTMA" |
| 62 | + "qlLhKHqujCfaweCAmV0p50dPzsNpEKpK01V/n55HIvTnfDC2odKlfeYadZN/T+AqDACUsnkhqaU1" |
| 63 | + "LRIVuX1x7ciuSWQxVIrunONrfq3dI6oh+T94Z8453vEem/HTqT8ZpFJ0qDXtGkPbAGAMeSRngQCA" |
| 64 | + "eUvgn195AwlZWyvjtQdhAAAAAElFTkSuQmCC"; |
| 65 | |
| 66 | /** The original value before current changes. */ |
| 67 | private Object orig; |
| 68 | |
| 69 | /** |
| 70 | * Create a new {@link ConfigItem} for the given {@link MetaInfo}. |
| 71 | * |
| 72 | * @param info |
| 73 | * the {@link MetaInfo} |
| 74 | * @param nhgap |
| 75 | * negative horisontal gap in pixel to use for the label, i.e., |
| 76 | * the step lock sized labels will start smaller by that amount |
| 77 | * (the use case would be to align controls that start at a |
| 78 | * different horisontal position) |
| 79 | */ |
| 80 | public ConfigItem(MetaInfo<E> info, int nhgap) { |
| 81 | this.setLayout(new BorderLayout()); |
| 82 | |
| 83 | // TODO: support arrays |
| 84 | Format fmt = info.getFormat(); |
| 85 | if (info.isArray()) { |
| 86 | fmt = Format.STRING; |
| 87 | } |
| 88 | |
| 89 | switch (fmt) { |
| 90 | case BOOLEAN: |
| 91 | addBooleanField(info, nhgap); |
| 92 | break; |
| 93 | case COLOR: |
| 94 | addColorField(info, nhgap); |
| 95 | break; |
| 96 | case FILE: |
| 97 | addBrowseField(info, nhgap, false); |
| 98 | break; |
| 99 | case DIRECTORY: |
| 100 | addBrowseField(info, nhgap, true); |
| 101 | break; |
| 102 | case COMBO_LIST: |
| 103 | addComboboxField(info, nhgap, true); |
| 104 | break; |
| 105 | case FIXED_LIST: |
| 106 | addComboboxField(info, nhgap, false); |
| 107 | break; |
| 108 | case INT: |
| 109 | addIntField(info, nhgap); |
| 110 | break; |
| 111 | case PASSWORD: |
| 112 | addPasswordField(info, nhgap); |
| 113 | break; |
| 114 | case STRING: |
| 115 | case LOCALE: // TODO? |
| 116 | default: |
| 117 | addStringField(info, nhgap); |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | private void reload(Object value) { |
| 123 | // We consider "" and NULL to be equals |
| 124 | if ("".equals(value)) { |
| 125 | value = null; |
| 126 | } |
| 127 | orig = value; |
| 128 | } |
| 129 | |
| 130 | private boolean isChanged(Object newValue) { |
| 131 | // We consider "" and NULL to be equals |
| 132 | if ("".equals(newValue)) { |
| 133 | newValue = null; |
| 134 | } |
| 135 | |
| 136 | if (newValue == null) { |
| 137 | return orig != null; |
| 138 | } |
| 139 | |
| 140 | return !newValue.equals(orig); |
| 141 | } |
| 142 | |
| 143 | private void addStringField(final MetaInfo<E> info, int nhgap) { |
| 144 | final JTextField field = new JTextField(); |
| 145 | field.setToolTipText(info.getDescription()); |
| 146 | String value = info.getString(false); |
| 147 | reload(value); |
| 148 | field.setText(value); |
| 149 | |
| 150 | info.addReloadedListener(new Runnable() { |
| 151 | @Override |
| 152 | public void run() { |
| 153 | String value = info.getString(false); |
| 154 | reload(value); |
| 155 | field.setText(value); |
| 156 | } |
| 157 | }); |
| 158 | info.addSaveListener(new Runnable() { |
| 159 | @Override |
| 160 | public void run() { |
| 161 | String value = field.getText(); |
| 162 | if (isChanged(value)) { |
| 163 | info.setString(value); |
| 164 | } |
| 165 | } |
| 166 | }); |
| 167 | |
| 168 | this.add(label(info, nhgap), BorderLayout.WEST); |
| 169 | this.add(field, BorderLayout.CENTER); |
| 170 | |
| 171 | setPreferredSize(field); |
| 172 | } |
| 173 | |
| 174 | private void addBooleanField(final MetaInfo<E> info, int nhgap) { |
| 175 | final JCheckBox field = new JCheckBox(); |
| 176 | field.setToolTipText(info.getDescription()); |
| 177 | Boolean state = info.getBoolean(true); |
| 178 | |
| 179 | // Should not happen! |
| 180 | if (state == null) { |
| 181 | System.err |
| 182 | .println("No default value given for BOOLEAN parameter \"" |
| 183 | + info.getName() + "\", we consider it is FALSE"); |
| 184 | state = false; |
| 185 | } |
| 186 | |
| 187 | reload(state); |
| 188 | field.setSelected(state); |
| 189 | |
| 190 | info.addReloadedListener(new Runnable() { |
| 191 | @Override |
| 192 | public void run() { |
| 193 | Boolean state = info.getBoolean(true); |
| 194 | if (state == null) { |
| 195 | state = false; |
| 196 | } |
| 197 | |
| 198 | reload(state); |
| 199 | field.setSelected(state); |
| 200 | } |
| 201 | }); |
| 202 | info.addSaveListener(new Runnable() { |
| 203 | @Override |
| 204 | public void run() { |
| 205 | boolean state = field.isSelected(); |
| 206 | if (isChanged(state)) { |
| 207 | info.setBoolean(state); |
| 208 | } |
| 209 | } |
| 210 | }); |
| 211 | |
| 212 | this.add(label(info, nhgap), BorderLayout.WEST); |
| 213 | this.add(field, BorderLayout.CENTER); |
| 214 | |
| 215 | setPreferredSize(field); |
| 216 | } |
| 217 | |
| 218 | private void addColorField(final MetaInfo<E> info, int nhgap) { |
| 219 | final JTextField field = new JTextField(); |
| 220 | field.setToolTipText(info.getDescription()); |
| 221 | String value = info.getString(false); |
| 222 | reload(value); |
| 223 | field.setText(value); |
| 224 | |
| 225 | info.addReloadedListener(new Runnable() { |
| 226 | @Override |
| 227 | public void run() { |
| 228 | String value = info.getString(false); |
| 229 | reload(value); |
| 230 | field.setText(value); |
| 231 | } |
| 232 | }); |
| 233 | info.addSaveListener(new Runnable() { |
| 234 | @Override |
| 235 | public void run() { |
| 236 | String value = field.getText(); |
| 237 | if (isChanged(value)) { |
| 238 | info.setString(value); |
| 239 | } |
| 240 | } |
| 241 | }); |
| 242 | |
| 243 | this.add(label(info, nhgap), BorderLayout.WEST); |
| 244 | JPanel pane = new JPanel(new BorderLayout()); |
| 245 | |
| 246 | final JButton colorWheel = new JButton(); |
| 247 | colorWheel.setIcon(getIcon(17, info.getColor(true))); |
| 248 | colorWheel.addActionListener(new ActionListener() { |
| 249 | @Override |
| 250 | public void actionPerformed(ActionEvent e) { |
| 251 | Integer icol = info.getColor(true); |
| 252 | if (icol == null) { |
| 253 | icol = new Color(255, 255, 255, 255).getRGB(); |
| 254 | } |
| 255 | Color initialColor = new Color(icol, true); |
| 256 | Color newColor = JColorChooser.showDialog(ConfigItem.this, |
| 257 | info.getName(), initialColor); |
| 258 | if (newColor != null) { |
| 259 | info.setColor(newColor.getRGB()); |
| 260 | field.setText(info.getString(false)); |
| 261 | colorWheel.setIcon(getIcon(17, info.getColor(true))); |
| 262 | } |
| 263 | } |
| 264 | }); |
| 265 | pane.add(colorWheel, BorderLayout.WEST); |
| 266 | pane.add(field, BorderLayout.CENTER); |
| 267 | this.add(pane, BorderLayout.CENTER); |
| 268 | |
| 269 | setPreferredSize(pane); |
| 270 | } |
| 271 | |
| 272 | private void addBrowseField(final MetaInfo<E> info, int nhgap, |
| 273 | final boolean dir) { |
| 274 | final JTextField field = new JTextField(); |
| 275 | field.setToolTipText(info.getDescription()); |
| 276 | String value = info.getString(false); |
| 277 | reload(value); |
| 278 | field.setText(value); |
| 279 | |
| 280 | info.addReloadedListener(new Runnable() { |
| 281 | @Override |
| 282 | public void run() { |
| 283 | String value = info.getString(false); |
| 284 | reload(value); |
| 285 | field.setText(value); |
| 286 | } |
| 287 | }); |
| 288 | info.addSaveListener(new Runnable() { |
| 289 | @Override |
| 290 | public void run() { |
| 291 | String value = field.getText(); |
| 292 | if (isChanged(value)) { |
| 293 | info.setString(value); |
| 294 | } |
| 295 | } |
| 296 | }); |
| 297 | |
| 298 | JButton browseButton = new JButton("..."); |
| 299 | browseButton.addActionListener(new ActionListener() { |
| 300 | @Override |
| 301 | public void actionPerformed(ActionEvent e) { |
| 302 | JFileChooser chooser = new JFileChooser(); |
| 303 | chooser.setCurrentDirectory(null); |
| 304 | chooser.setFileSelectionMode(dir ? JFileChooser.DIRECTORIES_ONLY |
| 305 | : JFileChooser.FILES_ONLY); |
| 306 | if (chooser.showOpenDialog(ConfigItem.this) == JFileChooser.APPROVE_OPTION) { |
| 307 | File file = chooser.getSelectedFile(); |
| 308 | if (file != null) { |
| 309 | String value = file.getAbsolutePath(); |
| 310 | if (isChanged(value)) { |
| 311 | info.setString(value); |
| 312 | } |
| 313 | field.setText(value); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | }); |
| 318 | |
| 319 | JPanel pane = new JPanel(new BorderLayout()); |
| 320 | this.add(label(info, nhgap), BorderLayout.WEST); |
| 321 | pane.add(browseButton, BorderLayout.WEST); |
| 322 | pane.add(field, BorderLayout.CENTER); |
| 323 | this.add(pane, BorderLayout.CENTER); |
| 324 | |
| 325 | setPreferredSize(pane); |
| 326 | } |
| 327 | |
| 328 | private void addComboboxField(final MetaInfo<E> info, int nhgap, |
| 329 | boolean editable) { |
| 330 | // rawtypes for Java 1.6 (and 1.7 ?) support |
| 331 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 332 | final JComboBox field = new JComboBox(info.getAllowedValues()); |
| 333 | field.setEditable(editable); |
| 334 | String value = info.getString(false); |
| 335 | reload(value); |
| 336 | field.setSelectedItem(value); |
| 337 | |
| 338 | info.addReloadedListener(new Runnable() { |
| 339 | @Override |
| 340 | public void run() { |
| 341 | String value = info.getString(false); |
| 342 | reload(value); |
| 343 | field.setSelectedItem(value); |
| 344 | } |
| 345 | }); |
| 346 | info.addSaveListener(new Runnable() { |
| 347 | @Override |
| 348 | public void run() { |
| 349 | String value = field.getSelectedItem().toString(); |
| 350 | if (isChanged(value)) { |
| 351 | info.setString(value); |
| 352 | } |
| 353 | } |
| 354 | }); |
| 355 | |
| 356 | this.add(label(info, nhgap), BorderLayout.WEST); |
| 357 | this.add(field, BorderLayout.CENTER); |
| 358 | |
| 359 | setPreferredSize(field); |
| 360 | } |
| 361 | |
| 362 | private void addPasswordField(final MetaInfo<E> info, int nhgap) { |
| 363 | final JPasswordField field = new JPasswordField(); |
| 364 | field.setToolTipText(info.getDescription()); |
| 365 | String value = info.getString(false); |
| 366 | reload(value); |
| 367 | field.setText(value); |
| 368 | |
| 369 | info.addReloadedListener(new Runnable() { |
| 370 | @Override |
| 371 | public void run() { |
| 372 | String value = info.getString(false); |
| 373 | reload(value); |
| 374 | field.setText(value); |
| 375 | } |
| 376 | }); |
| 377 | info.addSaveListener(new Runnable() { |
| 378 | @Override |
| 379 | public void run() { |
| 380 | String value = new String(field.getPassword()); |
| 381 | if (isChanged(value)) { |
| 382 | info.setString(value); |
| 383 | } |
| 384 | } |
| 385 | }); |
| 386 | |
| 387 | this.add(label(info, nhgap), BorderLayout.WEST); |
| 388 | this.add(field, BorderLayout.CENTER); |
| 389 | |
| 390 | setPreferredSize(field); |
| 391 | } |
| 392 | |
| 393 | private void addIntField(final MetaInfo<E> info, int nhgap) { |
| 394 | final JSpinner field = new JSpinner(); |
| 395 | field.setToolTipText(info.getDescription()); |
| 396 | int value = info.getInteger(true) == null ? 0 : info.getInteger(true); |
| 397 | reload(value); |
| 398 | field.setValue(value); |
| 399 | |
| 400 | info.addReloadedListener(new Runnable() { |
| 401 | @Override |
| 402 | public void run() { |
| 403 | int value = info.getInteger(true) == null ? 0 : info |
| 404 | .getInteger(true); |
| 405 | reload(value); |
| 406 | field.setValue(value); |
| 407 | } |
| 408 | }); |
| 409 | info.addSaveListener(new Runnable() { |
| 410 | @Override |
| 411 | public void run() { |
| 412 | int value = field.getValue() == null ? 0 : (Integer) field |
| 413 | .getValue(); |
| 414 | if (isChanged(value)) { |
| 415 | info.setInteger(value); |
| 416 | } |
| 417 | } |
| 418 | }); |
| 419 | |
| 420 | this.add(label(info, nhgap), BorderLayout.WEST); |
| 421 | this.add(field, BorderLayout.CENTER); |
| 422 | |
| 423 | setPreferredSize(field); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Create a label which width is constrained in lock steps. |
| 428 | * |
| 429 | * @param info |
| 430 | * the {@link MetaInfo} for which we want to add a label |
| 431 | * @param nhgap |
| 432 | * negative horisontal gap in pixel to use for the label, i.e., |
| 433 | * the step lock sized labels will start smaller by that amount |
| 434 | * (the use case would be to align controls that start at a |
| 435 | * different horisontal position) |
| 436 | * |
| 437 | * @return the label |
| 438 | */ |
| 439 | private JComponent label(final MetaInfo<E> info, int nhgap) { |
| 440 | final JLabel label = new JLabel(info.getName()); |
| 441 | |
| 442 | Dimension ps = label.getPreferredSize(); |
| 443 | if (ps == null) { |
| 444 | ps = label.getSize(); |
| 445 | } |
| 446 | |
| 447 | int w = ps.width; |
| 448 | int step = 150; |
| 449 | for (int i = 2 * step - nhgap; i < 10 * step; i += step) { |
| 450 | if (w < i) { |
| 451 | w = i; |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | final Runnable showInfo = new Runnable() { |
| 457 | @Override |
| 458 | public void run() { |
| 459 | StringBuilder builder = new StringBuilder(); |
| 460 | String text = (info.getDescription().replace("\\n", "\n")) |
| 461 | .trim(); |
| 462 | for (String line : StringUtils.justifyText(text, 80, |
| 463 | Alignment.LEFT)) { |
| 464 | if (builder.length() > 0) { |
| 465 | builder.append("\n"); |
| 466 | } |
| 467 | builder.append(line); |
| 468 | } |
| 469 | text = builder.toString(); |
| 470 | JOptionPane.showMessageDialog(ConfigItem.this, text, |
| 471 | info.getName(), JOptionPane.INFORMATION_MESSAGE); |
| 472 | } |
| 473 | }; |
| 474 | |
| 475 | JLabel help = new JLabel(""); |
| 476 | help.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
| 477 | try { |
| 478 | Image img = new Image(infoImage64); |
| 479 | try { |
| 480 | BufferedImage bImg = ImageUtilsAwt.fromImage(img); |
| 481 | help.setIcon(new ImageIcon(bImg)); |
| 482 | } finally { |
| 483 | img.close(); |
| 484 | } |
| 485 | } catch (IOException e) { |
| 486 | // This is an hard-coded image, should not happen |
| 487 | help.setText("?"); |
| 488 | } |
| 489 | |
| 490 | help.addMouseListener(new MouseAdapter() { |
| 491 | @Override |
| 492 | public void mouseClicked(MouseEvent e) { |
| 493 | showInfo.run(); |
| 494 | } |
| 495 | }); |
| 496 | |
| 497 | JPanel pane2 = new JPanel(new BorderLayout()); |
| 498 | pane2.add(help, BorderLayout.WEST); |
| 499 | pane2.add(new JLabel(" "), BorderLayout.CENTER); |
| 500 | |
| 501 | JPanel pane = new JPanel(new BorderLayout()); |
| 502 | pane.add(label, BorderLayout.WEST); |
| 503 | pane.add(pane2, BorderLayout.CENTER); |
| 504 | |
| 505 | ps.width = w + 30; // 30 for the (?) sign |
| 506 | pane.setSize(ps); |
| 507 | pane.setPreferredSize(ps); |
| 508 | |
| 509 | return pane; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Return an {@link Icon} to use as a colour badge for the colour field |
| 514 | * controls. |
| 515 | * |
| 516 | * @param size |
| 517 | * the size of the badge |
| 518 | * @param color |
| 519 | * the colour of the badge, which can be NULL (will return |
| 520 | * transparent white) |
| 521 | * |
| 522 | * @return the badge |
| 523 | */ |
| 524 | private Icon getIcon(int size, Integer color) { |
| 525 | // Allow null values |
| 526 | if (color == null) { |
| 527 | color = new Color(255, 255, 255, 255).getRGB(); |
| 528 | } |
| 529 | |
| 530 | Color c = new Color(color, true); |
| 531 | int avg = (c.getRed() + c.getGreen() + c.getBlue()) / 3; |
| 532 | Color border = (avg >= 128 ? Color.BLACK : Color.WHITE); |
| 533 | |
| 534 | BufferedImage img = new BufferedImage(size, size, |
| 535 | BufferedImage.TYPE_4BYTE_ABGR); |
| 536 | |
| 537 | Graphics2D g = img.createGraphics(); |
| 538 | try { |
| 539 | g.setColor(c); |
| 540 | g.fillRect(0, 0, img.getWidth(), img.getHeight()); |
| 541 | g.setColor(border); |
| 542 | g.drawRect(0, 0, img.getWidth() - 1, img.getHeight() - 1); |
| 543 | } finally { |
| 544 | g.dispose(); |
| 545 | } |
| 546 | |
| 547 | return new ImageIcon(img); |
| 548 | } |
| 549 | |
| 550 | private void setPreferredSize(JComponent field) { |
| 551 | JTextField a = new JTextField("Test"); |
| 552 | int height = Math.max(a.getMinimumSize().height, |
| 553 | field.getMinimumSize().height); |
| 554 | setPreferredSize(new Dimension(200, height)); |
| 555 | } |
| 556 | } |