Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/AboutDialog.java
40948 views
/*1* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jconsole;2627import java.awt.BasicStroke;28import java.awt.BorderLayout;29import java.awt.Color;30import java.awt.Desktop;31import java.awt.FlowLayout;32import java.awt.Graphics2D;33import java.awt.Graphics;34import java.awt.Rectangle;35import java.awt.Shape;36import java.awt.Stroke;37import java.awt.event.ActionEvent;38import java.awt.event.FocusEvent;39import java.awt.event.FocusListener;40import java.awt.event.KeyAdapter;41import java.awt.event.KeyEvent;42import java.awt.geom.Rectangle2D;43import java.beans.PropertyVetoException;44import java.net.URI;45import javax.swing.AbstractAction;46import javax.swing.Action;47import javax.swing.BorderFactory;48import javax.swing.Icon;49import javax.swing.ImageIcon;50import javax.swing.JButton;51import javax.swing.JComponent;52import javax.swing.JEditorPane;53import javax.swing.JLabel;54import javax.swing.JPanel;55import javax.swing.border.EmptyBorder;56import javax.swing.event.HyperlinkEvent;57import javax.swing.event.HyperlinkListener;58import javax.swing.text.BadLocationException;59import javax.swing.text.DefaultHighlighter;60import javax.swing.text.Document;61import javax.swing.text.Highlighter;62import javax.swing.text.JTextComponent;63import javax.swing.text.View;6465import static java.awt.BorderLayout.CENTER;66import static java.awt.BorderLayout.NORTH;67import static java.awt.BorderLayout.SOUTH;6869import static sun.tools.jconsole.Utilities.setAccessibleDescription;70import static sun.tools.jconsole.Utilities.setAccessibleName;7172@SuppressWarnings("serial")73public class AboutDialog extends InternalDialog {7475private static final Color textColor = new Color(87, 88, 89);76private static final Color bgColor = new Color(232, 237, 241);77private static final Color borderColor = Color.black;7879private Icon mastheadIcon =80new MastheadIcon(Messages.HELP_ABOUT_DIALOG_MASTHEAD_TITLE);8182private static AboutDialog aboutDialog;8384private JLabel statusBar;85private Action closeAction;86private JEditorPane helpLink;87private final String urlStr = getOnlineDocUrl();8889public AboutDialog(JConsole jConsole) {90super(jConsole, Messages.HELP_ABOUT_DIALOG_TITLE, false);9192setAccessibleDescription(this, Messages.HELP_ABOUT_DIALOG_ACCESSIBLE_DESCRIPTION);93setDefaultCloseOperation(HIDE_ON_CLOSE);94setResizable(false);95JComponent cp = (JComponent)getContentPane();9697createActions();9899JLabel mastheadLabel = new JLabel(mastheadIcon);100setAccessibleName(mastheadLabel,101Messages.HELP_ABOUT_DIALOG_MASTHEAD_ACCESSIBLE_NAME);102103JPanel mainPanel = new TPanel(0, 0);104mainPanel.add(mastheadLabel, NORTH);105106String jConsoleVersion = Version.getVersion();107String vmName = System.getProperty("java.vm.name");108String vmVersion = System.getProperty("java.vm.version");109String locUrlStr = urlStr;110if (isBrowseSupported()) {111locUrlStr = "<a style='color:#35556b' href=\"" + locUrlStr + "\">" + locUrlStr + "</a>";112}113114JPanel infoAndLogoPanel = new JPanel(new BorderLayout(10, 10));115infoAndLogoPanel.setBackground(bgColor);116117String colorStr = String.format("%06x", textColor.getRGB() & 0xFFFFFF);118helpLink = new JEditorPane("text/html",119"<html><font color=#"+ colorStr + ">" +120Resources.format(Messages.HELP_ABOUT_DIALOG_JCONSOLE_VERSION, jConsoleVersion) +121"<p>" + Resources.format(Messages.HELP_ABOUT_DIALOG_JAVA_VERSION, (vmName +", "+ vmVersion)) +122"<p>" + locUrlStr + "</html>");123124helpLink.setOpaque(false);125helpLink.setEditable(false);126helpLink.setForeground(textColor);127128mainPanel.setBorder(BorderFactory.createLineBorder(borderColor));129infoAndLogoPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));130131helpLink.addKeyListener(new KeyAdapter() {132@Override133public void keyPressed(KeyEvent e) {134if ((e.getKeyCode() == KeyEvent.VK_ENTER) || (e.getKeyCode() == KeyEvent.VK_SPACE)) {135browse(urlStr);136e.consume();137}138}139});140141helpLink.addFocusListener(new FocusListener() {142@Override143public void focusGained(FocusEvent e) {144highlight();145}146@Override147public void focusLost(FocusEvent e) {148removeHighlights();149}150});151152helpLink.addHyperlinkListener(new HyperlinkListener() {153public void hyperlinkUpdate(HyperlinkEvent e) {154if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {155browse(e.getDescription());156}157}158});159infoAndLogoPanel.add(helpLink, NORTH);160161ImageIcon brandLogoIcon = new ImageIcon(getClass().getResource("resources/brandlogo.png"));162JLabel brandLogo = new JLabel(brandLogoIcon, JLabel.LEADING);163164JButton closeButton = new JButton(closeAction);165166JPanel bottomPanel = new TPanel(0, 0);167JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));168buttonPanel.setOpaque(false);169170mainPanel.add(infoAndLogoPanel, CENTER);171cp.add(bottomPanel, SOUTH);172173infoAndLogoPanel.add(brandLogo, SOUTH);174175buttonPanel.setBorder(new EmptyBorder(2, 12, 2, 12));176buttonPanel.add(closeButton);177bottomPanel.add(buttonPanel, NORTH);178179statusBar = new JLabel(" ");180bottomPanel.add(statusBar, SOUTH);181182cp.add(mainPanel, NORTH);183184pack();185setLocationRelativeTo(jConsole);186Utilities.updateTransparency(this);187}188189public void highlight() {190try {191removeHighlights();192193Highlighter hilite = helpLink.getHighlighter();194Document doc = helpLink.getDocument();195String text = doc.getText(0, doc.getLength());196int pos = text.indexOf(urlStr, 0);197hilite.addHighlight(pos, pos + urlStr.length(), new HighlightPainter());198} catch (BadLocationException e) {199// ignore200}201}202203public void removeHighlights() {204Highlighter hilite = helpLink.getHighlighter();205hilite.removeAllHighlights();206}207208public void showDialog() {209statusBar.setText(" ");210setVisible(true);211try {212// Bring to front of other dialogs213setSelected(true);214} catch (PropertyVetoException e) {215// ignore216}217}218219private static AboutDialog getAboutDialog(JConsole jConsole) {220if (aboutDialog == null) {221aboutDialog = new AboutDialog(jConsole);222}223return aboutDialog;224}225226static void showAboutDialog(JConsole jConsole) {227getAboutDialog(jConsole).showDialog();228}229230static void browseUserGuide(JConsole jConsole) {231getAboutDialog(jConsole).browse(getOnlineDocUrl());232}233234static boolean isBrowseSupported() {235return (Desktop.isDesktopSupported() &&236Desktop.getDesktop().isSupported(Desktop.Action.BROWSE));237}238239void browse(String urlStr) {240try {241Desktop.getDesktop().browse(new URI(urlStr));242} catch (Exception ex) {243showDialog();244statusBar.setText(ex.getLocalizedMessage());245if (JConsole.isDebug()) {246ex.printStackTrace();247}248}249}250251private void createActions() {252closeAction = new AbstractAction(Messages.CLOSE) {253public void actionPerformed(ActionEvent ev) {254setVisible(false);255statusBar.setText("");256}257};258}259260private static String getOnlineDocUrl() {261String version = Integer.toString(Runtime.version().feature());262return Resources.format(Messages.HELP_ABOUT_DIALOG_USER_GUIDE_LINK_URL,263version);264}265266private static class TPanel extends JPanel {267TPanel(int hgap, int vgap) {268super(new BorderLayout(hgap, vgap));269setOpaque(false);270}271}272273private static class HighlightPainter274extends DefaultHighlighter.DefaultHighlightPainter {275276public HighlightPainter() {277super(null);278}279@Override280public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds,281JTextComponent c, View view) {282g.setColor(c.getSelectionColor());283Rectangle alloc;284285if (bounds instanceof Rectangle) {286alloc = (Rectangle)bounds;287} else {288alloc = bounds.getBounds();289}290291Graphics2D g2d = (Graphics2D)g;292293float[] dash = { 2F, 2F };294Stroke dashedStroke = new BasicStroke(1F, BasicStroke.CAP_SQUARE,295BasicStroke.JOIN_MITER, 3F, dash, 0F);296g2d.fill(dashedStroke.createStrokedShape(297new Rectangle2D.Float(alloc.x, alloc.y,298alloc.width - 1, alloc.height - 1)));299return alloc;300}301}302}303304305