001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.mlm.plugin.sample;
027:
028: import java.awt.FlowLayout;
029: import java.util.Enumeration;
030:
031: import javax.swing.DefaultListModel;
032: import javax.swing.JFrame;
033: import javax.swing.JList;
034: import javax.swing.JPanel;
035: import javax.swing.JScrollPane;
036:
037: import org.cougaar.core.blackboard.IncrementalSubscription;
038: import org.cougaar.planning.ldm.plan.Alert;
039: import org.cougaar.planning.plugin.legacy.SimplePlugin;
040: import org.cougaar.util.UnaryPredicate;
041:
042: /**
043: * The GSLRescindPlugin will rescind the ?? task
044: *
045: *
046: */
047:
048: public class AlertReaderPlugin extends SimplePlugin {
049: /** frame for UI **/
050: static JFrame frame;
051:
052: /** to display alerts **/
053: protected JList listbox;
054: protected DefaultListModel listmodel;
055:
056: /** Subscription to hold collection of input alerts **/
057: private IncrementalSubscription myalert;
058:
059: /** subscribes for all alerts **/
060: private static UnaryPredicate alertPred() {
061: return new UnaryPredicate() {
062: public boolean execute(Object o) {
063: return (o instanceof Alert);
064: }
065: };
066: }
067:
068: /** UI that shows alert **/
069: private void createGUI() {
070: frame = new JFrame("AlertReaderPlugin");
071: frame.getContentPane().setLayout(new FlowLayout());
072: JPanel panel = new JPanel();
073: listmodel = new DefaultListModel();
074: listbox = new JList(listmodel);
075: JScrollPane scrollPane = new JScrollPane(listbox);
076: panel.add(scrollPane);
077: frame.getContentPane().add("Center", panel);
078: frame.pack();
079: frame.setVisible(true);
080: }
081:
082: /**
083: * Overrides the setupSubscriptions() in the SimplePlugin.
084: */
085: protected void setupSubscriptions() {
086: myalert = (IncrementalSubscription) subscribe(alertPred());
087: createGUI();
088: }
089:
090: /* CCV2 execute method */
091: /* This will be called every time a alert matches the above predicate */
092: public synchronized void execute() {
093: Enumeration e = myalert.getAddedList();
094: while (e.hasMoreElements()) {
095: Alert al = (Alert) e.nextElement();
096: listmodel.addElement("Received Alert " + al.getAlertText());
097: }
098: e = myalert.getChangedList();
099: while (e.hasMoreElements()) {
100: Alert al = (Alert) e.nextElement();
101: listmodel.addElement("Received Alert " + al.getAlertText());
102: }
103:
104: }
105: }
|