1 /*
   2  * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.jconsole;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.beans.PropertyVetoException;
  31 import java.net.URI;
  32 
  33 import javax.swing.*;
  34 import javax.swing.border.*;
  35 import javax.swing.event.*;
  36 
  37 import static java.awt.BorderLayout.*;
  38 import static sun.tools.jconsole.Utilities.*;
  39 
  40 @SuppressWarnings("serial")
  41 public class AboutDialog extends InternalDialog {
  42 
  43     private static final Color textColor     = new Color(87,   88,  89);
  44     private static final Color bgColor       = new Color(232, 237, 241);
  45     private static final Color borderColor   = Color.black;
  46 
  47     private Icon mastheadIcon =
  48         new MastheadIcon(Messages.HELP_ABOUT_DIALOG_MASTHEAD_TITLE);
  49 
  50     private static AboutDialog aboutDialog;
  51 
  52     private JLabel statusBar;
  53     private Action closeAction;
  54 
  55     public AboutDialog(JConsole jConsole) {
  56         super(jConsole, Messages.HELP_ABOUT_DIALOG_TITLE, false);
  57 
  58         setAccessibleDescription(this, Messages.HELP_ABOUT_DIALOG_ACCESSIBLE_DESCRIPTION);
  59         setDefaultCloseOperation(HIDE_ON_CLOSE);
  60         setResizable(false);
  61         JComponent cp = (JComponent)getContentPane();
  62 
  63         createActions();
  64 
  65         JLabel mastheadLabel = new JLabel(mastheadIcon);
  66         setAccessibleName(mastheadLabel,
  67                 Messages.HELP_ABOUT_DIALOG_MASTHEAD_ACCESSIBLE_NAME);
  68 
  69         JPanel mainPanel = new TPanel(0, 0);
  70         mainPanel.add(mastheadLabel, NORTH);
  71 
  72         String jConsoleVersion = Version.getVersion();
  73         String vmName = System.getProperty("java.vm.name");
  74         String vmVersion = System.getProperty("java.vm.version");
  75         String urlStr = getOnlineDocUrl();
  76         if (isBrowseSupported()) {
  77             urlStr = "<a style='color:#35556b' href=\"" + urlStr + "\">" + urlStr + "</a>";
  78         }
  79 
  80         JPanel infoAndLogoPanel = new JPanel(new BorderLayout(10, 10));
  81         infoAndLogoPanel.setBackground(bgColor);
  82 
  83         String colorStr = String.format("%06x", textColor.getRGB() & 0xFFFFFF);
  84         JEditorPane helpLink = new JEditorPane("text/html",
  85                                 "<html><font color=#"+ colorStr + ">" +
  86                         Resources.format(Messages.HELP_ABOUT_DIALOG_JCONSOLE_VERSION, jConsoleVersion) +
  87                 "<p>" + Resources.format(Messages.HELP_ABOUT_DIALOG_JAVA_VERSION, (vmName +", "+ vmVersion)) +
  88                 "<p>" + urlStr + "</html>");
  89         helpLink.setOpaque(false);
  90         helpLink.setEditable(false);
  91         helpLink.setForeground(textColor);
  92         mainPanel.setBorder(BorderFactory.createLineBorder(borderColor));
  93         infoAndLogoPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  94         helpLink.addHyperlinkListener(new HyperlinkListener() {
  95             public void hyperlinkUpdate(HyperlinkEvent e) {
  96                 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
  97                     browse(e.getDescription());
  98                 }
  99             }
 100         });
 101         infoAndLogoPanel.add(helpLink, NORTH);
 102 
 103         ImageIcon brandLogoIcon = new ImageIcon(getClass().getResource("resources/brandlogo.png"));
 104         JLabel brandLogo = new JLabel(brandLogoIcon, JLabel.LEADING);
 105 
 106         JButton closeButton = new JButton(closeAction);
 107 
 108         JPanel bottomPanel = new TPanel(0, 0);
 109         JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
 110         buttonPanel.setOpaque(false);
 111 
 112         mainPanel.add(infoAndLogoPanel, CENTER);
 113         cp.add(bottomPanel, SOUTH);
 114 
 115         infoAndLogoPanel.add(brandLogo, SOUTH);
 116 
 117         buttonPanel.setBorder(new EmptyBorder(2, 12, 2, 12));
 118         buttonPanel.add(closeButton);
 119         bottomPanel.add(buttonPanel, NORTH);
 120 
 121         statusBar = new JLabel(" ");
 122         bottomPanel.add(statusBar, SOUTH);
 123 
 124         cp.add(mainPanel, NORTH);
 125 
 126         pack();
 127         setLocationRelativeTo(jConsole);
 128         Utilities.updateTransparency(this);
 129     }
 130 
 131     public void showDialog() {
 132         statusBar.setText(" ");
 133         setVisible(true);
 134         try {
 135             // Bring to front of other dialogs
 136             setSelected(true);
 137         } catch (PropertyVetoException e) {
 138             // ignore
 139         }
 140     }
 141 
 142     private static AboutDialog getAboutDialog(JConsole jConsole) {
 143         if (aboutDialog == null) {
 144             aboutDialog = new AboutDialog(jConsole);
 145         }
 146         return aboutDialog;
 147     }
 148 
 149     static void showAboutDialog(JConsole jConsole) {
 150         getAboutDialog(jConsole).showDialog();
 151     }
 152 
 153     static void browseUserGuide(JConsole jConsole) {
 154         getAboutDialog(jConsole).browse(getOnlineDocUrl());
 155     }
 156 
 157     static boolean isBrowseSupported() {
 158         return (Desktop.isDesktopSupported() &&
 159                 Desktop.getDesktop().isSupported(Desktop.Action.BROWSE));
 160     }
 161 
 162     void browse(String urlStr) {
 163         try {
 164             Desktop.getDesktop().browse(new URI(urlStr));
 165         } catch (Exception ex) {
 166             showDialog();
 167             statusBar.setText(ex.getLocalizedMessage());
 168             if (JConsole.isDebug()) {
 169                 ex.printStackTrace();
 170             }
 171         }
 172     }
 173 
 174     private void createActions() {
 175         closeAction = new AbstractAction(Messages.CLOSE) {
 176             public void actionPerformed(ActionEvent ev) {
 177                 setVisible(false);
 178                 statusBar.setText("");
 179             }
 180         };
 181     }
 182 
 183     private static String getOnlineDocUrl() {
 184         String version = Integer.toString(Runtime.version().major());
 185         return Resources.format(Messages.HELP_ABOUT_DIALOG_USER_GUIDE_LINK_URL,
 186                                 version);
 187     }
 188 
 189     private static class TPanel extends JPanel {
 190         TPanel(int hgap, int vgap) {
 191             super(new BorderLayout(hgap, vgap));
 192             setOpaque(false);
 193         }
 194     }
 195 }