package org.helioviewer.plugins.eveplugin.view.linedataselector;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public class LineDataSelectorModel implements TableModel {
    private final List<LineDataSelectorModelListener> listeners;
    private final List<LineDataSelectorElement> elements;
    public final static int NUMBEROFCOLUMNS = 4;
    private final List<TableModelListener> tableListeners;

    private static LineDataSelectorModel instance;

    private LineDataSelectorModel() {
        listeners = new ArrayList<LineDataSelectorModelListener>();
        elements = new ArrayList<LineDataSelectorElement>();
        tableListeners = new ArrayList<TableModelListener>();
    }

    public static LineDataSelectorModel getSingletonInstance() {
        if (instance == null) {
            instance = new LineDataSelectorModel();
        }
        return instance;
    }

    public void addLineDataSelectorModelListener(LineDataSelectorModelListener listener) {
        listeners.add(listener);
    }

    public void removeLineDataSelectorModelListener(LineDataSelectorModelListener listener) {
        listeners.remove(listener);
    }

    public void downloadStarted(LineDataSelectorElement element) {
        fireDownloadStarted(element);
        fireListeners();
    }

    public void downloadFinished(LineDataSelectorElement element) {
        fireDownloadFinished(element);
        fireListeners();
    }

    public int getNumberOfAvailableLineData() {
        return elements == null ? 0 : elements.size();
    }

    public void addLineData(LineDataSelectorElement element) {
        elements.add(element);
        fireLineDataSelectorElementAdded(element);
        fireListeners();
    }

    public List<LineDataSelectorElement> getAllLineDataSelectorElements() {
        return elements;
    }

    public void removeLineData(LineDataSelectorElement element) {
        if (elements != null) {
            elements.remove(element);
        }
        fireLineDataSelectorElementRemoved(element);
        fireListeners();
    }

    public void lineDataElementUpdated(LineDataSelectorElement element) {
        fireLineDataSelectorElementUpdated(element);
    }

    public boolean atLeastOneDownloading() {
        for (LineDataSelectorElement el : elements) {
            if (el.isDownloading()) {
                return true;
            }
        }
        return false;
    }

    public void addLineData(LineDataSelectorElement element, int rowIndex) {
        elements.add(rowIndex, element);
        fireListeners();
    }

    private void fireListeners() {
        for (TableModelListener listener : tableListeners) {
            TableModelEvent e = new TableModelEvent(this);
            listener.tableChanged(e);
        }
    }

    private void fireLineDataSelectorElementRemoved(LineDataSelectorElement element) {
        for (LineDataSelectorModelListener listener : listeners) {
            listener.lineDataRemoved(element);
        }

    }

    private void fireLineDataSelectorElementAdded(LineDataSelectorElement element) {
        for (LineDataSelectorModelListener listener : listeners) {
            listener.lineDataAdded(element);
        }
    }

    private void fireDownloadStarted(LineDataSelectorElement element) {
        for (LineDataSelectorModelListener listener : listeners) {
            listener.downloadStartded(element);
        }
    }

    private void fireDownloadFinished(LineDataSelectorElement element) {
        for (LineDataSelectorModelListener listener : listeners) {
            listener.downloadFinished(element);
        }
    }

    private void fireLineDataSelectorElementUpdated(LineDataSelectorElement element) {
        for (LineDataSelectorModelListener listener : listeners) {
            listener.lineDataUpdated(element);
        }
    }

    @Override
    public int getRowCount() {
        return elements.size();
    }

    @Override
    public int getColumnCount() {
        return NUMBEROFCOLUMNS;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return "" + columnIndex;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return LineDataSelectorElement.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return elements.get(rowIndex);
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        elements.add(rowIndex, (LineDataSelectorElement) aValue);
    }

    @Override
    public void addTableModelListener(TableModelListener l) {
        tableListeners.add(l);
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        tableListeners.remove(l);
    }

    public void removeRow(int row) {
        elements.remove(row);
    }
}

@@ -236,16 +236,81 @@
 Model;%0A%0A
+import org.helioviewer.plugins.eveplugin.lines.data.BandColors;%0A%0A
 public c
@@ -1811,32 +1811,89 @@
 s.add(element);%0A
+        element.setDataColor(BandColors.getNextColor());%0A
         fireLine
@@ -2195,32 +2195,91 @@
 emove(element);%0A
+            BandColors.resetColor(element.getDataColor());%0A
         %7D%0A      

Reactivate the colors