upgraded idmclib version to 0.2.0
[iDMC.git] / src / java / org / tsho / dmc2 / managers / ManifoldsManager.java
blob77f651cc53ab6f17b888a060ce5d3a0a0d838a71
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.chart.JFreeChart;
33 import org.jfree.chart.axis.NumberAxis;
34 import org.jfree.data.Range;
35 import org.tsho.dmc2.core.CoreStatusEvent;
36 import org.tsho.dmc2.core.CoreStatusListener;
37 import org.tsho.dmc2.core.VariableDoubles;
38 import org.tsho.dmc2.core.chart.DmcPlotRenderer;
39 import org.tsho.dmc2.core.chart.DmcRenderablePlot;
40 import org.tsho.dmc2.core.chart.ManifoldsRenderer;
41 import org.tsho.dmc2.core.chart.jfree.DmcChartPanel;
42 import org.tsho.dmc2.core.model.DifferentiableMap;
43 import org.tsho.dmc2.ui.InvalidData;
44 import org.tsho.dmc2.ui.manifolds.ManifoldsControlForm2;
46 public class ManifoldsManager extends AbstractManager
47 implements AbstractManager.GridLines,
48 AbstractManager.Crosshair,
49 AbstractManager.AxesVisibility {
51 private final DifferentiableMap model;
52 private final ManifoldsControlForm2 form;
54 private ManifoldsRenderer plotRenderer;
56 private boolean gridlines;
57 private boolean crosshair;
58 private byte plotType;
60 public ManifoldsManager(
61 final DifferentiableMap model,
62 final ManifoldsControlForm2 form,
63 final ManagerListener2 frame) {
65 super(frame);
67 this.model = model;
68 this.form = form;
70 /* defaults */
71 setGridlines(true);
72 crosshair = false;
74 plotRenderer = new ManifoldsRenderer(model, plot);
75 plot.setPlotRenderer(plotRenderer);
77 plot.setForegroundAlpha(0.2F);
78 plot.setAntialias(false);
80 setCrosshair(crosshair);
81 chart.setTitle(model.getName());
83 plot.addCoreStatusListener(new CoreStatusListener() {
84 public void sendCoreStatus(final CoreStatusEvent event) {
85 if (event.getType() == CoreStatusEvent.REDRAW) {
86 launchThread();
88 else if (event.getType() == CoreStatusEvent.REPAINT) {
89 chartPanel.repaint();
92 });
94 // stop everything on resizing
95 chartPanel.addComponentListener(new ComponentAdapter() {
96 public void componentResized(final ComponentEvent e) {
97 ManifoldsManager.this.stopRendering();
99 });
102 public ManifoldsManager(
103 final DifferentiableMap model,
104 final ManifoldsControlForm2 form,
105 final ManagerListener2 frame,
106 final DmcChartPanel otherPanel) {
108 super(frame);
110 this.model = model;
111 this.form = form;
113 /* defaults */
114 setGridlines(true);
115 crosshair = false;
117 xAxis = (NumberAxis) ((DmcRenderablePlot) otherPanel.getChart().getPlot()).getDomainAxis();
118 yAxis = (NumberAxis) ((DmcRenderablePlot) otherPanel.getChart().getPlot()).getRangeAxis();
120 plot = new DmcRenderablePlot(xAxis, yAxis);
121 plotRenderer = new ManifoldsRenderer(model, plot);
122 plot.setPlotRenderer(plotRenderer);
124 plot.addCoreStatusListener(new CoreStatusListener() {
125 public void sendCoreStatus(final CoreStatusEvent event) {
126 if (event.getType() == CoreStatusEvent.REDRAW) {
127 launchThread();
129 else if (event.getType() == CoreStatusEvent.REPAINT) {
130 chartPanel.repaint();
135 chart = new JFreeChart(model.getName(), plot);
137 chartPanel = new DmcChartPanel(chart);
138 chartPanel.setMouseZoomable(true, false);
140 // stop everything on resizing
141 chartPanel.addComponentListener(new ComponentAdapter() {
142 public void componentResized(final ComponentEvent e) {
143 ManifoldsManager.this.stopRendering();
147 setCrosshair(crosshair);
151 public boolean doRendering(final boolean redraw) {
153 return render(redraw);
157 public boolean render(boolean redraw) {
158 VariableDoubles parameters;
159 VariableDoubles initial;
160 double epsilon;
162 Range horRange, verRange;
163 Range lockoutHRange, lockoutVRange;
164 int firstIteration, lastIteration;
166 try {
167 initial = form.getNodeValues();
168 parameters = form.getParameterValues();
169 epsilon = form.getEpsilon();
170 horRange = form.getHorizontalRange();
171 verRange = form.getVerticalRange();
172 lockoutHRange = form.getLockoutHRange();
173 lockoutVRange = form.getLockoutVRange();
174 firstIteration = form.getIterationsFirst();
175 lastIteration = form.getIterationsLast();
177 catch (InvalidData e) {
178 frame.showInvalidDataDialog(e.getMessage());
179 return false;
182 plot.setNoData(false);
183 plot.setDrawGridLines(gridlines);
185 if (plot.getPlotRenderer() != plotRenderer) {
186 plot.setPlotRenderer(plotRenderer);
189 plotRenderer.initialize(
190 plotType,
191 initial, parameters, epsilon, lockoutHRange,
192 lockoutVRange, firstIteration, lastIteration);
194 //xAxis.setLabel(parLabel);
195 //yAxis.setLabel(varLabel);
197 if (!redraw) {
198 plot.setDataRanges(horRange, verRange);
201 launchThread();
202 return true;
205 class RefreshThread extends Thread {
206 int state = ManifoldsRenderer.STATE_NONE;
208 RefreshThread() {
209 super("DMCDUE - ManifoldsRefresh");
212 public void run() {
214 while (true) {
215 int newState = plotRenderer.getState();
217 if (newState != state) {
219 state = newState;
221 switch (newState) {
222 case ManifoldsRenderer.STATE_NONE:
223 // ratio = 0;
224 break;
226 case ManifoldsRenderer.STATE_RUNNING:
227 // ratio = plot.getIterations();
228 // frame.progressString("plotting...");
229 break;
231 case ManifoldsRenderer.STATE_FINISHED:
232 // frame.progressString("ok. ");
233 // report();
234 return;
236 case ManifoldsRenderer.STATE_STOPPED:
237 // frame.progressString("stopped, ok.");
238 // report();
239 return;
241 default:
242 // ratio = 0;
246 if ((state & ManifoldsRenderer.STATE_RUNNING) != 0 ) {
247 // report();
248 chartPanel.repaint();
251 try {
252 sleep(100);
254 catch (InterruptedException e2) {}
258 // private void report() {
259 // frame.progressCount(plot.getIndex());
260 // if (ratio != 0) {
261 // frame.progressPercent((plot.getIndex() * 100 / ratio));
262 // }
263 // }
266 private void launchThread() {
268 plot.getPlotRenderer().setState(DmcPlotRenderer.STATE_NONE);
270 Thread refreshJob = new RefreshThread();
271 refreshJob.start();
273 Thread plotJob = new PlotThread("DMCDUE - Manifoldes plotter", false);
274 plotJob.setPriority(Thread.currentThread().getPriority() - 1);
275 plotJob.start();
278 public void stopRendering() {
279 plotRenderer.stop();
282 public void setAlpha(boolean flag) {
283 plot.setAlpha(flag);
286 public void setAlphaValue(float f) {
287 plot.setForegroundAlpha(f);
290 public float getAlphaValue() {
291 return plot.getForegroundAlpha();
294 public void setCrosshair(boolean flag) {
295 crosshair = flag;
296 chartPanel.setHorizontalAxisTrace(flag);
297 chartPanel.setVerticalAxisTrace(flag);
300 public boolean isCrosshair() {
301 return crosshair;
304 public boolean isGridlines() {
305 return gridlines;
308 public void setGridlines(boolean b) {
309 gridlines = b;
312 public void clear() {
313 plot.setNoData(true);
314 plot.zoom(0.0);
315 launchThread();
318 public byte getPlotType() {
319 return plotType;
322 public void setPlotType(byte b) {
323 switch(b) {
324 case (ManifoldsRenderer.TYPE_UNSTABLE | ManifoldsRenderer.TYPE_STABLE):
325 plotType |= b;
326 break;
328 case ManifoldsRenderer.TYPE_UNSTABLE:
329 plotType &= ~ManifoldsRenderer.TYPE_STABLE;
330 plotType |= ManifoldsRenderer.TYPE_UNSTABLE;
331 break;
333 case ManifoldsRenderer.TYPE_STABLE:
334 plotType &= ~ManifoldsRenderer.TYPE_UNSTABLE;
335 plotType |= ManifoldsRenderer.TYPE_STABLE;
336 break;
338 case (ManifoldsRenderer.TYPE_RIGHT | ManifoldsRenderer.TYPE_LEFT):
339 plotType |= b;
340 break;
342 case ManifoldsRenderer.TYPE_RIGHT:
343 plotType &= ~ManifoldsRenderer.TYPE_LEFT;
344 plotType |= ManifoldsRenderer.TYPE_RIGHT;
345 break;
347 case ManifoldsRenderer.TYPE_LEFT:
348 plotType &= ~ManifoldsRenderer.TYPE_RIGHT;
349 plotType |= ManifoldsRenderer.TYPE_LEFT;
350 break;