/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. */
  24. package hudson.remoting.jnlp;
  25. import javax.swing.UIManager;
  26. import javax.swing.UnsupportedLookAndFeelException;
  27. import javax.swing.JPanel;
  28. import javax.swing.JComponent;
  29. import javax.swing.Icon;
  30. import javax.swing.JLabel;
  31. import java.awt.GridBagConstraints;
  32. import java.awt.GridBagLayout;
  33. import java.awt.Dimension;
  34. /**
  35. * GUI related helper code
  36. * @author Kohsuke Kawaguchi
  37. */
  38. public class GUI {
  39. /**
  40. * Sets to the platform native look and feel.
  41. *
  42. * see http://javaalmanac.com/egs/javax.swing/LookFeelNative.html
  43. */
  44. public static void setUILookAndFeel() {
  45. try {
  46. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  47. } catch (InstantiationException e) {
  48. } catch (ClassNotFoundException e) {
  49. } catch (UnsupportedLookAndFeelException e) {
  50. } catch (IllegalAccessException e) {
  51. }
  52. }
  53. // Set up contraints so that the user supplied component and the
  54. // background image label overlap and resize identically
  55. private static final GridBagConstraints gbc;
  56. static {
  57. gbc = new GridBagConstraints();
  58. gbc.gridx = 0;
  59. gbc.gridy = 0;
  60. gbc.weightx = 1.0;
  61. gbc.weighty = 1.0;
  62. gbc.fill = GridBagConstraints.BOTH;
  63. gbc.anchor = GridBagConstraints.NORTHWEST;
  64. }
  65. public static JPanel wrapInBackgroundImage(JComponent component,
  66. Icon backgroundIcon,
  67. int verticalAlignment,
  68. int horizontalAlignment) {
  69. // make the passed in swing component transparent
  70. component.setOpaque(false);
  71. // create wrapper JPanel
  72. JPanel backgroundPanel = new JPanel(new GridBagLayout());
  73. // add the passed in swing component first to ensure that it is in front
  74. backgroundPanel.add(component, gbc);
  75. // create a label to paint the background image
  76. JLabel backgroundImage = new JLabel(backgroundIcon);
  77. // set minimum and preferred sizes so that the size of the image
  78. // does not affect the layout size
  79. backgroundImage.setPreferredSize(new Dimension(1,1));
  80. backgroundImage.setMinimumSize(new Dimension(1,1));
  81. // align the image as specified.
  82. backgroundImage.setVerticalAlignment(verticalAlignment);
  83. backgroundImage.setHorizontalAlignment(horizontalAlignment);
  84. // add the background label
  85. backgroundPanel.add(backgroundImage, gbc);
  86. // return the wrapper
  87. return backgroundPanel;
  88. }
  89. }