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.glm.execution.eg;
027:
028: import java.io.IOException;
029: import java.util.ArrayList;
030: import java.util.Arrays;
031: import java.util.Calendar;
032: import java.util.Collection;
033: import java.util.Date;
034: import java.util.GregorianCalendar;
035: import java.util.Iterator;
036: import java.util.SortedSet;
037: import java.util.TreeSet;
038: import javax.swing.JComponent;
039: import org.cougaar.glm.execution.common.*;
040:
041: /**
042: * Keeps track of the inventory report schedules and decide if/when to
043: * generate a report.
044: **/
045: public class InventoryReportManager extends ManagerBase implements
046: ScheduleManager {
047: public InventoryReportManager(EventGenerator anEventGenerator) {
048: super (anEventGenerator);
049: }
050:
051: protected String getLogFileName(String prefix) {
052: return prefix + "InventoryReports" + LOGFILE_SUFFIX;
053: }
054:
055: public String getGUITitle() {
056: return "Inventory Reports";
057: }
058:
059: public FilterGUI createFilterGUI(EventGenerator anEventGenerator) {
060: return new InventoryReportFilterGUI(anEventGenerator);
061: }
062:
063: public Object[] getHandlers() {
064: return new Object[0];
065: }
066:
067: public void receiveInventoryReports(String source,
068: TimedInventoryReport[] reports) {
069: for (int i = 0; i < reports.length; i++) {
070: addToSchedule(reports[i]);
071: }
072: finishScheduleUpdate();
073: }
074:
075: public void sendInventoryReport(String source,
076: InventoryReport anInventoryReport) throws IOException {
077: Object[] handlers = {}; // There is no response
078: EventMonitorListener l = theEventGenerator
079: .createEventMonitorListener(source, handlers,
080: anInventoryReport);
081: if (l != null) {
082: theEventGenerator.addListener(l);
083: theEventGenerator.startListener(l);
084: }
085: }
086:
087: protected ManagerTableModel createTableModel() {
088: return new InventoryReportTableModel();
089: }
090:
091: private class InventoryReportTableModel extends ManagerTableModel {
092: private static final int ENABLE_COLUMN = 0;
093: private static final int CLUSTER_COLUMN = 1;
094: private static final int RECEIVE_TIME_COLUMN = 2;
095: private static final int REPORT_TIME_COLUMN = 3;
096: private static final int ITEM_COLUMN = 4;
097: private static final int QUANTITY_COLUMN = 5;
098: private static final int ANNOTATION_COLUMN = 6;
099: private static final int NCOLUMNS = 7;
100: private final int ITEM_WIDTH = getCellWidth("Inventory: NSN/XXXXXXXXXXXXX");
101:
102: public InventoryReportTableModel() {
103: logColumns = new int[] { CLUSTER_COLUMN,
104: RECEIVE_TIME_COLUMN, REPORT_TIME_COLUMN,
105: ITEM_COLUMN, QUANTITY_COLUMN, ANNOTATION_COLUMN, };
106: }
107:
108: public int getColumnCount() {
109: return NCOLUMNS;
110: }
111:
112: public int getPreferredColumnWidth(int col) {
113: switch (col) {
114: case ENABLE_COLUMN:
115: return SEND_WIDTH;
116: case CLUSTER_COLUMN:
117: return CLUSTER_WIDTH;
118: case RECEIVE_TIME_COLUMN:
119: return DATE_WIDTH;
120: case REPORT_TIME_COLUMN:
121: return DATE_WIDTH;
122: case ITEM_COLUMN:
123: return ITEM_WIDTH;
124: case QUANTITY_COLUMN:
125: return QUANTITY_WIDTH;
126: case ANNOTATION_COLUMN:
127: return ANNOTATION_WIDTH;
128: }
129: return 75;
130: }
131:
132: public int getMinColumnWidth(int col) {
133: switch (col) {
134: case ENABLE_COLUMN:
135: return SEND_WIDTH;
136: case CLUSTER_COLUMN:
137: return CLUSTER_WIDTH / 2;
138: case RECEIVE_TIME_COLUMN:
139: return DATE_WIDTH;
140: case REPORT_TIME_COLUMN:
141: return DATE_WIDTH;
142: case ITEM_COLUMN:
143: return ITEM_WIDTH / 2;
144: case QUANTITY_COLUMN:
145: return 50;
146: case ANNOTATION_COLUMN:
147: return ANNOTATION_WIDTH / 2;
148: }
149: return 75;
150: }
151:
152: public int getMaxColumnWidth(int col) {
153: switch (col) {
154: case ENABLE_COLUMN:
155: return SEND_WIDTH;
156: case CLUSTER_COLUMN:
157: return CLUSTER_WIDTH;
158: case RECEIVE_TIME_COLUMN:
159: return DATE_WIDTH;
160: case REPORT_TIME_COLUMN:
161: return DATE_WIDTH;
162: case ITEM_COLUMN:
163: return Integer.MAX_VALUE;
164: case QUANTITY_COLUMN:
165: return Integer.MAX_VALUE;
166: case ANNOTATION_COLUMN:
167: return Integer.MAX_VALUE;
168: }
169: return 75;
170: }
171:
172: public String getColumnName(int col) {
173: switch (col) {
174: case ENABLE_COLUMN:
175: return "Send";
176: case CLUSTER_COLUMN:
177: return "Cluster";
178: case RECEIVE_TIME_COLUMN:
179: return "Receive Time";
180: case REPORT_TIME_COLUMN:
181: return "Report Time";
182: case ITEM_COLUMN:
183: return "Item";
184: case QUANTITY_COLUMN:
185: return "Quantity";
186: case ANNOTATION_COLUMN:
187: return "Comment";
188: }
189: return null;
190: }
191:
192: public Class getColumnClass(int col) {
193: switch (col) {
194: case ENABLE_COLUMN:
195: return Boolean.class;
196: case CLUSTER_COLUMN:
197: return String.class;
198: case RECEIVE_TIME_COLUMN:
199: return EGDate.class;
200: case REPORT_TIME_COLUMN:
201: return EGDate.class;
202: case ITEM_COLUMN:
203: return String.class;
204: case QUANTITY_COLUMN:
205: return Double.class;
206: case ANNOTATION_COLUMN:
207: return Object.class;
208: }
209: return null;
210: }
211:
212: public boolean isCellEditable(int row, int col) {
213: theEventGenerator.setPaused(true);
214: switch (col) {
215: case ENABLE_COLUMN:
216: return true;
217: case RECEIVE_TIME_COLUMN:
218: return true;
219: case REPORT_TIME_COLUMN:
220: return true;
221: case ITEM_COLUMN:
222: return false;
223: case QUANTITY_COLUMN:
224: return true;
225: case ANNOTATION_COLUMN:
226: return false;
227: }
228: return false;
229: }
230:
231: public boolean cellHasBeenEdited(int row, int col) {
232: TimedInventoryReport tir = (TimedInventoryReport) getRowObject(row);
233: if (tir == null)
234: return false;
235: if (tir.theOriginalInventoryReport == null)
236: return false;
237: switch (col) {
238: case ENABLE_COLUMN:
239: return false; // May have been edited, but appearance doesn't change
240: case RECEIVE_TIME_COLUMN:
241: return (tir.theOriginalInventoryReport.theReceivedDate != tir.theInventoryReport.theReceivedDate);
242: case REPORT_TIME_COLUMN:
243: return (tir.theOriginalInventoryReport.theReportDate != tir.theInventoryReport.theReportDate);
244: case ITEM_COLUMN:
245: return false;
246: case QUANTITY_COLUMN:
247: if (tir.theOriginalInventoryReport.theQuantity != tir.theInventoryReport.theQuantity) {
248: return true;
249: } else {
250: return false;
251: }
252: }
253: return false;
254: }
255:
256: public void setValueAt(Object newValue, int row, int col) {
257: TimedInventoryReport tir = (TimedInventoryReport) getRowObject(row);
258: if (tir == null)
259: return;
260: String stringValue = "";
261: if (newValue instanceof String)
262: stringValue = (String) newValue;
263: switch (col) {
264: case ENABLE_COLUMN:
265: tir.setEnabled(((Boolean) newValue).booleanValue());
266: break;
267: case RECEIVE_TIME_COLUMN:
268: assureEditCopy(tir);
269: removeFromSchedule(tir);
270: if (stringValue.equals("")) {
271: tir.theInventoryReport.theReceivedDate = tir.theOriginalInventoryReport.theReceivedDate;
272: } else {
273: tir.theInventoryReport.theReceivedDate = new Date(
274: stringValue).getTime();
275: }
276: addToSchedule(tir);
277: finishScheduleUpdate();
278: break;
279: case REPORT_TIME_COLUMN:
280: assureEditCopy(tir);
281: if (stringValue.equals("")) {
282: tir.theInventoryReport.theReportDate = tir.theOriginalInventoryReport.theReportDate;
283: } else {
284: tir.theInventoryReport.theReportDate = new Date(
285: stringValue).getTime();
286: }
287: break;
288: case ITEM_COLUMN:
289: break;
290: case QUANTITY_COLUMN:
291: assureEditCopy(tir);
292: try {
293: if (stringValue.equals("")) {
294: tir.theInventoryReport.theQuantity = tir.theOriginalInventoryReport.theQuantity;
295: } else {
296: tir.theInventoryReport.theQuantity = Double
297: .parseDouble(stringValue);
298: }
299: } catch (NumberFormatException nfe) {
300: postErrorMessage("Value must be numeric");
301: }
302: break;
303: }
304: }
305:
306: private void assureEditCopy(TimedInventoryReport tir) {
307: tir.getModifiableInventoryReport();
308: }
309:
310: public Object getValue(int col, Object rowObject) {
311: TimedInventoryReport tir = (TimedInventoryReport) rowObject;
312: if (tir == null)
313: return null;
314: switch (col) {
315: case ENABLE_COLUMN:
316: return new Boolean(tir.isEnabled());
317: case CLUSTER_COLUMN:
318: return tir.theSource;
319: case RECEIVE_TIME_COLUMN:
320: return new EGDate(tir.getTime());
321: case REPORT_TIME_COLUMN:
322: return new EGDate(tir.theInventoryReport.theReportDate);
323: case ITEM_COLUMN:
324: return tir.theInventoryReport.theItemIdentification;
325: case QUANTITY_COLUMN:
326: return new Double(tir.theInventoryReport.theQuantity);
327: case ANNOTATION_COLUMN:
328: return tir.getAnnotation();
329: }
330: return null;
331: }
332: }
333: }
|