1 package net.sourceforge.cruisecontrol.distributed; 2 3 import org.apache.log4j.AppenderSkeleton; 4 import org.apache.log4j.Logger; 5 import org.apache.log4j.spi.LoggingEvent; 6 7 import java.awt.event.ActionEvent ; 8 import java.awt.event.ActionListener ; 9 import java.awt.event.WindowAdapter ; 10 import java.awt.event.WindowEvent ; 11 import java.awt.BorderLayout ; 12 import java.awt.Dimension ; 13 import javax.swing.JFrame ; 14 import javax.swing.JPanel ; 15 import javax.swing.JButton ; 16 import javax.swing.JTextArea ; 17 import javax.swing.JScrollPane ; 18 import javax.swing.text.BadLocationException ; 19 import javax.swing.SwingUtilities ; 20 import java.rmi.RemoteException ; 21 22 import net.sourceforge.cruisecontrol.distributed.util.MulticastDiscovery; 23 24 31 final class BuildAgentUI extends JFrame implements BuildAgent.AgentStatusListener { 33 34 private static final Logger LOG = Logger.getLogger(BuildAgentUI.class); 35 36 static final int CONSOLE_LINE_BUFFER_SIZE = 1000; 37 38 private final BuildAgent buildAgent; 39 private final JTextArea txaAgentInfo; 40 private final JButton btnStop = new JButton ("Stop"); 41 private final JTextArea txaConsole = new JTextArea (); 42 private final JScrollPane scrConsole = new JScrollPane (); 43 44 BuildAgentUI(final BuildAgent parentbuildAgent) { 45 super("CruiseControl Distributed - Build Agent"); 46 47 buildAgent = parentbuildAgent; 48 49 buildAgent.addAgentStatusListener(this); 50 51 btnStop.addActionListener(new ActionListener () { 52 public void actionPerformed(final ActionEvent e) { 53 exitForm(); 54 } 55 }); 56 addWindowListener(new WindowAdapter () { 57 public void windowClosing(final WindowEvent evt) { 58 exitForm(); 59 } 60 }); 61 62 txaConsole.setFont(new java.awt.Font ("Courier New", 0, 12)); 63 64 scrConsole.setViewportView(txaConsole); 65 scrConsole.setPreferredSize(new Dimension (500, 300)); 66 67 txaConsole.setEditable(false); 68 buildAgent.LOG.addAppender(new Log4JJTextAreaAppender(txaConsole)); 70 71 final JPanel pnlN = new JPanel (new BorderLayout ()); 72 txaAgentInfo = new JTextArea ("Registering with Lookup Services..."); 73 txaAgentInfo.setEditable(false); 74 pnlN.add(txaAgentInfo, BorderLayout.CENTER); 75 pnlN.add(btnStop, BorderLayout.EAST); 76 77 getContentPane().setLayout(new BorderLayout ()); 78 getContentPane().add(pnlN, BorderLayout.NORTH); 79 getContentPane().add(scrConsole, BorderLayout.CENTER); 80 pack(); 81 setVisible(true); 82 } 83 84 private void exitForm() { 85 btnStop.setEnabled(false); 86 final BuildAgentUI theThis = this; 87 new Thread () { 88 public void run() { 89 BuildAgent.kill(); 90 LOG.info("BuildAgent.kill() completed"); 91 buildAgent.removeAgentStatusListener(theThis); 92 LOG.info("AgentStatusListener removed"); 93 System.exit(0); 94 } 95 } .start(); 96 } 97 98 public void statusChanged(BuildAgentService buildAgentService) { 99 updateAgentInfoUI(buildAgentService); 100 } 101 102 void updateAgentInfoUI(final BuildAgentService buildAgentService) { 103 SwingUtilities.invokeLater(new Runnable () { 106 public void run() { 107 String agentInfo = ""; 108 if (buildAgentService != null) { 109 try { 110 agentInfo = buildAgentService.asString(); 111 } catch (RemoteException e) { 112 agentInfo = e.getMessage(); 113 } 114 } 115 116 txaAgentInfo.setText("Build Agent: " + buildAgent.getServiceID() + "\n" 117 + agentInfo 118 + MulticastDiscovery.toStringEntries(buildAgent.getEntries()) 119 ); 120 } 121 }); 122 } 123 124 125 128 public class Log4JJTextAreaAppender extends AppenderSkeleton { 129 130 private final JTextArea txaConsole; 131 132 public Log4JJTextAreaAppender(final JTextArea txaConsole) { 133 this.txaConsole = txaConsole; 134 } 135 136 protected void append(final LoggingEvent event) { 137 final String msg = event.getRenderedMessage(); 138 SwingUtilities.invokeLater(new Runnable () { 139 public void run() { 140 txaConsole.append(msg + "\n"); 141 if (txaConsole.getLineCount() > CONSOLE_LINE_BUFFER_SIZE) { 142 try { 144 txaConsole.replaceRange("", 0, 145 txaConsole.getLineEndOffset( 146 txaConsole.getLineCount() - CONSOLE_LINE_BUFFER_SIZE 147 )); 148 } catch (BadLocationException e) { 149 ; } 151 } 152 txaConsole.setCaretPosition(txaConsole.getDocument().getLength()); 154 } 155 }); 156 157 } 158 159 public boolean requiresLayout() { 160 return false; 161 } 162 163 public void close() { 164 } 165 } 166 } 167 | Popular Tags |