Changeset 2126 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2009-09-13T23:48:03+02:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java
r2120 r2126 9 9 import java.awt.event.MouseAdapter; 10 10 import java.awt.event.MouseEvent; 11 import java.util.ArrayList; 11 12 import java.util.Collection; 13 import java.util.Collections; 14 import java.util.Comparator; 12 15 import java.util.HashSet; 13 16 import java.util.logging.Logger; 14 17 15 18 import javax.swing.AbstractAction; 16 import javax.swing. DefaultListModel;19 import javax.swing.AbstractListModel; 17 20 import javax.swing.JList; 18 21 import javax.swing.JPanel; … … 25 28 import org.openstreetmap.josm.Main; 26 29 import org.openstreetmap.josm.data.osm.DataSet; 30 import org.openstreetmap.josm.data.osm.NameFormatter; 27 31 import org.openstreetmap.josm.data.osm.OsmPrimitive; 28 32 import org.openstreetmap.josm.data.osm.Relation; 29 33 import org.openstreetmap.josm.data.osm.RelationMember; 34 import org.openstreetmap.josm.gui.DefaultNameFormatter; 30 35 import org.openstreetmap.josm.gui.OsmPrimitivRenderer; 31 36 import org.openstreetmap.josm.gui.SideButton; … … 49 54 private static final Logger logger = Logger.getLogger(RelationListDialog.class.getName()); 50 55 51 /** 52 * The selection's list data. 53 */ 54 private final DefaultListModel list = new DefaultListModel(); 55 56 /** 57 * The display list. 58 */ 59 private JList displaylist = new JList(list); 56 /** The display list. */ 57 private JList displaylist; 58 /** the list model used */ 59 private RelationListModel model; 60 60 61 61 /** the edit action */ … … 74 74 // create the list of relations 75 75 // 76 model = new RelationListModel(); 77 displaylist = new JList(model); 76 78 displaylist.setCellRenderer(new OsmPrimitivRenderer()); 77 79 displaylist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); … … 125 127 } 126 128 129 /** 130 * Replies the list of complete, non-deleted relations in the dataset <code>ds</code>, 131 * sorted by display name. 132 * 133 * @param ds the dataset 134 * @return the list of relations 135 */ 136 protected ArrayList<Relation> getDisplayedRelationsInSortOrder(DataSet ds) { 137 ArrayList<Relation> relations = new ArrayList<Relation>(ds.relations.size()); 138 for (Relation r: ds.relations ){ 139 if (r.isDeleted() || r.incomplete) { 140 continue; 141 } 142 relations.add(r); 143 } 144 145 Collections.sort( 146 relations, 147 new Comparator<Relation>() { 148 NameFormatter formatter = DefaultNameFormatter.getInstance(); 149 public int compare(Relation r1, Relation r2) { 150 return r1.getDisplayName(formatter).compareTo(r2.getDisplayName(formatter)); 151 } 152 } 153 ); 154 return relations; 155 } 156 127 157 public void updateList() { 128 158 if (Main.main.getCurrentDataSet() == null) { 129 list.setSize(0);159 model.setRelations(null); 130 160 return; 131 161 } 132 162 Relation selected = getSelected(); 133 list.setSize(getNumRelations()); 134 if (getNumRelations() > 0 ) { 135 int i = 0; 136 for (OsmPrimitive e : DataSet.sort(Main.main.getCurrentDataSet().relations)) { 137 if (e.isUsable()){ 138 list.setElementAt(e, i++); 139 } 140 } 141 list.setSize(i); 142 } 143 if(getNumRelations() != 0) { 144 setTitle(tr("Relations: {0}", Main.main.getCurrentDataSet().relations.size())); 163 164 model.setRelations(getDisplayedRelationsInSortOrder(Main.main.getCurrentDataSet())); 165 if(model.getSize() > 0) { 166 setTitle(tr("Relations: {0}", model.getSize())); 145 167 } else { 146 168 setTitle(tr("Relations")); … … 201 223 */ 202 224 private Relation getSelected() { 203 if( list.size() == 1) {225 if(model.getSize() == 1) { 204 226 displaylist.setSelectedIndex(0); 205 227 } … … 213 235 */ 214 236 public void selectRelation(Relation relation) { 215 if (relation == null) 216 { 237 if (relation == null){ 217 238 displaylist.clearSelection(); 218 239 return; 219 240 } 220 int i = -1; 221 for (i=0; i < list.getSize(); i++) { 222 Relation r = (Relation)list.get(i); 223 if (r == relation) { 224 break; 225 } 226 } 227 if (i >= 0 && i < list.getSize()) { 228 displaylist.setSelectedIndex(i); 229 displaylist.ensureIndexIsVisible(i); 230 } 231 else 232 { 241 int idx = model.getIndexOfRelation(relation); 242 if (idx == -1) { 233 243 displaylist.clearSelection(); 244 } else { 245 displaylist.setSelectedIndex(idx); 246 displaylist.scrollRectToVisible(displaylist.getCellBounds(idx,idx)); 234 247 } 235 248 } … … 405 418 } 406 419 } 420 421 private static class RelationListModel extends AbstractListModel { 422 private ArrayList<Relation> relations; 423 424 public ArrayList<Relation> getRelations() { 425 return relations; 426 } 427 428 public void setRelations(ArrayList<Relation> relations) { 429 this.relations = relations; 430 fireIntervalAdded(this, 0, getSize()); 431 } 432 433 public Object getElementAt(int index) { 434 if (relations == null) return null; 435 return relations.get(index); 436 } 437 438 public int getSize() { 439 if (relations == null) return 0; 440 return relations.size(); 441 } 442 443 public int getIndexOfRelation(Relation relation) { 444 if (relation == null) return -1; 445 return relations.indexOf(relation); 446 } 447 } 448 449 407 450 }
Note:
See TracChangeset
for help on using the changeset viewer.