]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/AboutScreen.java
Version 17.2, February 2015
[GpsPrune.git] / tim / prune / function / AboutScreen.java
1 package tim.prune.function;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.awt.Font;
8 import java.awt.GridBagConstraints;
9 import java.awt.GridBagLayout;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.ActionListener;
12 import java.awt.event.KeyEvent;
13 import java.awt.event.KeyListener;
14 import java.io.ByteArrayOutputStream;
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.util.zip.GZIPInputStream;
20
21 import javax.swing.BorderFactory;
22 import javax.swing.BoxLayout;
23 import javax.swing.JButton;
24 import javax.swing.JDialog;
25 import javax.swing.JEditorPane;
26 import javax.swing.JLabel;
27 import javax.swing.JPanel;
28 import javax.swing.JScrollPane;
29 import javax.swing.JTabbedPane;
30 import javax.swing.JTextArea;
31
32 import tim.prune.App;
33 import tim.prune.ExternalTools;
34 import tim.prune.GenericFunction;
35 import tim.prune.GpsPrune;
36 import tim.prune.I18nManager;
37 import tim.prune.jpeg.ExifGateway;
38 import tim.prune.threedee.WindowFactory;
39
40 /**
41  * Class to represent the "About" popup window
42  */
43 public class AboutScreen extends GenericFunction
44 {
45         private JDialog _dialog = null;
46         private JTabbedPane _tabs = null;
47         private JButton _okButton = null;
48         private JTextArea _aboutTextArea = null;
49         /** Labels for whether tools installed or not */
50         private JLabel[] _installedLabels = null;
51
52
53         /**
54          * Constructor
55          * @param inApp app object
56          */
57         public AboutScreen(App inApp)
58         {
59                 super(inApp);
60         }
61
62         /**
63          * Return the name key for this function
64          */
65         public String getNameKey()
66         {
67                 return "function.about";
68         }
69
70         /**
71          * @return the contents of the window as a Component
72          */
73         private Component makeContents()
74         {
75                 JPanel mainPanel = new JPanel();
76                 mainPanel.setLayout(new BorderLayout());
77
78                 _tabs = new JTabbedPane();
79                 mainPanel.add(_tabs, BorderLayout.CENTER);
80
81                 JPanel aboutPanel = new JPanel();
82                 aboutPanel.setLayout(new BoxLayout(aboutPanel, BoxLayout.Y_AXIS));
83                 aboutPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
84                 JLabel titleLabel = new JLabel("GpsPrune");
85                 titleLabel.setFont(new Font("SansSerif", Font.BOLD, 24));
86                 titleLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
87                 aboutPanel.add(titleLabel);
88                 JLabel versionLabel = new JLabel(I18nManager.getText("dialog.about.version") + ": " + GpsPrune.VERSION_NUMBER);
89                 versionLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
90                 aboutPanel.add(versionLabel);
91                 JLabel buildLabel = new JLabel(I18nManager.getText("dialog.about.build") + ": " + GpsPrune.BUILD_NUMBER);
92                 buildLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
93                 aboutPanel.add(buildLabel);
94                 aboutPanel.add(new JLabel(" "));
95                 StringBuffer descBuffer = new StringBuffer();
96                 descBuffer.append("<p>").append(I18nManager.getText("dialog.about.summarytext1")).append("</p>");
97                 descBuffer.append("<p>").append(I18nManager.getText("dialog.about.summarytext2")).append("</p>");
98                 descBuffer.append("<p>").append(I18nManager.getText("dialog.about.summarytext3")).append("</p>");
99                 descBuffer.append("<p>").append(I18nManager.getText("dialog.about.languages")).append(" : ")
100                         .append("\u010de\u0161tina, deutsch, english, espa\u00F1ol, fran\u00E7ais, italiano, magyar,<br>" +
101                                 " nederlands, polski, portugu\u00EAs, \u0440\u0443\u0441\u0441\u043a\u0438\u0439 (russian), \u4e2d\u6587 (chinese), \u65E5\u672C\u8A9E (japanese),<br>" +
102                                 " \uD55C\uAD6D\uC5B4/\uC870\uC120\uB9D0 (korean), schwiizerd\u00FC\u00FCtsch, t\u00FCrk\u00E7e, afrikaans, rom\u00E2n\u0103, ukrainian</p>");
103                 descBuffer.append("<p>").append(I18nManager.getText("dialog.about.translatedby")).append("</p>");
104                 JEditorPane descPane = new JEditorPane("text/html", descBuffer.toString());
105                 descPane.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
106                 descPane.setEditable(false);
107                 descPane.setOpaque(false);
108                 descPane.setAlignmentX(JEditorPane.CENTER_ALIGNMENT);
109
110                 aboutPanel.add(descPane);
111                 aboutPanel.add(new JLabel(" "));
112                 _tabs.add(I18nManager.getText("function.about"), aboutPanel);
113
114                 // Second pane for system info
115                 JPanel sysInfoPanel = new JPanel();
116                 GridBagLayout gridBag = new GridBagLayout();
117                 sysInfoPanel.setLayout(gridBag);
118                 GridBagConstraints constraints = new GridBagConstraints();
119                 constraints.weightx = 0.0; constraints.weighty = 0.0;
120                 addToGridBagPanel(sysInfoPanel, gridBag, constraints,
121                         new JLabel(I18nManager.getText("dialog.about.systeminfo.os") + " : "),
122                         0, 0);
123                 addToGridBagPanel(sysInfoPanel, gridBag, constraints,
124                         new JLabel(System.getProperty("os.name")),
125                         1, 0);
126                 addToGridBagPanel(sysInfoPanel, gridBag, constraints,
127                         new JLabel(I18nManager.getText("dialog.about.systeminfo.java") + " : "),
128                         0, 1);
129                 addToGridBagPanel(sysInfoPanel, gridBag, constraints,
130                         new JLabel(System.getProperty("java.runtime.version")),
131                         1, 1);
132                 // Create install labels to be populated later
133                 final int NUM_INSTALL_CHECKS = 5;
134                 _installedLabels = new JLabel[NUM_INSTALL_CHECKS];
135                 for (int i=0; i<NUM_INSTALL_CHECKS; i++) {
136                         _installedLabels[i] = new JLabel("...");
137                 }
138                 addToGridBagPanel(sysInfoPanel, gridBag, constraints,
139                         new JLabel(I18nManager.getText("dialog.about.systeminfo.java3d") + " : "),
140                         0, 2);
141                 addToGridBagPanel(sysInfoPanel, gridBag, constraints, _installedLabels[0], 1, 2);
142                 addToGridBagPanel(sysInfoPanel, gridBag, constraints,
143                         new JLabel(I18nManager.getText("dialog.about.systeminfo.exiftool") + " : "),
144                         0, 3);
145                 addToGridBagPanel(sysInfoPanel, gridBag, constraints, _installedLabels[1], 1, 3);
146                 addToGridBagPanel(sysInfoPanel, gridBag, constraints,
147                         new JLabel(I18nManager.getText("dialog.about.systeminfo.gpsbabel") + " : "),
148                         0, 4);
149                 addToGridBagPanel(sysInfoPanel, gridBag, constraints, _installedLabels[2], 1, 4);
150                 addToGridBagPanel(sysInfoPanel, gridBag, constraints,
151                         new JLabel(I18nManager.getText("dialog.about.systeminfo.gnuplot") + " : "),
152                         0, 5);
153                 addToGridBagPanel(sysInfoPanel, gridBag, constraints, _installedLabels[3], 1, 5);
154                 addToGridBagPanel(sysInfoPanel, gridBag, constraints, new JLabel("Xerces : "), 0, 6);
155                 addToGridBagPanel(sysInfoPanel, gridBag, constraints, _installedLabels[4], 1, 6);
156                 // Exif library
157                 addToGridBagPanel(sysInfoPanel, gridBag, constraints,
158                         new JLabel(I18nManager.getText("dialog.about.systeminfo.exiflib") + " : "),
159                         0, 7);
160                 final String exiflibkey = "dialog.about.systeminfo.exiflib." + ExifGateway.getDescriptionKey();
161                 addToGridBagPanel(sysInfoPanel, gridBag, constraints,
162                         new JLabel(I18nManager.getText(exiflibkey)), 1, 7);
163                 _tabs.add(I18nManager.getText("dialog.about.systeminfo"), sysInfoPanel);
164
165                 // Third pane for credits
166                 JPanel creditsPanel = new JPanel();
167                 gridBag = new GridBagLayout();
168                 creditsPanel.setLayout(gridBag);
169                 constraints = new GridBagConstraints();
170                 constraints.weightx = 0.0; constraints.weighty = 0.0;
171
172                 addToGridBagPanel(creditsPanel, gridBag, constraints,
173                         new JLabel(I18nManager.getText("dialog.about.credits.code") + " : "),
174                         0, 0);
175                 addToGridBagPanel(creditsPanel, gridBag, constraints,
176                         new JLabel("activityworkshop.net"),
177                         1, 0);
178                 addToGridBagPanel(creditsPanel, gridBag, constraints,
179                         new JLabel(I18nManager.getText("dialog.about.credits.exifcode") + " : "),
180                         0, 1);
181                 addToGridBagPanel(creditsPanel, gridBag, constraints,
182                         new JLabel("Drew Noakes"),
183                         1, 1);
184                 addToGridBagPanel(creditsPanel, gridBag, constraints,
185                         new JLabel(I18nManager.getText("dialog.about.credits.icons") + " : "),
186                         0, 2);
187                 addToGridBagPanel(creditsPanel, gridBag, constraints,
188                         new JLabel("Eclipse"),
189                         1, 2);
190                 addToGridBagPanel(creditsPanel, gridBag, constraints,
191                         new JLabel(I18nManager.getText("dialog.about.credits.translators") + " : "),
192                         0, 3);
193                 addToGridBagPanel(creditsPanel, gridBag, constraints,
194                         new JLabel("Ramon, Miguel, In\u00E9s, Piotr, Petrovsk, Josatoc, Weehal,"),
195                         1, 3);
196                 addToGridBagPanel(creditsPanel, gridBag, constraints,
197                         new JLabel(" theYinYeti, Rothermographer, Sam, Rudolph, nazotoko,"),
198                         1, 4);
199                 addToGridBagPanel(creditsPanel, gridBag, constraints,
200                         new JLabel(" katpatuka, R\u00E9mi, Marcus, Ali, Javier, Jeroen, prot_d, Gy\u00F6rgy,"),
201                         1, 5);
202                 addToGridBagPanel(creditsPanel, gridBag, constraints,
203                         new JLabel(" HooAU, Sergey, P\u00E9ter, serhijdubyk, Peter"),
204                         1, 6);
205                 addToGridBagPanel(creditsPanel, gridBag, constraints,
206                         new JLabel(I18nManager.getText("dialog.about.credits.translations") + " : "),
207                         0, 7);
208                 addToGridBagPanel(creditsPanel, gridBag, constraints,
209                         new JLabel("Open Office, Gpsdrive, Babelfish, Leo, Launchpad"),
210                         1, 7);
211                 addToGridBagPanel(creditsPanel, gridBag, constraints,
212                         new JLabel(I18nManager.getText("dialog.about.credits.devtools") + " : "),
213                         0, 8);
214                 addToGridBagPanel(creditsPanel, gridBag, constraints,
215                         new JLabel("Debian Linux, Sun Java, OpenJDK, Eclipse, Svn, Gimp, Inkscape"),
216                         1, 8);
217                 addToGridBagPanel(creditsPanel, gridBag, constraints,
218                         new JLabel(I18nManager.getText("dialog.about.credits.othertools") + " : "),
219                         0, 9);
220                 addToGridBagPanel(creditsPanel, gridBag, constraints,
221                         new JLabel("Openstreetmap, Povray, Exiftool, Gpsbabel, Gnuplot"),
222                         1, 9);
223                 addToGridBagPanel(creditsPanel, gridBag, constraints,
224                         new JLabel(I18nManager.getText("dialog.about.credits.thanks") + " : "),
225                         0, 10);
226                 addToGridBagPanel(creditsPanel, gridBag, constraints,
227                         new JLabel("Friends and loved ones, for encouragement and support"),
228                         1, 10);
229                 _tabs.add(I18nManager.getText("dialog.about.credits"), creditsPanel);
230
231                 // Read me
232                 JPanel readmePanel = new JPanel();
233                 readmePanel.setLayout(new BorderLayout());
234                 _aboutTextArea = new JTextArea(I18nManager.getText("details.photo.loading"));
235                 // Set readme text in separate thread so that about screen pops up sooner
236                 new Thread(new Runnable() {
237                         public void run() {
238                                 _aboutTextArea.setText(getReadmeText());
239                         }
240                 }).start();
241                 _aboutTextArea.setEditable(false);
242                 _aboutTextArea.setLineWrap(true); _aboutTextArea.setWrapStyleWord(true);
243                 JScrollPane scrollPane = new JScrollPane(_aboutTextArea);
244                 scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
245                 scrollPane.setPreferredSize(new Dimension(600, 130));
246                 readmePanel.add(scrollPane, BorderLayout.CENTER);
247                 _tabs.add(I18nManager.getText("dialog.about.readme"), readmePanel);
248
249                 // OK button at the bottom
250                 JPanel okPanel = new JPanel();
251                 okPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
252                 _okButton = new JButton(I18nManager.getText("button.ok"));
253                 _okButton.addActionListener(new ActionListener()
254                 {
255                         public void actionPerformed(ActionEvent e)
256                         {
257                                 _dialog.dispose();
258                         }
259                 });
260                 _okButton.addKeyListener(new KeyListener() {
261                         public void keyPressed(KeyEvent e)
262                         {
263                                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {_dialog.dispose();}
264                         }
265                         public void keyTyped(KeyEvent e) {}
266                         public void keyReleased(KeyEvent e) {}
267                 });
268                 okPanel.add(_okButton);
269                 mainPanel.add(okPanel, BorderLayout.SOUTH);
270                 return mainPanel;
271         }
272
273         /**
274          * Helper function to reduce complexity of gui-making code
275          * when adding labels to a GridBagLayout
276          * @param inPanel panel to add to
277          * @param inLayout GridBagLayout object
278          * @param inConstraints GridBagConstraints object
279          * @param inLabel label to add
280          * @param inX grid x
281          * @param inY grid y
282          */
283         private static void addToGridBagPanel(JPanel inPanel, GridBagLayout inLayout,
284                 GridBagConstraints inConstraints, JLabel inLabel, int inX, int inY)
285         {
286                 // set x and y in constraints
287                 inConstraints.gridx = inX;
288                 inConstraints.gridy = inY;
289                 // set anchor
290                 inConstraints.anchor = (inX == 0?GridBagConstraints.EAST:GridBagConstraints.WEST);
291                 // set constraints to label
292                 inLayout.setConstraints(inLabel, inConstraints);
293                 // add label to panel
294                 inPanel.add(inLabel);
295         }
296
297         /**
298          * @return text from the readme file
299          */
300         private String getReadmeText()
301         {
302                 // First, try locally-held readme.txt if available (as it normally should be)
303                 // Readme file can either be in file system or packed in the same jar as code
304                 String errorMessage = null;
305                 String readme = null;
306                 InputStream in = null;
307                 try
308                 {
309                         // For some reason using ../readme.txt doesn't work, so need absolute path
310                         in = AboutScreen.class.getResourceAsStream("/tim/prune/readme.txt");
311                         if (in != null) {
312                                 byte[] buffer = new byte[in.available()];
313                                 in.read(buffer);
314                                 in.close();
315                                 readme = new String(buffer);
316                         }
317                 }
318                 catch (IOException e) {
319                         errorMessage =  e.getMessage();
320                 }
321                 finally {
322                         try {in.close();} catch (Exception e) {}
323                 }
324                 if (readme != null) {return readme;}
325
326                 // Locally-held file failed, so try to find gz file installed on system (eg Debian)
327                 try
328                 {
329                         File gzFile = new File("/usr/share/doc/gpsprune/readme.txt.gz");
330                         if (gzFile.exists())
331                         {
332                                 // Copy decompressed bytes from gz file into out
333                                 in = new GZIPInputStream(new FileInputStream(gzFile));
334                                 ByteArrayOutputStream out = new ByteArrayOutputStream();
335                                 byte[] buffer = new byte[8 * 1024];
336                                 int count = 0;
337                                 do {
338                                         out.write(buffer, 0, count);
339                                         count = in.read(buffer, 0, buffer.length);
340                                 } while (count != -1);
341                                 out.close();
342                                 in.close();
343                                 readme = out.toString();
344                         }
345                 }
346                 catch (IOException e) {
347                         System.err.println("Exception trying to get readme.gz : " + e.getMessage());
348                 }
349                 finally {
350                         try {in.close();} catch (Exception e) {}
351                 }
352                 if (readme != null) {return readme;}
353                 // Only show first error message if couldn't get readme from gz either
354                 if (errorMessage != null) {
355                         System.err.println("Exception trying to get readme: " + errorMessage);
356                 }
357                 return I18nManager.getText("error.readme.notfound");
358         }
359
360         /**
361          * Show window
362          */
363         public void begin()
364         {
365                 if (_dialog == null)
366                 {
367                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
368                         _dialog.getContentPane().add(makeContents());
369                         _dialog.pack();
370                 }
371                 _tabs.setSelectedIndex(0);
372                 checkInstalls();
373                 _dialog.setVisible(true);
374                 _okButton.requestFocus();
375         }
376
377         /**
378          * Check the installed components and set the label texts accordingly
379          */
380         private void checkInstalls()
381         {
382                 String yesText = I18nManager.getText("dialog.about.yes");
383                 String noText = I18nManager.getText("dialog.about.no");
384                 _installedLabels[0].setText(WindowFactory.isJava3dEnabled()?yesText:noText);
385                 final int[] tools = {ExternalTools.TOOL_EXIFTOOL, ExternalTools.TOOL_GPSBABEL,
386                         ExternalTools.TOOL_GNUPLOT, ExternalTools.TOOL_XERCES};
387                 for (int i=0; i<tools.length; i++) {
388                         _installedLabels[i+1].setText(ExternalTools.isToolInstalled(tools[i])?yesText:noText);
389                 }
390         }
391 }