Source code: org/altara/mars/plugin/swingnotify/SwingNotifyPlugin.java
1 /* MARS Swing UI Notification Plugin
2 Copyright (C) 2002 Leapfrog Research & Development, LLC
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, it is available at
16 http:///www.gnu.org/copyleft/gpl.html, or by writing to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21 package org.altara.mars.plugin.swingnotify;
22
23 import org.jdom.*;
24 import org.altara.util.*;
25 import org.altara.mars.*;
26 import org.altara.mars.engine.*;
27 import org.altara.mars.plugin.*;
28 import org.altara.mars.swingui.*;
29 import java.util.*;
30 import java.text.*;
31 import java.net.*;
32 import java.io.*;
33 import java.awt.*;
34 import java.awt.event.*;
35 import javax.swing.*;
36 import javax.swing.event.*;
37
38 public class SwingNotifyPlugin implements Plugin, NotificationListener {
39
40 private boolean enabled;
41 private boolean notifyBackUp;
42 private boolean beep;
43
44 private DialogWorker dialogWorker;
45
46 public SwingNotifyPlugin() {
47 enabled = false;
48 notifyBackUp = false;
49
50 dialogWorker = new DialogWorker();
51 dialogWorker.start();
52 }
53
54 public String getElementName() {
55 return "swingnotify";
56 }
57
58 public String getDisplayName() {
59 return "UI Notification";
60 }
61
62 public void notifyStatusChanged(StatusChangeEvent sce) {
63 // do nothing if not enabled
64 if (!enabled) return;
65
66 // check one: we just went down.
67 if (sce.getOldStatus().getCode() == Status.UP &&
68 sce.getNewStatus().isFault()) {
69 dialogWorker.addEvent(sce);
70 } else
71 // check two: we came back up, and we care about such things
72 if (notifyBackUp && sce.getOldStatus().isFault() &&
73 sce.getNewStatus().getCode() == Status.UP) {
74 dialogWorker.addEvent(sce);
75 }
76 }
77
78 private void showNotification (StatusChangeEvent sce) {
79 Status newStatus = sce.getNewStatus();
80 Service service = sce.getService();
81 String svcDesc = service.getName()+" on "+service.getHost().getName();
82 String typeDesc = newStatus.isFault() ? "Fault" : "Notice";
83 int msgType = newStatus.isFault() ? JOptionPane.WARNING_MESSAGE :
84 JOptionPane.INFORMATION_MESSAGE;
85 String message = svcDesc + " is " +newStatus.toString();
86 String title = typeDesc + ": "+svcDesc;
87 if (newStatus.isFault() && beep) Toolkit.getDefaultToolkit().beep();
88 JOptionPane.showMessageDialog(null,message,title,msgType);
89 }
90
91 /* ------------------------------------------------------
92 XML config serialization
93 ------------------------------------------------------ */
94
95 public Element getConfig() {
96 Element out = new Element(getElementName(),MarsModel.NAMESPACE);
97 out.setAttribute("enabled",String.valueOf(enabled));
98 out.setAttribute("notifyBackUp",String.valueOf(notifyBackUp));
99 out.setAttribute("beep",String.valueOf(beep));
100 return out;
101 }
102
103 public void setConfig(Element in) throws UnknownHostException,
104 InvalidDocumentException {
105 boolean enabled, notifyBackUp, beep;
106
107 String enabledStr = in.getAttributeValue("enabled");
108 if (enabledStr == null)
109 throw new InvalidDocumentException("Missing UI enabled");
110 enabled = Boolean.valueOf(enabledStr).booleanValue();
111
112 String backUpStr = in.getAttributeValue("notifyBackUp");
113 if (backUpStr == null)
114 throw new InvalidDocumentException("Missing UI backUp");
115 notifyBackUp = Boolean.valueOf(backUpStr).booleanValue();
116
117 String beepStr = in.getAttributeValue("notifyBackUp");
118 if (beepStr == null)
119 throw new InvalidDocumentException("Missing UI beep");
120 beep = Boolean.valueOf(beepStr).booleanValue();
121
122 // everything's here, set config
123 this.enabled = enabled;
124 this.notifyBackUp = notifyBackUp;
125 this.beep = beep;
126 }
127
128 /* ------------------------------------------------------
129 Editor
130 ------------------------------------------------------ */
131
132 public Editor getEditor() {
133 return new SwingNotifyEditor();
134 }
135
136 private class SwingNotifyEditor extends JPanel implements Editor {
137
138 private JCheckBox enabledBox;
139 private JCheckBox backUpBox;
140 private JCheckBox beepBox;
141
142 private SwingNotifyEditor() {
143 // create editable fields
144 enabledBox = new JCheckBox("Enabled",enabled);
145 backUpBox = new JCheckBox("Notify when a service comes back up",
146 notifyBackUp);
147 beepBox = new JCheckBox("Beep on fault", beep);
148
149 // set default field enable states
150 backUpBox.setEnabled(enabled);
151 beepBox.setEnabled(enabled);
152
153 // set up an action listener on the checkbox
154 enabledBox.addActionListener(new ActionListener() {
155 public void actionPerformed(ActionEvent ae) {
156 backUpBox.setEnabled(enabledBox.isSelected());
157 beepBox.setEnabled(enabledBox.isSelected());
158 }
159 });
160
161 // set up the layout manager
162 setLayout(new GridBagLayout());
163 GridBagConstraints c = new GridBagConstraints();
164 c.anchor = c.NORTHWEST; c.fill = c.BOTH;
165 c.weightx = 1.0; c.weighty = 0.0;
166 c.insets = new Insets(4,4,6,4);
167
168 // lay out the panel
169 c.gridx = 0; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1;
170 add(enabledBox,c);
171 c.gridx = 0; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1;
172 add(beepBox,c);
173 c.gridx = 0; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1;
174 add(backUpBox,c);
175 }
176
177 public void commit() throws UnknownHostException {
178 enabled = enabledBox.isSelected();
179 notifyBackUp = backUpBox.isSelected();
180 beep = beepBox.isSelected();
181 }
182
183 public String getEditorTitle() {
184 return "Configure UI Notification";
185 }
186 }
187
188 private class DialogWorker extends Worker {
189
190 private Queue sceQ;
191
192 private DialogWorker() {
193 super("UI Notification Worker");
194 sceQ = new Queue();
195 }
196
197 public void work() throws InterruptedException {
198 showNotification((StatusChangeEvent)sceQ.dequeue());
199 }
200
201 public synchronized void addEvent(StatusChangeEvent sce) {
202 sceQ.enqueue(sce);
203 }
204 }
205 }