source: josm/trunk/src/org/openstreetmap/josm/gui/conflict/tags/TagConflictResolverModel.java@ 2070

Last change on this file since 2070 was 2070, checked in by Gubaer, 15 years ago

new: rewrite of CombineWay action
new: conflict resolution dialog for CombineWay, including conflicts for different relation memberships
cleanup: cleanup in OsmReader, reduces memory footprint and reduces parsing time
cleanup: made most of the public fields in OsmPrimitive @deprecated, added accessors and changed the code
cleanup: replaced usages of @deprecated constructors for ExtendedDialog
fixed #3208: Combine ways brokes relation order

WARNING: this changeset touches a lot of code all over the code base. "latest" might become slightly unstable in the next days. Also experience incompatibility issues with plugins in the next few days.

File size: 5.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.conflict.tags;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.beans.PropertyChangeListener;
7import java.beans.PropertyChangeSupport;
8import java.util.ArrayList;
9import java.util.Collections;
10import java.util.Comparator;
11import java.util.HashMap;
12import java.util.List;
13
14import javax.swing.table.DefaultTableModel;
15
16import org.openstreetmap.josm.data.osm.TagCollection;
17
18public class TagConflictResolverModel extends DefaultTableModel {
19 static public final String NUM_CONFLICTS_PROP = TagConflictResolverModel.class.getName() + ".numConflicts";
20
21 private TagCollection tags;
22 private List<String> keys;
23 private HashMap<String, MultiValueResolutionDecision> decisions;
24 private int numConflicts;
25 private PropertyChangeSupport support;
26
27 public TagConflictResolverModel() {
28 numConflicts = 0;
29 support = new PropertyChangeSupport(this);
30 }
31
32 public void addPropertyChangeListener(PropertyChangeListener listener) {
33 support.addPropertyChangeListener(listener);
34 }
35
36 public void removePropertyChangeListener(PropertyChangeListener listener) {
37 support.removePropertyChangeListener(listener);
38 }
39
40
41 protected void setNumConflicts(int numConflicts) {
42 int oldValue = this.numConflicts;
43 this.numConflicts = numConflicts;
44 if (oldValue != this.numConflicts) {
45 support.firePropertyChange(NUM_CONFLICTS_PROP, oldValue, this.numConflicts);
46 }
47 }
48
49 protected void refreshNumConflicts() {
50 int count = 0;
51 for (MultiValueResolutionDecision d : decisions.values()) {
52 if (!d.isDecided()) {
53 count++;
54 }
55 }
56 setNumConflicts(count);
57 }
58
59 protected void sort() {
60 Collections.sort(
61 keys,
62 new Comparator<String>() {
63 public int compare(String o1, String o2) {
64 if (decisions.get(o1).isDecided() && ! decisions.get(o2).isDecided())
65 return 1;
66 else if (!decisions.get(o1).isDecided() && decisions.get(o2).isDecided())
67 return -1;
68 return o1.compareTo(o2);
69 }
70 }
71 );
72 }
73
74 protected void init() {
75 keys.clear();
76 keys.addAll(tags.getKeys());
77 for(String key: tags.getKeys()) {
78 MultiValueResolutionDecision decision = new MultiValueResolutionDecision(tags.getTagsFor(key));
79 decisions.put(key,decision);
80 }
81 refreshNumConflicts();
82 }
83
84 public void populate(TagCollection tags) {
85 if (tags == null)
86 throw new IllegalArgumentException(tr("parameter ''{0}'' must not be null", "tags"));
87 this.tags = tags;
88 keys = new ArrayList<String>();
89 decisions = new HashMap<String, MultiValueResolutionDecision>();
90 init();
91 sort();
92 fireTableDataChanged();
93 }
94
95
96 @Override
97 public int getRowCount() {
98 if (keys == null) return 0;
99 return keys.size();
100 }
101
102 @Override
103 public Object getValueAt(int row, int column) {
104 return decisions.get(keys.get(row));
105 }
106
107 @Override
108 public boolean isCellEditable(int row, int column) {
109 return column == 2;
110 }
111
112 @Override
113 public void setValueAt(Object value, int row, int column) {
114 MultiValueResolutionDecision decision = decisions.get(keys.get(row));
115 if (value instanceof String) {
116 decision.keepOne((String)value);
117 } else if (value instanceof MultiValueDecisionType) {
118 MultiValueDecisionType type = (MultiValueDecisionType)value;
119 switch(type) {
120 case KEEP_NONE:
121 decision.keepNone();
122 break;
123 case KEEP_ALL:
124 decision.keepAll();
125 break;
126 }
127 }
128 fireTableDataChanged();
129 refreshNumConflicts();
130 }
131
132 /**
133 * Replies true if each {@see MultiValueResolutionDecision} is decided.
134 *
135 * @return true if each {@see MultiValueResolutionDecision} is decided; false
136 * otherwise
137 */
138 public boolean isResolvedCompletely() {
139 return numConflicts == 0;
140 }
141
142 public int getNumConflicts() {
143 return numConflicts;
144 }
145
146 public int getNumDecisions() {
147 return getRowCount();
148 }
149
150 public TagCollection getResolution() {
151 TagCollection tc = new TagCollection();
152 for (String key: keys) {
153 tc.add(decisions.get(key).getResolution());
154 }
155 return tc;
156 }
157
158 public MultiValueResolutionDecision getDecision(int row) {
159 return decisions.get(keys.get(row));
160 }
161
162 public void refresh() {
163 fireTableDataChanged();
164 refreshNumConflicts();
165 }
166}
Note: See TracBrowser for help on using the repository browser.