package org.helioviewer.plugins.eveplugin.radio.model;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.helioviewer.plugins.eveplugin.model.PlotAreaSpaceListener;

public class ZoomDataConfig implements ZoomManagerListener, PlotAreaSpaceListener {
    private Date minX;
    private Date maxX;
    private Rectangle displaySize;
    private long ID;
    private final YValueModel yValueModel;

    private final List<ZoomDataConfigListener> listeners;

    public ZoomDataConfig(Date minX, Date maxX, Rectangle displaySize, long ID) {
        listeners = new ArrayList<ZoomDataConfigListener>();

        this.maxX = maxX;
        this.minX = minX;
        yValueModel = YValueModel.getSingletonInstance();
        this.displaySize = displaySize;
        if (displaySize != null) {
            requestData();
        }
        this.ID = ID;
    }

    public void addListener(ZoomDataConfigListener l) {
        listeners.add(l);
        double xRatio = 1.0 * (maxX.getTime() - minX.getTime()) / displaySize.getWidth();
        double yRatio = 1.0 * (yValueModel.getSelectedYMax() - yValueModel.getSelectedYMin()) / displaySize.getHeight();
        l.requestData(minX, maxX, yValueModel.getSelectedYMin(), yValueModel.getSelectedYMax(), xRatio, yRatio, ID);
    }

    public long getID() {
        return ID;
    }

    public void setID(long iD) {
        ID = iD;
    }

    public void removeListener(ZoomDataConfigListener l) {
        listeners.remove(l);
    }

    public Date getMinX() {
        return minX;
    }

    public void setMinX(Date minX) {
        this.minX = minX;
    }

    public Date getMaxX() {
        return maxX;
    }

    public void setMaxX(Date maxX) {
        this.maxX = maxX;
    }

    public Rectangle getDisplaySize() {
        return displaySize;
    }

    public void update() {
        requestData();
    }

    public void setDisplaySize(Rectangle displaySize) {
        this.displaySize = displaySize;
        requestData();
    }

    @Override
    public void displaySizeChanged(Rectangle area) {
        displaySize = area;
        requestData();
    }

    private void requestData() {
        double xRatio = 1.0 * (maxX.getTime() - minX.getTime()) / displaySize.getWidth();
        double yRatio = 1.0 * (yValueModel.getSelectedYMax() - yValueModel.getSelectedYMin()) / displaySize.getHeight();
        for (ZoomDataConfigListener l : listeners) {
            l.requestData(minX, maxX, yValueModel.getSelectedYMin(), yValueModel.getSelectedYMax(), xRatio, yRatio, ID);
        }
    }

    @Override
    public void XValuesChanged(Date minX, Date maxX) {
        this.minX = minX;
        this.maxX = maxX;
        requestData();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("MinX = ").append(minX).append("\n").append("MaxX = ").append(maxX).append("\n").append("DisplaySize = ").append(displaySize).append("\n");
        return sb.toString();
    }

    @Override
    public void plotAreaSpaceChanged(double scaledMinValue, double scaledMaxValue, double scaledMinTime, double scaledMaxTime, double scaledSelectedMinValue, double scaledSelectedMaxValue, double scaledSelectedMinTime, double scaledSelectedMaxTime, boolean forced) {
        synchronized (this) {
            requestData();
        }
    }

    @Override
    public void availablePlotAreaSpaceChanged(double oldMinValue, double oldMaxValue, double oldMinTime, double oldMaxTime, double newMinValue, double newMaxValue, double newMinTime, double newMaxTime) {
        // TODO Auto-generated method stub

    }
}

@@ -49,16 +49,44 @@
 model;%0A%0A
+import java.awt.EventQueue;%0A
 import j
@@ -179,16 +179,57 @@
 .List;%0A%0A
+import org.helioviewer.base.logging.Log;%0A
 import o
@@ -3398,66 +3398,253 @@
     
-synchronized (this) %7B%0A            requestData();%0A        %7D
+if (!EventQueue.isDispatchThread()) %7B%0A            Log.error(%22Function called by other thread than eventqueue : %22 + Thread.currentThread().getName());%0A            Thread.dumpStack();%0A            System.exit(400);%0A        %7D%0A        requestData();%0A
 %0A   

Remove synchronised from ZoomDataConfig