initial import
[iDMC.git] / src / java / org / tsho / dmc2 / managers / AbsorbingAreaManager.java
blobb2060c9a0573cf1613cd1abd332936697ec3f92f
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.AbsorbingAreaRenderer;
38 import org.tsho.dmc2.core.chart.DmcPlotRenderer;
39 import org.tsho.dmc2.core.model.DifferentiableMap;
40 import org.tsho.dmc2.ui.InvalidData;
41 import org.tsho.dmc2.ui.absorbingArea.AbsorbingAreaComponent;
42 import org.tsho.dmc2.ui.absorbingArea.AbsorbingAreaControlForm2;
44 /**
46 * @author tsho
48 public class AbsorbingAreaManager extends AbstractManager
49 implements AbstractManager.Crosshair,
50 AbstractManager.AxesVisibility{
52 private final DifferentiableMap model;
53 private final AbsorbingAreaControlForm2 form;
55 private AbsorbingAreaRenderer plotRenderer;
57 private boolean crosshair;
59 private final int refreshSleepTime = 500;
61 public AbsorbingAreaManager(final DifferentiableMap model,
62 final AbsorbingAreaControlForm2 form,
63 final ManagerListener2 frame) {
65 super(frame);
67 this.model = model;
68 this.form = form;
70 crosshair = false;
72 chartPanel.setMouseZoomable(true, false);
73 chartPanel.setPopupMenu(null);
74 setCrosshair(crosshair);
76 plotRenderer = new AbsorbingAreaRenderer(model, plot, (AbsorbingAreaComponent) frame);
77 plot.setPlotRenderer(plotRenderer);
79 plot.setDrawGridLines(false);
80 chart.setTitle(model.getName());
82 plot.addCoreStatusListener( new CoreStatusListener() {
83 public void sendCoreStatus(final CoreStatusEvent event) {
84 if (event.getType() == CoreStatusEvent.REDRAW) {
85 launchThread();
87 else if (event.getType() == CoreStatusEvent.REPAINT) {
88 chartPanel.repaint();
91 });
93 // stop everything on resizing
94 chartPanel.addComponentListener(new ComponentAdapter() {
95 public void componentResized(final ComponentEvent e) {
96 AbsorbingAreaManager.this.stopRendering();
98 });
101 public boolean doRendering() {
102 try {
103 plot.setNoData(false);
105 String[] vars = model.getVariables().labelsArray();
107 xAxis.setLabel(vars[0]);
108 if (model.getNVar() > 1) {
109 yAxis.setLabel(vars[1]);
111 else {
112 yAxis.setLabel(vars[0]);
115 plotRenderer.initialize(
116 VariableDoubles.toArray(form.getParameterValues()),
117 form.getEpsilon(),
118 form.getIterations(),
119 form.getTransients());
121 plot.setDataRanges(form.getXRange(), form.getYRange());
123 catch (InvalidData e) {
124 errorMessage = e.getMessage();
125 return false;
128 if (plot.getPlotRenderer() != plotRenderer) {
129 plot.setPlotRenderer(plotRenderer);
132 launchThread();
133 return true;
138 private void launchThread() {
140 plot.getPlotRenderer().setState(DmcPlotRenderer.STATE_NONE);
142 Thread refreshJob = new RefreshThread();
143 refreshJob.start();
145 Thread plotJob = new PlotThread("DMCDUE - AbsorbingArea plotter", false);
146 plotJob.setPriority(Thread.currentThread().getPriority() - 1);
147 plotJob.start();
151 class RefreshThread extends Thread {
153 int state = AbsorbingAreaRenderer.STATE_NONE;
154 int ratio;
156 RefreshThread() {
157 super("DMCDUE - AbsorbingArea refresh");
160 public void run() {
162 while (true) {
163 int newState = plotRenderer.getState();
165 if (newState != state) {
167 state = newState;
169 switch (newState) {
170 case AbsorbingAreaRenderer.STATE_NONE:
171 ratio = 0;
172 break;
174 case AbsorbingAreaRenderer.STATE_RUNNING:
175 // frame.progressString("plotting...");
176 break;
178 case AbsorbingAreaRenderer.STATE_FINISHED:
179 // frame.progressString("ok. ");
180 return;
182 default:
183 ratio = 0;
187 if ((state & AbsorbingAreaRenderer.STATE_RUNNING) != 0 ) {
188 chartPanel.repaint();
191 try {
192 sleep(refreshSleepTime);
194 catch (InterruptedException e2) {}
199 public void stopRendering() {
200 //here should come whatever is associated with stop in this case.
201 //note that this might depend on the current state
203 plotRenderer.stop();
206 public void setGridlines(final boolean flag) {
207 plot.setDrawGridLines(flag);
210 public void setCrosshair(final boolean flag) {
211 crosshair = flag;
212 chartPanel.setHorizontalAxisTrace(flag);
213 chartPanel.setVerticalAxisTrace(flag);
216 public boolean isCrosshair() {
217 return crosshair;
220 public void clear() {
221 plot.setNoData(true);
222 plot.zoom(0.0);
223 launchThread();
226 public AbsorbingAreaRenderer getPlotRenderer(){
227 return plotRenderer;
230 public void handleMouseClicked(int x, int y){
231 plotRenderer.setMouseClicked(true);
232 plotRenderer.setXClicked(x);
233 plotRenderer.setYClicked(y);