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