Changeset 24174 in osm for applications
- Timestamp:
- 2010-11-10T00:45:20+01:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/AddressEditTableModel.java
r24110 r24174 14 14 package org.openstreetmap.josm.plugins.fixAddresses.gui; 15 15 16 import java.awt.event.MouseAdapter; 17 import java.awt.event.MouseEvent; 18 import java.util.Comparator; 19 20 import javax.swing.JTable; 21 import javax.swing.event.TableModelEvent; 16 22 import javax.swing.table.DefaultTableModel; 23 import javax.swing.table.TableColumn; 24 import javax.swing.table.TableColumnModel; 17 25 18 26 import org.openstreetmap.josm.plugins.fixAddresses.AddressEditContainer; … … 20 28 import org.openstreetmap.josm.plugins.fixAddresses.IOSMEntity; 21 29 22 public abstract class AddressEditTableModel extends DefaultTableModel implements IAddressEditContainerListener{ 23 24 /** 25 * 26 */ 27 private static final long serialVersionUID = 424009321818130586L; 30 @SuppressWarnings("serial") 31 public abstract class AddressEditTableModel extends DefaultTableModel implements 32 IAddressEditContainerListener { 33 28 34 protected AddressEditContainer addressContainer; 35 protected int sortCol = 0; 36 protected boolean isSortAsc = true; 29 37 30 38 public AddressEditTableModel(AddressEditContainer addressContainer) { … … 46 54 } // else we don't do anything 47 55 } 48 56 49 57 /** 50 * Gets the node entity for the given row or null; if row contains no entity. 51 * @param row The row to get the entity object for. 58 * Gets the node entity for the given row or null; if row contains no 59 * entity. 60 * 61 * @param row 62 * The row to get the entity object for. 52 63 * @return 53 64 */ 54 65 public abstract IOSMEntity getEntityOfRow(int row); 55 66 56 67 /** 57 * Gets the row for the given node entity or -1; if the model does not contain the entity. 58 * @param entity The entity to get the row for. 68 * Gets the row for the given node entity or -1; if the model does not 69 * contain the entity. 70 * 71 * @param entity 72 * The entity to get the row for. 59 73 * @return 60 74 */ 61 75 public abstract int getRowOfEntity(IOSMEntity entity); 76 77 /** 78 * Sorts the model data by the given column. 79 * 80 * @param column 81 * the column 82 * @param ascending 83 * the ascending 84 */ 85 protected abstract void sortByColumn(int column, boolean ascending); 86 87 88 /** 89 * The listener interface for receiving column events. 90 * The class that is interested in processing a column 91 * event implements this interface, and the object created 92 * with that class is registered with a component using the 93 * component's <code>addColumnListener<code> method. When 94 * the column event occurs, that object's appropriate 95 * method is invoked. 96 * 97 * @see ColumnEvent 98 */ 99 class ColumnListener extends MouseAdapter { 100 protected JTable table; 101 102 /** 103 * Instantiates a new column listener. 104 * 105 * @param t the t 106 */ 107 public ColumnListener(JTable t) { 108 table = t; 109 } 110 111 /* (non-Javadoc) 112 * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent) 113 */ 114 public void mouseClicked(MouseEvent e) { 115 TableColumnModel colModel = table.getColumnModel(); 116 int columnModelIndex = colModel.getColumnIndexAtX(e.getX()); 117 int modelIndex = colModel.getColumn(columnModelIndex) 118 .getModelIndex(); 119 120 if (modelIndex < 0) { 121 return; 122 } 123 // Same column? If yes, flip order 124 if (sortCol == modelIndex) { 125 isSortAsc = !isSortAsc; 126 } else { 127 sortCol = modelIndex; 128 } 129 130 for (int i = 0; i < colModel.getColumnCount(); i++) { 131 TableColumn column = colModel.getColumn(i); 132 column.setHeaderValue(getColumnName(column.getModelIndex())); 133 } 134 table.getTableHeader().repaint(); 135 136 //Collections.sort(addressContainer, new MyComparator(isSortAsc)); 137 138 sortByColumn(sortCol, isSortAsc); 139 table.tableChanged(new TableModelEvent(AddressEditTableModel.this)); 140 table.repaint(); 141 } 142 } 143 144 /** 145 * Internal base class to sort items by different columns. 146 */ 147 protected abstract class ColumnSorter<E> implements Comparator<E> { 148 private int column; 149 private boolean ascending; 150 151 /** 152 * Instantiates a new address sorter. 153 * 154 * @param column the column to sort by 155 */ 156 public ColumnSorter(int column, boolean ascending) { 157 super(); 158 this.column = column; 159 this.ascending = ascending; 160 } 161 162 /** 163 * Gets the index of the column to sort. 164 * 165 * @return the column 166 */ 167 protected int getColumn() { 168 return column; 169 } 170 171 /** 172 * Checks if sort mode is ascending or not. 173 * 174 * @return true, if is ascending 175 */ 176 protected boolean isAscending() { 177 return ascending; 178 } 179 180 /* (non-Javadoc) 181 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 182 */ 183 @Override 184 public abstract int compare(E arg0, E arg1); 185 } 62 186 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/IncompleteAddressesDialog.java
r24133 r24174 23 23 import javax.swing.event.ListSelectionEvent; 24 24 import javax.swing.event.ListSelectionListener; 25 import javax.swing.table.JTableHeader; 25 26 26 27 import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent; … … 79 80 JPanel p = new JPanel(new BorderLayout()); 80 81 81 incompleteAddr = new JTable(new IncompleteAddressesTableModel(container)); 82 IncompleteAddressesTableModel model = new IncompleteAddressesTableModel(container); 83 incompleteAddr = new JTable(model); 84 JTableHeader header = incompleteAddr.getTableHeader(); 85 header.addMouseListener(model.new ColumnListener(incompleteAddr)); 82 86 incompleteAddr.getSelectionModel().addListSelectionListener(this); 83 87 -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/IncompleteAddressesTableModel.java
r24166 r24174 16 16 import static org.openstreetmap.josm.tools.I18n.tr; 17 17 18 import java.util.Collections; 19 18 20 import org.openstreetmap.josm.plugins.fixAddresses.AddressEditContainer; 21 import org.openstreetmap.josm.plugins.fixAddresses.IOSMEntity; 19 22 import org.openstreetmap.josm.plugins.fixAddresses.OSMAddress; 20 import org.openstreetmap.josm.plugins.fixAddresses.IOSMEntity;21 23 22 24 public class IncompleteAddressesTableModel extends AddressEditTableModel { … … 26 28 private static final long serialVersionUID = -5951629033395186324L; 27 29 28 // TODO: Add "state" colum , if required30 // TODO: Add "state" column, if required 29 31 private static final int NUMBER_OF_COLUMNS = 5; 30 32 private static final String[] COLUMN_NAMES = new String[]{tr("Country"), tr("City"), tr("Postcode"), tr("Street"), tr("Number")}; … … 110 112 @Override 111 113 public boolean isCellEditable(int row, int column) { 112 // TODO Auto-generated method stub113 114 return false; 114 115 } … … 139 140 return addressContainer.getIncompleteAddresses().indexOf(entity); 140 141 } 142 143 144 @Override 145 protected void sortByColumn(int column, boolean ascending) { 146 if (addressContainer.getNumberOfIncompleteAddresses() == 0) return; 147 148 Collections.sort(addressContainer.getIncompleteAddresses(), 149 new IncompleteAddressModelSorter(column, ascending)); 150 } 151 152 /** 153 * Internal class StreetModelSorter. 154 */ 155 class IncompleteAddressModelSorter extends ColumnSorter<OSMAddress> { 156 157 /** 158 * Instantiates a new incomplete address model sorter. 159 * 160 * @param column the column to sort 161 * @param asc sort ascending 162 */ 163 public IncompleteAddressModelSorter(int column, boolean asc) { 164 super(column, asc); 165 } 166 167 /* (non-Javadoc) 168 * @see org.openstreetmap.josm.plugins.fixAddresses.gui.AddressEditTableModel.ColumnSorter#compare(java.lang.Object, java.lang.Object) 169 */ 170 @Override 171 public int compare(OSMAddress arg0, OSMAddress arg1) { 172 int cc = 0; 173 174 switch (getColumn()) { 175 case 0: 176 cc=arg0.getCountry().compareTo(arg1.getCountry()); 177 break; 178 case 1: 179 cc=arg0.getCity().compareTo(arg1.getCity()); 180 break; 181 case 2: 182 cc=arg0.getPostCode().compareTo(arg1.getPostCode()); 183 break; 184 case 3: 185 cc= arg0.getStreetName().compareTo(arg1.getStreetName()); 186 break; 187 case 4: 188 cc=arg0.getHouseNumber().compareTo(arg1.getHouseNumber()); 189 break; 190 default: 191 throw new RuntimeException("Invalid column index: " + getColumn()); 192 } 193 194 if (!isAscending()) { 195 cc = -cc; 196 } 197 198 return cc; 199 } 200 } 141 201 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/StreetTableModel.java
r24172 r24174 16 16 import static org.openstreetmap.josm.tools.I18n.tr; 17 17 18 import java.util.Collections; 19 18 20 import org.openstreetmap.josm.plugins.fixAddresses.AddressEditContainer; 19 21 import org.openstreetmap.josm.plugins.fixAddresses.IOSMEntity; 20 22 import org.openstreetmap.josm.plugins.fixAddresses.OSMStreet; 21 23 24 @SuppressWarnings("serial") 22 25 public class StreetTableModel extends AddressEditTableModel { 23 24 26 private static final int NUMBER_OF_COLUMNS = 3; 25 27 private static final String[] COLUMN_NAMES = new String[]{tr("Type"), tr("Name"), tr("Addresses")}; 26 28 private static final Class<?>[] COLUMN_CLASSES = new Class<?>[]{String.class, String.class, Integer.class}; 27 28 /**29 *30 */31 private static final long serialVersionUID = 424009321818130586L;32 33 29 /** 34 30 * @param addressContainer … … 127 123 return addressContainer.getStreetList().indexOf(entity); 128 124 } 125 126 /* (non-Javadoc) 127 * @see org.openstreetmap.josm.plugins.fixAddresses.gui.AddressEditTableModel#sortByColumn(int, boolean) 128 */ 129 @Override 130 protected void sortByColumn(int column, boolean ascending) { 131 Collections.sort(addressContainer.getStreetList(), new StreetModelSorter(column, ascending)); 132 } 133 134 /** 135 * Internal class StreetModelSorter. 136 */ 137 class StreetModelSorter extends ColumnSorter<OSMStreet> { 138 139 public StreetModelSorter(int column, boolean asc) { 140 super(column, asc); 141 } 142 143 public int compare(OSMStreet arg0, OSMStreet arg1) { 144 if (arg0 == null || arg1 == null) return 0; 145 146 switch (getColumn()) { 147 case 0: 148 if (arg0.getType() != null) { 149 return arg0.getType().compareTo(arg1.getType()); 150 } else { 151 return arg1.hasName() ? -1 : 0; 152 } 153 case 1: 154 if (arg0.hasName()) { 155 return arg0.getName().compareTo(arg1.getName()); 156 } else { 157 return arg1.hasName() ? -1 : 0; 158 } 159 case 2: 160 return new Integer(arg0.getNumberOfAddresses()). 161 compareTo(new Integer(arg1.getNumberOfAddresses())); 162 default: 163 } 164 return 0; 165 } 166 } 129 167 } -
applications/editors/josm/plugins/FixAddresses/src/org/openstreetmap/josm/plugins/fixAddresses/gui/UnresolvedAddressesTableModel.java
r24091 r24174 31 31 import static org.openstreetmap.josm.tools.I18n.tr; 32 32 33 import java.util.Collections; 34 33 35 import org.openstreetmap.josm.plugins.fixAddresses.AddressEditContainer; 34 36 import org.openstreetmap.josm.plugins.fixAddresses.OSMAddress; … … 36 38 import org.openstreetmap.josm.plugins.fixAddresses.StringUtils; 37 39 import org.openstreetmap.josm.plugins.fixAddresses.TagUtils; 40 import org.openstreetmap.josm.plugins.fixAddresses.gui.AddressEditTableModel.ColumnSorter; 41 import org.openstreetmap.josm.plugins.fixAddresses.gui.IncompleteAddressesTableModel.IncompleteAddressModelSorter; 38 42 39 43 /** 40 44 * Provides a table model to show unresolved addresses. 45 * 41 46 * @author Oliver Wieland <oliver.wieland@online.de> 42 47 * … … 46 51 47 52 private static final int NUMBER_OF_COLUMNS = 5; 48 private static final String[] COLUMN_NAMES = new String[]{ 49 tr("Street"), tr("Number"), tr("City"), tr("Postcode"), tr("Name")}; 50 51 private static final Class<?>[] COLUMN_CLASSES = new Class<?>[]{ 52 String.class, String.class, String.class, String.class, String.class}; 53 53 private static final String[] COLUMN_NAMES = new String[] { tr("Street"), 54 tr("Number"), tr("City"), tr("Postcode"), tr("Name") }; 55 56 private static final Class<?>[] COLUMN_CLASSES = new Class<?>[] { 57 String.class, String.class, String.class, String.class, 58 String.class }; 59 54 60 /** 55 61 * 56 62 */ 57 63 private static final long serialVersionUID = 424009321818130586L; 58 64 59 65 /** 60 66 * @param addressContainer … … 64 70 } 65 71 66 /* (non-Javadoc) 72 /* 73 * (non-Javadoc) 74 * 67 75 * @see javax.swing.table.DefaultTableModel#getColumnCount() 68 76 */ … … 72 80 } 73 81 74 /* (non-Javadoc) 82 /* 83 * (non-Javadoc) 84 * 75 85 * @see javax.swing.table.DefaultTableModel#getColumnName(int) 76 86 */ … … 80 90 } 81 91 82 /* (non-Javadoc) 92 /* 93 * (non-Javadoc) 94 * 83 95 * @see javax.swing.table.DefaultTableModel#getRowCount() 84 96 */ 85 97 @Override 86 98 public int getRowCount() { 87 if (addressContainer == null || addressContainer.getUnresolvedAddresses() == null) { 99 if (addressContainer == null 100 || addressContainer.getUnresolvedAddresses() == null) { 88 101 return 0; 89 102 } … … 91 104 } 92 105 93 /* (non-Javadoc) 106 /* 107 * (non-Javadoc) 108 * 94 109 * @see javax.swing.table.DefaultTableModel#getValueAt(int, int) 95 110 */ … … 97 112 public Object getValueAt(int row, int column) { 98 113 OSMAddress aNode = (OSMAddress) getEntityOfRow(row); 99 114 100 115 if (aNode == null) { 101 116 return null; 102 117 } 103 118 104 119 switch (column) { 105 120 case 0: … … 121 136 throw new RuntimeException("Invalid column index: " + column); 122 137 } 123 124 } 125 126 /* (non-Javadoc) 138 139 } 140 141 /* 142 * (non-Javadoc) 143 * 127 144 * @see javax.swing.table.AbstractTableModel#getColumnClass(int) 128 145 */ … … 132 149 } 133 150 134 /* (non-Javadoc) 151 /* 152 * (non-Javadoc) 153 * 135 154 * @see javax.swing.table.DefaultTableModel#isCellEditable(int, int) 136 155 */ … … 140 159 } 141 160 142 /* (non-Javadoc) 143 * @see org.openstreetmap.josm.plugins.fixAddresses.gui.AddressEditTableModel#getEntityOfRow(int) 161 /* 162 * (non-Javadoc) 163 * 164 * @see 165 * org.openstreetmap.josm.plugins.fixAddresses.gui.AddressEditTableModel 166 * #getEntityOfRow(int) 144 167 */ 145 168 @Override 146 169 public IOSMEntity getEntityOfRow(int row) { 147 if (addressContainer == null || addressContainer.getUnresolvedAddresses() == null) { 170 if (addressContainer == null 171 || addressContainer.getUnresolvedAddresses() == null) { 148 172 return null; 149 173 } … … 151 175 return null; 152 176 } 153 return addressContainer.getUnresolvedAddresses().get(row); 154 } 155 177 return addressContainer.getUnresolvedAddresses().get(row); 178 } 179 156 180 @Override 157 181 public int getRowOfEntity(IOSMEntity entity) { 158 if (addressContainer == null || addressContainer.getUnresolvedAddresses() == null) { 182 if (addressContainer == null 183 || addressContainer.getUnresolvedAddresses() == null) { 159 184 return -1; 160 185 } 161 186 162 187 return addressContainer.getUnresolvedAddresses().indexOf(entity); 163 188 } 189 190 /* 191 * (non-Javadoc) 192 * 193 * @see 194 * org.openstreetmap.josm.plugins.fixAddresses.gui.AddressEditTableModel 195 * #sortByColumn(int, boolean) 196 */ 197 @Override 198 protected void sortByColumn(int column, boolean ascending) { 199 if (addressContainer.getNumberOfUnresolvedAddresses() == 0) 200 return; 201 202 Collections.sort(addressContainer.getUnresolvedAddresses(), 203 new UnresolvedAddressModelSorter(column, ascending)); 204 } 205 206 /** 207 * Internal class StreetModelSorter. 208 */ 209 class UnresolvedAddressModelSorter extends ColumnSorter<OSMAddress> { 210 211 public UnresolvedAddressModelSorter(int column, boolean asc) { 212 super(column, asc); 213 } 214 215 /* 216 * (non-Javadoc) 217 * 218 * @see 219 * org.openstreetmap.josm.plugins.fixAddresses.gui.AddressEditTableModel 220 * .ColumnSorter#compare(java.lang.Object, java.lang.Object) 221 */ 222 @Override 223 public int compare(OSMAddress arg0, OSMAddress arg1) { 224 int cc = 0; 225 switch (getColumn()) { 226 case 0: 227 cc = arg0.getStreetName().compareTo(arg1.getStreetName()); 228 break; 229 case 1: 230 cc = arg0.getHouseNumber().compareTo(arg1.getHouseNumber()); 231 break; 232 case 2: 233 cc = arg0.getCity().compareTo(arg1.getCity()); 234 break; 235 case 3: 236 cc = arg0.getPostCode().compareTo(arg1.getPostCode()); 237 break; 238 default: 239 throw new RuntimeException("Invalid column index: " 240 + getColumn()); 241 } 242 243 if (!isAscending()) { 244 cc = -cc; 245 } 246 247 return cc; 248 } 249 } 164 250 }
Note:
See TracChangeset
for help on using the changeset viewer.