package org.helioviewer.plugins.eveplugin;

import java.net.URL;
import java.util.LinkedList;

import javax.swing.JComponent;

import org.helioviewer.jhv.JavaHelioViewerLauncher;
import org.helioviewer.jhv.gui.ImageViewerGui;
import org.helioviewer.jhv.gui.components.CollapsiblePane;
import org.helioviewer.jhv.gui.dialogs.observation.ObservationDialog;
import org.helioviewer.jhv.gui.interfaces.MainContentPanelPlugin;
import org.helioviewer.plugins.eveplugin.controller.ZoomController;
import org.helioviewer.plugins.eveplugin.events.data.EventRequester;
import org.helioviewer.plugins.eveplugin.events.model.EventModel;
import org.helioviewer.plugins.eveplugin.lines.data.DatabaseController;
import org.helioviewer.plugins.eveplugin.radio.model.RadioPlotModel;
import org.helioviewer.plugins.eveplugin.settings.EVESettings;
import org.helioviewer.plugins.eveplugin.view.ControlsPanel;
import org.helioviewer.plugins.eveplugin.view.MainPanel;
import org.helioviewer.plugins.eveplugin.view.ObservationDialogUIPanel;
import org.helioviewer.plugins.eveplugin.view.SimpleObservationDialogUIPanel;
import org.helioviewer.viewmodelplugin.interfaces.Plugin;

/**
 * 
 * 
 * @author Stephan Pagel
 * */
public class EVEPlugin implements Plugin, MainContentPanelPlugin {

    private final LinkedList<JComponent> pluginPanes = new LinkedList<JComponent>();
    private MainPanel mainPanel;

    @Override
    public void installPlugin() {
        EventRequester eventRequester = EventRequester.getSingletonInstance();
        ZoomController.getSingletonInstance().addZoomControllerListener(eventRequester);
        eventRequester.addListener(EventModel.getSingletonInstance());
        ZoomController.getSingletonInstance().addZoomControllerListener(EventModel.getSingletonInstance());
        if (mainPanel == null) {
            mainPanel = new MainPanel();
        }

        pluginPanes.add(mainPanel);

        ImageViewerGui.getSingletonInstance().getLeftContentPane()
                .add(new CollapsiblePane("Timeline Layers", ControlsPanel.getSingletonInstance(), true));

        ImageViewerGui.getSingletonInstance().getMainContentPanel().addPlugin(this);
        ObservationDialog.getSingletonInstance().addUserInterface(EVESettings.OBSERVATION_UI_NAME,
                new ObservationDialogUIPanel(mainPanel.getPlotContainerPanel()));
        ObservationDialog.getSingletonInstance().addUserInterface(EVESettings.RADIO_OBSERVATION_UI_NAME,
                new SimpleObservationDialogUIPanel(mainPanel.getPlotContainerPanel()));
        // initialize database connection
        DatabaseController.getSingletonInstance();
        RadioPlotModel.getSingletonInstance();

    }

    @Override
    public void uninstallPlugin() {
        ObservationDialog.getSingletonInstance().removeUserInterface(EVESettings.OBSERVATION_UI_NAME,
                new ObservationDialogUIPanel(mainPanel.getPlotContainerPanel()));
        ObservationDialog.getSingletonInstance().removeUserInterface(EVESettings.RADIO_OBSERVATION_UI_NAME,
                new SimpleObservationDialogUIPanel(mainPanel.getPlotContainerPanel()));
        ImageViewerGui.getSingletonInstance().getMainContentPanel().removePlugin(this);
    }

    public static URL getResourceUrl(String name) {
        return EVEPlugin.class.getResource(name);
    }

    @Override
    public String getName() {
        return "EVEPlugin";
    }

    @Override
    public String getDescription() {
        return "This plugin visualizes the level 2 data of the EVE project";
    }

    @Override
    public String getAboutLicenseText() {
        String description = "";

        description += "<p>The plugin uses the <a href=\"http://www.json.org/java/\">JSON in Java</a> Library, licensed under a <a href=\"http://www.json.org/license.html\">custom License</a>.";
        description += "<p>The plugin uses <a href=\"http://db.apache.org/derby/\">Apache Derby, an Apache DB subproject</a>,<br>"
                + '\u00A9'
                + " 2011, Apache Software Foundation, <a href=\"http://www.apache.org/licenses/\">Apache License, Version 2.0</a><br>";

        return description;
    }

    /**
     * Used for testing the plugin
     * 
     * @see org.helioviewer.plugins.eveplugin.EVEPluginLauncher#main(String[])
     * @param args
     */
    public static void main(String[] args) {
        JavaHelioViewerLauncher.start(EVEPluginLauncher.class, args);
    }

    @Override
    public LinkedList<JComponent> getVisualInterfaces() {
        return pluginPanes;
    }

    @Override
    public String getTabName() {
        return "1-D and 2-D Time Series";
    }

    @Override
    public void setState(String state) {
        // TODO SP: Implement setState for EVEPlugin
    }

    @Override
    public String getState() {
        // TODO SP: Implement getState for EVEPlugin
        return "";
    }
}

@@ -223,67 +223,8 @@
 ui;%0A
-import org.helioviewer.jhv.gui.components.CollapsiblePane;%0A
 impo
@@ -1095,21 +1095,19 @@
 %0A%0A/**%0A *
- 
 %0A *
- 
 %0A * @aut
@@ -1904,49 +1904,12 @@
 ne()
-%0A                .add(new CollapsiblePane
+.add
 (%22Ti
@@ -1967,17 +1967,16 @@
 ), true)
-)
 ;%0A%0A     
@@ -2153,32 +2153,16 @@
 UI_NAME,
-%0A               
  new Obs
@@ -2324,32 +2324,16 @@
 UI_NAME,
-%0A               
  new Sim
@@ -2696,32 +2696,16 @@
 UI_NAME,
-%0A               
  new Obs
@@ -2870,32 +2870,16 @@
 UI_NAME,
-%0A               
  new Sim
@@ -3780,51 +3780,19 @@
 br%3E%22
-%0A                + '%5Cu00A9'%0A               
+ + '%5Cu00A9'
  + %22
@@ -3993,17 +3993,16 @@
 n%0A     *
- 
 %0A     * 

fix gui