+ // We consider "" and NULL to be equals
+ orig = (value == null ? "" : value);
+ }
+
+ /**
+ * If the item has been modified, set the {@link MetaInfo} to dirty then
+ * modify it to, reflect the changes so it can be saved later.
+ * <p>
+ * This method does <b>not</b> call {@link MetaInfo#save(boolean)}.
+ *
+ * @param item
+ * the item number to get for an array of values, or -1 to get
+ * the whole value (has no effect if {@link MetaInfo#isArray()}
+ * is FALSE)
+ */
+ protected void save(int item) {
+ Object value = getFromField(item);
+
+ boolean dirty = false;
+ if (dirtyBits != null) {
+ dirty = dirtyBits.remove((Integer) item);
+ } else {
+ // We consider "" and NULL to be equals
+ dirty = hasValueChanged(value);
+ }
+
+ if (dirty) {
+ info.setDirty();
+ setToInfo(value, item);
+ orig = (value == null ? "" : value);
+ }
+ }
+
+ /**
+ *
+ * @param item
+ * the item number to get for an array of values, or -1 to get
+ * the whole value (has no effect if {@link MetaInfo#isArray()}
+ * is FALSE)
+ * @param addTo
+ * @param nhgap
+ *
+ * @return
+ */
+ protected JComponent createComponent(final int item) {
+ setField(item, createField(item));
+ reload(item);
+
+ info.addReloadedListener(new Runnable() {
+ @Override
+ public void run() {
+ reload(item);
+ }
+ });
+ info.addSaveListener(new Runnable() {
+ @Override
+ public void run() {
+ save(item);
+ }
+ });
+
+ JComponent field = getField(item);
+ setPreferredSize(field);
+
+ return field;
+ }
+
+ /**
+ * Create a label which width is constrained in lock steps.
+ *
+ * @param nhgap
+ * negative horisontal gap in pixel to use for the label, i.e.,
+ * the step lock sized labels will start smaller by that amount
+ * (the use case would be to align controls that start at a
+ * different horisontal position)
+ *
+ * @return the label
+ */
+ protected JComponent label(int nhgap) {
+ final JLabel label = new JLabel(info.getName());
+
+ Dimension ps = label.getPreferredSize();
+ if (ps == null) {
+ ps = label.getSize();
+ }
+
+ ps.height = Math.max(ps.height, getMinimumHeight());
+
+ int w = ps.width;
+ int step = 150;
+ for (int i = 2 * step - nhgap; i < 10 * step; i += step) {
+ if (w < i) {
+ w = i;
+ break;
+ }
+ }
+
+ final Runnable showInfo = new Runnable() {
+ @Override
+ public void run() {
+ StringBuilder builder = new StringBuilder();
+ String text = (info.getDescription().replace("\\n", "\n"))
+ .trim();
+ for (String line : StringUtils.justifyText(text, 80,
+ Alignment.LEFT)) {
+ if (builder.length() > 0) {
+ builder.append("\n");
+ }
+ builder.append(line);
+ }
+ text = builder.toString();
+ JOptionPane.showMessageDialog(ConfigItem.this, text,
+ info.getName(), JOptionPane.INFORMATION_MESSAGE);
+ }
+ };
+
+ JLabel help = new JLabel("");
+ help.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
+ setImage(help, img64Info, "?");
+
+ help.addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ showInfo.run();
+ }
+ });
+
+ JPanel pane2 = new JPanel(new BorderLayout());
+ pane2.add(help, BorderLayout.WEST);
+ pane2.add(new JLabel(" "), BorderLayout.CENTER);
+
+ JPanel contentPane = new JPanel(new BorderLayout());
+ contentPane.add(label, BorderLayout.WEST);
+ contentPane.add(pane2, BorderLayout.CENTER);
+
+ ps.width = w + 30; // 30 for the (?) sign
+ contentPane.setSize(ps);
+ contentPane.setPreferredSize(ps);
+
+ JPanel pane = new JPanel(new BorderLayout());
+ pane.add(contentPane, BorderLayout.NORTH);
+
+ return pane;
+ }
+
+ protected void setPreferredSize(JComponent field) {
+ int height = Math
+ .max(getMinimumHeight(), field.getMinimumSize().height);
+ setPreferredSize(new Dimension(200, height));
+ }
+
+ static private int getMinimumHeight() {
+ if (minimumHeight < 0) {
+ minimumHeight = new JTextField("Test").getMinimumSize().height;
+ }
+
+ return minimumHeight;
+ }
+
+ /**
+ * Set an image to the given {@link JButton}, with a fallback text if it
+ * fails.
+ *
+ * @param button
+ * the button to set
+ * @param image64
+ * the image in BASE64 (should be PNG or similar)
+ * @param fallbackText
+ * text to use in case the image cannot be created
+ */
+ static protected void setImage(JLabel button, String image64,
+ String fallbackText) {
+ try {
+ Image img = new Image(image64);
+ try {
+ BufferedImage bImg = ImageUtilsAwt.fromImage(img);
+ button.setIcon(new ImageIcon(bImg));
+ } finally {
+ img.close();
+ }
+ } catch (IOException e) {
+ // This is an hard-coded image, should not happen
+ button.setText(fallbackText);
+ }
+ }
+
+ /**
+ * Set an image to the given {@link JButton}, with a fallback text if it
+ * fails.
+ *
+ * @param button
+ * the button to set
+ * @param image64
+ * the image in BASE64 (should be PNG or similar)
+ * @param fallbackText
+ * text to use in case the image cannot be created
+ */
+ static protected void setImage(JButton button, String image64,
+ String fallbackText) {
+ try {
+ Image img = new Image(image64);
+ try {
+ BufferedImage bImg = ImageUtilsAwt.fromImage(img);
+ button.setIcon(new ImageIcon(bImg));
+ } finally {
+ img.close();
+ }
+ } catch (IOException e) {
+ // This is an hard-coded image, should not happen
+ button.setText(fallbackText);
+ }