001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.tools.csmart.ui.console;
028:
029: import javax.swing.*;
030: import java.awt.*;
031: import java.net.URL;
032: import java.util.Observable;
033: import java.util.Observer;
034:
035: public class NodeStatusButton extends JRadioButton implements Observer {
036:
037: public static int STATUS_BUSY = 0;
038: public static int STATUS_HIGH_BUSY = 1;
039: public static int STATUS_MEDIUM_HIGH_BUSY = 2;
040: public static int STATUS_MEDIUM_BUSY = 3;
041: public static int STATUS_MEDIUM_LOW_BUSY = 4;
042: public static int STATUS_LOW_BUSY = 5;
043: public static int STATUS_NODE_CREATED = 6;
044: public static int STATUS_NODE_DESTROYED = 7;
045: public static int STATUS_NO_ANSWER = 8;
046: public static int STATUS_UNKNOWN = 9;
047: public static int STATUS_STD_ERROR = 10;
048: public static int STATUS_NOTIFY = 11;
049: public static int STATUS_MAX = 11;
050:
051: // status button colors
052: public static Color busyStatus = new Color(230, 255, 230); // shades of green
053: public static Color highBusyStatus = new Color(175, 255, 175);
054: public static Color mediumHighBusyStatus = new Color(0, 255, 0);
055: public static Color mediumBusyStatus = new Color(0, 235, 0);
056: public static Color mediumLowBusyStatus = new Color(0, 215, 0);
057: public static Color lowBusyStatus = new Color(0, 195, 0);
058: public static Color nodeCreatedStatus = new Color(0, 175, 0);
059: public static Color nodeDestroyedStatus = new Color(215, 0, 0); // red
060: public static Color noAnswerStatus = new Color(245, 245, 0); // yellow
061: public static Color unknownStatus = new Color(180, 180, 180); //gray
062: public static Color[] statusColors = { busyStatus, highBusyStatus,
063: mediumHighBusyStatus, mediumBusyStatus,
064: mediumLowBusyStatus, lowBusyStatus, nodeCreatedStatus,
065: nodeDestroyedStatus, noAnswerStatus, unknownStatus, };
066:
067: private static String[] descriptions = { "Extremely busy",
068: "Very busy", "Busy", "Somewhat busy", "Somewhat idle",
069: "Idle", "Node created", "Node destroyed", "No answer",
070: "Unknown", };
071:
072: private Image img = null; // error image
073: private NodeStatusButtonModel model = new NodeStatusButtonModel();
074:
075: /**
076: * Creates a new <code>NodeStatusButton</code> icon.
077: * @param icon Icon for this button.
078: */
079: public NodeStatusButton(Icon icon) {
080: super (icon);
081: model.addObserver(this );
082: }
083:
084: /**
085: * Updates the state of the button, based on the contents of the model.
086: *
087: * @param o the Oberved model that caused the change.
088: * @param arg Unique argument to identify the exact change.
089: */
090: public void update(Observable o, Object arg) {
091: if (arg.equals(model.CLEAR_ERROR_EVENT)) {
092: img = null;
093: setIcon(new ColoredCircle(statusColors[model.getStatus()],
094: 20, null));
095: setSelectedIcon(new SelectedColoredCircle(
096: statusColors[model.getStatus()], 20, null));
097: }
098:
099: if (arg.equals(model.NOTIFY_SET_WARNING)) {
100: URL iconURL = getClass().getResource("Bang.gif");
101: if (iconURL != null) {
102: ImageIcon icon = new ImageIcon(iconURL);
103: img = icon.getImage();
104: }
105: }
106:
107: if (arg.equals(model.NOTIFY_EVENT)) {
108: Color statusColor = statusColors[model.getStatus()];
109: setIcon(new ColoredCircle(statusColor, 20, img));
110: setSelectedIcon(new SelectedColoredCircle(statusColor, 20,
111: img));
112: String s = getToolTipText(null);
113: if (s != null) {
114: int index = s.lastIndexOf("), ");
115: if (index != -1) {
116: s = s.substring(0, index + 3)
117: + descriptions[model.getStatus()];
118: setToolTipText(s);
119: }
120: }
121: }
122: }
123:
124: /**
125: * Gets a list of all Status Descriptions.
126: *
127: * @return <code>String</code> array of all descriptions.
128: */
129: public static String[] getStatusDescriptions() {
130: return descriptions;
131: }
132:
133: /**
134: * Get a list of all possible status colors.
135: * @return array of <code>Color</code>s
136: */
137: public static Color[] getStatusColors() {
138: return statusColors;
139: }
140:
141: /**
142: * Gets a handle to the model for this button.
143: *
144: * @return <code>NodeStatusButtonModel</code> object
145: */
146: public NodeStatusButtonModel getMyModel() {
147: return model;
148: }
149:
150: public class NodeStatusButtonModel extends Observable {
151:
152: public String CLEAR_ERROR_EVENT = "Clear";
153: public String NOTIFY_SET_WARNING = "Warning";
154: public String NOTIFY_EVENT = "Notify";
155:
156: private int status = STATUS_UNKNOWN;
157: private boolean notifyOnStandardError = false;
158:
159: public NodeStatusButtonModel() {
160: }
161:
162: /**
163: * Gets the current status of the button.
164: *
165: * @return current status
166: */
167: public int getStatus() {
168: return this .status;
169: }
170:
171: /**
172: * Sets the current status of the button
173: *
174: * @param newStatus -- The new status
175: */
176: public void setStatus(int newStatus) {
177: if (newStatus < 0 || newStatus > STATUS_MAX)
178: return; // invalid status
179: // if there's an error, add the warning icon
180: if (newStatus == STATUS_NOTIFY
181: || (newStatus == STATUS_STD_ERROR && notifyOnStandardError)) {
182: setChanged();
183: notifyObservers(NOTIFY_SET_WARNING);
184: } else if (newStatus != STATUS_STD_ERROR) {
185: status = newStatus;
186: }
187: setChanged();
188: notifyObservers(NOTIFY_EVENT);
189: }
190:
191: /**
192: * Gets the description for the current status
193: *
194: * @return Current status description
195: */
196: public String getStatusDescription() {
197: return descriptions[status];
198: }
199:
200: /**
201: * Remove error icon from the button.
202: */
203: public void clearError() {
204: setChanged();
205: notifyObservers(CLEAR_ERROR_EVENT);
206: }
207:
208: /**
209: * Set whether to display warning icon when output is received on standard error.
210: *
211: * @param notifyOnStandardError - State to set.
212: */
213: public void setNotifyOnStandardError(
214: boolean notifyOnStandardError) {
215: this .notifyOnStandardError = notifyOnStandardError;
216: }
217:
218: /**
219: * Get the current state of the NotifyOnStandardError flag.
220: *
221: * @return Current state of NotifyOnStandardError
222: */
223: public boolean getNotifyOnStandardError() {
224: return notifyOnStandardError;
225: }
226: }
227: }
|