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