1 | package org.openstreetmap.josm.plugins.ywms;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.event.*;
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.net.URL;
|
---|
9 |
|
---|
10 | import javax.swing.*;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
14 | import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
|
---|
15 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
16 | import org.openstreetmap.josm.plugins.ywms.Util.Version;
|
---|
17 | import org.openstreetmap.josm.tools.GBC;
|
---|
18 | import org.openstreetmap.josm.tools.I18n;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Preference settings for the YWMS plugin
|
---|
22 | *
|
---|
23 | * @author frsantos
|
---|
24 | */
|
---|
25 | public class YWMSPreferenceSetting implements PreferenceSetting
|
---|
26 | {
|
---|
27 | /** WMS server name */
|
---|
28 | public static final String WMS_NAME = "Yahoo";
|
---|
29 | /** WMS URL server parameters */
|
---|
30 | public static final String WMS_URL_PARAMS = "/ymap?request=GetMap&format=image/jpeg";
|
---|
31 |
|
---|
32 | /** Firefox path text field */
|
---|
33 | private JTextField firefox = new JTextField(10);
|
---|
34 | /** Server port text field */
|
---|
35 | private JTextField port = new JTextField(10);
|
---|
36 | /** Firefox profile text field */
|
---|
37 | private JTextField profile = new JTextField(10);
|
---|
38 |
|
---|
39 | public void addGui( final PreferenceDialog gui )
|
---|
40 | {
|
---|
41 | firefox.setToolTipText(tr("<html>Path to firefox executable.<br>" +
|
---|
42 | "The Firefox profile used in this plugin <b>must</b> be configured with the javascript 'dump' method,<br>" +
|
---|
43 | "that can be activated with the property 'browser.dom.window.dump.enabled=true' in the about:config page.</html>"));
|
---|
44 | port.setToolTipText(tr("<html>The port that the server will use to listen WMS requests<br>" +
|
---|
45 | "The WMS plugin need to be configured to use this port"));
|
---|
46 | profile.setToolTipText(tr("<html>Name of the profile.<br>" +
|
---|
47 | "This profile is used to avoid nag firefox screens asking you to resume failed sessions.<br>" +
|
---|
48 | "Just set the selected profile as not default in the profile selection window and configure to not ask<br>" +
|
---|
49 | "about failed sessions with 'browser.sessionstore.resume_from_crash=false' in the about:config page"
|
---|
50 | ));
|
---|
51 |
|
---|
52 | Version ver = Util.getVersion();
|
---|
53 | String description = tr("A WMS server for Yahoo imagery based on Firefox.");
|
---|
54 | if( ver != null )
|
---|
55 | description += "<br><br>" + tr("Version: {0}<br>Last change at {1}", ver.revision, ver.time);
|
---|
56 | JPanel ywms = gui.createPreferenceTab("yahoo.gif", I18n.tr("Yahoo! WMS server"), description + I18n.tr("Settings for the Yahoo! imagery server."));
|
---|
57 | ywms.add(new JLabel(tr("YWMS options")), GBC.eol().insets(0,5,0,0));
|
---|
58 |
|
---|
59 | ywms.add(new JLabel(tr("Firefox executable")), GBC.std().insets(10,5,5,0));
|
---|
60 | ywms.add(firefox, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
|
---|
61 |
|
---|
62 | ywms.add(new JLabel(tr("Firefox profile")), GBC.std().insets(10,5,5,0));
|
---|
63 | ywms.add(profile, GBC.std().insets(0,5,0,0).fill(GBC.HORIZONTAL));
|
---|
64 | JButton create = new JButton(tr("Create"));
|
---|
65 | ywms.add(create, GBC.eol().insets(5,0,0,0).fill(GBC.EAST));
|
---|
66 | create.addActionListener(new ProfileCreatorActionListener());
|
---|
67 |
|
---|
68 | ywms.add(new JLabel(tr("Server port")), GBC.std().insets(10,5,5,0));
|
---|
69 | ywms.add(port, GBC.eol().insets(0,5,0,0).fill(GBC.HORIZONTAL));
|
---|
70 | ywms.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
|
---|
71 | WMSConfigurationActionListener configurationActionListener = new WMSConfigurationActionListener();
|
---|
72 | port.addActionListener(configurationActionListener);
|
---|
73 | port.addFocusListener(configurationActionListener);
|
---|
74 |
|
---|
75 | firefox.setText(Main.pref.get("ywms.firefox", "firefox"));
|
---|
76 | profile.setText(Main.pref.get("ywms.profile"));
|
---|
77 | port.setText(Main.pref.get("ywms.port", "8000"));
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void ok()
|
---|
81 | {
|
---|
82 | Main.pref.put("ywms.firefox", firefox.getText());
|
---|
83 | Main.pref.put("ywms.profile", profile.getText());
|
---|
84 |
|
---|
85 | String oldPort = Main.pref.get("ywms.port");
|
---|
86 | Main.pref.put("ywms.port", port.getText());
|
---|
87 | if( !oldPort.equals(port.getText()) )
|
---|
88 | {
|
---|
89 | YWMSPlugin plugin = (YWMSPlugin)Util.getPlugin(YWMSPlugin.class);
|
---|
90 | plugin.restartServer();
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * ActionListener for the configuration of WMS plugin
|
---|
96 | * @author frsantos
|
---|
97 | */
|
---|
98 | private final class WMSConfigurationActionListener implements ActionListener, FocusListener
|
---|
99 | {
|
---|
100 | /** If the action is already handled */
|
---|
101 | boolean alreadyHandled = false;
|
---|
102 | public void actionPerformed(ActionEvent e)
|
---|
103 | {
|
---|
104 | if( !alreadyHandled )
|
---|
105 | configureWMSPluginPreferences();
|
---|
106 | alreadyHandled = true;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public void focusGained(FocusEvent e)
|
---|
110 | {
|
---|
111 | alreadyHandled = false;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void focusLost(FocusEvent e)
|
---|
115 | {
|
---|
116 | if( !alreadyHandled )
|
---|
117 | configureWMSPluginPreferences();
|
---|
118 | alreadyHandled = true;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * ActionListener for the creation of a Mozilla profile
|
---|
124 | * @author frsantos
|
---|
125 | */
|
---|
126 | private final class ProfileCreatorActionListener implements ActionListener
|
---|
127 | {
|
---|
128 | public void actionPerformed(ActionEvent e)
|
---|
129 | {
|
---|
130 | String profileName = profile.getText();
|
---|
131 | if( profileName == null || profileName.length() == 0)
|
---|
132 | {
|
---|
133 | JOptionPane.showMessageDialog(Main.parent, tr("Please name the profile you want to create."));
|
---|
134 | return;
|
---|
135 | }
|
---|
136 |
|
---|
137 | try
|
---|
138 | {
|
---|
139 | PleaseWaitRunnable createProfileTask = new PleaseWaitRunnable(tr("Creating profile"))
|
---|
140 | {
|
---|
141 | Process process = null;
|
---|
142 | @Override
|
---|
143 | protected void realRun() throws IOException
|
---|
144 | {
|
---|
145 | process = GeckoSupport.createProfile(firefox.getText(), profile.getText());
|
---|
146 | try {
|
---|
147 | process.waitFor();
|
---|
148 | }
|
---|
149 | catch (InterruptedException e)
|
---|
150 | {
|
---|
151 | IOException ioe = new IOException();
|
---|
152 | ioe.initCause(e);
|
---|
153 | throw ioe;
|
---|
154 | }
|
---|
155 |
|
---|
156 | String configFile = new File(Util.getPluginDir(), "config.html").toURL().toString();
|
---|
157 | GeckoSupport.browse(firefox.getText(), profile.getText(), configFile, false);
|
---|
158 | configureWMSPluginPreferences();
|
---|
159 | }
|
---|
160 |
|
---|
161 | @Override
|
---|
162 | protected void finish() {}
|
---|
163 |
|
---|
164 | @Override
|
---|
165 | protected void cancel()
|
---|
166 | {
|
---|
167 | if( process != null )
|
---|
168 | process.destroy();
|
---|
169 | }
|
---|
170 | };
|
---|
171 | Main.worker.execute(createProfileTask);
|
---|
172 | }
|
---|
173 | catch(Exception e2)
|
---|
174 | {
|
---|
175 |
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Configures WMSPlugin preferences with a server "Yahoo" pointing to YWMS
|
---|
182 | */
|
---|
183 | private void configureWMSPluginPreferences()
|
---|
184 | {
|
---|
185 | try
|
---|
186 | {
|
---|
187 | PreferenceSetting wmsSetting = null;
|
---|
188 | for( PreferenceSetting setting : PreferenceDialog.settings)
|
---|
189 | {
|
---|
190 | if( setting.getClass().getName() == "wmsplugin.WMSPreferenceEditor" )
|
---|
191 | {
|
---|
192 | wmsSetting = setting;
|
---|
193 | break;
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | if( wmsSetting == null )
|
---|
198 | return;
|
---|
199 |
|
---|
200 | int portNumber = Integer.parseInt(port.getText());
|
---|
201 | String strUrl = (String)wmsSetting.getClass().getMethod("getServerUrl", String.class).invoke(wmsSetting, WMS_NAME);
|
---|
202 | if( strUrl == null )
|
---|
203 | strUrl = new URL("http", "localhost", portNumber, WMS_URL_PARAMS).toString();
|
---|
204 | else
|
---|
205 | {
|
---|
206 | URL oldUrl = new URL(strUrl);
|
---|
207 | strUrl = new URL("http", oldUrl.getHost(), portNumber, WMS_URL_PARAMS).toString();
|
---|
208 | }
|
---|
209 | wmsSetting.getClass().getMethod("setServerUrl", String.class, String.class).invoke(wmsSetting, WMS_NAME, strUrl);
|
---|
210 | } catch (NoSuchMethodException e) {
|
---|
211 | } catch (NumberFormatException nfe) {
|
---|
212 | }
|
---|
213 | catch (Exception e)
|
---|
214 | {
|
---|
215 | e.printStackTrace();
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|