fixed traj. plot bug when manual bounds
[iDMC.git] / src / java / org / tsho / dmc2 / managers / BasinManager.java
blobca6b7efab8cd9307d7504f54e1ba20b00e955576
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 javax.swing.JComponent;
34 import org.tsho.dmc2.core.CoreStatusEvent;
35 import org.tsho.dmc2.core.CoreStatusListener;
36 import org.tsho.dmc2.core.VariableDoubles;
37 import org.tsho.dmc2.core.chart.BasinRenderer;
38 import org.tsho.dmc2.core.chart.DmcPlotRenderer;
39 import org.tsho.dmc2.core.model.SimpleMap;
40 import org.tsho.dmc2.ui.InvalidData;
41 import org.tsho.dmc2.ui.basin.BasinControlForm2;
42 import org.tsho.dmc2.core.util.*;
43 import org.tsho.dmc2.ui.basin.*;
45 /**
47 * @author tsho
49 public class BasinManager extends AbstractManager
50 implements AbstractManager.Crosshair,
51 AbstractManager.AxesVisibility,
52 AbstractManager.BigDots{
54 private final SimpleMap model;
55 private final BasinControlForm2 form;
57 private BasinComponent component;
58 private BasinRenderer plotRenderer;
60 private boolean crosshair;
62 private boolean bigDotsEnabled=false;
64 private final int refreshSleepTime = 500;
66 public BasinManager(BasinComponent component) {
68 super((ManagerListener2) component);
69 this.component = component;
70 this.model = (SimpleMap) component.getModel();
71 this.form = (BasinControlForm2) component.getControlForm();
73 crosshair = false;
75 chartPanel.setMouseZoomable(true, false);
76 chartPanel.setPopupMenu(null);
77 setCrosshair(crosshair);
79 plotRenderer = new BasinRenderer(model, plot, component);
80 plot.setPlotRenderer(plotRenderer);
82 plot.setDrawGridLines(false);
83 chart.setTitle(model.getName());
85 plot.addCoreStatusListener( new CoreStatusListener() {
86 public void sendCoreStatus(final CoreStatusEvent event) {
87 if (event.getType() == CoreStatusEvent.REDRAW) {
88 launchThread();
90 else if (event.getType() == CoreStatusEvent.REPAINT) {
91 chartPanel.repaint();
94 });
96 // stop everything on resizing
97 chartPanel.addComponentListener(new ComponentAdapter() {
98 public void componentResized(final ComponentEvent e) {
99 BasinManager.this.stopRendering();
104 public boolean doRendering() {
105 try {
106 plot.setNoData(false);
108 String[] vars = model.getVariables().labelsArray();
110 xAxis.setLabel(vars[0]);
111 if (model.getNVar() > 1) {
112 yAxis.setLabel(vars[1]);
114 else {
115 yAxis.setLabel(vars[0]);
118 plotRenderer.initialize(
119 VariableDoubles.toArray(form.getParameterValues()),
120 form.getLimit(),
121 form.getIterations(),
122 form.getTrials(),
123 form.getInfinity());
125 plot.setDataRanges(form.getXRange(), form.getYRange());
127 catch (InvalidData e) {
128 errorMessage = e.getMessage();
129 return false;
132 if (plot.getPlotRenderer() != plotRenderer) {
133 plot.setPlotRenderer(plotRenderer);
136 launchThread();
137 return true;
142 private void launchThread() {
144 plot.getPlotRenderer().setState(DmcPlotRenderer.STATE_NONE);
146 Thread refreshJob = new RefreshThread();
147 refreshJob.start();
149 Thread plotJob = new PlotThread("DMCDUE - Basin plotter", false);
150 plotJob.setPriority(Thread.currentThread().getPriority() - 1);
151 plotJob.start();
155 class RefreshThread extends Thread {
157 int state = BasinRenderer.STATE_NONE;
158 int ratio;
160 RefreshThread() {
161 super("DMCDUE - Basin refresh");
164 public void run() {
166 while (true) {
167 int newState = plotRenderer.getState();
169 if (newState != state) {
171 state = newState;
173 switch (newState) {
174 case BasinRenderer.STATE_NONE:
175 ratio = 0;
176 break;
178 case BasinRenderer.STATE_RUNNING:
179 // frame.progressString("plotting...");
180 break;
182 case BasinRenderer.STATE_FINISHED:
183 // frame.progressString("ok. ");
184 return;
186 default:
187 ratio = 0;
191 if ((state & BasinRenderer.STATE_RUNNING) != 0 ) {
192 chartPanel.repaint();
195 try {
196 sleep(refreshSleepTime);
198 catch (InterruptedException e2) {}
203 public void stopRendering() {
204 plotRenderer.stop();
207 public void setGridlines(final boolean flag) {
208 plot.setDrawGridLines(flag);
211 public void setCrosshair(final boolean flag) {
212 crosshair = flag;
213 chartPanel.setHorizontalAxisTrace(flag);
214 chartPanel.setVerticalAxisTrace(flag);
217 public boolean isCrosshair() {
218 return crosshair;
221 public void clear() {
222 plot.setNoData(true);
223 plot.zoom(0.0);
224 launchThread();
227 public BasinRenderer getPlotRenderer(){
228 return plotRenderer;
231 public boolean isBigDots() {
232 return bigDotsEnabled;
235 public void setBigDots(boolean flag) {
236 bigDotsEnabled=flag;
237 plotRenderer.setBigDotsEnabled(flag);