/* Copyright ? 2005 Adobe Systems Incorporated and its suppliers. All rights reserved.
 * 
 * This file is example code that illustrates how to access the FlexBuilder Design Model
 * and Code Model from other eclipse plugins.
 */

package zorn.mxmlmodel.tests;

import org.eclipse.core.resources.IFile;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.part.WorkbenchPart;

import zorn.codemodel.common.CMFactory;
import zorn.editors.mxml.MXMLEditor;
import zorn.editors.mxml.views.MXMLEditorActivationListener;
import zorn.mxmlmodel.IMXMLModel;

/** This class illustrates how you can hook up with MXMLEditors.
 */
public class FlexBuilderModelAccess extends WorkbenchPart {
	
	private MXMLEditorActivationListener myEditorActivationListener;
	
	/* This function illustrates how to listen for the comings and goings of MXMLEditor instances.
	 */
	public void createPartControl(Composite parent) {
		myEditorActivationListener = new MXMLEditorActivationListener(getSite().getPage()) {
			public void notifyEditorActivated(MXMLEditor editor) {
				// a new MXMLEditor has been activated
				// <your code goes here>
			}
		};
	}
	
	/* This function illustrates how to get rid of an MXMLEditorActivationListener
	 */
	public void dispose() {
		if (myEditorActivationListener != null) {
			myEditorActivationListener.dispose();
			myEditorActivationListener = null;
		}
	}

	/* This function illustrates how to find a currently active MXMLEditor editor.
	 */
	public MXMLEditor findMXMLEditor(IWorkbenchPage page) {
		IEditorPart editor = page.getActiveEditor();
		if(editor instanceof MXMLEditor)
			return (MXMLEditor)editor;
		else
			return null;
	}

	/* This function illustrates how to find the Design Model corresponding to an MXML Editor
	 */
	public IMXMLModel findDesignModel(MXMLEditor editor) throws Exception {
		return editor.getModel();
	}

	/* This function illustrates how to find a codemodel project, given a file
	 */
	public zorn.codemodel.project.IProject findCodeModelProject(IFile file) {
		zorn.codemodel.project.IProject project = CMFactory.getManager().getProjectForFile(file.getFullPath());
		return project;
	}

	public void setFocus() {
	}
}