1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.Font;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.io.BufferedReader;
|
---|
12 | import java.io.IOException;
|
---|
13 | import java.io.InputStream;
|
---|
14 | import java.io.InputStreamReader;
|
---|
15 | import java.net.MalformedURLException;
|
---|
16 | import java.net.URL;
|
---|
17 | import java.util.Map.Entry;
|
---|
18 | import java.util.regex.Matcher;
|
---|
19 | import java.util.regex.Pattern;
|
---|
20 |
|
---|
21 | import javax.swing.AbstractAction;
|
---|
22 | import javax.swing.BorderFactory;
|
---|
23 | import javax.swing.Box;
|
---|
24 | import javax.swing.JButton;
|
---|
25 | import javax.swing.JLabel;
|
---|
26 | import javax.swing.JOptionPane;
|
---|
27 | import javax.swing.JPanel;
|
---|
28 | import javax.swing.JScrollPane;
|
---|
29 | import javax.swing.JTabbedPane;
|
---|
30 | import javax.swing.JTextArea;
|
---|
31 |
|
---|
32 | import org.openstreetmap.josm.Main;
|
---|
33 | import org.openstreetmap.josm.plugins.PluginProxy;
|
---|
34 | import org.openstreetmap.josm.tools.GBC;
|
---|
35 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
36 | import org.openstreetmap.josm.tools.UrlLabel;
|
---|
37 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Nice about screen. I guess every application need one these days.. *sigh*
|
---|
41 | *
|
---|
42 | * The REVISION resource is read and if present, it shows the revision
|
---|
43 | * information of the jar-file.
|
---|
44 | *
|
---|
45 | * @author imi
|
---|
46 | */
|
---|
47 | /**
|
---|
48 | * @author Stephan
|
---|
49 | *
|
---|
50 | */
|
---|
51 | public class AboutAction extends JosmAction {
|
---|
52 |
|
---|
53 | private static final String version;
|
---|
54 |
|
---|
55 | private final static JTextArea revision;
|
---|
56 | private static String time;
|
---|
57 |
|
---|
58 | static {
|
---|
59 | URL u = Main.class.getResource("/REVISION");
|
---|
60 | if(u == null) {
|
---|
61 | try {
|
---|
62 | u = new URL("jar:" + Main.class.getProtectionDomain().getCodeSource().getLocation().toString()
|
---|
63 | + "!/META-INF/MANIFEST.MF");
|
---|
64 | } catch (MalformedURLException e) {
|
---|
65 | e.printStackTrace();
|
---|
66 | }
|
---|
67 | }
|
---|
68 | revision = loadFile(u);
|
---|
69 |
|
---|
70 | Pattern versionPattern = Pattern.compile(".*?(?:Revision|Main-Version): ([0-9]*(?: SVN)?).*", Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
|
---|
71 | Matcher match = versionPattern.matcher(revision.getText());
|
---|
72 | version = match.matches() ? match.group(1) : tr("UNKNOWN");
|
---|
73 |
|
---|
74 | Pattern timePattern = Pattern.compile(".*?(?:Last Changed Date|Main-Date): ([^\n]*).*", Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
|
---|
75 | match = timePattern.matcher(revision.getText());
|
---|
76 | time = match.matches() ? match.group(1) : tr("UNKNOWN");
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Return string describing version.
|
---|
81 | * Note that the strinc contains the version number plus an optional suffix of " SVN" to indicate an unofficial development build.
|
---|
82 | * @return version string
|
---|
83 | */
|
---|
84 | static public String getVersionString() {
|
---|
85 | return version;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Return the number part of the version string.
|
---|
90 | * @return integer part of version number or Integer.MAX_VALUE if not available
|
---|
91 | */
|
---|
92 | public static int getVersionNumber() {
|
---|
93 | int myVersion=Integer.MAX_VALUE;
|
---|
94 | try {
|
---|
95 | myVersion = Integer.parseInt(version.split(" ")[0]);
|
---|
96 | } catch (NumberFormatException e) {
|
---|
97 | e.printStackTrace();
|
---|
98 | }
|
---|
99 | return myVersion;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * check whether the version is a development build out of SVN.
|
---|
104 | * @return true if it is a SVN unofficial build
|
---|
105 | */
|
---|
106 | public static boolean isDevelopmentVersion() {
|
---|
107 | return version.endsWith(" SVN");
|
---|
108 | }
|
---|
109 |
|
---|
110 | public AboutAction() {
|
---|
111 | super(tr("About"), "about", tr("Display the about screen."), Shortcut.registerShortcut("system:about", tr("About"), KeyEvent.VK_F1, Shortcut.GROUP_DIRECT, Shortcut.SHIFT_DEFAULT), true);
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void actionPerformed(ActionEvent e) {
|
---|
115 | JTabbedPane about = new JTabbedPane();
|
---|
116 |
|
---|
117 | JTextArea readme = loadFile(Main.class.getResource("/README"));
|
---|
118 | JTextArea contribution = loadFile(Main.class.getResource("/CONTRIBUTION"));
|
---|
119 |
|
---|
120 | JPanel info = new JPanel(new GridBagLayout());
|
---|
121 | JLabel caption = new JLabel("JOSM - " + tr("Java OpenStreetMap Editor"));
|
---|
122 | caption.setFont(new Font("Helvetica", Font.BOLD, 20));
|
---|
123 | info.add(caption, GBC.eol().fill(GBC.HORIZONTAL).insets(10,0,0,0));
|
---|
124 | info.add(GBC.glue(0,10), GBC.eol());
|
---|
125 | info.add(new JLabel(tr("Version {0}",version)), GBC.eol().fill(GBC.HORIZONTAL).insets(10,0,0,0));
|
---|
126 | info.add(GBC.glue(0,5), GBC.eol());
|
---|
127 | info.add(new JLabel(tr("Last change at {0}",time)), GBC.eol().fill(GBC.HORIZONTAL).insets(10,0,0,0));
|
---|
128 | info.add(GBC.glue(0,5), GBC.eol());
|
---|
129 | info.add(new JLabel(tr("Java Version {0}",System.getProperty("java.version"))), GBC.eol().fill(GBC.HORIZONTAL).insets(10,0,0,0));
|
---|
130 | info.add(GBC.glue(0,10), GBC.eol());
|
---|
131 | info.add(new JLabel(tr("Homepage")), GBC.std().insets(10,0,10,0));
|
---|
132 | info.add(new UrlLabel("http://josm.openstreetmap.de"), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
133 | info.add(new JLabel(tr("Bug Reports")), GBC.std().insets(10,0,10,0));
|
---|
134 | info.add(new UrlLabel("http://josm.openstreetmap.de/newticket"), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
135 | info.add(new JLabel(tr("News about JOSM")), GBC.std().insets(10,0,10,0));
|
---|
136 | info.add(new UrlLabel("http://www.opengeodata.org/?cat=17"), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
137 |
|
---|
138 | about.addTab(tr("Info"), info);
|
---|
139 | about.addTab(tr("Readme"), createScrollPane(readme));
|
---|
140 | about.addTab(tr("Revision"), createScrollPane(revision));
|
---|
141 | about.addTab(tr("Contribution"), createScrollPane(contribution));
|
---|
142 |
|
---|
143 | JPanel pluginTab = new JPanel(new GridBagLayout());
|
---|
144 | for (final PluginProxy p : Main.plugins) {
|
---|
145 | String name = p.info.name + (p.info.version != null && !p.info.version.equals("") ? " Version: "+p.info.version : "");
|
---|
146 | pluginTab.add(new JLabel(name), GBC.std());
|
---|
147 | pluginTab.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
|
---|
148 | pluginTab.add(new JButton(new AbstractAction(tr("Information")){
|
---|
149 | public void actionPerformed(ActionEvent event) {
|
---|
150 | StringBuilder b = new StringBuilder();
|
---|
151 | for (Entry<String,String> e : p.info.attr.entrySet()) {
|
---|
152 | b.append(e.getKey());
|
---|
153 | b.append(": ");
|
---|
154 | b.append(e.getValue());
|
---|
155 | b.append("\n");
|
---|
156 | }
|
---|
157 | JTextArea a = new JTextArea(10,40);
|
---|
158 | a.setEditable(false);
|
---|
159 | a.setText(b.toString());
|
---|
160 | JOptionPane.showMessageDialog(Main.parent, new JScrollPane(a));
|
---|
161 | }
|
---|
162 | }), GBC.eol());
|
---|
163 | JLabel label = new JLabel("<html><i>"+(p.info.description==null?tr("no description available"):p.info.description)+"</i></html>");
|
---|
164 | label.setBorder(BorderFactory.createEmptyBorder(0,20,0,0));
|
---|
165 | label.setMaximumSize(new Dimension(450,1000));
|
---|
166 | pluginTab.add(label, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
167 | }
|
---|
168 | about.addTab(tr("Plugins"), new JScrollPane(pluginTab));
|
---|
169 |
|
---|
170 | about.setPreferredSize(new Dimension(500,300));
|
---|
171 |
|
---|
172 | JOptionPane.showMessageDialog(Main.parent, about, tr("About JOSM..."),
|
---|
173 | JOptionPane.INFORMATION_MESSAGE, ImageProvider.get("logo"));
|
---|
174 | }
|
---|
175 |
|
---|
176 | private JScrollPane createScrollPane(JTextArea area) {
|
---|
177 | area.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
178 | area.setOpaque(false);
|
---|
179 | JScrollPane sp = new JScrollPane(area);
|
---|
180 | sp.setBorder(null);
|
---|
181 | sp.setOpaque(false);
|
---|
182 | return sp;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Retrieve the latest JOSM version from the JOSM homepage.
|
---|
187 | * @return An string with the latest version or "UNKNOWN" in case
|
---|
188 | * of problems (e.g. no internet connection).
|
---|
189 | */
|
---|
190 | public static String checkLatestVersion() {
|
---|
191 | String latest;
|
---|
192 | try {
|
---|
193 | InputStream s = new URL("http://josm.openstreetmap.de/current").openStream();
|
---|
194 | latest = new BufferedReader(new InputStreamReader(s)).readLine();
|
---|
195 | s.close();
|
---|
196 | } catch (IOException x) {
|
---|
197 | x.printStackTrace();
|
---|
198 | return tr("UNKNOWN");
|
---|
199 | }
|
---|
200 | return latest;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Load the specified resource into an TextArea and return it.
|
---|
205 | * @param resource The resource url to load
|
---|
206 | * @return An read-only text area with the content of "resource"
|
---|
207 | */
|
---|
208 | private static JTextArea loadFile(URL resource) {
|
---|
209 | JTextArea area = new JTextArea(tr("File could not be found."));
|
---|
210 | area.setEditable(false);
|
---|
211 | Font font = Font.getFont("monospaced");
|
---|
212 | if (font != null)
|
---|
213 | area.setFont(font);
|
---|
214 | if (resource == null)
|
---|
215 | return area;
|
---|
216 | BufferedReader in;
|
---|
217 | try {
|
---|
218 | in = new BufferedReader(new InputStreamReader(resource.openStream()));
|
---|
219 | StringBuilder sb = new StringBuilder();
|
---|
220 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
221 | sb.append(line);
|
---|
222 | sb.append('\n');
|
---|
223 | }
|
---|
224 | area.setText(sb.toString());
|
---|
225 | area.setCaretPosition(0);
|
---|
226 | } catch (IOException e) {
|
---|
227 | e.printStackTrace();
|
---|
228 | }
|
---|
229 | return area;
|
---|
230 | }
|
---|
231 | }
|
---|