1 | package josmlaunch;
|
---|
2 |
|
---|
3 | import java.awt.Dimension;
|
---|
4 | import java.awt.GridBagConstraints;
|
---|
5 | import java.awt.GridBagLayout;
|
---|
6 | import java.awt.GridLayout;
|
---|
7 | import java.awt.Toolkit;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.awt.event.WindowAdapter;
|
---|
11 | import java.awt.event.WindowEvent;
|
---|
12 | import java.io.File;
|
---|
13 | import java.io.FileNotFoundException;
|
---|
14 | import java.io.FileOutputStream;
|
---|
15 | import java.io.FileReader;
|
---|
16 | import java.io.FileWriter;
|
---|
17 | import java.io.IOException;
|
---|
18 | import java.io.InputStream;
|
---|
19 | import java.io.OutputStream;
|
---|
20 | import java.net.HttpURLConnection;
|
---|
21 | import java.net.URL;
|
---|
22 | import java.net.URLConnection;
|
---|
23 | import java.util.Properties;
|
---|
24 | import java.util.logging.Level;
|
---|
25 | import java.util.logging.Logger;
|
---|
26 | import javax.swing.JButton;
|
---|
27 | import javax.swing.JCheckBox;
|
---|
28 | import javax.swing.JDialog;
|
---|
29 | import javax.swing.JFrame;
|
---|
30 | import javax.swing.JTextField;
|
---|
31 |
|
---|
32 | class LaunchDialog extends JFrame {
|
---|
33 | JButton start = new JButton("Start JOSM");
|
---|
34 | JButton update = new JButton("Update JOSM");
|
---|
35 | JTextField opts = new JTextField(50);
|
---|
36 | JCheckBox portable = new JCheckBox("Portable insatllation");
|
---|
37 | Properties properties = new Properties();
|
---|
38 |
|
---|
39 | void launch() {
|
---|
40 | try {
|
---|
41 | String addOpts = "";
|
---|
42 | if (portable.isSelected()) {
|
---|
43 | addOpts = "-Djosm.home=data";
|
---|
44 | }
|
---|
45 | String cmd = String.format("java %s %s -jar josm-latest.jar", opts.getText(), addOpts);
|
---|
46 | Process p = Runtime.getRuntime().exec(cmd);
|
---|
47 | } catch (IOException ex) {
|
---|
48 | System.exit(1);
|
---|
49 | }
|
---|
50 | System.exit(0);
|
---|
51 | }
|
---|
52 |
|
---|
53 | void update() {
|
---|
54 | download();
|
---|
55 | }
|
---|
56 |
|
---|
57 | void center() {
|
---|
58 | // Get the size of the screen
|
---|
59 | Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
60 |
|
---|
61 | // Determine the new location of the window
|
---|
62 | int w = getSize().width;
|
---|
63 | int h = getSize().height;
|
---|
64 | int x = (dim.width-w)/2;
|
---|
65 | int y = (dim.height-h)/2;
|
---|
66 |
|
---|
67 | // Move the window
|
---|
68 | setLocation(x, y);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public LaunchDialog() {
|
---|
72 | loadProperties();
|
---|
73 | setSize(300,200);
|
---|
74 | center();
|
---|
75 | opts.setText(getProperty("options", "-Xmx1024m"));
|
---|
76 | portable.setSelected(getProperty("portable", "false").equals("true"));
|
---|
77 |
|
---|
78 | start.addActionListener(new ActionListener() {
|
---|
79 | @Override
|
---|
80 | public void actionPerformed(ActionEvent e) {
|
---|
81 | saveProperties();
|
---|
82 | launch();
|
---|
83 | }
|
---|
84 | });
|
---|
85 |
|
---|
86 | update.addActionListener(new ActionListener() {
|
---|
87 | @Override
|
---|
88 | public void actionPerformed(ActionEvent e) {
|
---|
89 | update();
|
---|
90 | }
|
---|
91 | });
|
---|
92 |
|
---|
93 | GridLayout gb =new GridLayout(4,1);
|
---|
94 | setLayout(gb);
|
---|
95 | addWindowListener(new WindowAdapter() {
|
---|
96 | public void windowClosing(WindowEvent e) {
|
---|
97 | saveProperties();
|
---|
98 | System.exit(0);
|
---|
99 | }
|
---|
100 | } );
|
---|
101 |
|
---|
102 |
|
---|
103 |
|
---|
104 | add(portable);
|
---|
105 | add(opts);
|
---|
106 | add(start);
|
---|
107 | add(update);
|
---|
108 | }
|
---|
109 |
|
---|
110 | String getProperty(String key, String def) {
|
---|
111 | return properties.getProperty(key, def);
|
---|
112 | }
|
---|
113 |
|
---|
114 | void loadProperties() {
|
---|
115 | FileReader fr = null;
|
---|
116 | try {
|
---|
117 | fr = new FileReader("launcher.ini");
|
---|
118 | properties.load(fr);
|
---|
119 | } catch (FileNotFoundException ex) {
|
---|
120 | System.err.println("Properties file is created");
|
---|
121 | } catch (IOException ex) {
|
---|
122 | System.err.println("Can not read properties file");
|
---|
123 | } finally {
|
---|
124 | try {
|
---|
125 | fr.close();
|
---|
126 | } catch (IOException ex) { }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | void saveProperties() {
|
---|
131 | properties.setProperty("options", opts.getText());
|
---|
132 | properties.setProperty("portable", String.valueOf(portable.isSelected()));
|
---|
133 |
|
---|
134 | FileWriter fr = null;
|
---|
135 | try {
|
---|
136 | fr = new FileWriter("launcher.ini");
|
---|
137 | properties.store(fr,"");
|
---|
138 | } catch (IOException ex) {
|
---|
139 | System.err.println("Can not write properties file");
|
---|
140 | } finally {
|
---|
141 | try {
|
---|
142 | fr.close();
|
---|
143 | } catch (IOException ex) { }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | URLConnection downloadConnection;
|
---|
148 | String address = "http://josm.openstreetmap.de/download/josm-latest.jar";
|
---|
149 |
|
---|
150 | public void download() {
|
---|
151 | OutputStream out = null;
|
---|
152 | InputStream in = null;
|
---|
153 | File file = new File("josm-latest.tmp");
|
---|
154 |
|
---|
155 | try {
|
---|
156 |
|
---|
157 | URL url = new URL(address);
|
---|
158 | int size;
|
---|
159 | downloadConnection = url.openConnection();
|
---|
160 | downloadConnection.setRequestProperty("Cache-Control", "no-cache");
|
---|
161 | downloadConnection.setRequestProperty("Host", url.getHost());
|
---|
162 | downloadConnection.connect();
|
---|
163 | size = downloadConnection.getContentLength();
|
---|
164 | if (size<3000000) {
|
---|
165 | System.err.println("Suspicious JOSM jar size, no download");
|
---|
166 | return;
|
---|
167 | }
|
---|
168 |
|
---|
169 | //progressMonitor.setTicksCount(100);
|
---|
170 | //progressMonitor.subTask(tr("Downloading File {0}: {1} bytes...", file.getName(),size));
|
---|
171 |
|
---|
172 | in = downloadConnection.getInputStream();
|
---|
173 | out = new FileOutputStream(file);
|
---|
174 | byte[] buffer = new byte[32768];
|
---|
175 | int count=0;
|
---|
176 | int p1=0, p2=0;
|
---|
177 | for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
|
---|
178 | out.write(buffer, 0, read);
|
---|
179 | count+=read;
|
---|
180 | // if (canceled) return;
|
---|
181 | p2 = 100 * count / size;
|
---|
182 | if (p2!=p1) {
|
---|
183 | // progressMonitor.setTicks(p2);
|
---|
184 | p1=p2;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | out.close();
|
---|
188 | new File("josm-latest.jar").renameTo(new File("josm-latest.old"));
|
---|
189 | file.renameTo(new File("josm-latest.jar"));
|
---|
190 | } catch(Exception e) {
|
---|
191 | //if (canceled) return;
|
---|
192 | e.printStackTrace();
|
---|
193 | } finally {
|
---|
194 | if (out!=null) try {
|
---|
195 | out.close();
|
---|
196 | } catch (IOException ex) { }
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | public static void main(String[] args) {
|
---|
201 | new LaunchDialog().setVisible(true);
|
---|
202 | }
|
---|
203 | }
|
---|