1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.conflict.tags;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.beans.PropertyChangeListener;
|
---|
7 | import java.beans.PropertyChangeSupport;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.Comparator;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Set;
|
---|
15 |
|
---|
16 | import javax.swing.table.DefaultTableModel;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.data.osm.TagCollection;
|
---|
19 |
|
---|
20 | public class TagConflictResolverModel extends DefaultTableModel {
|
---|
21 | static public final String NUM_CONFLICTS_PROP = TagConflictResolverModel.class.getName() + ".numConflicts";
|
---|
22 |
|
---|
23 | private TagCollection tags;
|
---|
24 | private List<String> displayedKeys;
|
---|
25 | private Set<String> keysWithConflicts;
|
---|
26 | private HashMap<String, MultiValueResolutionDecision> decisions;
|
---|
27 | private int numConflicts;
|
---|
28 | private PropertyChangeSupport support;
|
---|
29 | private boolean showTagsWithConflictsOnly = false;
|
---|
30 |
|
---|
31 | public TagConflictResolverModel() {
|
---|
32 | numConflicts = 0;
|
---|
33 | support = new PropertyChangeSupport(this);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void addPropertyChangeListener(PropertyChangeListener listener) {
|
---|
37 | support.addPropertyChangeListener(listener);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void removePropertyChangeListener(PropertyChangeListener listener) {
|
---|
41 | support.removePropertyChangeListener(listener);
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected void setNumConflicts(int numConflicts) {
|
---|
45 | int oldValue = this.numConflicts;
|
---|
46 | this.numConflicts = numConflicts;
|
---|
47 | if (oldValue != this.numConflicts) {
|
---|
48 | support.firePropertyChange(NUM_CONFLICTS_PROP, oldValue, this.numConflicts);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected void refreshNumConflicts() {
|
---|
53 | int count = 0;
|
---|
54 | for (MultiValueResolutionDecision d : decisions.values()) {
|
---|
55 | if (!d.isDecided()) {
|
---|
56 | count++;
|
---|
57 | }
|
---|
58 | }
|
---|
59 | setNumConflicts(count);
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected void sort() {
|
---|
63 | Collections.sort(
|
---|
64 | displayedKeys,
|
---|
65 | new Comparator<String>() {
|
---|
66 | public int compare(String key1, String key2) {
|
---|
67 | if (decisions.get(key1).isDecided() && ! decisions.get(key2).isDecided())
|
---|
68 | return 1;
|
---|
69 | else if (!decisions.get(key1).isDecided() && decisions.get(key2).isDecided())
|
---|
70 | return -1;
|
---|
71 | return key1.compareTo(key2);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | );
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * initializes the model from the current tags
|
---|
79 | *
|
---|
80 | */
|
---|
81 | protected void rebuild() {
|
---|
82 | if (tags == null) return;
|
---|
83 | for(String key: tags.getKeys()) {
|
---|
84 | MultiValueResolutionDecision decision = new MultiValueResolutionDecision(tags.getTagsFor(key));
|
---|
85 | if (decisions.get(key) == null) {
|
---|
86 | decisions.put(key,decision);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | displayedKeys.clear();
|
---|
90 | Set<String> keys = tags.getKeys();
|
---|
91 | if (showTagsWithConflictsOnly) {
|
---|
92 | keys.retainAll(keysWithConflicts);
|
---|
93 | for (String key: tags.getKeys()) {
|
---|
94 | if (!decisions.get(key).isDecided() && !keys.contains(key)) {
|
---|
95 | keys.add(key);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | displayedKeys.addAll(keys);
|
---|
100 | refreshNumConflicts();
|
---|
101 | sort();
|
---|
102 | fireTableDataChanged();
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Populates the model with the tags for which conflicts are to be resolved.
|
---|
107 | *
|
---|
108 | * @param tags the tag collection with the tags. Must not be null.
|
---|
109 | * @param keysWithConflicts the set of tag keys with conflicts
|
---|
110 | * @throws IllegalArgumentException thrown if tags is null
|
---|
111 | */
|
---|
112 | public void populate(TagCollection tags, Set<String> keysWithConflicts) {
|
---|
113 | if (tags == null)
|
---|
114 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "tags"));
|
---|
115 | this.tags = tags;
|
---|
116 | displayedKeys = new ArrayList<String>();
|
---|
117 | this.keysWithConflicts = keysWithConflicts == null ? new HashSet<String>() : keysWithConflicts;
|
---|
118 | decisions = new HashMap<String, MultiValueResolutionDecision>();
|
---|
119 | rebuild();
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | public int getRowCount() {
|
---|
124 | if (displayedKeys == null) return 0;
|
---|
125 | return displayedKeys.size();
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public Object getValueAt(int row, int column) {
|
---|
130 | return decisions.get(displayedKeys.get(row));
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | public boolean isCellEditable(int row, int column) {
|
---|
135 | return column == 2;
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | public void setValueAt(Object value, int row, int column) {
|
---|
140 | MultiValueResolutionDecision decision = decisions.get(displayedKeys.get(row));
|
---|
141 | if (value instanceof String) {
|
---|
142 | decision.keepOne((String)value);
|
---|
143 | } else if (value instanceof MultiValueDecisionType) {
|
---|
144 | MultiValueDecisionType type = (MultiValueDecisionType)value;
|
---|
145 | switch(type) {
|
---|
146 | case KEEP_NONE:
|
---|
147 | decision.keepNone();
|
---|
148 | break;
|
---|
149 | case KEEP_ALL:
|
---|
150 | decision.keepAll();
|
---|
151 | break;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | fireTableDataChanged();
|
---|
155 | refreshNumConflicts();
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Replies true if each {@see MultiValueResolutionDecision} is decided.
|
---|
160 | *
|
---|
161 | * @return true if each {@see MultiValueResolutionDecision} is decided; false
|
---|
162 | * otherwise
|
---|
163 | */
|
---|
164 | public boolean isResolvedCompletely() {
|
---|
165 | return numConflicts == 0;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public int getNumConflicts() {
|
---|
169 | return numConflicts;
|
---|
170 | }
|
---|
171 |
|
---|
172 | public int getNumDecisions() {
|
---|
173 | return getRowCount();
|
---|
174 | }
|
---|
175 |
|
---|
176 | public TagCollection getResolution() {
|
---|
177 | TagCollection tc = new TagCollection();
|
---|
178 | for (String key: displayedKeys) {
|
---|
179 | tc.add(decisions.get(key).getResolution());
|
---|
180 | }
|
---|
181 | return tc;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public MultiValueResolutionDecision getDecision(int row) {
|
---|
185 | return decisions.get(displayedKeys.get(row));
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Sets whether all tags or only tags with conflicts are displayed
|
---|
190 | *
|
---|
191 | * @param showTagsWithConflictsOnly if true, only tags with conflicts are displayed
|
---|
192 | */
|
---|
193 | public void setShowTagsWithConflictsOnly(boolean showTagsWithConflictsOnly) {
|
---|
194 | this.showTagsWithConflictsOnly = showTagsWithConflictsOnly;
|
---|
195 | rebuild();
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Prepare the default decisions for the current model
|
---|
200 | *
|
---|
201 | */
|
---|
202 | public void prepareDefaultTagDecisions() {
|
---|
203 | for (MultiValueResolutionDecision decision: decisions.values()) {
|
---|
204 | List<String> values = decision.getValues();
|
---|
205 | values.remove("");
|
---|
206 | if (values.size() == 1) {
|
---|
207 | decision.keepOne(values.get(0));
|
---|
208 | } else {
|
---|
209 | decision.keepAll();
|
---|
210 | }
|
---|
211 | }
|
---|
212 | rebuild();
|
---|
213 | }
|
---|
214 |
|
---|
215 | }
|
---|