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.List;
|
---|
13 |
|
---|
14 | import javax.swing.table.DefaultTableModel;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.osm.TagCollection;
|
---|
17 |
|
---|
18 | public 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 | }
|
---|