package org.helioviewer.jhv.plugins.eveplugin.events.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.util.ArrayList;
import java.util.Map;
import java.util.SortedMap;

import javax.swing.ImageIcon;

import org.helioviewer.jhv.base.interval.Interval;
import org.helioviewer.jhv.data.container.JHVEventContainer;
import org.helioviewer.jhv.data.container.cache.JHVEventCache.SortedDateInterval;
import org.helioviewer.jhv.data.datatype.event.JHVEventType;
import org.helioviewer.jhv.data.datatype.event.JHVRelatedEvents;
import org.helioviewer.jhv.plugins.eveplugin.DrawConstants;
import org.helioviewer.jhv.plugins.eveplugin.draw.DrawController;
import org.helioviewer.jhv.plugins.eveplugin.draw.DrawableElement;
import org.helioviewer.jhv.plugins.eveplugin.draw.DrawableElementType;
import org.helioviewer.jhv.plugins.eveplugin.draw.YAxisElement;
import org.helioviewer.jhv.plugins.eveplugin.events.model.EventModel;
import org.helioviewer.jhv.plugins.eveplugin.events.model.EventPlotConfiguration;

public class EventPanel implements DrawableElement {

    private static final float dash1[] = { 10f };

    @Override
    public DrawableElementType getDrawableElementType() {
        return DrawableElementType.EVENT;
    }

    @Override
    public void draw(Graphics2D g, Graphics2D leftAxis, Rectangle graphArea, Rectangle leftAxisArea, Point mousePosition) {
        if (!EventModel.getSingletonInstance().isEventsVisible())
            return;

        Map<JHVEventType, SortedMap<SortedDateInterval, JHVRelatedEvents>> events = EventModel.getSingletonInstance().getEvents();
        if (events.size() > 0) {
            final Interval selectedInterval = DrawController.getSingletonInstance().getSelectedInterval();
            long selectedIntervalStart = selectedInterval.start;
            long selectedIntervalEnd = selectedInterval.end;

            int nrEventTypes = events.size();
            int eventTypeNr = 0;
            int previousLine = 0;

            BasicStroke dashed = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10f, dash1, 0f);
            Stroke normalStroke = g.getStroke();
            JHVRelatedEvents highlightedEvent = null;
            int spacePerLine = 6;

            ArrayList<Long> endDates = new ArrayList<Long>();

            for (Map.Entry<JHVEventType, SortedMap<SortedDateInterval, JHVRelatedEvents>> entry : events.entrySet()) {
                JHVEventType eventType = entry.getKey();
                SortedMap<SortedDateInterval, JHVRelatedEvents> eventMap = entry.getValue();

                endDates.clear();

                int nrLines = 0;
                EventPlotConfiguration shouldRedraw = null;

                for (JHVRelatedEvents event : eventMap.values()) {
                    int i = 0;
                    while (i < nrLines && endDates.get(i) >= event.getStart()) {
                        i++;
                    }
                    if (i == nrLines) {
                        endDates.add(event.getEnd());
                    } else {
                        endDates.set(i, event.getEnd());
                    }
                    int eventPosition = i;
                    nrLines = endDates.size();

                    int x0 = (int) (graphArea.width * defineScaledValue(event.getStart(), selectedIntervalStart, selectedIntervalEnd));
                    int x1 = (int) (graphArea.width * defineScaledValue(event.getEnd(), selectedIntervalStart, selectedIntervalEnd));
                    JHVRelatedEvents rEvent = EventPlotConfiguration.draw(event, x0, x1, eventPosition, g, previousLine, mousePosition, event.isHighlighted());
                    if (rEvent != null) {
                        shouldRedraw = new EventPlotConfiguration(rEvent, x0, x1, eventPosition);
                        highlightedEvent = rEvent;
                    }
                }

                if (shouldRedraw != null) {
                    shouldRedraw.draw(g, previousLine, mousePosition);
                }

                int spaceNeeded = spacePerLine * nrLines;
                ImageIcon icon = eventType.getEventType().getEventIcon();
                leftAxis.drawImage(icon.getImage(), 0, leftAxisArea.y + previousLine * spacePerLine + spaceNeeded / 2 - icon.getIconHeight() / 2 / 2, icon.getIconWidth() / 2, leftAxisArea.y + previousLine * spacePerLine + spaceNeeded / 2 + icon.getIconHeight() / 2 / 2, 0, 0, icon.getIconWidth(), icon.getIconHeight(), null);

                previousLine += nrLines;
                if (eventTypeNr != nrEventTypes - 1) {
                    g.setStroke(dashed);
                    g.setColor(Color.black);
                    int sepLinePos = previousLine * spacePerLine - spacePerLine / 4 + DrawConstants.EVENT_OFFSET;
                    g.drawLine(0, sepLinePos, graphArea.width, sepLinePos);
                    g.setStroke(normalStroke);
                }

                eventTypeNr++;
            }
            EventModel.getSingletonInstance().setEventUnderMouse(highlightedEvent);
            if (mousePosition != null) {
                JHVEventContainer.highlight(highlightedEvent);
            }
        }
    }

    private double defineScaledValue(long date, long start, long end) {
        return (1.0 * (date - start)) / (end - start);
    }

    @Override
    public void setYAxisElement(YAxisElement yAxisElement) {
    }

    @Override
    public YAxisElement getYAxisElement() {
        return null;
    }

    @Override
    public boolean hasElementsToDraw() {
        return EventModel.getSingletonInstance().hasElementsToDraw();
    }

    @Override
    public long getLastDateWithData() {
        return EventModel.getSingletonInstance().getLastDateWithData();
    }

}

@@ -320,59 +320,8 @@
 n;%0A%0A
-import org.helioviewer.jhv.base.interval.Interval;%0A
 impo
@@ -373,24 +373,24 @@
 tContainer;%0A
+
 import org.h
@@ -1169,16 +1169,105 @@
 %7B 10f %7D;
+%0A    private final DrawController drawController = DrawController.getSingletonInstance();
 %0A%0A    @O
@@ -1584,16 +1584,18 @@
 sible())
+ %7B
 %0A       
@@ -1606,16 +1606,26 @@
  return;
+%0A        %7D
 %0A%0A      
@@ -1785,241 +1785,8 @@
 0) %7B
-%0A            final Interval selectedInterval = DrawController.getSingletonInstance().getSelectedInterval();%0A            long selectedIntervalStart = selectedInterval.start;%0A            long selectedIntervalEnd = selectedInterval.end;
 %0A%0A  
@@ -3169,245 +3169,137 @@
 0 = 
-(int) (graphArea.width * defineScaledValue(event.getStart(), selectedIntervalStart, selectedIntervalEnd));%0A                    int x1 = (int) (graphArea.width * defineScaledValue(event.getEnd(), selectedIntervalStart, selectedIntervalEnd
+drawController.calculateXLocation(event.getStart());%0A                    int x1 = drawController.calculateXLocation(event.getEnd(
 ));%0A
@@ -4960,32 +4960,32 @@
 ;%0A            %7D%0A
+
         %7D%0A    %7D%0A
@@ -4989,142 +4989,8 @@
  %7D%0A%0A
-    private double defineScaledValue(long date, long start, long end) %7B%0A        return (1.0 * (date - start)) / (end - start);%0A    %7D%0A%0A
     

Use draw controller in event panel