+ JComponent field = createField(-1);
+ add(field, BorderLayout.CENTER);
+ }
+ }
+
+ private void addItem(final int item) {
+ JPanel minusPanel = new JPanel(new BorderLayout());
+ itemFields.put(item, minusPanel);
+
+ JComponent field = createField(item);
+
+ final JButton remove = new JButton();
+ setImage(remove, img64remove, "-");
+
+ remove.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ removeItem(item);
+ }
+ });
+
+ minusPanel.add(field, BorderLayout.CENTER);
+ minusPanel.add(remove, BorderLayout.EAST);
+
+ main.add(minusPanel);
+ main.revalidate();
+ main.repaint();
+ }
+
+ private void removeItem(int item) {
+ int last = itemFields.size() - 1;
+
+ for (int i = item; i <= last; i++) {
+ Object value = null;
+ if (i < last) {
+ value = getFromField(i + 1);
+ }
+ setToField(value, i);
+ setToInfo(value, i);
+ setDirtyItem(i);
+ }
+
+ main.remove(itemFields.remove(last));
+ main.revalidate();
+ main.repaint();
+ }
+
+ /**
+ * Prepare a new {@link ConfigItem} instance, linked to the given
+ * {@link MetaInfo}.
+ *
+ * @param info
+ * the info
+ * @param autoDirtyHandling
+ * TRUE to automatically manage the setDirty/Save operations,
+ * FALSE if you want to do it yourself via
+ * {@link ConfigItem#setDirtyItem(int)}
+ */
+ protected ConfigItem(MetaInfo<E> info, boolean autoDirtyHandling) {
+ this.info = info;
+ if (!autoDirtyHandling) {
+ dirtyBits = new ArrayList<Integer>();
+ }
+ }
+
+ /**
+ * Create an empty graphical component to be used later by
+ * {@link ConfigItem#createField(int)}.
+ * <p>
+ * Note that {@link ConfigItem#reload(int)} will be called after it was
+ * created by {@link ConfigItem#createField(int)}.
+ *
+ * @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)
+ *
+ * @return the graphical component
+ */
+ abstract protected JComponent createEmptyField(int item);
+
+ /**
+ * Get the information from the {@link MetaInfo} in the subclass preferred
+ * format.
+ *
+ * @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)
+ *
+ * @return the information in the subclass preferred format
+ */
+ abstract protected Object getFromInfo(int item);
+
+ /**
+ * Set the value to the {@link MetaInfo}.
+ *
+ * @param value
+ * the value in the subclass preferred format
+ * @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)
+ */
+ abstract protected void setToInfo(Object value, int item);
+
+ /**
+ * The value present in the given item's related field in the subclass
+ * preferred format.
+ *
+ * @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)
+ *
+ * @return the value present in the given item's related field in the
+ * subclass preferred format
+ */
+ abstract protected Object getFromField(int item);
+
+ /**
+ * Set the value (in the subclass preferred format) into the field.
+ *
+ * @param value
+ * the value in the subclass preferred format
+ * @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)
+ */
+ abstract protected void setToField(Object value, int item);
+
+ /**
+ * Create a new field for the given graphical component at the given index
+ * (note that the component is usually created by
+ * {@link ConfigItem#createEmptyField(int)}).
+ *
+ * @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 field
+ * the graphical component
+ */
+ private void setField(int item, JComponent field) {
+ if (item < 0) {
+ this.field = field;
+ return;
+ }
+
+ for (int i = fields.size(); i <= item; i++) {
+ fields.add(null);
+ }
+
+ fields.set(item, field);
+ }
+
+ /**
+ * Retrieve the associated graphical component that was created with
+ * {@link ConfigItem#createEmptyField(int)}.
+ *
+ * @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)
+ *
+ * @return the graphical component
+ */
+ protected JComponent getField(int item) {
+ if (item < 0) {
+ return field;
+ }
+
+ if (item < fields.size()) {
+ return fields.get(item);
+ }
+
+ return null;
+ }
+
+ /**
+ * The original value (before any changes to the {@link MetaInfo}) for this
+ * item.
+ *
+ * @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)
+ *
+ * @return the original value
+ */
+ private Object getOrig(int item) {
+ if (item < 0) {
+ return orig;
+ }
+
+ if (item < origs.size()) {
+ return origs.get(item);
+ }
+
+ return null;
+ }
+
+ /**
+ * The original value (before any changes to the {@link MetaInfo}) for this
+ * item.
+ *
+ * @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 value
+ * the new original value
+ */
+ private void setOrig(Object value, int item) {
+ if (item < 0) {
+ orig = value;
+ } else {
+ while (item >= origs.size()) {
+ origs.add(null);
+ }
+
+ origs.set(item, value);
+ }
+ }
+
+ /**
+ * Manually specify that the given item is "dirty" and thus should be saved
+ * when asked.
+ * <p>
+ * Has no effect if the class is using automatic dirty handling (see
+ * {@link ConfigItem#ConfigItem(MetaInfo, 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 setDirtyItem(int item) {
+ if (dirtyBits != null) {
+ dirtyBits.add(item);
+ }
+ }
+
+ /**
+ * Check if the value changed since the last load/save into the linked
+ * {@link MetaInfo}.
+ * <p>
+ * Note that we consider NULL and an Empty {@link String} to be equals.
+ *
+ * @param value
+ * the value to test
+ * @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)
+ *
+ * @return TRUE if it has
+ */
+ protected boolean hasValueChanged(Object value, int item) {
+ // We consider "" and NULL to be equals
+ Object orig = getOrig(item);
+ if (orig == null) {
+ orig = "";
+ }
+ return !orig.equals(value == null ? "" : value);
+ }
+
+ /**
+ * Reload the values to what they currently are in the {@link MetaInfo}.
+ */
+ private void reload() {
+ if (info.isArray()) {
+ while (!itemFields.isEmpty()) {
+ main.remove(itemFields.remove(itemFields.size() - 1));
+ }
+ main.revalidate();
+ main.repaint();
+ for (int item = 0; item < info.getListSize(false); item++) {
+ reload(item);
+ }
+ } else {
+ reload(-1);
+ }
+ }
+
+ /**
+ * Reload the values to what they currently are in the {@link MetaInfo}.
+ *
+ * @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)
+ */
+ private void reload(int item) {
+ if (item >= 0 && !itemFields.containsKey(item)) {
+ addItem(item);
+ }
+
+ Object value = getFromInfo(item);
+ setToField(value, item);
+ setOrig(value == null ? "" : value, item);
+ }
+
+ /**
+ * 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)}.
+ */
+ private void save() {
+ if (info.isArray()) {
+ boolean dirty = itemFields.size() != info.getListSize(false);
+ for (int item = 0; item < itemFields.size(); item++) {
+ if (getDirtyBit(item)) {
+ dirty = true;