[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.history;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.event.ActionEvent;
|
---|
| 7 | import java.awt.event.MouseAdapter;
|
---|
| 8 | import java.awt.event.MouseEvent;
|
---|
| 9 | import java.util.Observable;
|
---|
| 10 | import java.util.Observer;
|
---|
| 11 | import java.util.logging.Logger;
|
---|
| 12 |
|
---|
| 13 | import javax.swing.DefaultListSelectionModel;
|
---|
| 14 | import javax.swing.JPopupMenu;
|
---|
| 15 | import javax.swing.JTable;
|
---|
| 16 | import javax.swing.ListSelectionModel;
|
---|
| 17 | import javax.swing.event.ListSelectionEvent;
|
---|
| 18 | import javax.swing.event.ListSelectionListener;
|
---|
| 19 |
|
---|
| 20 | import org.openstreetmap.josm.actions.AbstractInfoAction;
|
---|
| 21 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
| 22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * VersionTable shows a list of version in a {@see History} of an {@see OsmPrimitive}.
|
---|
| 26 | *
|
---|
| 27 | *
|
---|
| 28 | */
|
---|
| 29 | public class VersionTable extends JTable implements Observer{
|
---|
| 30 |
|
---|
| 31 | private static Logger logger = Logger.getLogger(VersionTable.class.getName());
|
---|
| 32 | private VersionTablePopupMenu popupMenu;
|
---|
| 33 |
|
---|
| 34 | protected void build() {
|
---|
| 35 | setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
| 36 | addMouseListener(new MouseHandler());
|
---|
| 37 | getSelectionModel().addListSelectionListener(new SelectionHandler());
|
---|
| 38 | popupMenu = new VersionTablePopupMenu();
|
---|
| 39 | addMouseListener(new PopupMenuTrigger());
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public VersionTable(HistoryBrowserModel model) {
|
---|
| 43 | super(model.getVersionTableModel(), new VersionTableColumnModel());
|
---|
| 44 | model.addObserver(this);
|
---|
| 45 | build();
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | protected void handleSelectReferencePointInTime(int row) {
|
---|
| 49 | getVersionTableModel().setReferencePointInTime(row);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | protected void handleSelectCurrentPointInTime(int row) {
|
---|
| 53 | getVersionTableModel().setCurrentPointInTime(row);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | protected HistoryBrowserModel.VersionTableModel getVersionTableModel() {
|
---|
| 57 | return (HistoryBrowserModel.VersionTableModel) getModel();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | class MouseHandler extends MouseAdapter {
|
---|
| 61 | protected void handleDoubleClick(MouseEvent e) {
|
---|
| 62 | int row = rowAtPoint(e.getPoint());
|
---|
| 63 | handleSelectReferencePointInTime(row);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | @Override
|
---|
| 67 | public void mouseClicked(MouseEvent e) {
|
---|
| 68 | switch(e.getClickCount()) {
|
---|
[2556] | 69 | case 2: handleDoubleClick(e); break;
|
---|
[2512] | 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | class SelectionHandler implements ListSelectionListener {
|
---|
| 75 | public void valueChanged(ListSelectionEvent e) {
|
---|
| 76 | DefaultListSelectionModel model = (DefaultListSelectionModel)e.getSource();
|
---|
| 77 | if (model.getMinSelectionIndex() >= 0) {
|
---|
| 78 | handleSelectCurrentPointInTime(model.getMinSelectionIndex());
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | public void update(Observable o, Object arg) {
|
---|
| 84 | repaint();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | protected void showPopupMenu(MouseEvent evt) {
|
---|
| 88 | HistoryBrowserModel.VersionTableModel model = getVersionTableModel();
|
---|
| 89 | int row = getSelectedRow();
|
---|
| 90 | if (row == -1) {
|
---|
| 91 | row = rowAtPoint(evt.getPoint());
|
---|
| 92 | }
|
---|
[2556] | 93 | if (!model.isLatest(row)) {
|
---|
| 94 | HistoryOsmPrimitive primitive = model.getPrimitive(row);
|
---|
| 95 | popupMenu.prepare(primitive);
|
---|
| 96 | popupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
|
---|
| 97 | }
|
---|
[2512] | 98 | }
|
---|
| 99 |
|
---|
| 100 | class PopupMenuTrigger extends MouseAdapter {
|
---|
| 101 | @Override
|
---|
| 102 | public void mousePressed(MouseEvent e) {
|
---|
| 103 | showPopup(e);
|
---|
| 104 | }
|
---|
| 105 | @Override
|
---|
| 106 | public void mouseReleased(MouseEvent e) {
|
---|
| 107 | showPopup(e);
|
---|
| 108 | }
|
---|
| 109 | private void showPopup(MouseEvent e) {
|
---|
| 110 | if (e.isPopupTrigger()) {
|
---|
| 111 | showPopupMenu(e);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | class ChangesetInfoAction extends AbstractInfoAction {
|
---|
| 117 | private HistoryOsmPrimitive primitive;
|
---|
| 118 |
|
---|
| 119 | public ChangesetInfoAction() {
|
---|
| 120 | putValue(NAME, tr("Changeset info"));
|
---|
| 121 | putValue(SHORT_DESCRIPTION, tr("Launch browser with information about the changeset"));
|
---|
| 122 | putValue(SMALL_ICON, ImageProvider.get("about"));
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | @Override
|
---|
| 126 | protected String createInfoUrl(Object infoObject) {
|
---|
| 127 | HistoryOsmPrimitive primitive = (HistoryOsmPrimitive) infoObject;
|
---|
| 128 | return getBaseBrowseUrl() + "/changeset/" + primitive.getChangesetId();
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | @Override
|
---|
| 132 | public void actionPerformed(ActionEvent e) {
|
---|
| 133 | if (!isEnabled())
|
---|
| 134 | return;
|
---|
| 135 | String url = createInfoUrl(primitive);
|
---|
| 136 | launchBrowser(url);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | public void prepare(HistoryOsmPrimitive primitive) {
|
---|
| 140 | putValue(NAME, tr("Show changeset {0}", primitive.getChangesetId()));
|
---|
| 141 | this.primitive = primitive;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | class VersionTablePopupMenu extends JPopupMenu {
|
---|
| 146 |
|
---|
| 147 | private ChangesetInfoAction changesetInfoAction;
|
---|
| 148 |
|
---|
| 149 | protected void build() {
|
---|
| 150 | changesetInfoAction = new ChangesetInfoAction();
|
---|
| 151 | add(changesetInfoAction);
|
---|
| 152 | }
|
---|
| 153 | public VersionTablePopupMenu() {
|
---|
| 154 | super();
|
---|
| 155 | build();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | public void prepare(HistoryOsmPrimitive primitive) {
|
---|
| 159 | changesetInfoAction.prepare(primitive);
|
---|
| 160 | invalidate();
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|