提交 c4c7cd06 编写于 作者: K kohsuke

adding abstraction to arbitrate menu bar

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@12403 71c3de6d-444a-0410-be80-ed276b4c234a
上级 b9f6524f
...@@ -86,6 +86,7 @@ public class Engine extends Thread { ...@@ -86,6 +86,7 @@ public class Engine extends Thread {
listener.status("Connected"); listener.status("Connected");
channel.join(); channel.join();
listener.status("Terminated"); listener.status("Terminated");
listener.onDisconnect();
// try to connect back to the server every 10 secs. // try to connect back to the server every 10 secs.
waitForServerToBack(); waitForServerToBack();
......
...@@ -10,4 +10,9 @@ package hudson.remoting; ...@@ -10,4 +10,9 @@ package hudson.remoting;
public interface EngineListener { public interface EngineListener {
void status(String msg); void status(String msg);
void error(Throwable t); void error(Throwable t);
/**
* Called when a connection is terminated.
*/
void onDisconnect();
} }
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();
}
});
}
}
...@@ -5,10 +5,6 @@ import org.kohsuke.args4j.CmdLineParser; ...@@ -5,10 +5,6 @@ import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException; 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.Logger;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.List; import java.util.List;
...@@ -79,40 +75,6 @@ public class Main { ...@@ -79,40 +75,6 @@ public class Main {
engine.start(); 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}. * {@link EngineListener} implementation that sends output to {@link Logger}.
*/ */
...@@ -129,6 +91,9 @@ public class Main { ...@@ -129,6 +91,9 @@ public class Main {
LOGGER.log(Level.SEVERE, t.getMessage(), t); LOGGER.log(Level.SEVERE, t.getMessage(), t);
System.exit(-1); System.exit(-1);
} }
public void onDisconnect() {
}
} }
private static final Logger LOGGER = Logger.getLogger(Main.class.getName()); private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
......
package hudson.remoting.jnlp; package hudson.remoting.jnlp;
import hudson.remoting.jnlp.GUI; import javax.swing.*;
import java.awt.*;
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;
/** /**
* Main window for JNLP slave agent.
*
* @author Kohsuke Kawaguchi * @author Kohsuke Kawaguchi
*/ */
public class MainDialog extends JFrame { public class MainDialog extends JFrame {
private JLabel statusLabel; private MainMenu mainMenu;
private final JLabel statusLabel;
public MainDialog() throws HeadlessException { public MainDialog() throws HeadlessException {
super("Hudson slave agent"); super("Hudson slave agent");
...@@ -33,6 +26,8 @@ public class MainDialog extends JFrame { ...@@ -33,6 +26,8 @@ public class MainDialog extends JFrame {
foregroundPanel.add(statusLabel, BorderLayout.CENTER); foregroundPanel.add(statusLabel, BorderLayout.CENTER);
setContentPane(GUI.wrapInBackgroundImage(foregroundPanel, background,JLabel.BOTTOM,JLabel.LEADING)); setContentPane(GUI.wrapInBackgroundImage(foregroundPanel, background,JLabel.BOTTOM,JLabel.LEADING));
resetMenuBar();
pack(); pack();
setSize(new Dimension(250,150)); setSize(new Dimension(250,150));
...@@ -42,6 +37,28 @@ public class MainDialog extends JFrame { ...@@ -42,6 +37,28 @@ public class MainDialog extends JFrame {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 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) { public void status(String msg) {
statusLabel.setText(msg); statusLabel.setText(msg);
} }
......
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();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册