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.sample;
028:
029: import java.awt.Dimension;
030: import java.awt.FlowLayout;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.beans.PropertyChangeEvent;
034: import java.beans.PropertyChangeListener;
035: import java.util.Enumeration;
036: import java.util.Hashtable;
037:
038: import javax.swing.ButtonGroup;
039: import javax.swing.JFrame;
040: import javax.swing.JLabel;
041: import javax.swing.JPanel;
042: import javax.swing.JRadioButton;
043: import javax.swing.JSlider;
044: import javax.swing.event.ChangeEvent;
045: import javax.swing.event.ChangeListener;
046:
047: import org.cougaar.core.blackboard.IncrementalSubscription;
048: import org.cougaar.glm.ldm.policy.ShipPolicy;
049: import org.cougaar.mlm.plugin.UICoordinator;
050: import org.cougaar.planning.ldm.policy.EnumerationRuleParameter;
051: import org.cougaar.planning.ldm.policy.IntegerRuleParameter;
052: import org.cougaar.planning.plugin.legacy.SimplePlugin;
053: import org.cougaar.util.UnaryPredicate;
054:
055: /**
056: * The PolicyPlugin
057: */
058:
059: public class PolicyPlugin extends SimplePlugin {
060: class PolicyGUI extends JFrame implements PropertyChangeListener {
061: protected JLabel label;
062:
063: protected JSlider slider = new JSlider(JSlider.HORIZONTAL, 1,
064: 10, 3) {
065: public Dimension getPreferredSize() {
066: return new Dimension(100,
067: super .getPreferredSize().height);
068: }
069:
070: public Dimension getMinimumSize() {
071: return new Dimension(100, super .getMinimumSize().height);
072: }
073: };
074:
075: protected ButtonGroup modeButtonGroup;
076: JRadioButton groundButton = null;
077: JRadioButton seaButton = null;
078: JRadioButton airButton = null;
079:
080: private ShipPolicy policy;
081:
082: PolicyGUI(ShipPolicy forPolicy) {
083: super ("PolicyPlugin");
084: policy = forPolicy;
085: forPolicy.addPropertyChangeListener(this );
086: getContentPane().setLayout(new FlowLayout());
087: JPanel panel = new JPanel(null);
088:
089: int days = policy.getShipDays();
090: label = new JLabel("ShipDays = " + days);
091: MySliderListener mySliderListener = new MySliderListener();
092: slider.setValue(days);
093: slider.addChangeListener(mySliderListener);
094: UICoordinator.layoutButtonAndLabel(panel, slider, label);
095:
096: JPanel modePanel = new JPanel();
097: ModeButtonListener mbl = new ModeButtonListener();
098: groundButton = new JRadioButton("Ground");
099: seaButton = new JRadioButton("Sea");
100: airButton = new JRadioButton("Air");
101: groundButton.addActionListener(mbl);
102: seaButton.addActionListener(mbl);
103: airButton.addActionListener(mbl);
104: modeButtonGroup = new ButtonGroup();
105: modeButtonGroup.add(groundButton);
106: modeButtonGroup.add(seaButton);
107: modeButtonGroup.add(airButton);
108: modePanel.add(groundButton);
109: modePanel.add(seaButton);
110: modePanel.add(airButton);
111: if (getPolicyMode().equals("Ground")) {
112: groundButton.setSelected(true);
113: } else if (getPolicyMode().equals("Air")) {
114: airButton.setSelected(true);
115: } else if (getPolicyMode().equals("Sea")) {
116: seaButton.setSelected(true);
117: }
118:
119: UICoordinator.layoutSecondRow(panel, modePanel);
120: setContentPane(panel);
121: pack();
122: UICoordinator.setBounds(this );
123: setVisible(true);
124: }
125:
126: /** An ActionListener that listens to the buttons. */
127: class MySliderListener implements ChangeListener {
128: public void stateChanged(ChangeEvent ce) {
129: openTheTransaction();
130: changePolicy(slider.getValue());
131: closeTheTransaction(false);
132: }
133: }
134:
135: class ModeButtonListener implements ActionListener {
136: public void actionPerformed(ActionEvent ae) {
137: openTheTransaction();
138: setPolicyMode(((JRadioButton) (ae.getSource()))
139: .getText());
140: closeTheTransaction(false);
141: }
142: }
143:
144: public void changePolicy(int value) {
145: policy.setShipDays(value);
146: label.setText("ShipDays = " + value);
147:
148: publishTheChange(policy);
149: }
150:
151: public String getPolicyMode() {
152: return policy.getShipMode();
153: }
154:
155: public void setPolicyMode(String newMode) {
156: policy.setShipMode(newMode);
157: publishTheChange(policy);
158: }
159:
160: public void propertyChange(PropertyChangeEvent evt) {
161: String propertyName = evt.getPropertyName();
162: Object property = evt.getNewValue();
163: try {
164: if (propertyName == "RuleParameters") {
165: Hashtable params = (Hashtable) property;
166: IntegerRuleParameter dayParam = (IntegerRuleParameter) params
167: .get(ShipPolicy.ShipDays);
168: Integer days = (Integer) dayParam.getValue();
169:
170: slider.setValue(days.intValue());
171: EnumerationRuleParameter modeParam = (EnumerationRuleParameter) params
172: .get(ShipPolicy.ShipMode);
173: String mode = (String) modeParam.getValue();
174: if (!groundButton.isSelected()
175: && mode.equals(ShipPolicy.Ground))
176: groundButton.setSelected(true);
177: else if (!airButton.isSelected()
178: && mode.equals(ShipPolicy.Air))
179: airButton.setSelected(true);
180: else if (!seaButton.isSelected()
181: && mode.equals(ShipPolicy.Sea))
182: seaButton.setSelected(true);
183: } else {
184: System.err.println("Unexpected Property Name "
185: + propertyName);
186: }
187: } catch (Exception e) {
188: System.err
189: .println("Problem resetting ShipPolicy GUI with new values");
190: e.printStackTrace();
191: }
192: }
193: }
194:
195: // Have to provide these on this plugin class, else the inner class
196: // below will not be able to find them
197: public void openTheTransaction() {
198: openTransaction();
199: }
200:
201: public void closeTheTransaction(boolean b) {
202: closeTransaction(b);
203: }
204:
205: public void publishTheChange(Object o) {
206: publishChange(o);
207: }
208:
209: private static UnaryPredicate policyPredicate() {
210: return new UnaryPredicate() {
211: public boolean execute(Object o) {
212: return (o instanceof ShipPolicy);
213: }
214: };
215: }
216:
217: private IncrementalSubscription myPolicies;
218:
219: /**
220: * Overrides the setupSubscriptions() in the SimplePlugin.
221: */
222: protected void setupSubscriptions() {
223: getSubscriber().setShouldBePersisted(false);
224: myPolicies = (IncrementalSubscription) subscribe(policyPredicate());
225: // addPolicies(myPolicies.elements());
226: }
227:
228: private Hashtable policyGUIs = new Hashtable();
229:
230: /* CCV2 execute method */
231: /* This will be called every time a new object matches the above predicate */
232: public synchronized void execute() {
233: if (myPolicies.hasChanged()) {
234: addPolicies(myPolicies.getAddedList());
235: removePolicies(myPolicies.getRemovedList());
236: }
237: }
238:
239: private void addPolicies(Enumeration e) {
240: while (e.hasMoreElements()) {
241: ShipPolicy policy = (ShipPolicy) e.nextElement();
242: policyGUIs.put(policy, new PolicyGUI(policy));
243: }
244: }
245:
246: private void removePolicies(Enumeration e) {
247: while (e.hasMoreElements()) {
248: ShipPolicy policy = (ShipPolicy) e.nextElement();
249: PolicyGUI gui = (PolicyGUI) policyGUIs.get(policy);
250: if (gui != null) {
251: gui.dispose();
252: policyGUIs.remove(policy);
253: }
254: }
255: }
256: }
|