View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2009  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  package davmail.ui;
20  
21  import davmail.BundleMessage;
22  import davmail.DavGateway;
23  import davmail.ui.browser.DesktopBrowser;
24  import davmail.ui.tray.DavGatewayTray;
25  
26  import javax.imageio.ImageIO;
27  import javax.swing.*;
28  import javax.swing.event.HyperlinkEvent;
29  import javax.swing.text.html.HTMLEditorKit;
30  import javax.swing.text.html.StyleSheet;
31  import java.awt.*;
32  import java.awt.event.ActionListener;
33  import java.io.IOException;
34  import java.net.URISyntaxException;
35  import java.net.URL;
36  
37  /**
38   * About frame
39   */
40  public class AboutFrame extends JFrame {
41      private final JEditorPane jEditorPane;
42  
43      /**
44       * About frame.
45       */
46      public AboutFrame() {
47          setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
48          setTitle(BundleMessage.format("UI_ABOUT_DAVMAIL"));
49          try {
50              setIconImages(DavGatewayTray.getFrameIcons());
51          } catch (NoSuchMethodError error) {
52              DavGatewayTray.debug(new BundleMessage("LOG_UNABLE_TO_SET_ICON_IMAGE"));
53          }
54          try {
55              JLabel imageLabel = new JLabel();
56              ClassLoader classloader = this.getClass().getClassLoader();
57              URL imageUrl = classloader.getResource("tray32.png");
58              if (imageUrl != null) {
59                  Image iconImage = ImageIO.read(imageUrl);
60                  ImageIcon icon = new ImageIcon(iconImage);
61                  imageLabel.setIcon(icon);
62  
63                  JPanel imagePanel = new JPanel();
64                  imagePanel.add(imageLabel);
65                  add(BorderLayout.WEST, imagePanel);
66              }
67          } catch (IOException e) {
68              DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_CREATE_ICON"), e);
69          }
70  
71          jEditorPane = new JEditorPane();
72          HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
73          StyleSheet stylesheet = htmlEditorKit.getStyleSheet();
74          Font font = jEditorPane.getFont();
75          stylesheet.addRule("body { font-size:small;font-family: " + ((font == null) ? "Arial" : font.getFamily()) + '}');
76          jEditorPane.setEditorKit(htmlEditorKit);
77          jEditorPane.setContentType("text/html");
78          jEditorPane.setText(getContent(null));
79  
80          jEditorPane.setEditable(false);
81          jEditorPane.setOpaque(false);
82          jEditorPane.addHyperlinkListener(hle -> {
83              if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
84                  try {
85                      DesktopBrowser.browse(hle.getURL().toURI());
86                  } catch (URISyntaxException e) {
87                      DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_OPEN_LINK"), e);
88                  }
89                  setVisible(false);
90              }
91          });
92  
93  
94          JPanel mainPanel = new JPanel();
95          mainPanel.add(jEditorPane);
96          add(BorderLayout.CENTER, mainPanel);
97  
98          JPanel buttonPanel = new JPanel();
99          JButton ok = new JButton(BundleMessage.format("UI_BUTTON_OK"));
100         ActionListener close = evt -> setVisible(false);
101         ok.addActionListener(close);
102 
103         buttonPanel.add(ok);
104 
105         add(BorderLayout.SOUTH, buttonPanel);
106 
107         pack();
108         setResizable(false);
109         // center frame
110         setLocation(getToolkit().getScreenSize().width / 2 -
111                         getSize().width / 2,
112                 getToolkit().getScreenSize().height / 2 -
113                         getSize().height / 2);
114     }
115 
116     String getContent(String releasedVersion) {
117         Package davmailPackage = DavGateway.class.getPackage();
118         StringBuilder buffer = new StringBuilder();
119         buffer.append(BundleMessage.format("UI_ABOUT_DAVMAIL_AUTHOR"));
120         String currentVersion = davmailPackage.getImplementationVersion();
121         if (currentVersion != null) {
122             buffer.append(BundleMessage.format("UI_CURRENT_VERSION", currentVersion));
123         }
124         if ((currentVersion != null && releasedVersion != null && currentVersion.compareTo(releasedVersion) != 0)
125                 || (currentVersion == null && releasedVersion != null)) {
126             buffer.append(BundleMessage.format("UI_LATEST_VERSION", releasedVersion));
127         }
128         buffer.append(BundleMessage.format("UI_HELP_INSTRUCTIONS"));
129         return buffer.toString();
130     }
131 
132 
133     /**
134      * Update about frame content with current released version.
135      */
136     public void update() {
137         jEditorPane.setText(getContent(DavGateway.getReleasedVersion()));
138         pack();
139     }
140 
141 }