source: subversion/applications/editors/josm/plugins/sds/src/org/openstreetmap/hot/sds/SdsMenu.java

Last change on this file was 34470, checked in by donvip, 8 months ago

update to JOSM 14153

File size: 5.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.hot.sds;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Dimension;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.lang.reflect.Method;
11
12import javax.swing.BorderFactory;
13import javax.swing.JMenu;
14import javax.swing.JMenuItem;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17import javax.swing.JScrollPane;
18import javax.swing.JTextArea;
19
20import org.openstreetmap.josm.actions.JosmAction;
21import org.openstreetmap.josm.gui.MainApplication;
22import org.openstreetmap.josm.gui.MainMenu;
23import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
24import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
25import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
26import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
27import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
28import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
29import org.openstreetmap.josm.gui.layer.OsmDataLayer;
30import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
31import org.openstreetmap.josm.tools.Logging;
32
33@SuppressWarnings("serial")
34public class SdsMenu extends JMenu implements LayerChangeListener, ActiveLayerChangeListener {
35
36    private JMenuItem saveItem;
37    private JMenuItem loadItem;
38    private JMenuItem prefsItem;
39    private JMenuItem aboutItem;
40    private JMenu menu;
41
42    public SdsMenu(final SeparateDataStorePlugin thePlugin) {
43        MainMenu mm = MainApplication.getMenu();
44        menu = mm.addMenu("SDS", tr("SDS"), KeyEvent.VK_S, mm.getDefaultMenuPos(), null);
45        saveItem = new JMenuItem(new SdsSaveAction());
46        menu.add(saveItem);
47        loadItem = new JMenuItem(new SdsLoadAction(thePlugin));
48        menu.add(loadItem);
49        menu.addSeparator();
50        prefsItem = new JMenuItem(new SdsPreferencesAction());
51        menu.add(prefsItem);
52        menu.addSeparator();
53        aboutItem = new JMenuItem(new SdsAboutAction());
54        menu.add(aboutItem);
55
56        MainApplication.getLayerManager().addLayerChangeListener(this);
57        MainApplication.getLayerManager().addActiveLayerChangeListener(this);
58        setEnabledState();
59    }
60
61    void setEnabledState() {
62        boolean en = MainApplication.getLayerManager().getActiveLayer() instanceof OsmDataLayer;
63        loadItem.setEnabled(en);
64        saveItem.setEnabled(en);
65    }
66
67    @Override
68    public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
69        setEnabledState();
70    }
71
72    @Override
73    public void layerOrderChanged(LayerOrderChangeEvent e) { }
74
75    @Override
76    public void layerAdded(LayerAddEvent e) {
77        setEnabledState();
78    }
79
80    @Override
81    public void layerRemoving(LayerRemoveEvent e) {
82        setEnabledState();
83    }
84
85    private static class SdsAboutAction extends JosmAction {
86
87        SdsAboutAction() {
88            super(tr("About"), "sds", tr("Information about SDS."), null, true);
89        }
90
91        @Override
92        public void actionPerformed(ActionEvent e) {
93            JPanel about = new JPanel();
94
95            JTextArea l = new JTextArea();
96            l.setLineWrap(true);
97            l.setWrapStyleWord(true);
98            l.setEditable(false);
99            l.setText(
100                "Separate Data Store\n\nThis plugin provides access to a \"Separate Data Store\" server. " +
101                "Whenever data is loaded from the OSM API, " +
102                "it queries the SDS for additional tags that have been stored for the objects just loaded, " +
103                "and adds these tags. When you upload data to JOSM, SDS tags will again be separated and, " +
104                "instead of sending them to OSM, they will be uploaded to SDS." +
105                "\n\n" +
106                "This depends on SDS tags starting with a special prefix, which can be configured in the SDS preferences." +
107                "\n\n" +
108                "Using the SDS server will usually require an account to be set up there, which is completely independent of your OSM account.");
109
110            l.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
111            l.setOpaque(false);
112            l.setPreferredSize(new Dimension(500, 300));
113            JScrollPane sp = new JScrollPane(l);
114            sp.setBorder(null);
115            sp.setOpaque(false);
116
117            about.add(sp);
118
119            about.setPreferredSize(new Dimension(500, 300));
120
121            JOptionPane.showMessageDialog(MainApplication.getMainFrame(), about, tr("About SDS..."),
122                    JOptionPane.INFORMATION_MESSAGE, null);
123        }
124    }
125
126    private static final class SdsPreferencesAction extends JosmAction implements Runnable {
127
128        private SdsPreferencesAction() {
129            super(tr("Preferences..."), "preference", tr("Open a preferences dialog for SDS."),
130                    null, true);
131            putValue("help", ht("/Action/Preferences"));
132        }
133
134        /**
135         * Launch the preferences dialog.
136         */
137        @Override
138        public void actionPerformed(ActionEvent e) {
139            run();
140        }
141
142        @Override
143        public void run() {
144            PreferenceDialog pd = new PreferenceDialog(MainApplication.getMainFrame());
145            // unusual reflection mechanism to cater for older JOSM versions where
146            // the selectPreferencesTabByName method was not public
147            try {
148                Method sptbn = pd.getClass().getMethod("selectPreferencesTabByName", String.class);
149                sptbn.invoke(pd, "sds");
150            } catch (Exception ex) {
151                Logging.trace(ex);
152            }
153            pd.setVisible(true);
154        }
155    }
156}
Note: See TracBrowser for help on using the repository browser.