| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.dialogs.changeset;
|
|---|
| 3 |
|
|---|
| 4 | import java.util.ArrayList;
|
|---|
| 5 | import java.util.Collections;
|
|---|
| 6 | import java.util.Comparator;
|
|---|
| 7 | import java.util.HashSet;
|
|---|
| 8 | import java.util.Iterator;
|
|---|
| 9 | import java.util.Set;
|
|---|
| 10 |
|
|---|
| 11 | import javax.swing.DefaultListSelectionModel;
|
|---|
| 12 | import javax.swing.table.AbstractTableModel;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.ChangesetDataSet;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetDataSetEntry;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType;
|
|---|
| 17 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * This is the table model for the content of a changeset.
|
|---|
| 21 | *
|
|---|
| 22 | */
|
|---|
| 23 | public class ChangesetContentTableModel extends AbstractTableModel {
|
|---|
| 24 |
|
|---|
| 25 | private final ArrayList<ChangesetContentEntry> data = new ArrayList<ChangesetContentEntry>();
|
|---|
| 26 | private DefaultListSelectionModel selectionModel;
|
|---|
| 27 |
|
|---|
| 28 | public ChangesetContentTableModel(DefaultListSelectionModel selectionModel) {
|
|---|
| 29 | this.selectionModel = selectionModel;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Replies true if there is at least one selected primitive in the table model
|
|---|
| 34 | *
|
|---|
| 35 | * @return true if there is at least one selected primitive in the table model
|
|---|
| 36 | */
|
|---|
| 37 | public boolean hasSelectedPrimitives() {
|
|---|
| 38 | return selectionModel.getMinSelectionIndex() >= 0;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | public void setSelectedByIdx(int row) {
|
|---|
| 42 | selectionModel.setSelectionInterval(row, row);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * Replies the selection model
|
|---|
| 47 | * @return the selection model
|
|---|
| 48 | */
|
|---|
| 49 | public DefaultListSelectionModel getSelectionModel() {
|
|---|
| 50 | return selectionModel;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | public Set<HistoryOsmPrimitive> getSelectedPrimitives() {
|
|---|
| 54 | Set<HistoryOsmPrimitive> ret = new HashSet<HistoryOsmPrimitive>();
|
|---|
| 55 | for (int i=0;i < data.size();i++) {
|
|---|
| 56 | if (selectionModel.isSelectedIndex(i)) {
|
|---|
| 57 | ret.add(data.get(i).getPrimitive());
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | return ret;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Populates the model with the content of a model. If ds is null, the
|
|---|
| 65 | * table is cleared.
|
|---|
| 66 | *
|
|---|
| 67 | * @param ds the changeset content.
|
|---|
| 68 | */
|
|---|
| 69 | public void populate(ChangesetDataSet ds) {
|
|---|
| 70 | this.data.clear();
|
|---|
| 71 | if (ds == null) {
|
|---|
| 72 | fireTableDataChanged();
|
|---|
| 73 | return;
|
|---|
| 74 | }
|
|---|
| 75 | for (Iterator<ChangesetDataSetEntry> it = ds.iterator(); it.hasNext();) {
|
|---|
| 76 | data.add(new ChangesetContentEntry(it.next()));
|
|---|
| 77 | }
|
|---|
| 78 | sort();
|
|---|
| 79 | fireTableDataChanged();
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | protected void sort() {
|
|---|
| 83 | Collections.sort(
|
|---|
| 84 | data,
|
|---|
| 85 | new Comparator<ChangesetDataSetEntry>() {
|
|---|
| 86 | public int compare(ChangesetDataSetEntry c1, ChangesetDataSetEntry c2) {
|
|---|
| 87 | if (c1.getModificationType().equals(c2.getModificationType())) {
|
|---|
| 88 | long id1 = c1.getPrimitive().getId();
|
|---|
| 89 | long id2 = c2.getPrimitive().getId();
|
|---|
| 90 |
|
|---|
| 91 | if (id1 == id2)
|
|---|
| 92 | return 0;
|
|---|
| 93 | else if (id1 < id2)
|
|---|
| 94 | return -1;
|
|---|
| 95 | return 1;
|
|---|
| 96 | }
|
|---|
| 97 | switch(c1.getModificationType()) {
|
|---|
| 98 | case CREATED: return -1;
|
|---|
| 99 | case UPDATED:
|
|---|
| 100 | switch(c2.getModificationType()) {
|
|---|
| 101 | case CREATED: return 1;
|
|---|
| 102 | default: return -1;
|
|---|
| 103 | }
|
|---|
| 104 | case DELETED:
|
|---|
| 105 | return 1;
|
|---|
| 106 | }
|
|---|
| 107 | // should not happen
|
|---|
| 108 | return 0;
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | );
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /* -------------------------------------------------------------- */
|
|---|
| 115 | /* interface TableModel */
|
|---|
| 116 | /* -------------------------------------------------------------- */
|
|---|
| 117 | public int getColumnCount() {
|
|---|
| 118 | return 3;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | public int getRowCount() {
|
|---|
| 122 | return data.size();
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | public Object getValueAt(int row, int col) {
|
|---|
| 126 | switch(col) {
|
|---|
| 127 | case 0: return data.get(row).getModificationType();
|
|---|
| 128 | default: return data.get(row).getPrimitive();
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /**
|
|---|
| 133 | * The type used internally to keep information about {@see HistoryOsmPrimitive}
|
|---|
| 134 | * with their {@see ChangesetModificationType}.
|
|---|
| 135 | *
|
|---|
| 136 | */
|
|---|
| 137 | static private class ChangesetContentEntry implements ChangesetDataSetEntry{
|
|---|
| 138 | private ChangesetModificationType modificationType;
|
|---|
| 139 | private HistoryOsmPrimitive primitive;
|
|---|
| 140 |
|
|---|
| 141 | public ChangesetContentEntry(ChangesetModificationType modificationType, HistoryOsmPrimitive primitive) {
|
|---|
| 142 | this.modificationType = modificationType;
|
|---|
| 143 | this.primitive = primitive;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | public ChangesetContentEntry(ChangesetDataSetEntry entry) {
|
|---|
| 147 | this.modificationType = entry.getModificationType();
|
|---|
| 148 | this.primitive = entry.getPrimitive();
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | public ChangesetModificationType getModificationType() {
|
|---|
| 152 | return modificationType;
|
|---|
| 153 | }
|
|---|
| 154 | public void setModificationType(ChangesetModificationType modificationType) {
|
|---|
| 155 | this.modificationType = modificationType;
|
|---|
| 156 | }
|
|---|
| 157 | public HistoryOsmPrimitive getPrimitive() {
|
|---|
| 158 | return primitive;
|
|---|
| 159 | }
|
|---|
| 160 | public void setPrimitive(HistoryOsmPrimitive primitive) {
|
|---|
| 161 | this.primitive = primitive;
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|