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.regex.Matcher;
|
---|
18 | import java.util.regex.Pattern;
|
---|
19 |
|
---|
20 | import javax.swing.BorderFactory;
|
---|
21 | import javax.swing.JLabel;
|
---|
22 | import javax.swing.JOptionPane;
|
---|
23 | import javax.swing.JPanel;
|
---|
24 | import javax.swing.JScrollPane;
|
---|
25 | import javax.swing.JTabbedPane;
|
---|
26 | import javax.swing.JTextArea;
|
---|
27 |
|
---|
28 | import org.openstreetmap.josm.Main;
|
---|
29 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
30 | import org.openstreetmap.josm.tools.GBC;
|
---|
31 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
32 | import org.openstreetmap.josm.tools.UrlLabel;
|
---|
33 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Nice about screen. I guess every application need one these days.. *sigh*
|
---|
37 | *
|
---|
38 | * The REVISION resource is read and if present, it shows the revision
|
---|
39 | * information of the jar-file.
|
---|
40 | *
|
---|
41 | * @author imi
|
---|
42 | */
|
---|
43 | /**
|
---|
44 | * @author Stephan
|
---|
45 | *
|
---|
46 | */
|
---|
47 | public class AboutAction extends JosmAction {
|
---|
48 |
|
---|
49 | private static final String version;
|
---|
50 |
|
---|
51 | private final static JTextArea revision;
|
---|
52 | private static String time;
|
---|
53 |
|
---|
54 | static {
|
---|
55 | URL u = Main.class.getResource("/REVISION");
|
---|
56 | if(u == null) {
|
---|
57 | try {
|
---|
58 | u = new URL("jar:" + Main.class.getProtectionDomain().getCodeSource().getLocation().toString()
|
---|
59 | + "!/META-INF/MANIFEST.MF");
|
---|
60 | } catch (MalformedURLException e) {
|
---|
61 | e.printStackTrace();
|
---|
62 | }
|
---|
63 | }
|
---|
64 | revision = loadFile(u);
|
---|
65 |
|
---|
66 | Pattern versionPattern = Pattern.compile(".*?(?:Revision|Main-Version): ([0-9]*(?: SVN)?).*", Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
|
---|
67 | Matcher match = versionPattern.matcher(revision.getText());
|
---|
68 | version = match.matches() ? match.group(1) : tr("UNKNOWN");
|
---|
69 |
|
---|
70 | Pattern timePattern = Pattern.compile(".*?(?:Last Changed Date|Main-Date): ([^\n]*).*", Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
|
---|
71 | match = timePattern.matcher(revision.getText());
|
---|
72 | time = match.matches() ? match.group(1) : tr("UNKNOWN");
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Return string describing version.
|
---|
77 | * Note that the strinc contains the version number plus an optional suffix of " SVN" to indicate an unofficial development build.
|
---|
78 | * @return version string
|
---|
79 | */
|
---|
80 | static public String getVersionString() {
|
---|
81 | return version;
|
---|
82 | }
|
---|
83 |
|
---|
84 | static public String getTextBlock() {
|
---|
85 | return revision.getText();
|
---|
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") || version.equals(tr("UNKNOWN"));
|
---|
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 | about.addTab(tr("Plugins"), new JScrollPane(PluginHandler.getInfoPanel()));
|
---|
143 |
|
---|
144 | about.setPreferredSize(new Dimension(500,300));
|
---|
145 |
|
---|
146 | JOptionPane.showMessageDialog(Main.parent, about, tr("About JOSM..."),
|
---|
147 | JOptionPane.INFORMATION_MESSAGE, ImageProvider.get("logo"));
|
---|
148 | }
|
---|
149 |
|
---|
150 | private JScrollPane createScrollPane(JTextArea area) {
|
---|
151 | area.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
152 | area.setOpaque(false);
|
---|
153 | JScrollPane sp = new JScrollPane(area);
|
---|
154 | sp.setBorder(null);
|
---|
155 | sp.setOpaque(false);
|
---|
156 | return sp;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Retrieve the latest JOSM version from the JOSM homepage.
|
---|
161 | * @return An string with the latest version or "UNKNOWN" in case
|
---|
162 | * of problems (e.g. no internet connection).
|
---|
163 | */
|
---|
164 | public static String checkLatestVersion() {
|
---|
165 | String latest;
|
---|
166 | try {
|
---|
167 | InputStream s = new URL("http://josm.openstreetmap.de/current").openStream();
|
---|
168 | latest = new BufferedReader(new InputStreamReader(s)).readLine();
|
---|
169 | s.close();
|
---|
170 | } catch (IOException x) {
|
---|
171 | x.printStackTrace();
|
---|
172 | return tr("UNKNOWN");
|
---|
173 | }
|
---|
174 | return latest;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Load the specified resource into an TextArea and return it.
|
---|
179 | * @param resource The resource url to load
|
---|
180 | * @return An read-only text area with the content of "resource"
|
---|
181 | */
|
---|
182 | private static JTextArea loadFile(URL resource) {
|
---|
183 | JTextArea area = new JTextArea(tr("File could not be found."));
|
---|
184 | area.setEditable(false);
|
---|
185 | Font font = Font.getFont("monospaced");
|
---|
186 | if (font != null)
|
---|
187 | area.setFont(font);
|
---|
188 | if (resource == null)
|
---|
189 | return area;
|
---|
190 | BufferedReader in;
|
---|
191 | try {
|
---|
192 | in = new BufferedReader(new InputStreamReader(resource.openStream()));
|
---|
193 | StringBuilder sb = new StringBuilder();
|
---|
194 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
195 | sb.append(line);
|
---|
196 | sb.append('\n');
|
---|
197 | }
|
---|
198 | area.setText(sb.toString());
|
---|
199 | area.setCaretPosition(0);
|
---|
200 | } catch (IOException e) {
|
---|
201 | e.printStackTrace();
|
---|
202 | }
|
---|
203 | return area;
|
---|
204 | }
|
---|
205 | }
|
---|