source: josm/src/org/openstreetmap/josm/actions/AboutAction.java@ 19

Last change on this file since 19 was 19, checked in by imi, 19 years ago

added about dialog

File size: 3.9 KB
Line 
1package org.openstreetmap.josm.actions;
2
3import java.awt.Dimension;
4import java.awt.Font;
5import java.awt.GridBagLayout;
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.io.BufferedReader;
9import java.io.IOException;
10import java.io.InputStreamReader;
11import java.net.URL;
12import java.util.regex.Matcher;
13import java.util.regex.Pattern;
14
15import javax.swing.AbstractAction;
16import javax.swing.JEditorPane;
17import javax.swing.JLabel;
18import javax.swing.JOptionPane;
19import javax.swing.JPanel;
20import javax.swing.JScrollPane;
21import javax.swing.JTabbedPane;
22import javax.swing.JTextArea;
23import javax.swing.event.HyperlinkEvent;
24import javax.swing.event.HyperlinkListener;
25
26import org.openstreetmap.josm.gui.GBC;
27import org.openstreetmap.josm.gui.ImageProvider;
28import org.openstreetmap.josm.gui.Main;
29
30/**
31 * Nice about screen. I guess every application need one these days.. *sigh*
32 *
33 * The REVISION resource is read and if present, it shows the revision
34 * information of the jar-file.
35 *
36 * @author imi
37 */
38public class AboutAction extends AbstractAction {
39
40 public AboutAction() {
41 super("About", ImageProvider.get("about"));
42 putValue(MNEMONIC_KEY, KeyEvent.VK_A);
43 putValue(SHORT_DESCRIPTION, "Display the about screen.");
44 }
45
46 public void actionPerformed(ActionEvent e) {
47 JTabbedPane about = new JTabbedPane();
48
49 JTextArea readme = loadFile(Main.class.getResource("/README"));
50 JTextArea revision = loadFile(Main.class.getResource("/REVISION"));
51
52 Pattern versionPattern = Pattern.compile(".*?Revision: ([0-9]*).*", Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
53 Pattern timePattern = Pattern.compile(".*?Last Changed Date: ([^\n]*).*", Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
54
55 Matcher match = versionPattern.matcher(revision.getText());
56 String version = match.matches() ? match.group(1) : "UNKNOWN";
57 match = timePattern.matcher(revision.getText());
58 String time = match.matches() ? match.group(1) : "UNKNOWN";
59
60 JPanel info = new JPanel(new GridBagLayout());
61 info.add(new JLabel("Java OpenStreetMap Editor Version "+version), GBC.eop());
62 info.add(new JLabel("last change at "+time), GBC.eop());
63 info.add(new JLabel("Homepage"), GBC.std().insets(0,0,10,0));
64 JEditorPane homepage = new JEditorPane();
65 homepage.setContentType("text/html");
66 homepage.setText("<html><a href=\"http://wiki.eigenheimstrasse.de/wiki/JOSM\">" +
67 "http://wiki.eigenheimstrasse.de/wiki/JOSM</a></html>");
68 homepage.setEditable(false);
69 homepage.setOpaque(false);
70 homepage.addHyperlinkListener(new HyperlinkListener(){
71 public void hyperlinkUpdate(HyperlinkEvent e) {
72 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
73 //TODO: Open browser
74 }
75 }
76 });
77 info.add(homepage, GBC.eol());
78
79
80
81 about.addTab("Info", info);
82 about.addTab("Readme", new JScrollPane(readme));
83 about.addTab("Revision", new JScrollPane(revision));
84
85 about.setPreferredSize(new Dimension(500,300));
86
87 JOptionPane.showMessageDialog(Main.main, about, "About JOSM...",
88 JOptionPane.INFORMATION_MESSAGE, ImageProvider.get("logo"));
89 }
90
91 /**
92 * Load the specified ressource into an TextArea and return it.
93 * @param resource The resource url to load
94 * @return An read-only text area with the content of "resource"
95 */
96 private JTextArea loadFile(URL resource) {
97 JTextArea area = new JTextArea("File could not be found.");
98 area.setEditable(false);
99 Font font = Font.getFont("monospaced");
100 if (font != null)
101 area.setFont(font);
102 if (resource == null)
103 return area;
104 BufferedReader in;
105 try {
106 in = new BufferedReader(new InputStreamReader(resource.openStream()));
107 StringBuilder sb = new StringBuilder();
108 for (String line = in.readLine(); line != null; line = in.readLine()) {
109 sb.append(line);
110 sb.append('\n');
111 }
112 area.setText(sb.toString());
113 area.setCaretPosition(0);
114 } catch (IOException e) {
115 e.printStackTrace();
116 }
117 return area;
118 }
119}
Note: See TracBrowser for help on using the repository browser.