- /**
- * 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);
- }
-
- // if (item >= 0) {
- // Object value = getFromField(item);
- // if (value == null) {
- // value = "";
- // }
- //
- // boolean empty = value.equals("");
- //
- // if (!empty && item >= info.getListSize(false)) {
- // // item was deleted, remove it
- // removeItem(item);
- // return;
- // }
- //
- // // in case of reload after remove
- // if (!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 = fields.size() != info.getListSize(false);
- for (int item = 0; item < fields.size(); item++) {
- if (getDirtyBit(item)) {
- dirty = true;
- }
- }
-
- if (dirty) {
- info.setString(null, -1);
- for (int item = 0; item < fields.size(); item++) {
- Object value = null;
- if (getField(item) != null) {
- value = getFromField(item);
- if ("".equals(value)) {
- value = null;
- }
- }
-
- info.setDirty();
- setToInfo(value, item);
- setOrig(value, item);
- }
- }
- } else {
- if (getDirtyBit(-1)) {
- Object value = getFromField(-1);
-
- info.setDirty();
- setToInfo(value, -1);
- setOrig(value, -1);
- }
- }
- }
-
- /**
- * Check if the item is dirty, and clear the dirty bit if set.
- *
- * @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 was dirty, FALSE if not
- */
- private boolean getDirtyBit(int item) {
- if (dirtyBits != null) {
- return dirtyBits.remove((Integer) item);
- }
-
- Object value = null;
- if (getField(item) != null) {
- value = getFromField(item);
- }
-
- return hasValueChanged(value, item);
- }
-
- /**
- * Create a new field for the given 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 newly created field
- */
- protected JComponent createField(final int item) {
- JComponent field = createEmptyField(item);
- setField(item, field);
- reload(item);
-
- info.addReloadedListener(new Runnable() {
- @Override
- public void run() {
- reload();
- }
- });
- info.addSaveListener(new Runnable() {
- @Override
- public void run() {
- save();
- }
- });
-
- int height = Math
- .max(getMinimumHeight(), field.getMinimumSize().height);
- field.setPreferredSize(new Dimension(200, height));
-
- return field;
- }
-