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