001: /*
002: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jconsole;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030:
031: import javax.swing.*;
032:
033: import static javax.swing.JLayeredPane.*;
034: import static sun.tools.jconsole.Resources.*;
035:
036: /**
037: * Used instead of JDialog in a JDesktopPane/JInternalFrame environment.
038: */
039: @SuppressWarnings("serial")
040: public class InternalDialog extends JInternalFrame {
041: protected JLabel statusBar;
042:
043: public InternalDialog(JConsole jConsole, String title, boolean modal) {
044: super (title, true, true, false, false);
045:
046: setLayer(PALETTE_LAYER);
047: putClientProperty("JInternalFrame.frameType", "optionDialog");
048:
049: jConsole.getDesktopPane().add(this );
050:
051: getActionMap().put("cancel", new AbstractAction() {
052: public void actionPerformed(ActionEvent evt) {
053: setVisible(false);
054: if (statusBar != null) {
055: statusBar.setText("");
056: }
057: }
058: });
059: InputMap inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
060: inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
061: "cancel");
062: }
063:
064: public void setLocationRelativeTo(Component c) {
065: setLocation((c.getWidth() - getWidth()) / 2,
066: (c.getHeight() - getHeight()) / 2);
067: }
068:
069: protected class MastheadIcon implements Icon {
070: // Important: Assume image background is white!
071: private ImageIcon leftIcon = new ImageIcon(InternalDialog.class
072: .getResource("resources/masthead-left.png"));
073: private ImageIcon rightIcon = new ImageIcon(
074: InternalDialog.class
075: .getResource("resources/masthead-right.png"));
076:
077: private Font font = Font.decode(getText("Masthead.font"));
078: private int gap = 10;
079: private String title;
080:
081: public MastheadIcon(String title) {
082: this .title = title;
083: }
084:
085: public synchronized void paintIcon(Component c, Graphics g,
086: int x, int y) {
087: // Clone the Graphics object
088: g = g.create();
089:
090: // Ignore x to make sure we fill entire component width
091: x = 0;
092: int width = c.getWidth();
093: int lWidth = leftIcon.getIconWidth();
094: int rWidth = rightIcon.getIconWidth();
095: int height = getIconHeight();
096: int textHeight = g.getFontMetrics(font).getAscent();
097:
098: g.setColor(Color.white);
099: g.fillRect(x, y, width, height);
100:
101: leftIcon.paintIcon(c, g, x, y);
102: rightIcon.paintIcon(c, g, width - rWidth, y);
103:
104: g.setFont(font);
105: ((Graphics2D) g).setRenderingHint(
106: RenderingHints.KEY_TEXT_ANTIALIASING,
107: RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
108: g.setColor(new Color(0x35556b));
109: g.drawString(title, lWidth + gap, height / 2 + textHeight
110: / 2);
111: }
112:
113: public int getIconWidth() {
114: int textWidth = 0;
115: Graphics g = getGraphics();
116: if (g != null) {
117: FontMetrics fm = g.getFontMetrics(font);
118: if (fm != null) {
119: textWidth = fm.stringWidth(title);
120: }
121: }
122: return (leftIcon.getIconWidth() + gap + textWidth + gap + rightIcon
123: .getIconWidth());
124: }
125:
126: public int getIconHeight() {
127: return leftIcon.getIconHeight();
128: }
129: }
130: }
|