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

http://github.com/hudson/hudson · Java · 101 lines · 47 code · 16 blank · 38 comment · 5 complexity · 0188131db1375ec1ebb37c86a351ac90 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 hudson.remoting.Engine;
  26. import javax.swing.*;
  27. import java.awt.*;
  28. /**
  29. * Main window for JNLP slave agent.
  30. *
  31. * @author Kohsuke Kawaguchi
  32. */
  33. public class MainDialog extends JFrame {
  34. private MainMenu mainMenu;
  35. private final JLabel statusLabel;
  36. public MainDialog() throws HeadlessException {
  37. super("Hudson slave agent");
  38. ImageIcon background = new ImageIcon(getClass().getResource("title.png"));
  39. JPanel foregroundPanel = new JPanel(new BorderLayout(10, 10));
  40. foregroundPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
  41. foregroundPanel.setOpaque(false);
  42. statusLabel = new JLabel("",JLabel.TRAILING);
  43. foregroundPanel.add(statusLabel, BorderLayout.CENTER);
  44. setContentPane(GUI.wrapInBackgroundImage(foregroundPanel, background,JLabel.BOTTOM,JLabel.LEADING));
  45. resetMenuBar();
  46. pack();
  47. setSize(new Dimension(250,150));
  48. getContentPane().setBackground(Color.WHITE);
  49. setLocationByPlatform(true);
  50. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  51. }
  52. /**
  53. * Gets the main menu of this window, so that the caller can add
  54. * additional menu items.
  55. *
  56. * @return never null.
  57. */
  58. public MainMenu getMainMenu() {
  59. return mainMenu;
  60. }
  61. public void resetMenuBar() {
  62. mainMenu = new MainMenu(this);
  63. if(mainMenu.getComponentCount()>0) {
  64. setJMenuBar(mainMenu);
  65. mainMenu.commit();
  66. } else {
  67. setJMenuBar(null);
  68. if(isVisible())
  69. setVisible(true); // work around for paint problem. see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4949810
  70. }
  71. }
  72. public void status(String msg) {
  73. statusLabel.setText(msg);
  74. }
  75. /**
  76. * If the current JVM runs a {@link MainDialog} as a JNLP slave agent,
  77. * return its reference, otherwise null.
  78. */
  79. public static MainDialog get() {
  80. Engine e = Engine.current();
  81. if(e==null) return null;
  82. if (!(e.listener instanceof GuiListener)) return null;
  83. return ((GuiListener) e.listener).frame;
  84. }
  85. }