package org.helioviewer.jhv.gui.components.statusplugins;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;

import org.helioviewer.jhv.gui.IconBank;
import org.helioviewer.jhv.gui.IconBank.JHVIcon;
import org.helioviewer.jhv.layers.LayersModel;
import org.helioviewer.viewmodel.metadata.HelioviewerMetaData;
import org.helioviewer.viewmodel.metadata.MetaData;
import org.helioviewer.viewmodel.view.MetaDataView;
import org.helioviewer.viewmodel.view.View;

/**
 * Status panel for displaying whether the image contains helioviewer meta data
 * or not.
 * 
 * <p>
 * The information of this panel is always shown for the active layer.
 * 
 * <p>
 * If there is no layer present, this panel will be invisible.
 * 
 * @author Markus Langenberg
 */
public class MetaDataStatusPanel extends ViewStatusPanelPlugin {

    private static final long serialVersionUID = 1L;

    private static final Icon checkIcon = IconBank.getIcon(JHVIcon.CHECK);
    private static final Icon exIcon = IconBank.getIcon(JHVIcon.EX);

    /**
     * Default constructor.
     */
    public MetaDataStatusPanel() {
        setBorder(BorderFactory.createEtchedBorder());

        setText("Meta:");
        setIcon(exIcon);
        setVerticalTextPosition(JLabel.CENTER);
        setHorizontalTextPosition(JLabel.LEFT);
        validate();
        setPreferredSize(null);

        LayersModel.getSingletonInstance().addLayersListener(this);

    }

    /**
     * {@inheritDoc}
     */
    public void activeLayerChanged(int idx) {

        if (LayersModel.getSingletonInstance().isValidIndex(idx)) {
            View view = LayersModel.getSingletonInstance().getLayer(idx);
            setVisible(true);

            MetaDataView metaDataView = view.getAdapter(MetaDataView.class);

            // this operates on the raw viewchain...
            // it might be a good idea to push that functionality to LayersModel
            if (metaDataView != null) {
                MetaData m = metaDataView.getMetaData();

                if (m instanceof HelioviewerMetaData)
                    setIcon(checkIcon);
                else
                    setIcon(exIcon);
            }

            validate();
            setPreferredSize(null);

        } else {
            setVisible(false);
        }
    }

}

@@ -52,16 +52,45 @@
 ugins;%0A%0A
+import java.awt.EventQueue;%0A%0A
 import j
@@ -1352,32 +1352,34 @@
 .LEFT);%0A        
+//
 validate();%0A    
@@ -1374,32 +1374,34 @@
 date();%0A        
+//
 setPreferredSize
@@ -1527,21 +1527,22 @@
 */%0A    p
-ublic
+rivate
  void ac
@@ -1557,16 +1557,20 @@
 rChanged
+_raw
 (int idx
@@ -1573,17 +1573,16 @@
  idx) %7B%0A
-%0A
         
@@ -1715,16 +1715,17 @@
 r(idx);%0A
+%0A
         
@@ -1820,25 +1820,24 @@
 iew.class);%0A
-%0A
             
@@ -2055,17 +2055,16 @@
 Data();%0A
-%0A
         
@@ -2217,25 +2217,24 @@
           %7D%0A
-%0A
             
@@ -2233,16 +2233,18 @@
         
+//
 validate
@@ -2259,16 +2259,18 @@
         
+//
 setPrefe
@@ -2277,33 +2277,32 @@
 rredSize(null);%0A
-%0A
         %7D else %7B
@@ -2350,10 +2350,379 @@
 %0A    %7D%0A%0A
+    public void activeLayerChanged(final int idx) %7B%0A        if (EventQueue.isDispatchThread()) %7B%0A            activeLayerChanged_raw(idx);%0A        %7D else %7B%0A            EventQueue.invokeLater(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    activeLayerChanged_raw(idx);%0A                %7D%0A            %7D);%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Update metadata status from EventQueue