KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > distributed > BuildAgentUI


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 JavaDoc;
8 import java.awt.event.ActionListener JavaDoc;
9 import java.awt.event.WindowAdapter JavaDoc;
10 import java.awt.event.WindowEvent JavaDoc;
11 import java.awt.BorderLayout JavaDoc;
12 import java.awt.Dimension JavaDoc;
13 import javax.swing.JFrame JavaDoc;
14 import javax.swing.JPanel JavaDoc;
15 import javax.swing.JButton JavaDoc;
16 import javax.swing.JTextArea JavaDoc;
17 import javax.swing.JScrollPane JavaDoc;
18 import javax.swing.text.BadLocationException JavaDoc;
19 import javax.swing.SwingUtilities JavaDoc;
20 import java.rmi.RemoteException JavaDoc;
21
22 import net.sourceforge.cruisecontrol.distributed.util.MulticastDiscovery;
23
24 /**
25  * Created by IntelliJ IDEA.
26  * User: drollo
27  * Date: Aug 25, 2005
28  * Time: 3:38:53 PM
29  * To change this template use File | Settings | File Templates.
30  */

31 // @todo Use JDesktop stuff for tray icon??
32
final class BuildAgentUI extends JFrame JavaDoc 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 JavaDoc txaAgentInfo;
40     private final JButton JavaDoc btnStop = new JButton JavaDoc("Stop");
41     private final JTextArea JavaDoc txaConsole = new JTextArea JavaDoc();
42     private final JScrollPane JavaDoc scrConsole = new JScrollPane JavaDoc();
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 JavaDoc() {
52             public void actionPerformed(final ActionEvent JavaDoc e) {
53                 exitForm();
54             }
55         });
56         addWindowListener(new WindowAdapter JavaDoc() {
57             public void windowClosing(final WindowEvent JavaDoc evt) {
58                 exitForm();
59             }
60         });
61
62         txaConsole.setFont(new java.awt.Font JavaDoc("Courier New", 0, 12));
63
64         scrConsole.setViewportView(txaConsole);
65         scrConsole.setPreferredSize(new Dimension JavaDoc(500, 300));
66
67         txaConsole.setEditable(false);
68         // need to register with BuildAgent Logger (not UI Logger).
69
buildAgent.LOG.addAppender(new Log4JJTextAreaAppender(txaConsole));
70
71         final JPanel JavaDoc pnlN = new JPanel JavaDoc(new BorderLayout JavaDoc());
72         txaAgentInfo = new JTextArea JavaDoc("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 JavaDoc());
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 JavaDoc() {
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         // only update UI on event dispatch thread,
104
// it's also good to wait a bit for the agent info to be updated...
105
SwingUtilities.invokeLater(new Runnable JavaDoc() {
106                 public void run() {
107                     String JavaDoc agentInfo = "";
108                     if (buildAgentService != null) {
109                         try {
110                             agentInfo = buildAgentService.asString();
111                         } catch (RemoteException JavaDoc 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     /**
126      * Log4J appender to duplicate log output to a JTextArea
127      */

128     public class Log4JJTextAreaAppender extends AppenderSkeleton {
129
130         private final JTextArea JavaDoc txaConsole;
131
132         public Log4JJTextAreaAppender(final JTextArea JavaDoc txaConsole) {
133             this.txaConsole = txaConsole;
134         }
135
136         protected void append(final LoggingEvent event) {
137             final String JavaDoc msg = event.getRenderedMessage();
138             SwingUtilities.invokeLater(new Runnable JavaDoc() {
139                 public void run() {
140                     txaConsole.append(msg + "\n");
141                     if (txaConsole.getLineCount() > CONSOLE_LINE_BUFFER_SIZE) {
142                         // remove old lines
143
try {
144                             txaConsole.replaceRange("", 0,
145                                     txaConsole.getLineEndOffset(
146                                             txaConsole.getLineCount() - CONSOLE_LINE_BUFFER_SIZE
147                                     ));
148                         } catch (BadLocationException JavaDoc e) {
149                             ; //ignore
150
}
151                     }
152                     // Make sure the last line is always visible
153
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