Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/InternalDialog.java
40948 views
/*1* Copyright (c) 2005, 2012, 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.*;28import java.awt.event.*;2930import javax.swing.*;313233import static javax.swing.JLayeredPane.*;3435/**36* Used instead of JDialog in a JDesktopPane/JInternalFrame environment.37*/38@SuppressWarnings("serial")39public class InternalDialog extends JInternalFrame {40protected JLabel statusBar;4142public InternalDialog(JConsole jConsole, String title, boolean modal) {43super(title, true, true, false, false);4445setLayer(PALETTE_LAYER);46putClientProperty("JInternalFrame.frameType", "optionDialog");4748jConsole.getDesktopPane().add(this);495051getActionMap().put("cancel", new AbstractAction() {52public void actionPerformed(ActionEvent evt) {53setVisible(false);54if (statusBar != null) {55statusBar.setText("");56}57}58});59InputMap inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);60inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");61}6263public void setLocationRelativeTo(Component c) {64setLocation((c.getWidth() - getWidth()) / 2,65(c.getHeight() - getHeight()) / 2);66}6768protected class MastheadIcon implements Icon {69// Important: Assume image background is white!70private ImageIcon leftIcon =71new ImageIcon(InternalDialog.class.getResource("resources/masthead-left.png"));72private ImageIcon rightIcon =73new ImageIcon(InternalDialog.class.getResource("resources/masthead-right.png"));7475private Font font = Font.decode(Messages.MASTHEAD_FONT);76private int gap = 10;77private String title;7879public MastheadIcon(String title) {80this.title = title;81}8283public synchronized void paintIcon(Component c, Graphics g, int x, int y) {84// Clone the Graphics object85g = g.create();8687// Ignore x to make sure we fill entire component width88x = 0;89int width = c.getWidth();90int lWidth = leftIcon.getIconWidth();91int rWidth = rightIcon.getIconWidth();92int height = getIconHeight();93int textHeight = g.getFontMetrics(font).getAscent();9495g.setColor(Color.white);96g.fillRect(x, y, width, height);9798leftIcon.paintIcon(c, g, x, y);99rightIcon.paintIcon(c, g, width - rWidth, y);100101g.setFont(font);102((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,103RenderingHints.VALUE_TEXT_ANTIALIAS_ON);104g.setColor(new Color(0x35556b));105g.drawString(title, lWidth + gap, height/2 + textHeight/2);106}107108public int getIconWidth() {109int textWidth = 0;110Graphics g = getGraphics();111if (g != null) {112FontMetrics fm = g.getFontMetrics(font);113if (fm != null) {114textWidth = fm.stringWidth(title);115}116}117return (leftIcon.getIconWidth() + gap + textWidth +118gap + rightIcon.getIconWidth());119}120121122public int getIconHeight() {123return leftIcon.getIconHeight();124}125}126}127128129