1 // ******************************************************************************
2 //
3 // Title: Force Field X.
4 // Description: Force Field X - Software for Molecular Biophysics.
5 // Copyright: Copyright (c) Michael J. Schnieders 2001-2025.
6 //
7 // This file is part of Force Field X.
8 //
9 // Force Field X is free software; you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License version 3 as published by
11 // the Free Software Foundation.
12 //
13 // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 // details.
17 //
18 // You should have received a copy of the GNU General Public License along with
19 // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20 // Place, Suite 330, Boston, MA 02111-1307 USA
21 //
22 // Linking this library statically or dynamically with other modules is making a
23 // combined work based on this library. Thus, the terms and conditions of the
24 // GNU General Public License cover the whole combination.
25 //
26 // As a special exception, the copyright holders of this library give you
27 // permission to link this library with independent modules to produce an
28 // executable, regardless of the license terms of these independent modules, and
29 // to copy and distribute the resulting executable under terms of your choice,
30 // provided that you also meet, for each linked independent module, the terms
31 // and conditions of the license of that module. An independent module is a
32 // module which is not derived from or based on this library. If you modify this
33 // library, you may extend this exception to your version of the library, but
34 // you are not obligated to do so. If you do not wish to do so, delete this
35 // exception statement from your version.
36 //
37 // ******************************************************************************
38 package ffx.ui;
39
40 import ffx.potential.MolecularAssembly;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import javax.swing.Timer;
44
45 /**
46 * The Trajectory class controls playback of a TINKER trajectory.
47 *
48 * @author Michael J. Schnieders
49 */
50 public class Trajectory implements ActionListener {
51
52 private MolecularAssembly molecularSystem;
53 private MainPanel mainPanel;
54 private Timer timer;
55 private int delay = 50;
56 private int desiredspeed = 20;
57 private int cycle = 1;
58 private int sign = 1;
59 private int skip = 1;
60 private boolean oscillate = false;
61
62 /**
63 * Constructor for Trajectory.
64 *
65 * @param mol a {@link ffx.potential.MolecularAssembly} object.
66 * @param f a {@link ffx.ui.MainPanel} object.
67 */
68 public Trajectory(MolecularAssembly mol, MainPanel f) {
69 molecularSystem = mol;
70 mainPanel = f;
71 timer = new Timer(delay, this);
72 timer.setCoalesce(true);
73 }
74
75 /** {@inheritDoc} */
76 public void actionPerformed(ActionEvent e) {
77 if (mainPanel.getGraphics3D().isCacheFull()) {
78 return;
79 }
80 cycle = advance(skip * sign);
81 setFrame(cycle);
82 }
83
84 /**
85 * getFSystem
86 *
87 * @return a {@link ffx.potential.MolecularAssembly} object.
88 */
89 public MolecularAssembly getFSystem() {
90 return molecularSystem;
91 }
92
93 /**
94 * getFrame
95 *
96 * @return a int.
97 */
98 public int getFrame() {
99 return molecularSystem.getCurrentCycle();
100 }
101
102 /**
103 * setFrame
104 *
105 * @param f a int.
106 */
107 public void setFrame(int f) {
108 if (molecularSystem != null) {
109 molecularSystem.setCurrentCycle(f);
110 mainPanel.getGraphics3D().updateScene(molecularSystem, true, false, null, false, null);
111 mainPanel.getHierarchy().updateStatus();
112 }
113 }
114
115 /**
116 * Getter for the field <code>oscillate</code>.
117 *
118 * @return a boolean.
119 */
120 public boolean getOscillate() {
121 return oscillate;
122 }
123
124 /**
125 * Setter for the field <code>oscillate</code>.
126 *
127 * @param o a boolean.
128 */
129 void setOscillate(boolean o) {
130 oscillate = o;
131 }
132
133 /**
134 * getRate
135 *
136 * @return a int.
137 */
138 public int getRate() {
139 return desiredspeed;
140 }
141
142 /**
143 * setRate
144 *
145 * @param s a int.
146 */
147 public void setRate(int s) {
148 if (s > 0 && s <= 100) {
149 desiredspeed = s;
150 delay = 1000 / s;
151 timer.setDelay(delay);
152 }
153 }
154
155 /** start */
156 public void start() {
157 timer.start();
158 }
159
160 /** stop */
161 public void stop() {
162 timer.stop();
163 }
164
165 private int advance(int adv) {
166 if (molecularSystem != null) {
167 cycle = molecularSystem.getCurrentCycle();
168 int frame = cycle + adv;
169 if ((frame) <= 0) {
170 sign = 1;
171 if (oscillate) {
172 frame = -adv - cycle;
173 } else {
174 frame = molecularSystem.getCycles() + (adv + cycle);
175 }
176 } else if ((frame) > molecularSystem.getCycles()) {
177 if (oscillate) {
178 frame = molecularSystem.getCycles() + (-adv + cycle);
179 sign = -1;
180 } else {
181 sign = 1;
182 frame = cycle - molecularSystem.getCycles() + adv;
183 }
184 }
185 return frame;
186 }
187 return 0;
188 }
189
190 /** back */
191 void back() {
192 setFrame(getFrame() - 1);
193 }
194
195 /** forward */
196 void forward() {
197 setFrame(getFrame() + 1);
198 }
199
200 /**
201 * Getter for the field <code>skip</code>.
202 *
203 * @return a int.
204 */
205 int getSkip() {
206 return skip;
207 }
208
209 /**
210 * Setter for the field <code>skip</code>.
211 *
212 * @param s a int.
213 */
214 void setSkip(int s) {
215 if (s < 1) {
216 return;
217 }
218 skip = s % molecularSystem.getAtomList().size();
219 }
220
221 /** rewind */
222 void rewind() {
223 setFrame(1);
224 }
225 }