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.ListSelectionModel;
|
---|
9 | import javax.swing.SwingUtilities;
|
---|
10 | import javax.swing.table.TableColumnModel;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.actions.ZoomToAction;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Table displaying OSM primitives.
|
---|
19 | * @since 5297
|
---|
20 | */
|
---|
21 | public abstract class OsmPrimitivesTable extends JosmTable {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * the data layer in whose context primitives are edited in this table
|
---|
25 | */
|
---|
26 | private transient OsmDataLayer layer;
|
---|
27 |
|
---|
28 | /** the popup menu */
|
---|
29 | private JPopupMenu popupMenu;
|
---|
30 | private ZoomToAction zoomToAction;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructs a new {@code OsmPrimitivesTable}.
|
---|
34 | * @param dm table model
|
---|
35 | * @param cm column model
|
---|
36 | * @param sm selection model
|
---|
37 | */
|
---|
38 | public OsmPrimitivesTable(OsmPrimitivesTableModel dm, TableColumnModel cm, ListSelectionModel sm) {
|
---|
39 | super(dm, cm, sm);
|
---|
40 | addMouseListener(new PopupMenuLauncher(getPopUpMenu()));
|
---|
41 | addMouseListener(new DblClickHandler());
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Returns the table model.
|
---|
46 | * @return the table model
|
---|
47 | */
|
---|
48 | public OsmPrimitivesTableModel getOsmPrimitivesTableModel() {
|
---|
49 | return (OsmPrimitivesTableModel) getModel();
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Returns the data layer.
|
---|
54 | * @return the data layer
|
---|
55 | */
|
---|
56 | public final OsmDataLayer getLayer() {
|
---|
57 | return layer;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Sets the data layer.
|
---|
62 | * @param layer the data layer
|
---|
63 | */
|
---|
64 | public final void setLayer(OsmDataLayer layer) {
|
---|
65 | this.layer = layer;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Replies the popup menu for this table
|
---|
70 | *
|
---|
71 | * @return the popup menu
|
---|
72 | */
|
---|
73 | protected final JPopupMenu getPopUpMenu() {
|
---|
74 | if (popupMenu == null) {
|
---|
75 | popupMenu = buildPopupMenu();
|
---|
76 | }
|
---|
77 | return popupMenu;
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected abstract ZoomToAction buildZoomToAction();
|
---|
81 |
|
---|
82 | protected JPopupMenu buildPopupMenu() {
|
---|
83 | JPopupMenu menu = new JPopupMenu();
|
---|
84 | zoomToAction = buildZoomToAction();
|
---|
85 | getSelectionModel().addListSelectionListener(zoomToAction);
|
---|
86 | menu.add(zoomToAction);
|
---|
87 | return menu;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Adds all registered listeners by this table
|
---|
92 | * @see #unregisterListeners()
|
---|
93 | * @since 10454
|
---|
94 | */
|
---|
95 | public void registerListeners() {
|
---|
96 | Main.getLayerManager().addLayerChangeListener(zoomToAction);
|
---|
97 | Main.getLayerManager().addActiveLayerChangeListener(zoomToAction);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Removes all registered listeners by this table
|
---|
102 | * @since 10454
|
---|
103 | */
|
---|
104 | public void unregisterListeners() {
|
---|
105 | Main.getLayerManager().removeLayerChangeListener(zoomToAction);
|
---|
106 | Main.getLayerManager().removeActiveLayerChangeListener(zoomToAction);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Returns primitive at the specified row.
|
---|
111 | * @param row table row
|
---|
112 | * @param layer unused in this implementation, can be useful for subclasses
|
---|
113 | * @return primitive at the specified row
|
---|
114 | */
|
---|
115 | public OsmPrimitive getPrimitiveInLayer(int row, OsmDataLayer layer) {
|
---|
116 | return getOsmPrimitivesTableModel().getReferredPrimitive(row);
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected class DblClickHandler extends MouseAdapter {
|
---|
120 |
|
---|
121 | protected void setSelection(MouseEvent e) {
|
---|
122 | int row = rowAtPoint(e.getPoint());
|
---|
123 | if (row < 0) return;
|
---|
124 | OsmPrimitive primitive = getPrimitiveInLayer(row, layer);
|
---|
125 | if (layer != null && primitive != null) {
|
---|
126 | layer.data.setSelected(primitive.getPrimitiveId());
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected void addSelection(MouseEvent e) {
|
---|
131 | int row = rowAtPoint(e.getPoint());
|
---|
132 | if (row < 0) return;
|
---|
133 | OsmPrimitive primitive = getPrimitiveInLayer(row, layer);
|
---|
134 | getSelectionModel().addSelectionInterval(row, row);
|
---|
135 | if (layer != null && primitive != null) {
|
---|
136 | layer.data.addSelected(primitive.getPrimitiveId());
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | public void mouseClicked(MouseEvent e) {
|
---|
142 | if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() > 1) {
|
---|
143 | if (e.isControlDown()) {
|
---|
144 | addSelection(e);
|
---|
145 | } else {
|
---|
146 | setSelection(e);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|