1 /*
2  * Copyright (c) 2005, 2012, 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 
31 import javax.swing.*;
32 
33 
34 import static javax.swing.JLayeredPane.*;
35 
36 /**
37  * Used instead of JDialog in a JDesktopPane/JInternalFrame environment.
38  */
39 @SuppressWarnings("serial")
40 public class InternalDialog extends JInternalFrame {
41     protected JLabel statusBar;
42 
InternalDialog(JConsole jConsole, String title, boolean modal)43     public InternalDialog(JConsole jConsole, String title, boolean modal) {
44         super(title, true, true, false, false);
45 
46         setLayer(PALETTE_LAYER);
47         putClientProperty("JInternalFrame.frameType", "optionDialog");
48 
49         jConsole.getDesktopPane().add(this);
50 
51 
52         getActionMap().put("cancel", new AbstractAction() {
53             public void actionPerformed(ActionEvent evt) {
54                 setVisible(false);
55                 if (statusBar != null) {
56                     statusBar.setText("");
57                 }
58             }
59         });
60         InputMap inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
61         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
62     }
63 
setLocationRelativeTo(Component c)64     public void setLocationRelativeTo(Component c) {
65         setLocation((c.getWidth()  - getWidth())  / 2,
66                     (c.getHeight() - getHeight()) / 2);
67     }
68 
69     protected class MastheadIcon implements Icon {
70         // Important: Assume image background is white!
71         private ImageIcon leftIcon =
72             new ImageIcon(InternalDialog.class.getResource("resources/masthead-left.png"));
73         private ImageIcon rightIcon =
74             new ImageIcon(InternalDialog.class.getResource("resources/masthead-right.png"));
75 
76         private Font font = Font.decode(Messages.MASTHEAD_FONT);
77         private int gap = 10;
78         private String title;
79 
MastheadIcon(String title)80         public MastheadIcon(String title) {
81             this.title = title;
82         }
83 
paintIcon(Component c, Graphics g, int x, int y)84         public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
85             // Clone the Graphics object
86             g = g.create();
87 
88             // Ignore x to make sure we fill entire component width
89             x = 0;
90             int width = c.getWidth();
91             int lWidth = leftIcon.getIconWidth();
92             int rWidth = rightIcon.getIconWidth();
93             int height = getIconHeight();
94             int textHeight = g.getFontMetrics(font).getAscent();
95 
96             g.setColor(Color.white);
97             g.fillRect(x, y, width, height);
98 
99             leftIcon.paintIcon(c, g, x, y);
100             rightIcon.paintIcon(c, g, width - rWidth, y);
101 
102             g.setFont(font);
103             ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
104                                              RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
105             g.setColor(new Color(0x35556b));
106             g.drawString(title, lWidth + gap, height/2 + textHeight/2);
107         }
108 
getIconWidth()109         public int getIconWidth() {
110             int textWidth = 0;
111             Graphics g = getGraphics();
112             if (g != null) {
113                 FontMetrics fm = g.getFontMetrics(font);
114                 if (fm != null) {
115                     textWidth = fm.stringWidth(title);
116                 }
117             }
118             return (leftIcon.getIconWidth() + gap + textWidth +
119                     gap + rightIcon.getIconWidth());
120         }
121 
122 
getIconHeight()123         public int getIconHeight() {
124             return leftIcon.getIconHeight();
125         }
126     }
127 }
128