001/*
002 * Copyright (c) 2011 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-11-26 22:23:07 +0000 (Mon, 26 Nov 2012) $' 
007 * '$Revision: 31125 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.provenance.gui;
031
032import java.awt.Color;
033import java.awt.Cursor;
034import java.awt.Graphics;
035import java.awt.GridBagConstraints;
036import java.awt.GridBagLayout;
037import java.awt.event.KeyEvent;
038import java.awt.event.KeyListener;
039import java.awt.event.MouseAdapter;
040import java.awt.event.MouseMotionAdapter;
041
042import javax.swing.JComponent;
043import javax.swing.JLabel;
044import javax.swing.UIManager;
045import javax.swing.border.Border;
046import javax.swing.border.EmptyBorder;
047
048/*
049 *  Simple implementation of a Glass Pane that will capture and ignore all
050 *  events as well paint the glass pane to give the frame a "disabled" look.
051 *
052 *  The background color of the glass pane should use a color with an
053 *  alpha value to create the disabled look.
054 */
055public class DisabledGlassPane extends JComponent implements KeyListener 
056{
057
058        public DisabledGlassPane() 
059        {
060                // Set glass pane properties
061
062                setOpaque(false);
063                Color base = UIManager.getColor("inactiveCaptionBorder");
064                Color background = null;
065                if (base == null){
066                        background = new Color(LIGHT_GRAY_RED, LIGHT_GRAY_GREEN,
067                                        LIGHT_GRAY_BLUE, BACKGROUND_ALPHA);
068                }
069                else{
070                        background = new Color(base.getRed(), base.getGreen(),
071                                base.getBlue(), BACKGROUND_ALPHA);
072                }
073                setBackground(background);
074                setLayout(new GridBagLayout());
075
076                // Add a message label to the glass pane
077
078                add(message, new GridBagConstraints());
079                message.setOpaque(true);
080                message.setBorder(MESSAGE_BORDER);
081
082                // Disable Mouse, Key and Focus events for the glass pane
083
084                addMouseListener(new MouseAdapter() {
085                });
086                addMouseMotionListener(new MouseMotionAdapter() {
087                });
088
089                addKeyListener(this);
090
091                setFocusTraversalKeysEnabled(false);
092        }
093
094        /*
095         * The component is transparent but we want to paint the background to give
096         * it the disabled look.
097         */
098        @Override
099        protected void paintComponent(Graphics g)
100        {
101                g.setColor(getBackground());
102                g.fillRect(0, 0, getSize().width, getSize().height);
103        }
104
105        /*
106         * The background color of the message label will be the same as the
107         * background of the glass pane without the alpha value
108         */
109        @Override
110        public void setBackground(Color background)
111        {
112                super.setBackground(background);
113
114                Color messageBackground = new Color(background.getRGB());
115                message.setBackground(messageBackground);
116        }
117
118        //
119        // Implement the KeyListener to consume events
120        //
121        public void keyPressed(KeyEvent e)
122        {
123                e.consume();
124        }
125
126        public void keyTyped(KeyEvent e)
127        {
128        }
129
130        public void keyReleased(KeyEvent e)
131        {
132                e.consume();
133        }
134
135        /*
136         * Make the glass pane visible and change the cursor to the wait cursor
137         * 
138         * A message can be displayed and it will be centered on the frame.
139         */
140        public void activate(String text)
141        {
142                if (text != null && text.length() > 0)
143                {
144                        message.setVisible(true);
145                        message.setText(text);
146                        message.setForeground(getForeground());
147                }
148                else 
149                {
150                        message.setVisible(false);
151                }
152
153                setVisible(true);
154                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
155                requestFocusInWindow();
156        }
157
158        /*
159         * Hide the glass pane and restore the cursor
160         */
161        public void deactivate()
162        {
163                setCursor(null);
164                setVisible(false);
165        }
166        
167        private final static Border MESSAGE_BORDER = new EmptyBorder(10, 10, 10, 10);
168        private JLabel message = new JLabel();
169        private static int BACKGROUND_ALPHA = 128;
170        // only used if UIManager.getColor("inactiveCaptionBorder") returns null
171        private static int LIGHT_GRAY_RED = 211;
172        private static int LIGHT_GRAY_GREEN = 211;
173        private static int LIGHT_GRAY_BLUE = 211;
174        
175}