PageRenderTime 48ms CodeModel.GetById 30ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/hudson-remoting/src/main/java/hudson/remoting/jnlp/GUI.java

http://github.com/hudson/hudson
Java | 102 lines | 46 code | 13 blank | 43 comment | 0 complexity | 05f717d1fef620d9ca706387cb5062e5 MD5 | raw file
  1/*
  2 * The MIT License
  3 * 
  4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
  5 * 
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 * 
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 * 
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24package hudson.remoting.jnlp;
 25
 26import javax.swing.UIManager;
 27import javax.swing.UnsupportedLookAndFeelException;
 28import javax.swing.JPanel;
 29import javax.swing.JComponent;
 30import javax.swing.Icon;
 31import javax.swing.JLabel;
 32import java.awt.GridBagConstraints;
 33import java.awt.GridBagLayout;
 34import java.awt.Dimension;
 35
 36/**
 37 * GUI related helper code
 38 * @author Kohsuke Kawaguchi
 39 */
 40public class GUI {
 41    /**
 42     * Sets to the platform native look and feel.
 43     *
 44     * see http://javaalmanac.com/egs/javax.swing/LookFeelNative.html
 45     */
 46    public static void setUILookAndFeel() {
 47        try {
 48            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 49        } catch (InstantiationException e) {
 50        } catch (ClassNotFoundException e) {
 51        } catch (UnsupportedLookAndFeelException e) {
 52        } catch (IllegalAccessException e) {
 53        }
 54    }
 55
 56    // Set up contraints so that the user supplied component and the
 57    // background image label overlap and resize identically
 58    private static final GridBagConstraints gbc;
 59
 60    static {
 61        gbc = new GridBagConstraints();
 62        gbc.gridx = 0;
 63        gbc.gridy = 0;
 64        gbc.weightx = 1.0;
 65        gbc.weighty = 1.0;
 66        gbc.fill = GridBagConstraints.BOTH;
 67        gbc.anchor = GridBagConstraints.NORTHWEST;
 68    }
 69
 70    public static JPanel wrapInBackgroundImage(JComponent component,
 71             Icon backgroundIcon,
 72             int verticalAlignment,
 73             int horizontalAlignment) {
 74
 75         // make the passed in swing component transparent
 76         component.setOpaque(false);
 77
 78         // create wrapper JPanel
 79         JPanel backgroundPanel = new JPanel(new GridBagLayout());
 80
 81         // add the passed in swing component first to ensure that it is in front
 82         backgroundPanel.add(component, gbc);
 83
 84         // create a label to paint the background image
 85         JLabel backgroundImage = new JLabel(backgroundIcon);
 86
 87         // set minimum and preferred sizes so that the size of the image
 88         // does not affect the layout size
 89         backgroundImage.setPreferredSize(new Dimension(1,1));
 90         backgroundImage.setMinimumSize(new Dimension(1,1));
 91
 92         // align the image as specified.
 93         backgroundImage.setVerticalAlignment(verticalAlignment);
 94         backgroundImage.setHorizontalAlignment(horizontalAlignment);
 95
 96         // add the background label
 97         backgroundPanel.add(backgroundImage, gbc);
 98
 99         // return the wrapper
100         return backgroundPanel;
101     }
102}