PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

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