| 1 | package desmoj.core.simulator; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.Toolkit; |
| 5 | import java.awt.event.ActionEvent; |
| 6 | import java.awt.event.ActionListener; |
| 7 | |
| 8 | import javax.swing.BorderFactory; |
| 9 | import javax.swing.JFrame; |
| 10 | import javax.swing.JPanel; |
| 11 | import javax.swing.JProgressBar; |
| 12 | import javax.swing.Timer; |
| 13 | |
| 14 | /** |
| 15 | * The progress bar to display the progress of the experiment. |
| 16 | * |
| 17 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
| 18 | * @author Soenke Claassen |
| 19 | * |
| 20 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | * you may not use this file except in compliance with the License. You |
| 22 | * may obtain a copy of the License at |
| 23 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | * |
| 25 | * Unless required by applicable law or agreed to in writing, software |
| 26 | * distributed under the License is distributed on an "AS IS" |
| 27 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 28 | * or implied. See the License for the specific language governing |
| 29 | * permissions and limitations under the License. |
| 30 | * |
| 31 | * @sharpen.ignore |
| 32 | * |
| 33 | */ |
| 34 | public class ExpProgressBar extends JFrame { |
| 35 | |
| 36 | private static final long serialVersionUID = 1L; |
| 37 | |
| 38 | /** |
| 39 | * A constant defining half a second as 500 milliseconds. Every half a |
| 40 | * second the progress bar will be updated. |
| 41 | */ |
| 42 | public final static int HALF_A_SECOND = 500; |
| 43 | |
| 44 | /** |
| 45 | * The experiment this ExpProgressBar is connected to. |
| 46 | */ |
| 47 | private Experiment _myExperiment; |
| 48 | |
| 49 | /** |
| 50 | * The ProgressBar displaying the progress of the experiment. |
| 51 | */ |
| 52 | private JProgressBar _progressBar; |
| 53 | |
| 54 | /** |
| 55 | * The timer timing the update of the progress bar. |
| 56 | */ |
| 57 | private Timer _timer; |
| 58 | |
| 59 | /** |
| 60 | * Constructs an ExpProgressBar for an <code>Experiment</code> to display |
| 61 | * its progress on the screen. |
| 62 | * |
| 63 | * @param experiment |
| 64 | * Experiment : The experiment which progress will be displayed |
| 65 | * of the progress bar. |
| 66 | */ |
| 67 | public ExpProgressBar(Experiment experiment) { |
| 68 | |
| 69 | super("Progress of the experiment"); // make a JFrame |
| 70 | |
| 71 | this._myExperiment = experiment; |
| 72 | |
| 73 | setTitle("Progress of " + _myExperiment.getName()); |
| 74 | |
| 75 | // create the UI |
| 76 | _progressBar = new JProgressBar(0, 100); |
| 77 | _progressBar.setValue(0); |
| 78 | _progressBar.setStringPainted(true); |
| 79 | // set the preferred size |
| 80 | _progressBar.setPreferredSize(new java.awt.Dimension(320, 22)); |
| 81 | |
| 82 | JPanel contentPane = new JPanel(); |
| 83 | contentPane.setLayout(new BorderLayout()); |
| 84 | contentPane.add(_progressBar, BorderLayout.CENTER); |
| 85 | contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); |
| 86 | setContentPane(contentPane); |
| 87 | |
| 88 | // Create a timer. |
| 89 | _timer = new Timer(HALF_A_SECOND, new ActionListener() { |
| 90 | public void actionPerformed(ActionEvent evt) { |
| 91 | long crntTime = _myExperiment.getSimClock().getTime().getTimeInEpsilon(); |
| 92 | long start = TimeOperations.getStartTime().getTimeInEpsilon(); |
| 93 | long stop = _myExperiment.getStopTime().getTimeInEpsilon(); |
| 94 | int progress = (int) (100.0D * (crntTime - start) / (stop - start)); |
| 95 | _progressBar.setValue(progress); |
| 96 | if (_myExperiment.isAborted()) { |
| 97 | Toolkit.getDefaultToolkit().beep(); |
| 98 | _timer.stop(); |
| 99 | } |
| 100 | } |
| 101 | }); |
| 102 | |
| 103 | // start the timer |
| 104 | _timer.start(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the <code>Experiment</code> this progress bar monitors. |
| 109 | * |
| 110 | * @return desmoj.Experiment : the <code>Experiment</code> this progress |
| 111 | * bar monitors. |
| 112 | */ |
| 113 | public Experiment getExperiment() { |
| 114 | |
| 115 | return _myExperiment; |
| 116 | } |
| 117 | } |