1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.advanced;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.spi.preferences.Setting;
|
---|
5 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Class to store single preference line for the table.
|
---|
9 | * @since 6021
|
---|
10 | */
|
---|
11 | public class PrefEntry implements Comparable<PrefEntry> {
|
---|
12 | private final String key;
|
---|
13 | private Setting<?> value;
|
---|
14 | private final Setting<?> defaultValue;
|
---|
15 | private boolean isDefault;
|
---|
16 | private boolean changed;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Constructs a new {@code PrefEntry}.
|
---|
20 | * @param key The preference key
|
---|
21 | * @param value The preference value
|
---|
22 | * @param defaultValue The preference default value
|
---|
23 | * @param isDefault determines if the current value is the default value
|
---|
24 | */
|
---|
25 | public PrefEntry(String key, Setting<?> value, Setting<?> defaultValue, boolean isDefault) {
|
---|
26 | CheckParameterUtil.ensureParameterNotNull(key);
|
---|
27 | CheckParameterUtil.ensureParameterNotNull(value);
|
---|
28 | CheckParameterUtil.ensureParameterNotNull(defaultValue);
|
---|
29 | this.key = key;
|
---|
30 | this.value = value;
|
---|
31 | this.defaultValue = defaultValue;
|
---|
32 | this.isDefault = isDefault;
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Returns the preference key.
|
---|
37 | * @return the preference key
|
---|
38 | */
|
---|
39 | public String getKey() {
|
---|
40 | return key;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Returns the preference value.
|
---|
45 | * @return the preference value
|
---|
46 | */
|
---|
47 | public Setting<?> getValue() {
|
---|
48 | return value;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Returns the preference default value.
|
---|
53 | * @return the preference default value
|
---|
54 | */
|
---|
55 | public Setting<?> getDefaultValue() {
|
---|
56 | return defaultValue;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Sets the preference value.
|
---|
61 | * @param value the preference value
|
---|
62 | */
|
---|
63 | public void setValue(Setting<?> value) {
|
---|
64 | this.value = value;
|
---|
65 | changed = true;
|
---|
66 | isDefault = false;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Determines if the current value is the default value.
|
---|
71 | * @return {@code true} if the current value is the default value, {@code false} otherwise
|
---|
72 | */
|
---|
73 | public boolean isDefault() {
|
---|
74 | return isDefault;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Determines if this preference entry has been modified.
|
---|
79 | * @return {@code true} if this preference entry has been modified, {@code false} otherwise
|
---|
80 | */
|
---|
81 | public boolean isChanged() {
|
---|
82 | return changed;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Marks this preference entry as modified.
|
---|
87 | */
|
---|
88 | public void markAsChanged() {
|
---|
89 | changed = true;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Resets this preference entry to default state.
|
---|
94 | */
|
---|
95 | public void reset() {
|
---|
96 | value = defaultValue;
|
---|
97 | changed = true;
|
---|
98 | isDefault = true;
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public int compareTo(PrefEntry other) {
|
---|
103 | return key.compareTo(other.key);
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public int hashCode() {
|
---|
108 | return 31 + ((key == null) ? 0 : key.hashCode());
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public boolean equals(Object obj) {
|
---|
113 | if (this == obj)
|
---|
114 | return true;
|
---|
115 | if (obj == null || getClass() != obj.getClass())
|
---|
116 | return false;
|
---|
117 | PrefEntry other = (PrefEntry) obj;
|
---|
118 | if (key == null) {
|
---|
119 | if (other.key != null)
|
---|
120 | return false;
|
---|
121 | } else if (!key.equals(other.key))
|
---|
122 | return false;
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | public String toString() {
|
---|
128 | return value.toString();
|
---|
129 | }
|
---|
130 | }
|
---|