1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
6 |
|
---|
7 | import java.awt.BorderLayout;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.GridLayout;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.awt.event.ActionListener;
|
---|
13 | import java.awt.event.KeyEvent;
|
---|
14 | import java.util.Calendar;
|
---|
15 | import java.util.Collection;
|
---|
16 | import java.util.Date;
|
---|
17 | import java.util.HashMap;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Map;
|
---|
20 | import java.util.SortedSet;
|
---|
21 | import java.util.TreeSet;
|
---|
22 |
|
---|
23 | import javax.swing.JComponent;
|
---|
24 | import javax.swing.JLabel;
|
---|
25 | import javax.swing.JOptionPane;
|
---|
26 | import javax.swing.JPanel;
|
---|
27 | import javax.swing.JScrollPane;
|
---|
28 | import javax.swing.JTable;
|
---|
29 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
30 | import javax.swing.table.DefaultTableModel;
|
---|
31 | import javax.swing.table.TableCellRenderer;
|
---|
32 |
|
---|
33 | import org.openstreetmap.josm.Main;
|
---|
34 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
35 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
36 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
37 | import org.openstreetmap.josm.gui.SideButton;
|
---|
38 | import org.openstreetmap.josm.tools.GBC;
|
---|
39 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
40 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * History dialog works like follows:
|
---|
44 | *
|
---|
45 | * There is a history cache hold in the back for primitives of the last refresh.
|
---|
46 | * When the user refreshes, this cache is cleared and all currently selected items
|
---|
47 | * are reloaded.
|
---|
48 | * If the user has selected at least one primitive not in the cache, the list
|
---|
49 | * is not displayed. Elsewhere, the list of all changes of all currently selected
|
---|
50 | * objects are displayed.
|
---|
51 | *
|
---|
52 | * @author imi
|
---|
53 | */
|
---|
54 | public class HistoryDialog extends ToggleDialog implements SelectionChangedListener {
|
---|
55 |
|
---|
56 | public static final Date unifyDate(Date d) {
|
---|
57 | Calendar c = Calendar.getInstance();
|
---|
58 | c.setTime(d);
|
---|
59 | c.set(Calendar.MINUTE, 0);
|
---|
60 | c.set(Calendar.SECOND, 0);
|
---|
61 | return c.getTime();
|
---|
62 | }
|
---|
63 |
|
---|
64 | private static class HistoryItem implements Comparable<HistoryItem> {
|
---|
65 | OsmPrimitive osm;
|
---|
66 | boolean visible;
|
---|
67 |
|
---|
68 | public int compareTo(HistoryItem o) {
|
---|
69 | return unifyDate(osm.getTimestamp()).compareTo(unifyDate(o.osm.getTimestamp()));
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | private final DefaultTableModel data = new DefaultTableModel(){
|
---|
74 | @Override public boolean isCellEditable(int row, int column) {
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 | };
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Main table. 3 columns:
|
---|
81 | * Object | Date | visible (icon, no text)
|
---|
82 | */
|
---|
83 | private JTable history = new JTable(data);
|
---|
84 | private JScrollPane historyPane = new JScrollPane(history);
|
---|
85 |
|
---|
86 | private Map<OsmPrimitive, List<HistoryItem>> cache = new HashMap<OsmPrimitive, List<HistoryItem>>();
|
---|
87 | private JLabel notLoaded = new JLabel("<html><i>"+tr("Click Reload to refresh list")+"</i></html>");
|
---|
88 |
|
---|
89 | public HistoryDialog() {
|
---|
90 | super(tr("History"), "history", tr("Display the history of all selected items."),
|
---|
91 | Shortcut.registerShortcut("subwindow:history", tr("Toggle: {0}", tr("History")), KeyEvent.VK_H,
|
---|
92 | Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 150);
|
---|
93 | historyPane.setVisible(false);
|
---|
94 | notLoaded.setVisible(true);
|
---|
95 | notLoaded.setHorizontalAlignment(JLabel.CENTER);
|
---|
96 |
|
---|
97 | history.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
|
---|
98 | @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
99 | return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
|
---|
100 | }
|
---|
101 | });
|
---|
102 | data.setColumnIdentifiers(new Object[]{tr("Object"),tr("Date"),""});
|
---|
103 | history.getColumnModel().getColumn(0).setPreferredWidth(200);
|
---|
104 | history.getColumnModel().getColumn(1).setPreferredWidth(200);
|
---|
105 | history.getColumnModel().getColumn(2).setPreferredWidth(20);
|
---|
106 | final TableCellRenderer oldRenderer = history.getTableHeader().getDefaultRenderer();
|
---|
107 | history.getTableHeader().setDefaultRenderer(new DefaultTableCellRenderer(){
|
---|
108 | @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
109 | JComponent c = (JComponent)oldRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
|
---|
110 | if (!value.equals(""))
|
---|
111 | return c;
|
---|
112 | JLabel l = new JLabel(ImageProvider.get("misc","showhide"));
|
---|
113 | l.setForeground(c.getForeground());
|
---|
114 | l.setBackground(c.getBackground());
|
---|
115 | l.setFont(c.getFont());
|
---|
116 | l.setBorder(c.getBorder());
|
---|
117 | l.setOpaque(true);
|
---|
118 | return l;
|
---|
119 | }
|
---|
120 | });
|
---|
121 |
|
---|
122 | JPanel centerPanel = new JPanel(new GridBagLayout());
|
---|
123 | centerPanel.add(notLoaded, GBC.eol().fill(GBC.BOTH));
|
---|
124 | centerPanel.add(historyPane, GBC.eol().fill(GBC.BOTH));
|
---|
125 | add(centerPanel, BorderLayout.CENTER);
|
---|
126 |
|
---|
127 | JPanel buttons = new JPanel(new GridLayout(1,2));
|
---|
128 | buttons.add(new SideButton(marktr("Reload"), "refresh", "History", tr("Reload all currently selected objects and refresh the list."),
|
---|
129 | new ActionListener(){
|
---|
130 | public void actionPerformed(ActionEvent e) {
|
---|
131 | reload();
|
---|
132 | }
|
---|
133 | }));
|
---|
134 | buttons.add(new SideButton(marktr("Revert"), "revert", "History",
|
---|
135 | tr("Revert the state of all currently selected objects to the version selected in the history list."), new ActionListener(){
|
---|
136 | public void actionPerformed(ActionEvent e) {
|
---|
137 | JOptionPane.showMessageDialog(Main.parent, tr("Not implemented yet."));
|
---|
138 | }
|
---|
139 | }));
|
---|
140 | add(buttons, BorderLayout.SOUTH);
|
---|
141 |
|
---|
142 | DataSet.selListeners.add(this);
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | @Override public void setVisible(boolean b) {
|
---|
147 | super.setVisible(b);
|
---|
148 | if (b)
|
---|
149 | update();
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
154 | if (isVisible())
|
---|
155 | update();
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Identify all new objects in the selection and if any, hide the list.
|
---|
160 | * Else, update the list with the selected items shown.
|
---|
161 | */
|
---|
162 | private void update() {
|
---|
163 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
164 | if (!cache.keySet().containsAll(sel)) {
|
---|
165 | historyPane.setVisible(false);
|
---|
166 | notLoaded.setVisible(true);
|
---|
167 | } else {
|
---|
168 | SortedSet<HistoryItem> orderedHistory = new TreeSet<HistoryItem>();
|
---|
169 | for (OsmPrimitive osm : sel)
|
---|
170 | orderedHistory.addAll(cache.get(osm));
|
---|
171 | data.setRowCount(0);
|
---|
172 | for (HistoryItem i : orderedHistory)
|
---|
173 | data.addRow(new Object[]{i.osm, i.osm.timestamp, i.visible});
|
---|
174 | historyPane.setVisible(true);
|
---|
175 | notLoaded.setVisible(false);
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | void reload() {
|
---|
180 | JOptionPane.showMessageDialog(Main.parent, tr("Not implemented yet."));
|
---|
181 | }
|
---|
182 | }
|
---|