/**
 * 
 * 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.Insets;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.Action;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.text.DefaultEditorKit;

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

public class TextView extends AbstractView {
	private static Map<String,String> ACTION_MAPPING=new LinkedHashMap<String,String>();
	static {
		ACTION_MAPPING.put("Select all",DefaultEditorKit.selectAllAction);
		ACTION_MAPPING.put("Cut",DefaultEditorKit.cutAction);
		ACTION_MAPPING.put("Copy",DefaultEditorKit.copyAction);
		ACTION_MAPPING.put("Paste",DefaultEditorKit.pasteAction);
	};
	
	private Component m_component;
	private JEditorPane m_textPane;
	
	public TextView() {
	}
	
	public TextView(String title, ViewData data) {
		initialize(title, data);
	}
	
	@Override
	protected void dataChanged() {
		if (m_textPane!=null) {
	    	ViewData data=getData();
	        if (data instanceof URLViewData) {
	        	try {
	        		m_textPane.setPage(((URLViewData)data).getURL());
	            } catch (IOException e) {
	                System.err.println("TextView: Error reading from '" + data.toString() + "'");
	            }
	        }
	        else {
	        	m_textPane.setText("");
	        	System.err.println("TextView: This view can only display URLViewData!");
	        }
		}
	}
	
	public Component createComponent() {
    	m_textPane = new JEditorPane();
    	m_textPane.setCaretPosition(0);
    	m_textPane.setMargin(new Insets(5,5,5,5));
        
    	dataChanged();
        
        m_component=new JScrollPane(m_textPane);

        return m_component;
	}

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

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

	@Override
	protected Component getFocusComponent() {
		return m_textPane;
	}
}