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.mlm.plugin.ldm;
028:
029: import java.awt.LayoutManager;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.Enumeration;
035: import java.util.Iterator;
036: import java.util.Vector;
037:
038: import javax.swing.JButton;
039: import javax.swing.JFrame;
040: import javax.swing.JLabel;
041: import javax.swing.JPanel;
042:
043: import org.cougaar.core.blackboard.IncrementalSubscription;
044: import org.cougaar.core.util.UID;
045: import org.cougaar.glm.ldm.oplan.Oplan;
046: import org.cougaar.glm.ldm.oplan.OplanContributor;
047: import org.cougaar.glm.ldm.oplan.OplanCoupon;
048: import org.cougaar.mlm.plugin.UICoordinator;
049: import org.cougaar.planning.plugin.legacy.SimplePlugin;
050: import org.cougaar.util.UnaryPredicate;
051:
052: /**
053: * The OPlanPlugin instantiates the OPlan
054: * and adds it to the LogPlan.
055: *
056: **/
057: public class OPlanPlugin extends SimplePlugin {
058: /** frame for 1-button UI **/
059: private JFrame frame;
060:
061: /** for feedback to user on whether root GLS was successful **/
062: JLabel oplanLabel;
063:
064: protected JButton oplanButton;
065:
066: private IncrementalSubscription oplanSubscription;
067:
068: private IncrementalSubscription stateSubscription;
069:
070: private ArrayList contributors;
071:
072: private static class MyPrivateState implements java.io.Serializable {
073: boolean oplanExists = false;
074: boolean errorOccurred = false;
075: }
076:
077: private MyPrivateState myPrivateState;
078:
079: private static UnaryPredicate oplanPredicate = new UnaryPredicate() {
080: public boolean execute(Object o) {
081: return (o instanceof Oplan);
082: }
083: };
084:
085: private static UnaryPredicate statePredicate = new UnaryPredicate() {
086: public boolean execute(Object o) {
087: return (o instanceof MyPrivateState);
088: }
089: };
090:
091: /*
092: * Creates a subscription.
093: */
094: protected void setupSubscriptions() {
095: getBlackboardService().setShouldBePersisted(false);
096: oplanSubscription = (IncrementalSubscription) subscribe(oplanPredicate);
097: stateSubscription = (IncrementalSubscription) subscribe(statePredicate);
098:
099: contributors = new ArrayList(13);
100: // refill contributors Collection on rehydrate
101: processOplanAdds(oplanSubscription.getCollection());
102:
103: createGUI();
104:
105: if (didRehydrate()) {
106: checkForPrivateState(stateSubscription.elements());
107: } else {
108: publishAdd(new MyPrivateState());
109: }
110: }
111:
112: /**
113: * Executes Plugin functionality.
114: */
115: protected void execute() {
116: if (stateSubscription.hasChanged()) {
117: checkForPrivateState(stateSubscription.getAddedList());
118: }
119: if (oplanSubscription.hasChanged()) {
120: Collection adds = oplanSubscription.getAddedCollection();
121: if (adds != null) {
122: processOplanAdds(adds);
123: }
124: Collection changes = oplanSubscription
125: .getChangedCollection();
126: if (changes != null) {
127: processChanges(changes);
128: }
129: Collection deletes = oplanSubscription
130: .getRemovedCollection();
131: if (deletes != null) {
132: processOplanDeletes(deletes);
133: }
134: }
135:
136: for (Iterator it = contributors.iterator(); it.hasNext();) {
137: IncrementalSubscription is = (IncrementalSubscription) it
138: .next();
139: // we don't care about adds, just deletes and changes
140: if (is.hasChanged()) {
141: Collection changes = is.getChangedCollection();
142: if (changes != null) {
143: processChanges(changes);
144: }
145:
146: // Deletes of OplanContributors are treated as changes to the Oplan
147: changes = is.getRemovedCollection();
148: if (changes != null) {
149: processChanges(changes);
150: }
151: }
152: }
153: }
154:
155: public void unload() {
156: destroyGUI();
157: super .unload();
158: }
159:
160: private void checkForPrivateState(Enumeration e) {
161: if (myPrivateState == null) {
162: while (e.hasMoreElements()) {
163: myPrivateState = (MyPrivateState) e.nextElement();
164: checkButtonEnable();
165: }
166: }
167: }
168:
169: private void createGUI() {
170: frame = new JFrame("OplanPlugin");
171: JPanel panel = new JPanel((LayoutManager) null);
172: // Create the button
173: oplanButton = new JButton("Publish Oplan");
174: oplanLabel = new JLabel("No Oplan has been published.");
175:
176: // Register a listener for the check box
177: OplanButtonListener myOplanListener = new OplanButtonListener();
178: oplanButton.addActionListener(myOplanListener);
179: oplanButton.setEnabled(false);
180: UICoordinator.layoutButtonAndLabel(panel, oplanButton,
181: oplanLabel);
182: frame.getRootPane().setDefaultButton(oplanButton); // hitting return sends the oplan
183: frame.setContentPane(panel);
184: frame.pack();
185: UICoordinator.setBounds(frame);
186: frame.setVisible(true);
187: }
188:
189: private void destroyGUI() {
190: frame.dispose();
191: }
192:
193: /** An ActionListener that listens to the GLS buttons. */
194: class OplanButtonListener implements ActionListener {
195: public void actionPerformed(ActionEvent ae) {
196: publishOplan();
197: }
198: }
199:
200: private void checkButtonEnable() {
201: // Log status
202: oplanButton.setEnabled(!myPrivateState.oplanExists);
203: if (myPrivateState.oplanExists) {
204: oplanLabel.setText("Published successfully");
205: } else if (myPrivateState.errorOccurred) {
206: oplanLabel
207: .setText("ERROR::Unable to add the Oplan to the LogPlan.");
208: } else {
209: oplanLabel.setText("No Oplan has been published.");
210: }
211: }
212:
213: private void publishOplan() {
214: // Get the Plugin Parameters
215: Vector params = getParameters();
216: ArrayList oplans = new ArrayList(params.size());
217: ArrayList oplanComponents = new ArrayList(params.size());
218: // Instantiate the OPlanFileReader to create the OPlan
219: if (params.size() != 0) {
220: for (int i = 0, n = params.size(); i < n; i++) {
221: String fileName = (String) (params.elementAt(i));
222: OplanFileReader ofr = new OplanFileReader(fileName,
223: getFactory(), getCluster());
224: Oplan oplan = ofr.readOplan();
225: oplans.add(oplan);
226: oplanComponents.addAll(ofr.getForcePackages(oplan));
227: oplanComponents.addAll(ofr.getOrgActivities(oplan));
228: oplanComponents.addAll(ofr.getOrgRelations(oplan));
229: oplanComponents.addAll(ofr.getPolicies(oplan));
230:
231: if (oplan.getEndDay() == null) {
232: oplan.inferEndDay(oplanComponents);
233: }
234: oplan.setMaxActiveStage(0);
235: }
236: } else {
237: System.err
238: .println("OPlanPlugin : No parameters were specified");
239: }
240: openTransaction();
241: int n = oplans.size();
242: if (n > 0) {
243: for (int i = 0; i < n; i++) {
244: Oplan oplan = (Oplan) oplans.get(i);
245: // Add the OPLAN to the LogPlan.
246: publishAdd(oplan);
247: // publish the subs
248: for (Iterator iterator = oplanComponents.iterator(); iterator
249: .hasNext();) {
250: publishAdd(iterator.next());
251: }
252:
253: OplanCoupon ow = new OplanCoupon(oplan.getUID(),
254: getMessageAddress());
255: getCluster().getUIDServer().registerUniqueObject(ow);
256: publishAdd(ow);
257: }
258: myPrivateState.oplanExists = true;
259: } else {
260: myPrivateState.errorOccurred = true;
261: }
262:
263: publishChange(myPrivateState);
264: closeTransactionDontReset();
265: checkButtonEnable();
266: }
267:
268: private void processOplanAdds(Collection adds) {
269: for (Iterator it = adds.iterator(); it.hasNext();) {
270: Oplan oplan = (Oplan) it.next();
271:
272: IncrementalSubscription is = (IncrementalSubscription) subscribe(new ContributorPredicate(
273: oplan.getUID()));
274: contributors.add(is);
275: }
276: }
277:
278: private void processChanges(Collection changes) {
279: for (Iterator it = changes.iterator(); it.hasNext();) {
280: UID oplanUID = null;
281: Object o = it.next();
282: if (o instanceof Oplan) {
283: oplanUID = ((Oplan) o).getUID();
284: } else if (o instanceof OplanContributor) {
285: oplanUID = ((OplanContributor) o).getOplanUID();
286: } else
287: continue;
288:
289: Collection coupons = query(new CouponPredicate(oplanUID));
290: for (Iterator couponIt = coupons.iterator(); couponIt
291: .hasNext();) {
292: //System.out.println("OPlanPlugin: publishChanging OplanCoupon");
293: publishChange(couponIt.next());
294: }
295: }
296: }
297:
298: private void processOplanDeletes(Collection deletes) {
299: for (Iterator it = deletes.iterator(); it.hasNext();) {
300: Oplan oplan = (Oplan) it.next();
301: Collection coupons = query(new CouponPredicate(oplan
302: .getUID()));
303: for (Iterator couponIt = coupons.iterator(); couponIt
304: .hasNext();) {
305: publishRemove(couponIt.next());
306: }
307: }
308: }
309:
310: private class CouponPredicate implements UnaryPredicate {
311: UID _oplanUID;
312:
313: public CouponPredicate(UID oplanUID) {
314: _oplanUID = oplanUID;
315: }
316:
317: public boolean execute(Object o) {
318: if (o instanceof OplanCoupon) {
319: if (((OplanCoupon) o).getOplanUID().equals(_oplanUID)) {
320: return true;
321: }
322: }
323: return false;
324: }
325: }
326:
327: private class ContributorPredicate implements UnaryPredicate {
328: UID _oplanUID;
329:
330: public ContributorPredicate(UID oplanUID) {
331: _oplanUID = oplanUID;
332: }
333:
334: public boolean execute(Object o) {
335: if (o instanceof OplanContributor) {
336: if (((OplanContributor) o).getOplanUID().equals(
337: _oplanUID)) {
338: return true;
339: }
340: }
341: return false;
342: }
343: }
344: }
|