source: josm/trunk/src/org/openstreetmap/josm/gui/MainApplet.java @ 5241

Revision 4982, 7.2 KB checked in by stoecker, 3 months ago (diff)

see #7226 - patch by akks (fixed a bit) - fix shortcut deprecations

  • Property svn:eol-style set to native
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.applet.AppletContext;
7import java.applet.AppletStub;
8import java.awt.GridBagLayout;
9import java.awt.event.ActionEvent;
10import java.awt.event.KeyEvent;
11import java.io.File;
12import java.net.URL;
13import java.util.Arrays;
14import java.util.Collection;
15import java.util.HashMap;
16import java.util.LinkedList;
17import java.util.Map;
18
19import javax.swing.JApplet;
20import javax.swing.JFrame;
21import javax.swing.JLabel;
22import javax.swing.JOptionPane;
23import javax.swing.JPanel;
24import javax.swing.JPasswordField;
25import javax.swing.JTextField;
26
27import org.openstreetmap.josm.Main;
28import org.openstreetmap.josm.actions.JosmAction;
29import org.openstreetmap.josm.data.ServerSidePreferences;
30import org.openstreetmap.josm.tools.GBC;
31import org.openstreetmap.josm.tools.I18n;
32import org.openstreetmap.josm.tools.Shortcut;
33
34public class MainApplet extends JApplet {
35
36    final static JFrame frame = new JFrame("Java OpenStreetMap Editor");
37
38    public static final class UploadPreferencesAction extends JosmAction {
39        public UploadPreferencesAction() {
40            super(tr("Upload Preferences"), "upload-preferences", tr("Upload the current preferences to the server"),
41                    Shortcut.registerShortcut("applet:uploadprefs", tr("Upload Preferences"), KeyEvent.VK_U, Shortcut.ALT_CTRL_SHIFT), true);
42        }
43        public void actionPerformed(ActionEvent e) {
44            ((ServerSidePreferences)Main.pref).upload();
45        }
46    }
47
48    private final class MainCaller extends Main {
49        private MainCaller() {
50            setContentPane(contentPanePrivate);
51            setJMenuBar(menu);
52        }
53    }
54
55    private final static String[][] paramInfo = {
56        {"username", tr("string"), tr("Name of the user.")},
57        {"password", tr("string"), tr("OSM Password.")},
58        {"geometry", tr("string"), tr("Resize the applet to the given geometry (format: WIDTHxHEIGHT)")},
59        {"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")},
60        {"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")},
61        {"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")},
62        {"reset-preferences", tr("any"),tr("If specified, reset the configuration instead of reading it.")}
63    };
64
65    private Map<String, Collection<String>> args = new HashMap<String, Collection<String>>();
66
67    @Override public String[][] getParameterInfo() {
68        return paramInfo;
69    }
70
71    @Override public void init() {
72        for (String[] s : paramInfo) {
73            Collection<String> p = readParameter(s[0], args.get(s[0]));
74            if (p != null) {
75                args.put(s[0], p);
76            }
77        }
78        if (!args.containsKey("geometry") && getParameter("width") != null && getParameter("height") != null) {
79            args.put("geometry", Arrays.asList(new String[]{getParameter("width")+"x"+getParameter("height")}));
80        }
81    }
82
83    @Override public void start() {
84        I18n.init();
85        Main.checkJava6();
86
87        String url = getParameter("load_url");
88        if(url != null)
89            args.put("download", Arrays.asList(new String[]{url}));
90
91        // initialize the platform hook, and
92        Main.determinePlatformHook();
93        // call the really early hook before we do anything else
94        Main.platform.preStartupHook();
95
96        Main.pref = new ServerSidePreferences(getCodeBase());
97
98        String lang = getParameter("language");
99        I18n.set(lang != null ? lang : Main.pref.get("language", null));
100
101        try
102        {
103            ((ServerSidePreferences)Main.pref).download();
104        } catch (ServerSidePreferences.MissingPassword e) {
105            String username = args.containsKey("username") ? args.get("username").iterator().next() : null;
106            String password = args.containsKey("password") ? args.get("password").iterator().next() : null;
107            if (username == null || password == null) {
108                JPanel p = new JPanel(new GridBagLayout());
109                p.add(new JLabel(tr(e.realm)), GBC.eol().fill(GBC.HORIZONTAL));
110                p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,20,0));
111                JTextField user = new JTextField(username == null ? "" : username);
112                p.add(user, GBC.eol().fill(GBC.HORIZONTAL));
113                p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,20,0));
114                JPasswordField pass = new JPasswordField(password == null ? "" : password);
115                p.add(pass, GBC.eol().fill(GBC.HORIZONTAL));
116                JOptionPane.showMessageDialog(null, p);
117                username = user.getText();
118                if("".equals(username))
119                    username = null;
120                password = new String(pass.getPassword());
121                if("".equals(password))
122                    password = null;
123            }
124            if (username != null && password != null) {
125                ((ServerSidePreferences)Main.pref).download(username, password);
126            }
127        }
128
129        Main.preConstructorInit(args);
130        Main.parent = frame;
131        Main.addListener();
132
133        new MainCaller().postConstructorProcessCmdLine(args);
134
135        MainMenu m = Main.main.menu; // shortcut
136
137        // remove offending stuff from JOSM (that would break the SecurityManager)
138        m.editMenu.add(new UploadPreferencesAction());
139        m.openFile.setEnabled(false);
140        m.exit.setEnabled(false);
141        m.save.setEnabled(false);
142        m.saveAs.setEnabled(false);
143        m.gpxExport.setEnabled(false);
144    }
145
146    private Collection<String> readParameter(String s, Collection<String> v) {
147        String param = getParameter(s);
148        if (param != null) {
149            if (v == null) {
150                v = new LinkedList<String>();
151            }
152            v.addAll(Arrays.asList(param.split(";")));
153        }
154        return v;
155    }
156
157    public static void main(String[] args) {
158        Main.applet = true;
159        MainApplet applet = new MainApplet();
160        Main.pref = new ServerSidePreferences(applet.getCodeBase());
161        applet.setStub(new AppletStub() {
162            public void appletResize(int w, int h) {
163                frame.setSize(w, h);
164            }
165
166            public AppletContext getAppletContext() {
167                return null;
168            }
169
170            public URL getCodeBase() {
171                try {
172                    return new File(".").toURI().toURL();
173                } catch (Exception e) {
174                    e.printStackTrace();
175                    return null;
176                }
177            }
178
179            public URL getDocumentBase() {
180                return getCodeBase();
181            }
182
183            public String getParameter(String k) {
184                return null;
185            }
186
187            public boolean isActive() {
188                return true;
189            }
190        });
191        applet.init();
192        applet.start();
193        frame.setContentPane(applet);
194        frame.setVisible(true);
195    }
196}
Note: See TracBrowser for help on using the repository browser.