diff --git a/remoting/src/main/java/hudson/remoting/Engine.java b/remoting/src/main/java/hudson/remoting/Engine.java index b716946dbb05750fe13437050fcdaef6a32b0d30..7c80e2e7b12540d4b811c17c61fab69684efd304 100644 --- a/remoting/src/main/java/hudson/remoting/Engine.java +++ b/remoting/src/main/java/hudson/remoting/Engine.java @@ -86,6 +86,7 @@ public class Engine extends Thread { listener.status("Connected"); channel.join(); listener.status("Terminated"); + listener.onDisconnect(); // try to connect back to the server every 10 secs. waitForServerToBack(); diff --git a/remoting/src/main/java/hudson/remoting/EngineListener.java b/remoting/src/main/java/hudson/remoting/EngineListener.java index 6947a89f3ff00a0dcf499a5cf6b930fc3d17b8c8..2d0081667fdb62e0e3296cb1f1f6700185eed7df 100644 --- a/remoting/src/main/java/hudson/remoting/EngineListener.java +++ b/remoting/src/main/java/hudson/remoting/EngineListener.java @@ -10,4 +10,9 @@ package hudson.remoting; public interface EngineListener { void status(String msg); void error(Throwable t); + + /** + * Called when a connection is terminated. + */ + void onDisconnect(); } diff --git a/remoting/src/main/java/hudson/remoting/jnlp/GuiListener.java b/remoting/src/main/java/hudson/remoting/jnlp/GuiListener.java new file mode 100644 index 0000000000000000000000000000000000000000..cae9b312967ecc975661e4fce0ba1f2a617e609f --- /dev/null +++ b/remoting/src/main/java/hudson/remoting/jnlp/GuiListener.java @@ -0,0 +1,50 @@ +package hudson.remoting.jnlp; + +import hudson.remoting.EngineListener; + +import javax.swing.*; +import java.io.StringWriter; +import java.io.PrintWriter; + +/** + * {@link EngineListener} implementation that shows GUI. + */ +public final class GuiListener implements EngineListener { + public final MainDialog frame; + + public GuiListener() { + GUI.setUILookAndFeel(); + frame = new MainDialog(); + frame.setVisible(true); + } + + public void status(final String msg) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + frame.status(msg); + } + }); + } + + public void error(final Throwable t) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + StringWriter sw = new StringWriter(); + t.printStackTrace(new PrintWriter(sw)); + JOptionPane.showMessageDialog( + frame,sw.toString(),"Error", + JOptionPane.ERROR_MESSAGE); + System.exit(-1); + } + }); + } + + public void onDisconnect() { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + // discard all the menu items that might have been added by the master. + frame.resetMenuBar(); + } + }); + } +} diff --git a/remoting/src/main/java/hudson/remoting/jnlp/Main.java b/remoting/src/main/java/hudson/remoting/jnlp/Main.java index ef600e49b6fa32ad92bb75b7abe2d474524f3328..46eb1bd61199b000a8f5472895c6a150f7b8d635 100644 --- a/remoting/src/main/java/hudson/remoting/jnlp/Main.java +++ b/remoting/src/main/java/hudson/remoting/jnlp/Main.java @@ -5,10 +5,6 @@ import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.CmdLineException; -import javax.swing.SwingUtilities; -import javax.swing.JOptionPane; -import java.io.StringWriter; -import java.io.PrintWriter; import java.util.logging.Logger; import java.util.logging.Level; import java.util.List; @@ -79,40 +75,6 @@ public class Main { engine.start(); } - /** - * {@link EngineListener} implementation that shows GUI. - */ - public static final class GuiListener implements EngineListener { - public final MainDialog frame; - - public GuiListener() { - GUI.setUILookAndFeel(); - frame = new MainDialog(); - frame.setVisible(true); - } - - public void status(final String msg) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - frame.status(msg); - } - }); - } - - public void error(final Throwable t) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - StringWriter sw = new StringWriter(); - t.printStackTrace(new PrintWriter(sw)); - JOptionPane.showMessageDialog( - frame,sw.toString(),"Error", - JOptionPane.ERROR_MESSAGE); - System.exit(-1); - } - }); - } - } - /** * {@link EngineListener} implementation that sends output to {@link Logger}. */ @@ -129,6 +91,9 @@ public class Main { LOGGER.log(Level.SEVERE, t.getMessage(), t); System.exit(-1); } + + public void onDisconnect() { + } } private static final Logger LOGGER = Logger.getLogger(Main.class.getName()); diff --git a/remoting/src/main/java/hudson/remoting/jnlp/MainDialog.java b/remoting/src/main/java/hudson/remoting/jnlp/MainDialog.java index c25b985f77cd687efd2ccff803229c2e90758428..bcbb22a3706f649a0541342bfeaabd2dfbd80798 100644 --- a/remoting/src/main/java/hudson/remoting/jnlp/MainDialog.java +++ b/remoting/src/main/java/hudson/remoting/jnlp/MainDialog.java @@ -1,24 +1,17 @@ package hudson.remoting.jnlp; -import hudson.remoting.jnlp.GUI; - -import javax.swing.BorderFactory; -import javax.swing.ImageIcon; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.WindowConstants; -import java.awt.BorderLayout; -import java.awt.Color; -import java.awt.Dimension; -import java.awt.HeadlessException; +import javax.swing.*; +import java.awt.*; /** + * Main window for JNLP slave agent. + * * @author Kohsuke Kawaguchi */ public class MainDialog extends JFrame { - private JLabel statusLabel; + private MainMenu mainMenu; + private final JLabel statusLabel; public MainDialog() throws HeadlessException { super("Hudson slave agent"); @@ -33,6 +26,8 @@ public class MainDialog extends JFrame { foregroundPanel.add(statusLabel, BorderLayout.CENTER); setContentPane(GUI.wrapInBackgroundImage(foregroundPanel, background,JLabel.BOTTOM,JLabel.LEADING)); + resetMenuBar(); + pack(); setSize(new Dimension(250,150)); @@ -42,6 +37,28 @@ public class MainDialog extends JFrame { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } + /** + * Gets the main menu of this window, so that the caller can add + * additional menu items. + * + * @return never null. + */ + public MainMenu getMainMenu() { + return mainMenu; + } + + public void resetMenuBar() { + mainMenu = new MainMenu(this); + if(mainMenu.getComponentCount()>0) { + setJMenuBar(mainMenu); + mainMenu.commit(); + } else { + setJMenuBar(null); + if(isVisible()) + setVisible(true); // work around for paint problem. see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4949810 + } + } + public void status(String msg) { statusLabel.setText(msg); } diff --git a/remoting/src/main/java/hudson/remoting/jnlp/MainMenu.java b/remoting/src/main/java/hudson/remoting/jnlp/MainMenu.java new file mode 100644 index 0000000000000000000000000000000000000000..74edfb26b4e315425f80d257da57187d1c13937e --- /dev/null +++ b/remoting/src/main/java/hudson/remoting/jnlp/MainMenu.java @@ -0,0 +1,44 @@ +package hudson.remoting.jnlp; + +import javax.swing.*; +import java.awt.event.KeyEvent; + +/** + * Main menu of {@link MainDialog}. + * + * @author Kohsuke Kawaguchi + */ +public final class MainMenu extends JMenuBar { + private final MainDialog owner; + private JMenu fileMenu; + + MainMenu(MainDialog owner) { + this.owner = owner; + } + + /** + * Obtains the file menu (and creates it if necessary), + * so that the caller can add items in this menu. + */ + public JMenu getFileMenu() { + if(fileMenu==null) { + fileMenu = new JMenu("File"); + fileMenu.setMnemonic(KeyEvent.VK_F); + add(fileMenu,0); + } + return fileMenu; + } + + /** + * Reflects the changes made in the menu objects to GUI. + */ + public void commit() { + invalidate(); + repaint(); + if(getComponentCount()>0) { + owner.setJMenuBar(this); + owner.invalidate(); + owner.repaint(); + } + } +}