1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.opendata.core.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.awt.event.MouseAdapter;
|
---|
10 | import java.awt.event.MouseEvent;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.swing.JPopupMenu;
|
---|
15 | import javax.swing.JTree;
|
---|
16 | import javax.swing.tree.DefaultTreeModel;
|
---|
17 | import javax.swing.tree.TreeModel;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
20 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
21 | import org.openstreetmap.josm.gui.SideButton;
|
---|
22 | import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
|
---|
23 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
24 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
---|
25 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
---|
26 | import org.openstreetmap.josm.plugins.opendata.core.OdConstants;
|
---|
27 | import org.openstreetmap.josm.plugins.opendata.core.layers.OdDataLayer;
|
---|
28 | import org.openstreetmap.josm.plugins.opendata.core.layers.OdLayer;
|
---|
29 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
30 |
|
---|
31 | @SuppressWarnings("serial")
|
---|
32 | public class OdDialog extends ToggleDialog implements ActiveLayerChangeListener {
|
---|
33 |
|
---|
34 | //private final SideButton selectButton;
|
---|
35 | private final SideButton downloadButton;
|
---|
36 | private final SideButton diffButton;
|
---|
37 | private final SideButton toolsButton;
|
---|
38 |
|
---|
39 | private final List<SideButton> buttons;
|
---|
40 |
|
---|
41 | private final TreeModel treeModel;
|
---|
42 |
|
---|
43 | private OdDataLayer dataLayer;
|
---|
44 |
|
---|
45 | private class DownloadAction extends JosmAction {
|
---|
46 | DownloadAction() {
|
---|
47 | super(marktr("Download"), "download", tr("Download OSM data corresponding to the current data set."), null, false);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public void actionPerformed(ActionEvent e) {
|
---|
52 | if (MainApplication.getLayerManager().getEditLayer() instanceof OdLayer) {
|
---|
53 | dataLayer.downloadOsmData();
|
---|
54 | diffButton.setEnabled(dataLayer.osmLayer != null);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | private static class SelectAction extends JosmAction {
|
---|
60 | SelectAction() {
|
---|
61 | super(marktr("Select"), "dialogs/select",
|
---|
62 | tr("Set the selected elements on the map to the selected items in the list above."), null, false);
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public void actionPerformed(ActionEvent e) {
|
---|
67 | // TODO
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | private static class DiffAction extends JosmAction {
|
---|
72 | DiffAction() {
|
---|
73 | super(marktr("Diff"), "dialogs/diff", tr("Perform diff between current data set and existing OSM data."), null, false);
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public void actionPerformed(ActionEvent e) {
|
---|
78 | // TODO
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | private static class ToolsAction extends JosmAction {
|
---|
83 | ToolsAction() {
|
---|
84 | super(marktr("Tools"), "dialogs/utils", tr("Open tools menu for this data."), null, false);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public void actionPerformed(ActionEvent e) {
|
---|
89 | // Done via mouseListener
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public OdDialog() {
|
---|
94 | super("OpenData", OdConstants.ICON_CORE_24, tr("Open the OpenData window."),
|
---|
95 | Shortcut.registerShortcut("subwindow:opendata", tr("Toggle: {0}", "OpenData"),
|
---|
96 | KeyEvent.VK_A, Shortcut.ALT_CTRL_SHIFT), 150, false, OdPreferenceSetting.class);
|
---|
97 |
|
---|
98 | this.downloadButton = new SideButton(new DownloadAction());
|
---|
99 | this.diffButton = new SideButton(new DiffAction());
|
---|
100 | this.toolsButton = new SideButton(new ToolsAction());
|
---|
101 | this.buttons = Arrays.asList(/*selectButton =*/ new SideButton(new SelectAction()),
|
---|
102 | this.downloadButton, this.diffButton, this.toolsButton);
|
---|
103 |
|
---|
104 |
|
---|
105 | this.toolsButton.addMouseListener(new MouseAdapter() {
|
---|
106 | @Override
|
---|
107 | public void mousePressed(MouseEvent e) {
|
---|
108 | if (MainApplication.getLayerManager().getEditLayer() instanceof OdLayer) {
|
---|
109 | JPopupMenu popup = new JPopupMenu();
|
---|
110 | for (JosmAction tool : dataLayer.handler.getTools()) {
|
---|
111 | popup.add(tool);
|
---|
112 | }
|
---|
113 | popup.show(e.getComponent(), e.getX(), e.getY());
|
---|
114 | }
|
---|
115 | }
|
---|
116 | });
|
---|
117 |
|
---|
118 | disableAllButtons();
|
---|
119 |
|
---|
120 | this.treeModel = new DefaultTreeModel(null); // TODO: treeNode
|
---|
121 | this.dataLayer = null;
|
---|
122 |
|
---|
123 | createLayout(new JTree(treeModel), true, buttons);
|
---|
124 |
|
---|
125 | MainApplication.getLayerManager().addActiveLayerChangeListener(this);
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void disableAllButtons() {
|
---|
129 | for (SideButton button : buttons) {
|
---|
130 | button.setEnabled(false);
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
136 | Layer newLayer = MainApplication.getLayerManager().getActiveLayer();
|
---|
137 | if (newLayer instanceof OdLayer) {
|
---|
138 | dataLayer = ((OdLayer) newLayer).getDataLayer();
|
---|
139 | } else {
|
---|
140 | dataLayer = null;
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (dataLayer != null) {
|
---|
144 | if (dataLayer.osmLayer == null) {
|
---|
145 | downloadButton.setEnabled(true);
|
---|
146 | }
|
---|
147 | toolsButton.setEnabled(dataLayer.handler != null && !dataLayer.handler.getTools().isEmpty());
|
---|
148 | } else {
|
---|
149 | disableAllButtons();
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | @Override
|
---|
154 | public void destroy() {
|
---|
155 | super.destroy();
|
---|
156 | MainApplication.getLayerManager().removeActiveLayerChangeListener(this);
|
---|
157 | }
|
---|
158 |
|
---|
159 | public OdDataLayer getDataLayer() {
|
---|
160 | return dataLayer;
|
---|
161 | }
|
---|
162 | }
|
---|