source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/HistoryDialog.java@ 6084

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 11.9 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.awt.event.MouseAdapter;
10import java.awt.event.MouseEvent;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Collection;
14import java.util.Collections;
15import java.util.List;
16
17import javax.swing.AbstractAction;
18import javax.swing.Action;
19import javax.swing.DefaultListSelectionModel;
20import javax.swing.JComponent;
21import javax.swing.JLabel;
22import javax.swing.JTable;
23import javax.swing.ListSelectionModel;
24import javax.swing.event.ListSelectionEvent;
25import javax.swing.event.ListSelectionListener;
26import javax.swing.table.DefaultTableCellRenderer;
27import javax.swing.table.DefaultTableColumnModel;
28import javax.swing.table.DefaultTableModel;
29import javax.swing.table.TableCellRenderer;
30import javax.swing.table.TableColumn;
31
32import org.openstreetmap.josm.Main;
33import org.openstreetmap.josm.data.SelectionChangedListener;
34import org.openstreetmap.josm.data.osm.DataSet;
35import org.openstreetmap.josm.data.osm.OsmPrimitive;
36import org.openstreetmap.josm.data.osm.PrimitiveId;
37import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
38import org.openstreetmap.josm.data.osm.history.HistoryDataSetListener;
39import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
40import org.openstreetmap.josm.gui.SideButton;
41import org.openstreetmap.josm.gui.help.HelpUtil;
42import org.openstreetmap.josm.gui.history.HistoryBrowserDialogManager;
43import org.openstreetmap.josm.gui.history.HistoryLoadTask;
44import org.openstreetmap.josm.tools.ImageProvider;
45import org.openstreetmap.josm.tools.InputMapUtils;
46import org.openstreetmap.josm.tools.Shortcut;
47
48/**
49 * HistoryDialog displays a list of the currently selected primitives and provides
50 * two actions for (1) (re)loading the history of the selected primitives and (2)
51 * for launching a history browser for each selected primitive.
52 *
53 */
54public class HistoryDialog extends ToggleDialog implements HistoryDataSetListener {
55
56 /** the table model */
57 protected HistoryItemTableModel model;
58 /** the table with the history items */
59 protected JTable historyTable;
60
61 protected ShowHistoryAction showHistoryAction;
62 protected ReloadAction reloadAction;
63
64 public HistoryDialog() {
65 super(tr("History"), "history", tr("Display the history of all selected items."),
66 Shortcut.registerShortcut("subwindow:history", tr("Toggle: {0}", tr("History")), KeyEvent.VK_H,
67 Shortcut.ALT_SHIFT), 150);
68 build();
69 HelpUtil.setHelpContext(this, HelpUtil.ht("/Dialog/History"));
70 }
71
72 /**
73 * builds the GUI
74 */
75 protected void build() {
76 DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
77 historyTable = new JTable(
78 model = new HistoryItemTableModel(selectionModel),
79 new HistoryTableColumnModel(),
80 selectionModel
81 );
82 historyTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
83 final TableCellRenderer oldRenderer = historyTable.getTableHeader().getDefaultRenderer();
84 historyTable.getTableHeader().setDefaultRenderer(new DefaultTableCellRenderer(){
85 @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
86 JComponent c = (JComponent)oldRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
87 if (!"".equals(value))
88 return c;
89 JLabel l = new JLabel(ImageProvider.get("misc","showhide"));
90 l.setForeground(c.getForeground());
91 l.setBackground(c.getBackground());
92 l.setFont(c.getFont());
93 l.setBorder(c.getBorder());
94 l.setOpaque(true);
95 return l;
96 }
97 });
98 historyTable.addMouseListener(new ShowHistoryMouseAdapter());
99 historyTable.setTableHeader(null);
100
101 createLayout(historyTable, true, Arrays.asList(new SideButton[] {
102 new SideButton(reloadAction = new ReloadAction()),
103 new SideButton(showHistoryAction = new ShowHistoryAction())
104 }));
105
106 // wire actions
107 //
108 historyTable.getSelectionModel().addListSelectionListener(showHistoryAction);
109 historyTable.getSelectionModel().addListSelectionListener(reloadAction);
110
111 // Show history dialog on Enter/Spacebar
112 InputMapUtils.addEnterAction(historyTable, showHistoryAction);
113 InputMapUtils.addSpacebarAction(historyTable, showHistoryAction);
114 }
115
116 @Override
117 public void showNotify() {
118 HistoryDataSet.getInstance().addHistoryDataSetListener(this);
119 DataSet.addSelectionListener(model);
120 if (Main.main.getCurrentDataSet() == null) {
121 model.selectionChanged(null);
122 } else {
123 model.selectionChanged(Main.main.getCurrentDataSet().getAllSelected());
124 }
125 }
126
127 @Override
128 public void hideNotify() {
129 HistoryDataSet.getInstance().removeHistoryDataSetListener(this);
130 DataSet.removeSelectionListener(model);
131 }
132
133 /* ----------------------------------------------------------------------------- */
134 /* interface HistoryDataSetListener */
135 /* ----------------------------------------------------------------------------- */
136 @Override
137 public void historyUpdated(HistoryDataSet source, PrimitiveId primitiveId) {
138 model.refresh();
139 }
140
141 @Override
142 public void historyDataSetCleared(HistoryDataSet source) {
143 model.refresh();
144 }
145
146 /**
147 * The table model with the history items
148 *
149 */
150 static class HistoryItemTableModel extends DefaultTableModel implements SelectionChangedListener{
151 private ArrayList<OsmPrimitive> data;
152 private DefaultListSelectionModel selectionModel;
153
154 public HistoryItemTableModel(DefaultListSelectionModel selectionModel) {
155 data = new ArrayList<OsmPrimitive>();
156 this.selectionModel = selectionModel;
157 }
158
159 @Override
160 public int getRowCount() {
161 if (data == null)
162 return 0;
163 return data.size();
164 }
165
166 @Override
167 public Object getValueAt(int row, int column) {
168 return data.get(row);
169 }
170
171 @Override
172 public boolean isCellEditable(int row, int column) {
173 return false;
174 }
175
176 protected List<OsmPrimitive> getSelectedPrimitives() {
177 ArrayList<OsmPrimitive> ret = new ArrayList<OsmPrimitive>();
178 for (int i=0; i< data.size(); i++) {
179 if (selectionModel.isSelectedIndex(i)) {
180 ret.add(data.get(i));
181 }
182 }
183 return ret;
184 }
185
186 protected void selectPrimitives(Collection<OsmPrimitive> primitives) {
187 for (OsmPrimitive p: primitives) {
188 int idx = data.indexOf(p);
189 if (idx < 0) {
190 continue;
191 }
192 selectionModel.addSelectionInterval(idx, idx);
193 }
194 }
195
196 public void refresh() {
197 List<OsmPrimitive> selectedPrimitives = getSelectedPrimitives();
198 data.clear();
199 if (Main.main.getCurrentDataSet() == null)
200 return;
201 for (OsmPrimitive primitive: Main.main.getCurrentDataSet().getAllSelected()) {
202 if (primitive.isNew()) {
203 continue;
204 }
205 data.add(primitive);
206 }
207 fireTableDataChanged();
208 selectPrimitives(selectedPrimitives);
209 }
210
211 @Override
212 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
213 data.clear();
214 selectionModel.clearSelection();
215 if (newSelection != null && !newSelection.isEmpty()) {
216 for (OsmPrimitive primitive: newSelection) {
217 if (primitive.isNew()) {
218 continue;
219 }
220 data.add(primitive);
221 }
222 }
223 fireTableDataChanged();
224 selectionModel.addSelectionInterval(0, data.size()-1);
225 }
226
227 public List<OsmPrimitive> getPrimitives(int [] rows) {
228 if (rows == null || rows.length == 0) return Collections.emptyList();
229 ArrayList<OsmPrimitive> ret = new ArrayList<OsmPrimitive>(rows.length);
230 for (int row: rows) {
231 ret.add(data.get(row));
232 }
233 return ret;
234 }
235
236 public OsmPrimitive getPrimitive(int row) {
237 return data.get(row);
238 }
239 }
240
241 /**
242 * The column model
243 */
244 static class HistoryTableColumnModel extends DefaultTableColumnModel {
245 protected void createColumns() {
246 TableColumn col = null;
247 OsmPrimitivRenderer renderer = new OsmPrimitivRenderer();
248 // column 0 - History item
249 col = new TableColumn(0);
250 col.setHeaderValue(tr("Object with history"));
251 col.setCellRenderer(renderer);
252 addColumn(col);
253 }
254
255 public HistoryTableColumnModel() {
256 createColumns();
257 }
258 }
259
260 /**
261 * The action for reloading history information of the currently selected primitives.
262 *
263 */
264 class ReloadAction extends AbstractAction implements ListSelectionListener {
265
266 public ReloadAction() {
267 putValue(Action.SMALL_ICON, ImageProvider.get("dialogs","refresh"));
268 putValue(Action.NAME, tr("Reload"));
269 putValue(Action.SHORT_DESCRIPTION, tr("Reload all currently selected objects and refresh the list."));
270 updateEnabledState();
271 }
272
273 @Override
274 public void actionPerformed(ActionEvent e) {
275 int [] rows = historyTable.getSelectedRows();
276 if (rows == null || rows.length == 0) return;
277
278 List<OsmPrimitive> selectedItems = model.getPrimitives(rows);
279 HistoryLoadTask task = new HistoryLoadTask();
280 task.add(selectedItems);
281 Main.worker.execute(task);
282 }
283
284 protected void updateEnabledState() {
285 setEnabled(historyTable.getSelectedRowCount() > 0);
286 }
287
288 @Override
289 public void valueChanged(ListSelectionEvent e) {
290 updateEnabledState();
291 }
292 }
293
294 class ShowHistoryMouseAdapter extends MouseAdapter {
295 @Override
296 public void mouseClicked(MouseEvent e) {
297 if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
298 int row = historyTable.rowAtPoint(e.getPoint());
299 HistoryBrowserDialogManager.getInstance().showHistory(Collections.singletonList(model.getPrimitive(row)));
300 }
301 }
302 }
303
304 /**
305 * The action for showing history information of the current history item.
306 */
307 class ShowHistoryAction extends AbstractAction implements ListSelectionListener {
308 public ShowHistoryAction() {
309 putValue(Action.SMALL_ICON, ImageProvider.get("dialogs","history"));
310 putValue(Action.NAME, tr("Show"));
311 putValue(Action.SHORT_DESCRIPTION, tr("Display the history of the selected objects."));
312 updateEnabledState();
313 }
314
315 @Override
316 public void actionPerformed(ActionEvent e) {
317 int [] rows = historyTable.getSelectedRows();
318 if (rows == null || rows.length == 0) return;
319 HistoryBrowserDialogManager.getInstance().showHistory(model.getPrimitives(rows));
320 }
321
322 protected void updateEnabledState() {
323 setEnabled(historyTable.getSelectedRowCount() > 0);
324 }
325
326 @Override
327 public void valueChanged(ListSelectionEvent e) {
328 updateEnabledState();
329 }
330 }
331}
Note: See TracBrowser for help on using the repository browser.