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

Last change on this file since 5886 was 5886, checked in by Don-vip, 11 years ago

see #4429 - Right click menu "undo, cut, copy, paste, delete, select all" for each text component (originally based on patch by NooN)

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