Changeset 2239 in josm
- Timestamp:
- 2009-10-03T16:24:29+02:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/HistoryDialog.java
r2181 r2239 2 2 package org.openstreetmap.josm.gui.dialogs; 3 3 4 import static org.openstreetmap.josm.tools.I18n.marktr;5 4 import static org.openstreetmap.josm.tools.I18n.tr; 6 5 7 6 import java.awt.BorderLayout; 8 import java.awt.Color;9 7 import java.awt.Component; 10 8 import java.awt.GridLayout; … … 13 11 import java.awt.event.MouseAdapter; 14 12 import java.awt.event.MouseEvent; 15 import java.io.IOException;16 13 import java.util.ArrayList; 17 14 import java.util.Collection; 18 import java.util. HashMap;19 import java.util. Iterator;15 import java.util.Collections; 16 import java.util.List; 20 17 21 18 import javax.swing.AbstractAction; 22 19 import javax.swing.Action; 23 import javax.swing. ImageIcon;20 import javax.swing.DefaultListSelectionModel; 24 21 import javax.swing.JComponent; 25 22 import javax.swing.JLabel; 26 import javax.swing.JOptionPane;27 23 import javax.swing.JPanel; 28 24 import javax.swing.JScrollPane; … … 41 37 import org.openstreetmap.josm.data.osm.DataSet; 42 38 import org.openstreetmap.josm.data.osm.OsmPrimitive; 43 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;44 39 import org.openstreetmap.josm.data.osm.history.History; 45 40 import org.openstreetmap.josm.data.osm.history.HistoryDataSet; 46 41 import org.openstreetmap.josm.data.osm.history.HistoryDataSetListener; 47 import org.openstreetmap.josm.gui. PleaseWaitRunnable;42 import org.openstreetmap.josm.gui.OsmPrimitivRenderer; 48 43 import org.openstreetmap.josm.gui.SideButton; 49 44 import org.openstreetmap.josm.gui.history.HistoryBrowserDialog; 50 45 import org.openstreetmap.josm.gui.history.HistoryBrowserDialogManager; 51 46 import org.openstreetmap.josm.gui.history.HistoryLoadTask; 52 import org.openstreetmap.josm.io.OsmApi;53 import org.openstreetmap.josm.io.OsmApiException;54 import org.openstreetmap.josm.io.OsmServerHistoryReader;55 import org.openstreetmap.josm.io.OsmTransferException;56 47 import org.openstreetmap.josm.tools.ImageProvider; 57 48 import org.openstreetmap.josm.tools.Shortcut; 58 import org.xml.sax.SAXException;59 49 60 50 /** 61 * History dialog works like follows: 62 * 63 * There is a history cache hold in the back for primitives of the last refresh. 64 * When the user refreshes, this cache is cleared and all currently selected items 65 * are reloaded. 66 * If the user has selected at least one primitive not in the cache, the list 67 * is not displayed. Elsewhere, the list of all changes of all currently selected 68 * objects are displayed. 69 * 70 * @author imi 51 * HistoryDialog displays a list of the currently selected primitives and provides 52 * two actions for (1) (re)loading the history of the selected primitives and (2) 53 * for launching a history browser for each selected primitive. 54 * 71 55 */ 72 56 public class HistoryDialog extends ToggleDialog implements HistoryDataSetListener { 73 57 74 58 /** the table model */ 75 protected HistoryItem DataModel model;59 protected HistoryItemTableModel model; 76 60 /** the table with the history items */ 77 61 protected JTable historyTable; … … 103 87 */ 104 88 protected void build() { 105 model = new HistoryItemDataModel(); 106 //setLayout(new BorderLayout()); 89 DefaultListSelectionModel selectionModel = new DefaultListSelectionModel(); 107 90 historyTable = new JTable( 108 model, 109 new HistoryTableColumnModel() 91 model = new HistoryItemTableModel(selectionModel), 92 new HistoryTableColumnModel(), 93 selectionModel 110 94 ); 111 historyTable.setSelectionMode(ListSelectionModel. SINGLE_SELECTION);95 historyTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 112 96 historyTable.setName("table.historyitems"); 113 97 final TableCellRenderer oldRenderer = historyTable.getTableHeader().getDefaultRenderer(); … … 126 110 } 127 111 }); 128 historyTable.addMouseListener(129 new MouseAdapter() {130 @Override131 public void mouseClicked(MouseEvent e) {132 if (e.getClickCount() == 2) {133 int row = historyTable.rowAtPoint(e.getPoint());134 History h = model.get(row);135 showHistory(h);136 }137 }138 }139 );140 112 141 113 JScrollPane pane = new JScrollPane(historyTable); … … 151 123 // 152 124 historyTable.getSelectionModel().addListSelectionListener(showHistoryAction); 153 DataSet.selListeners.add(reloadAction);125 historyTable.getSelectionModel().addListSelectionListener(reloadAction); 154 126 } 155 127 … … 162 134 HistoryDataSet.getInstance().addHistoryDataSetListener(this); 163 135 } 164 165 166 136 167 137 public void historyUpdated(HistoryDataSet source, long primitiveId) { … … 191 161 * 192 162 */ 193 class HistoryItemDataModel extends DefaultTableModel implements SelectionChangedListener{ 194 private ArrayList<History> data; 195 196 public HistoryItemDataModel() { 197 data = new ArrayList<History>(); 163 class HistoryItemTableModel extends DefaultTableModel implements SelectionChangedListener{ 164 private ArrayList<OsmPrimitive> data; 165 private DefaultListSelectionModel selectionModel; 166 167 public HistoryItemTableModel(DefaultListSelectionModel selectionModel) { 168 data = new ArrayList<OsmPrimitive>(); 169 this.selectionModel = selectionModel; 198 170 } 199 171 … … 215 187 } 216 188 189 protected List<OsmPrimitive> getSelectedPrimitives() { 190 ArrayList<OsmPrimitive> ret = new ArrayList<OsmPrimitive>(); 191 for (int i=0; i< data.size(); i++) { 192 if (selectionModel.isSelectedIndex(i)) { 193 ret.add(data.get(i)); 194 } 195 } 196 return ret; 197 } 198 199 protected void selectPrimitives(Collection<OsmPrimitive> primitives) { 200 for (OsmPrimitive p: primitives) { 201 int idx = data.indexOf(p); 202 if (idx < 0) { 203 continue; 204 } 205 selectionModel.addSelectionInterval(idx, idx); 206 } 207 } 208 217 209 public void refresh() { 210 List<OsmPrimitive> selectedPrimitives = getSelectedPrimitives(); 218 211 data.clear(); 219 212 if (Main.main.getCurrentDataSet() == null) … … 223 216 continue; 224 217 } 225 History h = HistoryDataSet.getInstance().getHistory(primitive.getId()); 226 if (h !=null) { 227 data.add(h); 228 } 218 data.add(primitive); 229 219 } 230 220 fireTableDataChanged(); 221 selectPrimitives(selectedPrimitives); 231 222 } 232 223 … … 235 226 } 236 227 237 public History get(int idx) throws IndexOutOfBoundsException { 238 if (idx < 0 || idx >= data.size()) 239 throw new IndexOutOfBoundsException(tr("Index out of bounds. Got {0}.", idx)); 240 return data.get(idx); 241 } 242 } 243 244 245 /** 246 * The table cell renderer for the history items. 247 * 248 */ 249 class HistoryTableCellRenderer extends JLabel implements TableCellRenderer { 250 251 public final Color BGCOLOR_SELECTED = new Color(143,170,255); 252 253 private HashMap<OsmPrimitiveType, ImageIcon> icons; 254 255 public HistoryTableCellRenderer() { 256 setOpaque(true); 257 icons = new HashMap<OsmPrimitiveType, ImageIcon>(); 258 icons.put(OsmPrimitiveType.NODE, ImageProvider.get("data", "node")); 259 icons.put(OsmPrimitiveType.WAY, ImageProvider.get("data", "way")); 260 icons.put(OsmPrimitiveType.RELATION, ImageProvider.get("data", "relation")); 261 } 262 263 protected void renderIcon(History history) { 264 setIcon(icons.get(history.getEarliest().getType())); 265 } 266 267 protected void renderText(History h) { 268 String msg = ""; 269 switch(h.getEarliest().getType()) { 270 case NODE: msg = marktr("Node {0}"); break; 271 case WAY: msg = marktr("Way {0}"); break; 272 case RELATION: msg = marktr("Relation {0}"); break; 273 } 274 setText(tr(msg,h.getId())); 275 } 276 277 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 278 boolean hasFocus, int row, int column) { 279 History h = (History)value; 280 renderIcon(h); 281 renderText(h); 282 if (isSelected) { 283 setBackground(BGCOLOR_SELECTED); 284 } else { 285 setBackground(Color.WHITE); 286 } 287 return this; 228 public List<OsmPrimitive> getPrimitives(int [] rows) { 229 if (rows == null || rows.length == 0) return Collections.emptyList(); 230 ArrayList<OsmPrimitive> ret = new ArrayList<OsmPrimitive>(rows.length); 231 for (int row: rows) { 232 ret.add(data.get(row)); 233 } 234 return ret; 288 235 } 289 236 } … … 295 242 protected void createColumns() { 296 243 TableColumn col = null; 297 HistoryTableCellRenderer renderer = new HistoryTableCellRenderer();244 OsmPrimitivRenderer renderer = new OsmPrimitivRenderer(); 298 245 // column 0 - History item 299 246 col = new TableColumn(0); 300 col.setHeaderValue(tr(" History item"));247 col.setHeaderValue(tr("Object with history")); 301 248 col.setCellRenderer(renderer); 302 249 addColumn(col); … … 312 259 * 313 260 */ 314 class ReloadAction extends AbstractAction implements SelectionChangedListener {261 class ReloadAction extends AbstractAction implements ListSelectionListener { 315 262 public ReloadAction() { 316 263 putValue(Action.SMALL_ICON, ImageProvider.get("dialogs","refresh")); 264 putValue(Action.NAME, tr("Reload")); 317 265 putValue(Action.SHORT_DESCRIPTION, tr("Reload all currently selected objects and refresh the list.")); 266 updateEnabledState(); 318 267 } 319 268 320 269 public void actionPerformed(ActionEvent e) { 270 int [] rows = historyTable.getSelectedRows(); 271 if (rows == null || rows.length == 0) return; 272 273 List<OsmPrimitive> selectedItems = model.getPrimitives(rows); 321 274 HistoryLoadTask task = new HistoryLoadTask(); 322 task.add( Main.main.getCurrentDataSet().getSelected());275 task.add(selectedItems); 323 276 Main.worker.execute(task); 324 277 } 325 278 326 p ublic void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {327 if (Main.main.getCurrentDataSet() == null) {328 setEnabled(false);329 } else { 330 setEnabled(Main.main.getCurrentDataSet().getSelected().size() > 0);331 }279 protected void updateEnabledState() { 280 setEnabled(historyTable.getSelectedRowCount() > 0); 281 } 282 283 public void valueChanged(ListSelectionEvent e) { 284 updateEnabledState(); 332 285 } 333 286 } … … 338 291 class ShowHistoryAction extends AbstractAction implements ListSelectionListener { 339 292 public ShowHistoryAction() { 340 //putValue(Action.SMALL_ICON, ImageProvider.get("dialogs","refresh"));293 putValue(Action.SMALL_ICON, ImageProvider.get("dialogs","history")); 341 294 putValue(Action.NAME, tr("Show")); 342 putValue(Action.SHORT_DESCRIPTION, tr("Display the history of the selected primitive.")); 295 putValue(Action.SHORT_DESCRIPTION, tr("Display the history of the selected objects.")); 296 updateEnabledState(); 297 } 298 299 protected List<OsmPrimitive> filterPrimitivesWithUnloadedHistory(Collection<OsmPrimitive> primitives) { 300 ArrayList<OsmPrimitive> ret = new ArrayList<OsmPrimitive>(primitives.size()); 301 for (OsmPrimitive p: primitives) { 302 if (HistoryDataSet.getInstance().getHistory(p.getId()) == null) { 303 ret.add(p); 304 } 305 } 306 return ret; 343 307 } 344 308 345 309 public void actionPerformed(ActionEvent e) { 346 int row = historyTable.getSelectionModel().getMinSelectionIndex(); 347 if (row < 0) return; 348 History h = model.get(row); 349 showHistory(h); 310 int [] rows = historyTable.getSelectedRows(); 311 if (rows == null || rows.length == 0) return; 312 313 final List<OsmPrimitive> selectedItems = model.getPrimitives(rows); 314 List<OsmPrimitive> toLoad = filterPrimitivesWithUnloadedHistory(selectedItems); 315 if (!toLoad.isEmpty()) { 316 HistoryLoadTask task = new HistoryLoadTask(); 317 task.add(selectedItems); 318 Main.worker.submit(task); 319 } 320 321 Runnable r = new Runnable() { 322 public void run() { 323 for (OsmPrimitive p : selectedItems) { 324 History h = HistoryDataSet.getInstance().getHistory(p.getId()); 325 if (h == null) { 326 continue; 327 } 328 showHistory(h); 329 } 330 } 331 }; 332 Main.worker.submit(r); 333 } 334 335 protected void updateEnabledState() { 336 setEnabled(historyTable.getSelectedRowCount() > 0); 350 337 } 351 338 352 339 public void valueChanged(ListSelectionEvent e) { 353 setEnabled(historyTable.getSelectionModel().getMinSelectionIndex() >= 0);340 updateEnabledState(); 354 341 } 355 342 } -
trunk/src/org/openstreetmap/josm/gui/history/HistoryLoadTask.java
r2181 r2239 8 8 import java.util.Collection; 9 9 import java.util.HashMap; 10 import java.util.Iterator;11 10 import java.util.Map; 12 11 13 import org.openstreetmap.josm.Main;14 12 import org.openstreetmap.josm.data.osm.OsmPrimitive; 15 13 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
Note:
See TracChangeset
for help on using the changeset viewer.