1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.event.MouseAdapter;
|
---|
5 | import java.awt.event.MouseEvent;
|
---|
6 |
|
---|
7 | import javax.swing.JPopupMenu;
|
---|
8 | import javax.swing.JTable;
|
---|
9 | import javax.swing.ListSelectionModel;
|
---|
10 | import javax.swing.SwingUtilities;
|
---|
11 | import javax.swing.table.TableColumnModel;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.actions.ZoomToAction;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.gui.MapView;
|
---|
16 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
17 |
|
---|
18 | public abstract class OsmPrimitivesTable extends JTable {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * the data layer in whose context primitives are edited in this table
|
---|
22 | */
|
---|
23 | private OsmDataLayer layer;
|
---|
24 |
|
---|
25 | /** the popup menu */
|
---|
26 | private JPopupMenu popupMenu;
|
---|
27 | private ZoomToAction zoomToAction;
|
---|
28 |
|
---|
29 | public final OsmDataLayer getLayer() {
|
---|
30 | return layer;
|
---|
31 | }
|
---|
32 |
|
---|
33 | public final void setLayer(OsmDataLayer layer) {
|
---|
34 | this.layer = layer;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public OsmPrimitivesTable(OsmPrimitivesTableModel dm, TableColumnModel cm, ListSelectionModel sm) {
|
---|
38 | super(dm, cm, sm);
|
---|
39 | addMouseListener(new PopupMenuLauncher(getPopUpMenu()));
|
---|
40 | addMouseListener(new DblClickHandler());
|
---|
41 | }
|
---|
42 |
|
---|
43 | public OsmPrimitivesTableModel getOsmPrimitivesTableModel() {
|
---|
44 | return (OsmPrimitivesTableModel) getModel();
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Replies the popup menu for this table
|
---|
49 | *
|
---|
50 | * @return the popup menu
|
---|
51 | */
|
---|
52 | protected final JPopupMenu getPopUpMenu() {
|
---|
53 | if (popupMenu == null) {
|
---|
54 | popupMenu = buildPopupMenu();
|
---|
55 | }
|
---|
56 | return popupMenu;
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected abstract ZoomToAction buildZoomToAction();
|
---|
60 |
|
---|
61 | protected JPopupMenu buildPopupMenu() {
|
---|
62 | JPopupMenu menu = new JPopupMenu();
|
---|
63 | zoomToAction = buildZoomToAction();
|
---|
64 | MapView.addLayerChangeListener(zoomToAction);
|
---|
65 | getSelectionModel().addListSelectionListener(zoomToAction);
|
---|
66 | menu.add(zoomToAction);
|
---|
67 | return menu;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void unlinkAsListener() {
|
---|
71 | MapView.removeLayerChangeListener(zoomToAction);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public OsmPrimitive getPrimitiveInLayer(int row, OsmDataLayer layer) {
|
---|
75 | return getOsmPrimitivesTableModel().getReferredPrimitive(row);
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected class DblClickHandler extends MouseAdapter {
|
---|
79 |
|
---|
80 | protected void setSelection(MouseEvent e) {
|
---|
81 | int row = rowAtPoint(e.getPoint());
|
---|
82 | if (row < 0) return;
|
---|
83 | OsmPrimitive primitive = getPrimitiveInLayer(row, layer);
|
---|
84 | if (layer != null && primitive != null) {
|
---|
85 | layer.data.setSelected(primitive.getPrimitiveId());
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected void addSelection(MouseEvent e) {
|
---|
90 | int row = rowAtPoint(e.getPoint());
|
---|
91 | if (row < 0) return;
|
---|
92 | OsmPrimitive primitive = getPrimitiveInLayer(row, layer);
|
---|
93 | getSelectionModel().addSelectionInterval(row, row);
|
---|
94 | if (layer != null && primitive != null) {
|
---|
95 | layer.data.addSelected(primitive.getPrimitiveId());
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public void mouseClicked(MouseEvent e) {
|
---|
101 | if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() > 1) {
|
---|
102 | if (e.isControlDown()) {
|
---|
103 | addSelection(e);
|
---|
104 | } else {
|
---|
105 | setSelection(e);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|