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:
027: package org.cougaar.planning.examples;
028:
029: import java.awt.*;
030: import java.awt.event.*;
031: import java.net.URL;
032: import java.net.MalformedURLException;
033: import java.util.*;
034: import javax.swing.*;
035:
036: import org.cougaar.planning.ldm.plan.*;
037: import org.cougaar.planning.ldm.asset.*;
038: import org.cougaar.planning.plugin.legacy.SimplePlugin;
039:
040: public class AlertCreatorPlugin extends SimplePlugin {
041:
042: // This plugin doesn't subscribe, but needs to set up a UI
043: // for the creation of alerts
044: public void setupSubscriptions() {
045: // System.out.println("AlertCreatorPlugin.setupSubscriptions...");
046:
047: ActionListener listener = new ActionListener() {
048: public void actionPerformed(ActionEvent e) {
049: generateAlert();
050: }
051: };
052: createGUI("Create Alert", "Alert", listener);
053: }
054:
055: private static int counter = 1;
056:
057: private static String[] paramAction = { "infomessage.html", "cat",
058: "dog", "parrot" };
059:
060: private static String[] paramDescription = { "View Info Messages",
061: "A Cat", "A Dog", "A Parrot" };
062:
063: private void generateAlert() {
064: openTransaction();
065: NewAlert alert = theLDMF.newAlert();
066: alert.setAlertText("AlertText-" + counter);
067:
068: int numParams = (counter % 4);
069:
070: AlertParameter[] params = new AlertParameter[numParams];
071: for (int i = 0; i < numParams; i++) {
072: NewAlertParameter param = theLDMF.newAlertParameter();
073: param.setParameter(paramAction[i]);
074: param.setDescription(paramDescription[i]);
075: params[i] = param;
076: }
077: alert.setAlertParameters(params);
078:
079: alert.setAcknowledged(false);
080:
081: alert.setSeverity(counter);
082:
083: alert.setOperatorResponseRequired((numParams > 0));
084:
085: System.out.println("Publishing new alert "
086: + alert.getAlertText());
087:
088: counter++;
089:
090: publishAdd(alert);
091: closeTransactionDontReset();
092: }
093:
094: public void execute() {
095: // System.out.println("AlertCreatorPlugin.execute...");
096: }
097:
098: /**
099: * Create a simple free-floating GUI button with a label
100: */
101: private static void createGUI(String button_label,
102: String frame_label, ActionListener listener) {
103: JFrame frame = new JFrame(frame_label);
104: frame.getContentPane().setLayout(new FlowLayout());
105: JPanel panel = new JPanel();
106: // Create the button
107: JButton button = new JButton(button_label);
108:
109: // Register a listener for the button
110: button.addActionListener(listener);
111: panel.add(button);
112: frame.getContentPane().add("Center", panel);
113: frame.pack();
114: frame.setVisible(true);
115: }
116:
117: }
|