1
2
3
4
5
6 package org.abraracourcix.alipes.listeners.status;
7 import java.awt.BorderLayout;
8 import java.util.Collection;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.Map;
12 import javax.swing.BorderFactory;
13 import javax.swing.JLabel;
14 import org.abraracourcix.alipes.common.Event;
15 import org.abraracourcix.alipes.listeners.Listener;
16 import org.abraracourcix.alipes.listeners.status.StatusListener.ResourceInfo;
17 /***
18 * GUI status display. Tied closely to StatusListener
19 * @author jdt
20 *
21 */
22 public final class StatusFrame extends javax.swing.JFrame implements Listener {
23 /***
24 * Commons logger
25 */
26 private static final org.apache.commons.logging.Log log =
27 org.apache.commons.logging.LogFactory.getLog(StatusFrame.class);
28 /***
29 * The actual class doing the listening
30 */
31 private StatusListener statusListener;
32 /*** Creates new form StatusFrame */
33 public StatusFrame() {
34 log.debug("StatusFrame created");
35 initComponents();
36 log.debug("StatusFrame initialised");
37 }
38 /***
39 * If we know that an event is *BAD*, we could report it in red, for
40 * example. This is simply a map of statuses to colours.
41 */
42 private Map knownEventColours = new HashMap();
43 /***
44 * Register the one and only statusListener that this frame receives events
45 * from
46 *
47 * @param statusListener
48 * The statusListener
49 */
50 public void registerStatusListener(StatusListener statusListener) {
51 this.statusListener = statusListener;
52 }
53 /***
54 * This method is called from within the constructor to initialize the
55 * form. WARNING: Do NOT modify this code. The content of this method is
56 * always regenerated by the Form Editor.
57 */
58 private void initComponents() {
59 titleLbl = new javax.swing.JLabel();
60 textPane = new javax.swing.JTextPane();
61 addWindowListener(new java.awt.event.WindowAdapter() {
62 public void windowClosing(java.awt.event.WindowEvent evt) {
63 exitForm(evt);
64 }
65 });
66 titleLbl.setText("Status");
67 getContentPane().add(titleLbl, java.awt.BorderLayout.NORTH);
68 textPane.setBorder(
69 new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
70 getContentPane().add(textPane, java.awt.BorderLayout.CENTER);
71 lastEventLbl = new JLabel("Pending..................");
72 lastEventLbl.setBorder(
73 BorderFactory.createTitledBorder("Most recent change"));
74 getContentPane().add(lastEventLbl, BorderLayout.SOUTH);
75 pack();
76 }
77 /***
78 * Exit the Application
79 *
80 * @param evt
81 * the event closing the form
82 */
83 private void exitForm(final java.awt.event.WindowEvent evt) {
84 System.exit(0);
85 }
86 /***
87 * Fire up the frame @TODO consider deletion
88 *
89 * @param args
90 * ignored
91 */
92 public static void main(final String[] args) {
93 new StatusFrame().show();
94 }
95
96 /***
97 * GUI components
98 */
99 private javax.swing.JTextPane textPane;
100 /***
101 * GUI components
102 */
103 private javax.swing.JLabel titleLbl;
104 /***
105 * GUI components
106 */
107 private javax.swing.JLabel lastEventLbl;
108
109 /***
110 * if an event occurs we need to update
111 *
112 * @see org.abraracourcix.alipes.listeners.Listener#eventOccurred(org.abraracourcix.alipes.common.Event,
113 * java.lang.Object)
114 */
115 public void eventOccurred(final Event event, final Object resource) {
116 log.debug("Event: "+event+", Resource "+resource);
117 if (statusListener != null) {
118 final Map statuses = statusListener.getStatuses();
119
120 final Collection resources = statuses.keySet();
121 final Iterator it = resources.iterator();
122 final StringBuffer text = new StringBuffer();
123 while (it.hasNext()) {
124 final Object resourceObj = it.next();
125 final String resourceName = resourceObj.toString();
126 final ResourceInfo info = (ResourceInfo) statuses.get(resourceObj);
127 final String resourceStatus = info.getEvent().toString();
128 final String time = info.getDate().toString();
129 text.append(
130 resourceName + ": " + resourceStatus + " @ " + time + "\n");
131 }
132 textPane.setText(text.toString());
133 lastEventLbl.setText(resource.toString());
134 }
135 }
136 /***
137 * Set a map associating Events with colours
138 *
139 * @param knownStatusColours
140 * The knownEventColours to set.
141 */
142 public void setKnownEventColours(final Map knownStatusColours) {
143 this.knownEventColours = knownStatusColours;
144 }
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167