[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[283] | 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[2017] | 6 | import java.applet.AppletContext;
|
---|
[431] | 7 | import java.applet.AppletStub;
|
---|
[283] | 8 | import java.awt.GridBagLayout;
|
---|
| 9 | import java.awt.event.ActionEvent;
|
---|
| 10 | import java.awt.event.KeyEvent;
|
---|
[431] | 11 | import java.io.File;
|
---|
[2053] | 12 | import java.io.IOException;
|
---|
[431] | 13 | import java.net.URL;
|
---|
[283] | 14 | import java.util.Arrays;
|
---|
| 15 | import java.util.Collection;
|
---|
| 16 | import java.util.HashMap;
|
---|
| 17 | import java.util.LinkedList;
|
---|
| 18 | import java.util.Map;
|
---|
| 19 |
|
---|
| 20 | import javax.swing.JApplet;
|
---|
[431] | 21 | import javax.swing.JFrame;
|
---|
[283] | 22 | import javax.swing.JLabel;
|
---|
| 23 | import javax.swing.JOptionPane;
|
---|
| 24 | import javax.swing.JPanel;
|
---|
| 25 | import javax.swing.JPasswordField;
|
---|
| 26 | import javax.swing.JTextField;
|
---|
| 27 |
|
---|
| 28 | import org.openstreetmap.josm.Main;
|
---|
| 29 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
| 30 | import org.openstreetmap.josm.data.ServerSidePreferences;
|
---|
| 31 | import org.openstreetmap.josm.tools.GBC;
|
---|
[1084] | 32 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[283] | 33 |
|
---|
| 34 | public class MainApplet extends JApplet {
|
---|
| 35 |
|
---|
[1169] | 36 | public static final class UploadPreferencesAction extends JosmAction {
|
---|
| 37 | public UploadPreferencesAction() {
|
---|
| 38 | super(tr("Upload Preferences"), "upload-preferences", tr("Upload the current preferences to the server"),
|
---|
[2053] | 39 | Shortcut.registerShortcut("applet:uploadprefs", tr("Upload Preferences"), KeyEvent.VK_U, Shortcut.GROUP_HOTKEY), true);
|
---|
[283] | 40 | }
|
---|
[1169] | 41 | public void actionPerformed(ActionEvent e) {
|
---|
| 42 | ((ServerSidePreferences)Main.pref).upload();
|
---|
| 43 | }
|
---|
[283] | 44 | }
|
---|
| 45 |
|
---|
| 46 | private final class MainCaller extends Main {
|
---|
[1169] | 47 | private MainCaller() {
|
---|
| 48 | setContentPane(contentPane);
|
---|
| 49 | setJMenuBar(menu);
|
---|
| 50 | setBounds(bounds);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
[283] | 53 |
|
---|
[1169] | 54 | private final static String[][] paramInfo = {
|
---|
| 55 | {"username", tr("string"), tr("Name of the user.")},
|
---|
| 56 | {"password", tr("string"), tr("OSM Password.")},
|
---|
| 57 | {"geometry", tr("string"), tr("Resize the applet to the given geometry (format: WIDTHxHEIGHT)")},
|
---|
[1195] | 58 | {"download", tr("string;string;..."), tr("Download each. Can be x1,y1,x2,y2 an URL containing lat=y&lon=x&zoom=z or a filename")},
|
---|
| 59 | {"downloadgps", tr("string;string;..."), tr("Download each as raw gps. Can be x1,y1,x2,y2 an URL containing lat=y&lon=x&zoom=z or a filename")},
|
---|
| 60 | {"selection", tr("string;string;..."), tr("Add each to the initial selection. Can be a google-like search string or an URL which returns osm-xml")},
|
---|
[1169] | 61 | {"reset-preferences", tr("any"),tr("If specified, reset the configuration instead of reading it.")}
|
---|
| 62 | };
|
---|
[283] | 63 |
|
---|
[1169] | 64 | private Map<String, Collection<String>> args = new HashMap<String, Collection<String>>();
|
---|
[283] | 65 |
|
---|
[1169] | 66 | @Override public String[][] getParameterInfo() {
|
---|
| 67 | return paramInfo;
|
---|
| 68 | }
|
---|
[283] | 69 |
|
---|
[1169] | 70 | @Override public void init() {
|
---|
| 71 | for (String[] s : paramInfo) {
|
---|
| 72 | Collection<String> p = readParameter(s[0], args.get(s[0]));
|
---|
[2053] | 73 | if (p != null) {
|
---|
[1169] | 74 | args.put(s[0], p);
|
---|
[2053] | 75 | }
|
---|
[1169] | 76 | }
|
---|
| 77 | if (!args.containsKey("geometry") && getParameter("width") != null && getParameter("height") != null) {
|
---|
| 78 | args.put("geometry", Arrays.asList(new String[]{getParameter("width")+"x"+getParameter("height")}));
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
[283] | 81 |
|
---|
[1169] | 82 | @Override public void start() {
|
---|
| 83 | String username = args.containsKey("username") ? args.get("username").iterator().next() : null;
|
---|
| 84 | String password = args.containsKey("password") ? args.get("password").iterator().next() : null;
|
---|
| 85 | if (username == null || password == null) {
|
---|
| 86 | JPanel p = new JPanel(new GridBagLayout());
|
---|
| 87 | p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,20,0));
|
---|
| 88 | JTextField user = new JTextField(username == null ? "" : username);
|
---|
| 89 | p.add(user, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
| 90 | p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,20,0));
|
---|
| 91 | JPasswordField pass = new JPasswordField(password == null ? "" : password);
|
---|
| 92 | p.add(pass, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
| 93 | JOptionPane.showMessageDialog(null, p);
|
---|
| 94 | username = user.getText();
|
---|
| 95 | password = new String(pass.getPassword());
|
---|
| 96 | args.put("password", Arrays.asList(new String[]{password}));
|
---|
| 97 | }
|
---|
[283] | 98 |
|
---|
[1169] | 99 | Main.applet = true;
|
---|
| 100 | Main.pref = new ServerSidePreferences(getCodeBase());
|
---|
| 101 | ((ServerSidePreferences)Main.pref).download(username, password);
|
---|
| 102 | Main.preConstructorInit(args);
|
---|
| 103 | Main.parent = this;
|
---|
[1849] | 104 |
|
---|
| 105 | // initialize the plaform hook, and
|
---|
| 106 | Main.determinePlatformHook();
|
---|
| 107 | // call the really early hook before we anything else
|
---|
| 108 | Main.platform.preStartupHook();
|
---|
| 109 |
|
---|
[1169] | 110 | new MainCaller().postConstructorProcessCmdLine(args);
|
---|
[1023] | 111 |
|
---|
[1169] | 112 | MainMenu m = Main.main.menu; // shortcut
|
---|
[283] | 113 |
|
---|
[1169] | 114 | // remove offending stuff from JOSM (that would break the SecurityManager)
|
---|
| 115 | m.remove(m.fileMenu);
|
---|
| 116 | m.editMenu.add(new UploadPreferencesAction());
|
---|
| 117 | m.openFile.setEnabled(false);
|
---|
| 118 | m.exit.setEnabled(false);
|
---|
| 119 | m.save.setEnabled(false);
|
---|
| 120 | m.saveAs.setEnabled(false);
|
---|
| 121 | m.gpxExport.setEnabled(false);
|
---|
| 122 | }
|
---|
[283] | 123 |
|
---|
[1169] | 124 | private Collection<String> readParameter(String s, Collection<String> v) {
|
---|
| 125 | String param = getParameter(s);
|
---|
| 126 | if (param != null) {
|
---|
[2053] | 127 | if (v == null) {
|
---|
[1169] | 128 | v = new LinkedList<String>();
|
---|
[2053] | 129 | }
|
---|
[1169] | 130 | v.addAll(Arrays.asList(param.split(";")));
|
---|
| 131 | }
|
---|
| 132 | return v;
|
---|
| 133 | }
|
---|
[431] | 134 |
|
---|
[1169] | 135 | public static void main(String[] args) {
|
---|
[1179] | 136 | final JFrame frame = new JFrame("Java OpenStreetMap Editor");
|
---|
[1169] | 137 | MainApplet applet = new MainApplet();
|
---|
| 138 | applet.setStub(new AppletStub() {
|
---|
| 139 | public void appletResize(int w, int h) {
|
---|
| 140 | frame.setSize(w, h);
|
---|
| 141 | }
|
---|
[431] | 142 |
|
---|
[1169] | 143 | public AppletContext getAppletContext() {
|
---|
| 144 | return null;
|
---|
| 145 | }
|
---|
[431] | 146 |
|
---|
[1169] | 147 | public URL getCodeBase() {
|
---|
| 148 | try {
|
---|
| 149 | return new File(".").toURI().toURL();
|
---|
| 150 | } catch (Exception e) {
|
---|
| 151 | e.printStackTrace();
|
---|
| 152 | return null;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
[431] | 155 |
|
---|
[1169] | 156 | public URL getDocumentBase() {
|
---|
| 157 | return getCodeBase();
|
---|
| 158 | }
|
---|
[431] | 159 |
|
---|
[1169] | 160 | public String getParameter(String k) {
|
---|
| 161 | return null;
|
---|
| 162 | }
|
---|
[431] | 163 |
|
---|
[1169] | 164 | public boolean isActive() {
|
---|
| 165 | return true;
|
---|
| 166 | }
|
---|
| 167 | });
|
---|
| 168 | applet.init();
|
---|
| 169 | applet.start();
|
---|
| 170 | frame.setContentPane(applet);
|
---|
| 171 | frame.setVisible(true);
|
---|
| 172 | }
|
---|
[283] | 173 | }
|
---|