/**
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * Copyright © 2009 Mindoo GmbH
 *
 * @author Tammo Riedinger
 *
 */
package com.mindoo.ec09.legacy;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JScrollPane;

import com.mindoo.ec09.framework.views.ViewData;

public class PictureView extends AbstractView {
	private static Map<String,String> ACTION_MAPPING=new LinkedHashMap<String,String>();
	static {
		ACTION_MAPPING.put("Zoom in",ImageComponent.ACTION_ZOOM_IN);
		ACTION_MAPPING.put("Zoom out",ImageComponent.ACTION_ZOOM_OUT);
	};
	
	private Component m_component;
	ImageComponent m_imageComponent;

	public PictureView() {
	}
	
	public PictureView(String title, ViewData data) {
		initialize(title, data);
	}

	@Override
	protected Component getFocusComponent() {
		return m_imageComponent;
	}
	
	@Override
	protected void dataChanged() {
		if (m_imageComponent!=null) {
			Image img=null;
			ViewData data=getData();
			if (data!=null) {
				InputStream in=null;
				try {
					in=data.openStream();
					
					img = ImageIO.read(in);
				} catch (IOException e) {
					e.printStackTrace();
				}
				finally {
					try {
						if (in!=null)
							in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			
			m_imageComponent.setImage(img);
		}
	}
	
	public Component createComponent() {
		m_imageComponent=new ImageComponent();
		
		dataChanged();
		
        m_component=new JScrollPane(m_imageComponent);

        return m_component;
	}

	public Action getAction(String name) {
		if (m_imageComponent!=null) {
			String mappedName=ACTION_MAPPING.get(name);
			
			if (mappedName!=null)
				return ((JComponent)m_imageComponent).getActionMap().get(mappedName);
		}
		return null;
	}

	public Iterator<String> getActionNames() {
		return ACTION_MAPPING.keySet().iterator();
	}

	public static class ImageComponent extends ScaledComponent {
		private static final long serialVersionUID = 7516895545722219045L;
		private Image m_image;
		private boolean m_dirty;
		
		public ImageComponent() {
			super();
		}
		
		public void setImage(Image image) {
			if (m_image==image)
				return;
			
			m_dirty=true;
			m_image=image;
			
			updateUI();
			repaint();
		}
		
		public void paintContent(Graphics g) {
			if (m_image!=null)
				g.drawImage(m_image, 0, 0, this);
		}
		
		public Dimension getContentSize() {
			if (m_image!=null) {
				Dimension size=new Dimension(m_image.getWidth(null),m_image.getHeight(null));
				if (m_dirty) {
					Rectangle visRect=getVisibleRect();
					if (visRect!=null && visRect.width>50 && visRect.height>50) {
						if (size.width>visRect.width || size.height>visRect.height)
							setScale(Math.min(visRect.getHeight()/size.getHeight(),visRect.getWidth()/size.getWidth()));
						m_dirty=false;
					}
				}
				return size;
			}
			else
				return super.getContentSize();
		}
	}
}