Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/SheetDialog.java
40948 views
/*1* Copyright (c) 2005, 2006, 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.*;29import java.beans.*;3031import javax.swing.*;32import javax.swing.border.*;33import javax.swing.text.*;3435import static javax.swing.JOptionPane.*;3637@SuppressWarnings("serial")38public final class SheetDialog {39// Reusable objects40private static Rectangle iconR = new Rectangle();41private static Rectangle textR = new Rectangle();42private static Rectangle viewR = new Rectangle();43private static Insets viewInsets = new Insets(0, 0, 0, 0);4445/** Don't let anyone instantiate this class */46private SheetDialog() {47}4849static JOptionPane showOptionDialog(final VMPanel vmPanel, Object message,50int optionType, int messageType,51Icon icon, Object[] options, Object initialValue) {5253JRootPane rootPane = SwingUtilities.getRootPane(vmPanel);54JPanel glassPane = (JPanel)rootPane.getGlassPane();5556if (!(glassPane instanceof SlideAndFadeGlassPane)) {57glassPane = new SlideAndFadeGlassPane();58glassPane.setName(rootPane.getName()+".glassPane");59rootPane.setGlassPane(glassPane);60rootPane.revalidate();61}6263final SlideAndFadeGlassPane safGlassPane = (SlideAndFadeGlassPane)glassPane;6465// Workaround for the fact that JOptionPane does not handle66// limiting the width when using multi-line html messages.67// See Swing bug 5074006 and JConsole bug 642631768message = fixWrapping(message, rootPane.getWidth() - 75); // Leave room for icon6970final SheetOptionPane optionPane = new SheetOptionPane(message, messageType, optionType,71icon, options, initialValue);7273optionPane.setComponentOrientation(vmPanel.getComponentOrientation());74optionPane.addPropertyChangeListener(new PropertyChangeListener() {75public void propertyChange(PropertyChangeEvent event) {76if (event.getPropertyName().equals(VALUE_PROPERTY) &&77event.getNewValue() != null &&78event.getNewValue() != UNINITIALIZED_VALUE) {79((SlideAndFadeGlassPane)optionPane.getParent()).hide(optionPane);80}81}82});8384// Delay this (even though we're already on the EDT)85EventQueue.invokeLater(new Runnable() {86public void run() {87safGlassPane.show(optionPane);88}89});9091return optionPane;92}9394private static Object fixWrapping(Object message, final int maxWidth) {95if (message instanceof Object[]) {96Object[] arr = (Object[])message;97for (int i = 0; i < arr.length; i++) {98arr[i] = fixWrapping(arr[i], maxWidth);99}100} else if (message instanceof String &&101((String)message).startsWith("<html>")) {102message = new JLabel((String)message) {103public Dimension getPreferredSize() {104String text = getText();105Insets insets = getInsets(viewInsets);106FontMetrics fm = getFontMetrics(getFont());107Dimension pref = super.getPreferredSize();108Dimension min = getMinimumSize();109110iconR.x = iconR.y = iconR.width = iconR.height = 0;111textR.x = textR.y = textR.width = textR.height = 0;112int dx = insets.left + insets.right;113int dy = insets.top + insets.bottom;114viewR.x = dx;115viewR.y = dy;116viewR.width = viewR.height = Short.MAX_VALUE;117118View v = (View)getClientProperty("html");119if (v != null) {120// Use pref width if less than 300, otherwise121// min width up to size of window.122int w = Math.min(maxWidth,123Math.min(pref.width,124Math.max(min.width, 300)));125v.setSize((float)w, 0F);126127SwingUtilities.layoutCompoundLabel(this, fm, text, null,128getVerticalAlignment(),129getHorizontalAlignment(),130getVerticalTextPosition(),131getHorizontalTextPosition(),132viewR, iconR, textR,133getIconTextGap());134return new Dimension(textR.width + dx,135textR.height + dy);136} else {137return pref; // Should not happen138}139}140};141}142return message;143}144145private static class SlideAndFadeGlassPane extends JPanel {146SheetOptionPane optionPane;147148int fade = 20;149boolean slideIn = true;150151SlideAndFadeGlassPane() {152super(null);153setVisible(false);154setOpaque(false);155156// Grab mouse input, making the dialog modal157addMouseListener(new MouseAdapter() {});158}159160public void show(SheetOptionPane optionPane) {161this.optionPane = optionPane;162removeAll();163add(optionPane);164setVisible(true);165slideIn = true;166revalidate();167repaint();168doSlide();169}170171public void hide(SheetOptionPane optionPane) {172if (optionPane != this.optionPane) {173return;174}175176slideIn = false;177revalidate();178repaint();179doSlide();180}181182private void doSlide() {183if (optionPane.getParent() == null) {184return;185}186187if (optionPane.getWidth() == 0) {188optionPane.setSize(optionPane.getPreferredSize());189}190191int glassPaneWidth = getWidth();192if (glassPaneWidth == 0 && getParent() != null) {193glassPaneWidth = getParent().getWidth();194}195196int x = (glassPaneWidth - optionPane.getWidth()) / 2;197198if (!slideIn) {199remove(optionPane);200setVisible(false);201return;202} else {203optionPane.setLocation(x, 0);204setGrayLevel(fade);205return;206}207}208209public void setGrayLevel(int gray) {210gray = gray * 255 / 100;211setBackground(new Color(0, 0, 0, gray));212}213214public void paint(Graphics g) {215g.setColor(getBackground());216g.fillRect(0, 0, getWidth(), getHeight());217super.paint(g);218}219}220221222223static class SheetOptionPane extends JOptionPane {224SheetOptionPane(Object message, int messageType, int optionType,225Icon icon, Object[] options, Object initialValue) {226super(message, messageType, optionType, icon, options, initialValue);227228setBorder(new CompoundBorder(new LineBorder(new Color(204, 204, 204), 1),229new EmptyBorder(4, 4, 4, 4)));230}231232233private static Composite comp =234AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8F);235236private static Color bgColor = new Color(241, 239, 239);237238public void setVisible(boolean visible) {239SlideAndFadeGlassPane glassPane = (SlideAndFadeGlassPane)getParent();240if (glassPane != null) {241if (visible) {242glassPane.show(this);243} else {244glassPane.hide(this);245}246}247}248249public void paint(Graphics g) {250Graphics2D g2d = (Graphics2D)g;251Composite oldComp = g2d.getComposite();252g2d.setComposite(comp);253Color oldColor = g2d.getColor();254g2d.setColor(bgColor);255g2d.fillRect(0, 0, getWidth(), getHeight());256g2d.setColor(oldColor);257g2d.setComposite(oldComp);258super.paint(g);259}260}261262}263264265