initial import
[iDMC.git] / src / java / org / tsho / dmc2 / managers / CyclesManager.java
blobe55151b5b0084348e15f072ce24bd78ac9afb1df
1 /*
2 * iDMC the interactive Dynamical Model Calculator simulates and performs
3 * graphical and numerical analysis of systems of differential and
4 * difference equations.
6 * Copyright (C) 2004 Marji Lines and Alfredo Medio.
8 * Written by Daniele Pizzoni <auouo@tin.it>.
9 * Extended by Alexei Grigoriev <alexei_grigoriev@libero.it>.
13 * The software program was developed within a research project financed
14 * by the Italian Ministry of Universities, the Universities of Udine and
15 * Ca'Foscari of Venice, the Friuli-Venezia Giulia Region.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or any
20 * later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 package org.tsho.dmc2.managers;
29 import java.awt.event.ComponentAdapter;
30 import java.awt.event.ComponentEvent;
32 import org.jfree.data.Range;
33 import org.tsho.dmc2.core.CoreStatusEvent;
34 import org.tsho.dmc2.core.CoreStatusListener;
35 import org.tsho.dmc2.core.VariableDoubles;
36 import org.tsho.dmc2.core.chart.CyclesRenderer;
37 import org.tsho.dmc2.core.chart.DmcPlotRenderer;
38 import org.tsho.dmc2.core.model.SimpleMap;
39 import org.tsho.dmc2.ui.InvalidData;
40 import org.tsho.dmc2.ui.cycles.CyclesControlForm2;
42 /**
44 * @author tsho
46 public class CyclesManager extends AbstractManager
47 implements AbstractManager.Crosshair,
48 AbstractManager.BigDots,
49 AbstractManager.GridLines,
50 AbstractManager.Transparency,
51 AbstractManager.AxesVisibility {
53 private final SimpleMap model;
54 private final CyclesControlForm2 form;
56 private CyclesRenderer plotRenderer;
58 private boolean crosshair;
59 private boolean bigDots;
60 private boolean gridLines;
62 private final int refreshSleepTime = 500;
64 public CyclesManager(final SimpleMap model,
65 final CyclesControlForm2 form,
66 final ManagerListener2 frame) {
68 super(frame);
70 this.model = (SimpleMap) model;
71 this.form = form;
73 crosshair = false;
74 gridLines = true;
76 plotRenderer = new CyclesRenderer(model, plot);
77 plot.setPlotRenderer(plotRenderer);
79 plot.setDrawGridLines(gridLines);
80 plot.setForegroundAlpha(0.1F);
81 chart.setTitle(model.getName());
83 setCrosshair(crosshair);
85 // stop everything on resizing
86 chartPanel.addComponentListener(new ComponentAdapter() {
87 public void componentResized(final ComponentEvent e) {
88 CyclesManager.this.stopRendering();
90 });
92 plot.addCoreStatusListener( new CoreStatusListener() {
93 public void sendCoreStatus(final CoreStatusEvent event) {
94 if (event.getType() == CoreStatusEvent.REDRAW) {
95 launchThread();
97 else if (event.getType() == CoreStatusEvent.REPAINT) {
98 chartPanel.repaint();
104 public boolean doRendering() {
106 VariableDoubles initial;
107 VariableDoubles parameters;
108 Range xRange;
109 Range yRange;
110 String xLabel;
111 String yLabel;
112 double epsilon;
113 int period;
114 int maxTries;
116 try {
117 initial = model.getVariables();
118 parameters = form.getParameterValues();
120 xRange = form.getXRange();
121 yRange = form.getYRange();
122 xLabel = form.getLabelOnX();
123 yLabel = form.getLabelOnY();
124 epsilon = form.getEpsilon();
125 period = form.getPeriod();
126 maxTries = form.getMaxTries();
128 catch (InvalidData e) {
129 errorMessage = e.getMessage();
130 return false;
133 plot.setNoData(false);
134 plot.zoom(0.0);
136 plotRenderer.initialize(
137 parameters, initial, xLabel, yLabel,
138 epsilon, period, maxTries);
140 plotRenderer.setBigDots(bigDots);
142 plot.setDataRanges(xRange, yRange);
144 xAxis.setLabel(xLabel);
145 yAxis.setLabel(yLabel);
147 launchThread();
148 return true;
151 private void launchThread() {
153 plot.getPlotRenderer().setState(DmcPlotRenderer.STATE_NONE);
155 Thread refreshJob = new RefreshThread();
156 refreshJob.start();
158 Thread plotJob = new PlotThread("DMCDUE - Cycles plotter", false);
159 plotJob.setPriority(Thread.currentThread().getPriority() - 1);
160 plotJob.start();
163 class RefreshThread extends Thread {
165 int state = CyclesRenderer.STATE_NONE;
166 int ratio;
168 RefreshThread() {
169 super("DMCDUE - CyclesRefresh");
172 public void run() {
174 while (true) {
175 int newState = plotRenderer.getState();
177 if (newState != state) {
179 state = newState;
181 switch (newState) {
182 case CyclesRenderer.STATE_NONE:
183 ratio = 0;
184 break;
186 case CyclesRenderer.STATE_RUNNING:
187 // frame.progressString("plotting...");
188 break;
190 case CyclesRenderer.STATE_FINISHED:
191 case CyclesRenderer.STATE_STOPPED:
192 // frame.progressString("ok. ");
193 return;
195 default:
196 ratio = 0;
200 if ((state & CyclesRenderer.STATE_RUNNING) != 0 ) {
201 chartPanel.repaint();
204 try {
205 sleep(refreshSleepTime);
207 catch (InterruptedException e2) {}
212 public void stopRendering() {
213 plotRenderer.stop();
216 public void setGridlines(final boolean flag) {
217 plot.setDrawGridLines(flag);
220 public void setCrosshair(final boolean flag) {
221 crosshair = flag;
222 chartPanel.setHorizontalAxisTrace(flag);
223 chartPanel.setVerticalAxisTrace(flag);
226 public boolean isCrosshair() {
227 return crosshair;
230 public void clear() {
231 plot.setNoData(true);
232 plot.zoom(0.0);
233 launchThread();
236 public boolean isBigDots() {
237 return bigDots;
240 public void setBigDots(boolean b) {
241 bigDots = b;
244 public boolean isGridlines() {
245 return gridLines;
248 public void setAlpha(final boolean flag) {
249 plot.setAlpha(flag);
252 public void setAlphaValue(final float f) {
253 plot.setForegroundAlpha(f);
256 public float getAlphaValue() {
257 return plot.getForegroundAlpha();