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: /**
028: * Modify an Org Activity GUI.
029: * <p>
030: * Allows user to look up the self org's org activity for a given
031: * activity type. Several fields in the org activity can then
032: * be changed:<br>
033: * <ul>
034: * <li>OpTempo</li>
035: * <li>start timespan date</li>
036: * <li>stop timespan date</li>
037: * </ul>
038: * <p>
039: * This plugin isn't responsible for forwarding the altered org
040: * activity. As far as this plugin is concerned, the change is
041: * only visible to it's logplan. (That's why the OrgId is fixed
042: * to the self org...)
043: */package org.cougaar.mlm.plugin.sample;
044:
045: import java.awt.BorderLayout;
046: import java.awt.Color;
047: import java.awt.GridBagConstraints;
048: import java.awt.GridBagLayout;
049: import java.awt.GridLayout;
050: import java.awt.Insets;
051: import java.awt.LayoutManager;
052: import java.awt.event.ActionEvent;
053: import java.awt.event.ActionListener;
054: import java.util.Date;
055: import java.util.Enumeration;
056:
057: import javax.swing.JButton;
058: import javax.swing.JFrame;
059: import javax.swing.JLabel;
060: import javax.swing.JPanel;
061: import javax.swing.JTextField;
062:
063: import org.cougaar.core.blackboard.IncrementalSubscription;
064: import org.cougaar.glm.ldm.asset.Organization;
065: import org.cougaar.glm.ldm.oplan.Oplan;
066: import org.cougaar.glm.ldm.oplan.OrgActivity;
067: import org.cougaar.glm.ldm.oplan.TimeSpan;
068: import org.cougaar.planning.ldm.PlanningFactory;
069: import org.cougaar.planning.plugin.legacy.SimplePlugin;
070: import org.cougaar.util.ShortDateFormat;
071: import org.cougaar.util.UnaryPredicate;
072:
073: public class ModifyOrgActivityGUIPlugin extends SimplePlugin {
074:
075: /** org activity info **/
076: private JLabel orgIdLabel;
077: private JTextField typeText;
078: private JTextField opTempoText;
079: private JLabel cDayLabel;
080: private JTextField startText;
081: private JTextField endText;
082:
083: /** status label **/
084: private JLabel statusLabel;
085:
086: /** Control buttons */
087: private JButton getOrgActButton;
088: private JButton modifyOrgActButton;
089:
090: /** Subscription to hold collection of input tasks **/
091: private IncrementalSubscription orgsSub;
092: private IncrementalSubscription orgActivitiesSub;
093: private IncrementalSubscription oplansSub;
094:
095: /** Date formatter for "month/day/year" **/
096: private ShortDateFormat dateFormatter = new ShortDateFormat();
097:
098: /** Need this for creating new instances of certain objects **/
099: private PlanningFactory ldmf;
100:
101: private static final String defaultOrgActivityType = "Deployment";
102:
103: /*
104: * Organization predicate.
105: **/
106: protected static UnaryPredicate newOrgPred() {
107: return new UnaryPredicate() {
108: public boolean execute(Object o) {
109: return (o instanceof Organization);
110: }
111: };
112: }
113:
114: /**
115: * Oplan predicate.
116: **/
117: protected static UnaryPredicate newOplanPred() {
118: return new UnaryPredicate() {
119: public boolean execute(Object o) {
120: return (o instanceof Oplan);
121: }
122: };
123: }
124:
125: /**
126: * OrgActivity predicate.
127: **/
128: protected static UnaryPredicate newOrgActivityPred() {
129: return new UnaryPredicate() {
130: public boolean execute(Object o) {
131: return (o instanceof OrgActivity);
132: }
133: };
134: }
135:
136: // Have to provide these on this plugin class, else the inner class
137: // below will not be able to find them
138: protected void openTheTransaction() {
139: openTransaction();
140: }
141:
142: protected void closeTheTransaction(boolean b) {
143: closeTransaction(b);
144: }
145:
146: protected void setStatus(String s) {
147: setStatus(false, s);
148: }
149:
150: protected void setStatus(boolean success, String s) {
151: statusLabel
152: .setForeground((success ? Color.darkGray : Color.red));
153: statusLabel.setText(s);
154: }
155:
156: /**
157: * For GUI internal use. Update GUI org activity info.
158: * @return error string, null if success
159: */
160: protected String drawOrgActivity(OrgActivity orgAct, Oplan oplan) {
161: Date cDay;
162: if ((oplan == null) || ((cDay = oplan.getCday()) == null)) {
163: return "Oplan lacks cDay";
164: }
165: String cDateString = dateFormatter.toString(cDay);
166: if (orgAct == null) {
167: orgIdLabel.setText("");
168: //typeText.setText("");
169: opTempoText.setText("");
170: cDayLabel.setText(cDateString);
171: startText.setText("");
172: endText.setText("");
173: return "Missing Org Activity";
174: }
175: if (orgAct.getOplanUID() != oplan.getUID()) {
176: return ("Wrong Oplan (" + oplan.getUID()
177: + ") for this Org Activity (" + orgAct.getUID() + ")!");
178: }
179: TimeSpan timeSpan = orgAct.getTimeSpan();
180: if (timeSpan == null) {
181: return "Missing Org Activity TimeSpan";
182: }
183: String startDateString;
184: if (timeSpan.getStartDate() == null)
185: startDateString = cDateString;
186: else {
187: startDateString = dateFormatter.toString(timeSpan
188: .getStartDate());
189: }
190: String endDateString;
191: if (timeSpan.getEndDate() == null)
192: endDateString = cDateString;
193: else {
194: endDateString = dateFormatter.toString(timeSpan
195: .getEndDate());
196: }
197: orgIdLabel.setText(orgAct.getOrgID());
198: //typeText.setText(orgAct.getActivityType());
199: opTempoText.setText(orgAct.getOpTempo());
200: cDayLabel.setText(cDateString);
201: startText.setText(startDateString);
202: endText.setText(endDateString);
203: return null;
204: }
205:
206: /**
207: * For GUI internal use. change org activity to GUI's info.
208: * @return error string, null if success
209: */
210: protected String changeOrgActivity(OrgActivity orgAct, Oplan oplan) {
211: Date cDay;
212: if ((oplan == null) || ((cDay = oplan.getCday()) == null)) {
213: return "Oplan lacks cDay";
214: }
215: String opTempo = opTempoText.getText().trim();
216: if (opTempo.length() < 1) {
217: return "Invalid OpTempo";
218: }
219: TimeSpan timeSpan = orgAct.getTimeSpan();
220: if (timeSpan == null) {
221: return "Missing Org Activity TimeSpan";
222: }
223: Date startDate = dateFormatter.toDate(startText.getText()
224: .trim(), false);
225: if (startDate == null) {
226: return "Invalid start date";
227: }
228: Date endDate = dateFormatter.toDate(endText.getText().trim(),
229: false);
230: if (endDate == null) {
231: return "Invalid end date";
232: }
233: boolean changed = false;
234: if (!opTempo.equals(orgAct.getOpTempo())) {
235: orgAct.setOpTempo(opTempo);
236: changed = true;
237: }
238: if (!startDate.equals(timeSpan.getStartDate())) {
239: timeSpan.setStartDate(startDate);
240: changed = true;
241: }
242: if (!endDate.equals(timeSpan.getEndDate())) {
243: timeSpan.setEndDate(endDate);
244: changed = true;
245: }
246: if (!changed) {
247: return "No change to Org Activity";
248: }
249: publishChange(orgAct);
250: return null;
251: }
252:
253: /**
254: * An ActionListener that listens to the buttons.
255: */
256: class DRTListener implements ActionListener {
257: public void actionPerformed(ActionEvent e) {
258: JButton button = (JButton) e.getSource();
259:
260: String activityType = typeText.getText().trim();
261: Organization selfOrg;
262: Oplan oplan;
263: OrgActivity orgAct;
264: if ((selfOrg = getSelfOrg()) == null) {
265: setStatus("Missing self organization.");
266: } else if ((oplan = getOplan()) == null) {
267: setStatus("No Oplan yet.");
268: } else if ((orgAct = getOrgActivity(selfOrg, activityType)) == null) {
269: drawOrgActivity(null, oplan);
270: setStatus("No \"" + activityType + "\" Org activities.");
271: } else {
272: try {
273: // Have to do this as within transaction boundary
274: openTheTransaction();
275: if (button == getOrgActButton) {
276: String sError = drawOrgActivity(orgAct, oplan);
277: if (sError == null) {
278: setStatus(true, "Read Org Activity");
279: } else {
280: setStatus(sError);
281: }
282: } else {
283: String sError = changeOrgActivity(orgAct, oplan);
284: if (sError == null) {
285: setStatus(true, "Modified Org Activity");
286: } else {
287: setStatus(sError);
288: }
289: }
290: closeTheTransaction(false);
291: } catch (Exception exc) {
292: setStatus("Failed: " + exc.getMessage());
293: System.err.println("Could not execute button: "
294: + e.getActionCommand());
295: }
296: }
297: }
298: }
299:
300: private String getClusterID() {
301: try {
302: return getAgentIdentifier().toString();
303: } catch (Exception e) {
304: return "<UNKNOWN>";
305: }
306: }
307:
308: private void createGUI() {
309: // Create buttons, labels, etc
310: orgIdLabel = new JLabel();
311: orgIdLabel.setForeground(Color.black);
312: typeText = new JTextField(11);
313: typeText.setText(defaultOrgActivityType);
314: opTempoText = new JTextField(11);
315: cDayLabel = new JLabel();
316: cDayLabel.setForeground(Color.black);
317: startText = new JTextField(11);
318: endText = new JTextField(11);
319: getOrgActButton = new JButton("Get Org Activity");
320: getOrgActButton.addActionListener(new DRTListener());
321: modifyOrgActButton = new JButton("Modify Org Activity");
322: modifyOrgActButton.addActionListener(new DRTListener());
323: statusLabel = new JLabel();
324: setStatus(true, "< >");
325:
326: // do layout
327: JFrame frame = new JFrame("ModifyOrgActivityGUIPlugin "
328: + getClusterID());
329: frame.setLocation(0, 0);
330: JPanel rootPanel = new JPanel((LayoutManager) null);
331: GridBagLayout gbl = new GridBagLayout();
332: GridBagConstraints gbc = new GridBagConstraints();
333: gbc.insets = new Insets(15, 15, 15, 15);
334: gbc.fill = GridBagConstraints.BOTH;
335: rootPanel.setLayout(gbl);
336:
337: // org activity info
338: JPanel orgActPanel = new JPanel();
339: orgActPanel.setLayout(new BorderLayout());
340: JLabel orgActLabel = new JLabel("Org Activity:");
341: orgActLabel.setForeground(Color.blue);
342: orgActPanel.add(orgActLabel, BorderLayout.NORTH);
343: JPanel orgActInfoPanel = new JPanel();
344: orgActInfoPanel.setLayout(new BorderLayout());
345: JPanel orgActInfoTextPanel = new JPanel();
346: orgActInfoTextPanel.setLayout(new GridLayout(6, 1));
347: orgActInfoTextPanel.add(new JLabel("OrgId:"));
348: orgActInfoTextPanel.add(new JLabel("Type:"));
349: orgActInfoTextPanel.add(new JLabel("OpTempo:"));
350: orgActInfoTextPanel.add(new JLabel("cDay:"));
351: orgActInfoTextPanel.add(new JLabel("Start Date:"));
352: orgActInfoTextPanel.add(new JLabel("End Date:"));
353: JPanel orgActInfoValuePanel = new JPanel();
354: orgActInfoValuePanel.setLayout(new GridLayout(6, 1));
355: orgActInfoValuePanel.add(orgIdLabel);
356: orgActInfoValuePanel.add(typeText);
357: orgActInfoValuePanel.add(opTempoText);
358: orgActInfoValuePanel.add(cDayLabel);
359: orgActInfoValuePanel.add(startText);
360: orgActInfoValuePanel.add(endText);
361: orgActInfoPanel.add(orgActInfoTextPanel, BorderLayout.CENTER);
362: orgActInfoPanel.add(orgActInfoValuePanel, BorderLayout.EAST);
363: orgActPanel.add(orgActInfoPanel, BorderLayout.CENTER);
364: gbl.setConstraints(orgActPanel, gbc);
365: rootPanel.add(orgActPanel);
366:
367: gbc.gridy = 2;
368: gbl.setConstraints(getOrgActButton, gbc);
369: rootPanel.add(getOrgActButton);
370:
371: gbc.gridy = 3;
372: gbl.setConstraints(modifyOrgActButton, gbc);
373: rootPanel.add(modifyOrgActButton);
374:
375: JPanel statusPanel = new JPanel();
376: statusPanel.setLayout(new GridLayout(2, 1));
377: JLabel statusLabelLabel = new JLabel("Status:");
378: statusLabelLabel.setForeground(Color.blue);
379: statusPanel.add(statusLabelLabel);
380: statusPanel.add(statusLabel);
381: gbc.gridy = 4;
382: gbl.setConstraints(statusPanel, gbc);
383: rootPanel.add(statusPanel);
384:
385: frame.setContentPane(rootPanel);
386: frame.pack();
387: frame.setVisible(true);
388:
389: setStatus(true, "Ready");
390: }
391:
392: /**
393: * Overrides the setupSubscriptions() in the SimplePlugin.
394: */
395: protected void setupSubscriptions() {
396: ldmf = theLDMF;
397:
398: getSubscriber().setShouldBePersisted(false);
399:
400: orgsSub = (IncrementalSubscription) subscribe(newOrgPred());
401: orgActivitiesSub = (IncrementalSubscription) subscribe(newOrgActivityPred());
402: oplansSub = (IncrementalSubscription) subscribe(newOplanPred());
403:
404: createGUI();
405: }
406:
407: protected void execute() {
408: }
409:
410: protected static String getOrgId(Organization org) {
411: String s = null;
412: try {
413: // FOR NOW:
414: s = org.getClusterPG().getMessageAddress().toString();
415: // FOR LATER:
416: //s = org.getItemIdentificationPG().getItemIdentification();
417: } catch (Exception e) {
418: }
419: return s;
420: }
421:
422: protected Organization getSelfOrg() {
423: Enumeration eOrgs = orgsSub.elements();
424: while (eOrgs.hasMoreElements()) {
425: Organization org = (Organization) eOrgs.nextElement();
426:
427: if (org.isSelf()) {
428: return org;
429: }
430: }
431: return null;
432: }
433:
434: protected OrgActivity getOrgActivity(Organization org,
435: String activityType) {
436: return getOrgActivity(getOrgId(org), activityType);
437: }
438:
439: protected OrgActivity getOrgActivity(String orgId,
440: String activityType) {
441: OrgActivity orgAct = null;
442: if ((orgId != null) && (activityType != null)) {
443: Enumeration eOrgActs = orgActivitiesSub.elements();
444: while (eOrgActs.hasMoreElements()) {
445: OrgActivity oa = (OrgActivity) eOrgActs.nextElement();
446: if (orgId.equals(oa.getOrgID())
447: && activityType.equals(oa.getActivityType())) {
448: orgAct = oa;
449: break;
450: }
451: }
452: }
453: return orgAct;
454: }
455:
456: protected Oplan getOplan() {
457: Oplan oplan = null;
458: // which oplan? take the first one for now.
459: Enumeration eOplans = oplansSub.elements();
460: if (eOplans.hasMoreElements()) {
461: oplan = (Oplan) eOplans.nextElement();
462: }
463: return oplan;
464: }
465: }
|