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;
028:
029: import java.awt.Component;
030: import java.util.Random;
031:
032: import javax.swing.JButton;
033: import javax.swing.SwingUtilities;
034:
035: import org.cougaar.core.blackboard.IncrementalSubscription;
036: import org.cougaar.planning.plugin.legacy.PluginDelegate;
037: import org.cougaar.util.MinMaxPanel;
038: import org.cougaar.util.UnaryPredicate;
039:
040: public class RandomButtonPusher implements java.io.Serializable {
041:
042: private static final Random random = new Random();
043:
044: private boolean enabled;
045:
046: private int nPushes = 0;
047:
048: private int remainingTime = 0;
049:
050: private int totalTime = 1;
051:
052: private transient int accumulatedDelta = 0;
053:
054: private int minSleepTime;
055:
056: private int maxSleepTime;
057:
058: private transient String buttonLabel;
059:
060: private transient JButton theButtonToPush;
061:
062: private transient MinMaxPanel theMinMaxPanel;
063:
064: private transient Thread sleepThread;
065:
066: private transient Runnable clicker;
067:
068: private transient PluginDelegate plugInDelegate;
069:
070: protected RandomButtonPusher(int minSleepTime, int maxSleepTime,
071: boolean initialState) {
072: this .minSleepTime = minSleepTime;
073: this .maxSleepTime = maxSleepTime;
074: enabled = initialState;
075: }
076:
077: public static IncrementalSubscription subscribe(PluginDelegate pid,
078: Class randomButtonPusherClass) {
079: final String randomButtonPusherClassName = randomButtonPusherClass
080: .getName();
081: UnaryPredicate predicate = new UnaryPredicate() {
082: public boolean execute(Object o) {
083: return randomButtonPusherClassName.equals(o.getClass()
084: .getName());
085: }
086: };
087: return (IncrementalSubscription) pid.subscribe(predicate);
088: }
089:
090: public Component init(String label, PluginDelegate delegate,
091: JButton aButtonToPush) {
092: buttonLabel = label;
093: plugInDelegate = delegate;
094: theButtonToPush = aButtonToPush;
095: theMinMaxPanel = new MinMaxPanel() {
096: protected void newMin(int min) {
097: minSleepTime = min * 1000;
098: }
099:
100: protected void newMax(int max) {
101: maxSleepTime = max * 1000;
102: }
103:
104: protected void newEnable(boolean newEnabled) {
105: enabled = newEnabled;
106: RandomButtonPusher.this .setEnabled(enabled);
107: stopTimer();
108: if (enabled) {
109: startTimer();
110: }
111: }
112: };
113: theMinMaxPanel.setText(buttonLabel);
114: theMinMaxPanel.setColumns(3);
115: theMinMaxPanel.setEnabled(enabled);
116: theMinMaxPanel.setMin(minSleepTime / 1000);
117: theMinMaxPanel.setMax(maxSleepTime / 1000);
118: if (enabled) {
119: startTimer();
120: }
121: return theMinMaxPanel;
122: }
123:
124: private void publishChange() {
125: plugInDelegate.openTransaction();
126: plugInDelegate.publishChange(this );
127: plugInDelegate.closeTransactionDontReset();
128: }
129:
130: private void setEnabled(boolean newEnabled) {
131: if (newEnabled != enabled) {
132: enabled = newEnabled;
133: publishChange();
134: }
135: }
136:
137: private synchronized void setTotalTime(int howLong) {
138: totalTime = howLong;
139: theMinMaxPanel.setProgressMax(totalTime / 1000);
140: }
141:
142: private synchronized void setRemainingTime(int howLong) {
143: remainingTime = howLong;
144: accumulatedDelta = 0;
145: publishChange();
146: setCheckBoxText();
147: }
148:
149: private synchronized int getRemainingTime() {
150: return remainingTime - accumulatedDelta;
151: }
152:
153: private synchronized boolean decreaseRemainingTime(int delta) {
154: accumulatedDelta += delta;
155: if (getRemainingTime() <= 0) {
156: setRemainingTime(0);
157: return true;
158: }
159: if (accumulatedDelta > 10000) {
160: setRemainingTime(getRemainingTime());
161: } else {
162: setCheckBoxText();
163: }
164: return false;
165: }
166:
167: private void incrementNPushes() {
168: nPushes++;
169: publishChange();
170: setCheckBoxText();
171: }
172:
173: private void setCheckBoxText() {
174: theMinMaxPanel
175: .setProgressValue((totalTime - getRemainingTime())
176: / (1.0f * totalTime));
177: theMinMaxPanel.setText(buttonLabel + " " + (nPushes + 1));
178: }
179:
180: private Runnable getClicker() {
181: if (clicker == null) {
182: clicker = new Runnable() {
183: public void run() {
184: theButtonToPush.doClick();
185: incrementNPushes();
186: }
187: };
188: }
189: return clicker;
190: }
191:
192: private void startTimer() {
193: sleepThread = new Thread(buttonLabel) {
194: public void run() {
195: try {
196: while (true) {
197: if (getRemainingTime() == 0) {
198: int time = minSleepTime
199: + random.nextInt(maxSleepTime
200: - minSleepTime);
201: setTotalTime(time);
202: setRemainingTime(time);
203: }
204: int sleepTime = getRemainingTime() % 1000;
205: if (sleepTime == 0) {
206: sleepTime = 1000;
207: }
208: sleep(sleepTime);
209: if (decreaseRemainingTime(sleepTime)) {
210: SwingUtilities.invokeLater(getClicker());
211: }
212: }
213: } catch (InterruptedException e) {
214: }
215: }
216: };
217: sleepThread.start();
218: }
219:
220: private synchronized void stopTimer() {
221: if (sleepThread != null) {
222: sleepThread.interrupt();
223: sleepThread = null;
224: }
225: setRemainingTime(0);
226: }
227: }
|