src/jdk.jconsole/share/classes/sun/tools/jconsole/InternalDialog.java
author egahlin
Mon, 30 Sep 2019 15:35:56 +0200
branchJEP-349-branch
changeset 58402 d284ff23fa4c
parent 47216 71c04702a3d5
permissions -rw-r--r--
Remove test applications
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12851
diff changeset
     2
 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jconsole;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    33
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import static javax.swing.JLayeredPane.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Used instead of JDialog in a JDesktopPane/JInternalFrame environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
@SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class InternalDialog extends JInternalFrame {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    protected JLabel statusBar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public InternalDialog(JConsole jConsole, String title, boolean modal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        super(title, true, true, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        setLayer(PALETTE_LAYER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        putClientProperty("JInternalFrame.frameType", "optionDialog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        jConsole.getDesktopPane().add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        getActionMap().put("cancel", new AbstractAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            public void actionPerformed(ActionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                if (statusBar != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    statusBar.setText("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        InputMap inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public void setLocationRelativeTo(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        setLocation((c.getWidth()  - getWidth())  / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    (c.getHeight() - getHeight()) / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected class MastheadIcon implements Icon {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // Important: Assume image background is white!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        private ImageIcon leftIcon =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            new ImageIcon(InternalDialog.class.getResource("resources/masthead-left.png"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        private ImageIcon rightIcon =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            new ImageIcon(InternalDialog.class.getResource("resources/masthead-right.png"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    76
        private Font font = Font.decode(Messages.MASTHEAD_FONT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        private int gap = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        private String title;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        public MastheadIcon(String title) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            this.title = title;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // Clone the Graphics object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            g = g.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            // Ignore x to make sure we fill entire component width
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            int width = c.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            int lWidth = leftIcon.getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            int rWidth = rightIcon.getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int height = getIconHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            int textHeight = g.getFontMetrics(font).getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            g.setColor(Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            g.fillRect(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            leftIcon.paintIcon(c, g, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            rightIcon.paintIcon(c, g, width - rWidth, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            g.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                             RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            g.setColor(new Color(0x35556b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            g.drawString(title, lWidth + gap, height/2 + textHeight/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        public int getIconWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            int textWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            Graphics g = getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (g != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                FontMetrics fm = g.getFontMetrics(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (fm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    textWidth = fm.stringWidth(title);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return (leftIcon.getIconWidth() + gap + textWidth +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    gap + rightIcon.getIconWidth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        public int getIconHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return leftIcon.getIconHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}