package com.mindoo.ec09.legacy;

import java.awt.Component;
import java.net.URL;

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

public abstract class AbstractView implements View {
	private ViewData m_data;
	private String m_title;

	public AbstractView() {
		super();
	}
	
	public AbstractView(String title, ViewData data) {
		super();
		
		initialize(title, data);
	}
	
	public void initialize(String title, ViewData data) {
		setTitle(title);
		setData(data);
	}
	
	protected abstract Component getFocusComponent();
	
	public void setTitle(String title) {
		m_title=title;
	}

	public void setFocus() {
		Component c=getFocusComponent();
		if (c!=null && c.isVisible())
			c.requestFocus();
	}

	public String getTitle() {
		return m_title;
	}
	
	public ViewData getData() {
		return m_data;
	}

	public void setData(ViewData data) {
		boolean reload=true;
		
		if (m_data instanceof URLViewData && data instanceof URLViewData) {
			URL formerURL=((URLViewData)m_data).getURL();
			URL newURL=((URLViewData)data).getURL();

			if (formerURL!=null && newURL!=null && formerURL.equals(newURL))
				reload=false;
		}
		m_data=data;
		
		if (reload)
			dataChanged();
	}
	
	protected void dataChanged() {
	}

	public String getID() {
		ViewData data=getData();
		
		return (data!=null) ? data.getID() : null;
	}
}