upgraded idmclib version to 0.2.0
[iDMC.git] / src / java / org / tsho / dmc2 / managers / CyclesManager.java
blob84984fa7411e0da687e69a7e495cc6f20cdc467d
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 VariableDoubles rangeMin, rangeMax;
109 Range xRange;
110 Range yRange;
111 String xLabel;
112 String yLabel;
113 double epsilon;
114 int period;
115 int maxTries;
117 try {
118 initial = model.getVariables();
119 parameters = form.getParameterValues();
120 rangeMin = form.getRangeMin();
121 rangeMax = form.getRangeMax();
123 xRange = form.getXRange();
124 yRange = form.getYRange();
125 xLabel = form.getLabelOnX();
126 yLabel = form.getLabelOnY();
127 epsilon = form.getEpsilon();
128 period = form.getPeriod();
129 maxTries = form.getMaxTries();
131 catch (InvalidData e) {
132 errorMessage = e.getMessage();
133 return false;
136 plot.setNoData(false);
137 plot.zoom(0.0);
139 plotRenderer.initialize(
140 parameters, initial,
141 rangeMin, rangeMax,
142 xLabel, yLabel,
143 epsilon, period, maxTries);
145 plotRenderer.setBigDots(bigDots);
147 plot.setDataRanges(xRange, yRange);
149 xAxis.setLabel(xLabel);
150 yAxis.setLabel(yLabel);
152 launchThread();
153 return true;
156 private void launchThread() {
158 plot.getPlotRenderer().setState(DmcPlotRenderer.STATE_NONE);
160 Thread refreshJob = new RefreshThread();
161 refreshJob.start();
163 Thread plotJob = new PlotThread("DMCDUE - Cycles plotter", false);
164 plotJob.setPriority(Thread.currentThread().getPriority() - 1);
165 plotJob.start();
168 class RefreshThread extends Thread {
170 int state = CyclesRenderer.STATE_NONE;
171 int ratio;
173 RefreshThread() {
174 super("DMCDUE - CyclesRefresh");
177 public void run() {
179 while (true) {
180 int newState = plotRenderer.getState();
182 if (newState != state) {
184 state = newState;
186 switch (newState) {
187 case CyclesRenderer.STATE_NONE:
188 ratio = 0;
189 break;
191 case CyclesRenderer.STATE_RUNNING:
192 // frame.progressString("plotting...");
193 break;
195 case CyclesRenderer.STATE_FINISHED:
196 case CyclesRenderer.STATE_STOPPED:
197 // frame.progressString("ok. ");
198 return;
200 default:
201 ratio = 0;
205 if ((state & CyclesRenderer.STATE_RUNNING) != 0 ) {
206 chartPanel.repaint();
209 try {
210 sleep(refreshSleepTime);
212 catch (InterruptedException e2) {}
217 public void stopRendering() {
218 plotRenderer.stop();
221 public void setGridlines(final boolean flag) {
222 plot.setDrawGridLines(flag);
225 public void setCrosshair(final boolean flag) {
226 crosshair = flag;
227 chartPanel.setHorizontalAxisTrace(flag);
228 chartPanel.setVerticalAxisTrace(flag);
231 public boolean isCrosshair() {
232 return crosshair;
235 public void clear() {
236 plot.setNoData(true);
237 plot.zoom(0.0);
238 launchThread();
241 public boolean isBigDots() {
242 return bigDots;
245 public void setBigDots(boolean b) {
246 bigDots = b;
249 public boolean isGridlines() {
250 return gridLines;
253 public void setAlpha(final boolean flag) {
254 plot.setAlpha(flag);
257 public void setAlphaValue(final float f) {
258 plot.setForegroundAlpha(f);
261 public float getAlphaValue() {
262 return plot.getForegroundAlpha();