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.glm.plugins;
028:
029: import org.cougaar.core.blackboard.IncrementalSubscription;
030: import org.cougaar.planning.ldm.policy.IntegerRuleParameter;
031: import org.cougaar.planning.ldm.policy.Policy;
032: import org.cougaar.planning.ldm.policy.RuleParameter;
033: import org.cougaar.planning.ldm.policy.StringRuleParameter;
034: import org.cougaar.planning.plugin.legacy.SimplePlugin;
035: import org.cougaar.util.UnaryPredicate;
036: import org.cougaar.util.log.Logger;
037: import org.cougaar.util.log.Logging;
038:
039: import javax.swing.*;
040: import javax.swing.event.ChangeEvent;
041: import javax.swing.event.ChangeListener;
042: import java.awt.*;
043: import java.awt.event.ActionEvent;
044: import java.awt.event.ActionListener;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.util.Enumeration;
048: import java.util.Hashtable;
049:
050: /**
051: * The Policy GUI Plugin based on
052: * org.cougaar.mlm.plugin.sample.PolicyPlugin
053: */
054:
055: public class GLMPolicyPlugin extends SimplePlugin {
056: private static Logger logger = Logging
057: .getLogger(GLMPolicyPlugin.class);
058:
059: class GLMPolicyGUI extends JFrame implements PropertyChangeListener {
060: protected JButton changeBtn = new JButton("Change Policy");
061: private Hashtable sliders_;
062: private Hashtable textFields_;
063: private Policy policy;
064:
065: GLMPolicyGUI(String name) {
066: super (name);
067: }
068:
069: GLMPolicyGUI(Policy forPolicy) {
070: super (forPolicy.getName() + " Plugin");
071: policy = forPolicy;
072: sliders_ = new Hashtable();
073: textFields_ = new Hashtable();
074: setLocation(0, 310);
075: RuleParameter rules[] = policy.getRuleParameters();
076: getContentPane().setLayout(
077: new GridLayout(rules.length + 1, 0));
078:
079: for (int ii = 0; ii < rules.length; ii++) {
080: RuleParameter rule = rules[ii];
081: if (rule instanceof IntegerRuleParameter) {
082: addIntegerRule((IntegerRuleParameter) rule);
083: } else if (rule instanceof StringRuleParameter) {
084: addStringRule((StringRuleParameter) rule);
085: } else {
086: if (logger.isErrorEnabled()) {
087: logger.error("Unexpected Rule Parameter type "
088: + rule);
089: }
090: }
091: }
092:
093: changeBtn.addActionListener(new MyButtonListener(sliders_,
094: textFields_));
095: getContentPane().add(changeBtn);
096: pack();
097: setVisible(true);
098: policy.addPropertyChangeListener(this );
099: }
100:
101: public void addStringRule(StringRuleParameter rule) {
102: JPanel panel = new JPanel();
103:
104: String hashKey = rule.getName().trim();
105: // Create the text field with label
106: Label label = new Label(rule.getName() + " = "
107: + rule.getValue());
108: String value = (String) rule.getValue();
109: JTextField textField = new JTextField(value);
110: panel.add(textField);
111: panel.add(label);
112:
113: getContentPane().add(panel);
114: textFields_.put(hashKey, textField);
115: }
116:
117: public void addIntegerRule(IntegerRuleParameter rule) {
118: JPanel panel = new JPanel();
119:
120: String hashKey = rule.getName().trim();
121: // Create the slider with label
122: Label label = new Label(rule.getName() + " = "
123: + rule.getValue());
124: int value = ((Integer) rule.getValue()).intValue();
125: // JSlider slider = new JSlider(JSlider.HORIZONTAL, rule.my_min, rule.my_max, value);
126: JSlider slider = new JSlider(JSlider.HORIZONTAL, 1, 50,
127: value);
128: slider.addChangeListener(new MySliderListener(slider, rule,
129: label));
130: panel.add(slider);
131: panel.add(label);
132:
133: getContentPane().add(panel);
134: sliders_.put(hashKey, slider);
135: }
136:
137: /**
138: * An ActionListener that listens to the buttons.
139: */
140:
141: class MySliderListener implements ChangeListener {
142: JSlider slider_;
143: RuleParameter rule_;
144: Label label_;
145:
146: public MySliderListener(JSlider slider, RuleParameter rule,
147: Label l) {
148: super ();
149: slider_ = slider;
150: rule_ = rule;
151: label_ = l;
152: }
153:
154: public void stateChanged(ChangeEvent ce) {
155: openTheTransaction();
156: try {
157: rule_.setValue(new Integer(slider_.getValue()));
158: } catch (org.cougaar.planning.ldm.policy.RuleParameterIllegalValueException pe) {
159: if (logger.isErrorEnabled()) {
160: logger.error("changePolicy error :" + pe);
161: }
162: return;
163: }
164: label_.setText(rule_.getName() + " = "
165: + rule_.getValue());
166:
167: closeTheTransaction(false);
168: }
169: }
170:
171: /**
172: * An ActionListener that listens to the buttons.
173: */
174: class MyButtonListener implements ActionListener {
175: Hashtable sliders_;
176: Hashtable textFields_;
177:
178: public MyButtonListener(Hashtable sliders,
179: Hashtable textFields) {
180: super ();
181: sliders_ = sliders;
182: textFields_ = textFields;
183: }
184:
185: public void actionPerformed(ActionEvent ae) {
186: openTheTransaction();
187: Enumeration keys = sliders_.keys();
188: RuleParameter rule;
189: JSlider slide;
190: String hashKey;
191: while (keys.hasMoreElements()) {
192: hashKey = (String) keys.nextElement();
193: rule = policy.Lookup(hashKey);
194: slide = (JSlider) sliders_.get(hashKey);
195: changePolicy(slide.getValue(), rule);
196: }
197: keys = textFields_.keys();
198: JTextField textField;
199: String value;
200: while (keys.hasMoreElements()) {
201: hashKey = (String) keys.nextElement();
202: rule = policy.Lookup(hashKey);
203: value = (String) rule.getValue();
204: textField = (JTextField) textFields_.get(hashKey);
205: changePolicy(textField.getText(), rule);
206: }
207: closeTheTransaction(false);
208: }
209: }
210:
211: public void changePolicy(int value, RuleParameter rule) {
212: try {
213: rule.setValue(new Integer(value));
214: } catch (org.cougaar.planning.ldm.policy.RuleParameterIllegalValueException pe) {
215: if (logger.isErrorEnabled()) {
216: logger.error("changePolicy error :" + pe);
217: }
218: return;
219: }
220: publishTheChange(policy);
221: }
222:
223: public void changePolicy(String value, RuleParameter rule) {
224: try {
225: rule.setValue(value);
226: } catch (org.cougaar.planning.ldm.policy.RuleParameterIllegalValueException pe) {
227: if (logger.isErrorEnabled()) {
228: logger.error("changePolicy error :" + pe);
229: }
230: return;
231: }
232: publishTheChange(policy);
233: }
234:
235: public void propertyChange(PropertyChangeEvent evt) {
236: String propertyName = evt.getPropertyName();
237: Object property = evt.getNewValue();
238: try {
239: if (propertyName == "RuleParameters") {
240: Hashtable params = (Hashtable) property;
241: Enumeration keys = params.keys();
242: String hashKey, svalue;
243: IntegerRuleParameter irule;
244: RuleParameter rule;
245: StringRuleParameter srule;
246: Integer ivalue;
247: JSlider slider;
248: JTextField textField;
249: int intValue;
250:
251: while (keys.hasMoreElements()) {
252: rule = (RuleParameter) params.get(keys
253: .nextElement());
254: if (rule instanceof IntegerRuleParameter) {
255: irule = (IntegerRuleParameter) rule;
256: ivalue = (Integer) irule.getValue();
257: hashKey = irule.getName();
258: slider = (JSlider) sliders_.get(hashKey);
259: intValue = ivalue.intValue();
260:
261: // don't cause unnecessary redraws
262: if (slider.getValue() != intValue)
263: slider.setValue(intValue);
264:
265: } else if (rule instanceof StringRuleParameter) {
266: srule = (StringRuleParameter) rule;
267: hashKey = srule.getName().trim();
268: svalue = (String) srule.getValue();
269: textField = (JTextField) textFields_
270: .get(hashKey);
271:
272: if (!(textField.getText().equals(svalue)))
273: textField.setText(svalue);
274:
275: } else {
276: if (logger.isErrorEnabled()) {
277: logger
278: .error("Unexpected Rule Parameter "
279: + rule);
280: }
281: }
282: }
283: } else {
284: if (logger.isErrorEnabled()) {
285: logger.error("Unexpected Property Name "
286: + propertyName);
287: }
288: }
289: } catch (Exception e) {
290: if (logger.isErrorEnabled()) {
291: logger
292: .error(" Problem resetting Policy GUI with new values");
293: }
294: e.printStackTrace();
295: }
296: }
297: }
298:
299: private IncrementalSubscription myPolicies;
300: private UnaryPredicate policyPredicate;
301: private Hashtable policyGUIs = new Hashtable();
302:
303: public GLMPolicyPlugin(UnaryPredicate pol_pred) {
304: policyPredicate = pol_pred;
305: }
306:
307: // Have to provide these on this plugin class, else the inner class
308: // below will not be able to find them
309: public void openTheTransaction() {
310: openTransaction();
311: }
312:
313: public void closeTheTransaction(boolean b) {
314: closeTransaction(b);
315: }
316:
317: public void publishTheChange(Object o) {
318: publishChange(o);
319: }
320:
321: /**
322: * Overrides the setupSubscriptions() in the SimplePlugin.
323: */
324: protected void setupSubscriptions() {
325: getSubscriber().setShouldBePersisted(false);
326: myPolicies = (IncrementalSubscription) subscribe(policyPredicate);
327: }
328:
329: public synchronized void execute() {
330: for (Enumeration e = myPolicies.getAddedList(); e
331: .hasMoreElements();) {
332: Policy policy = (Policy) e.nextElement();
333: policyGUIs.put(policy, new GLMPolicyGUI(policy));
334: }
335: for (Enumeration e = myPolicies.getRemovedList(); e
336: .hasMoreElements();) {
337: Policy policy = (Policy) e.nextElement();
338: GLMPolicyGUI gui = (GLMPolicyGUI) policyGUIs.get(policy);
339: if (gui != null) {
340: gui.dispose();
341: policyGUIs.remove(policy);
342: }
343: }
344: }
345: }
|