/*******************************************************************************
 * Copyright (c) 2014, 2014 Bruno Medeiros and other Contributors.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Bruno Medeiros - initial API and implementation
 *******************************************************************************/
package melnorme.lang.ide.ui.dialogs;


import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.dialogs.PropertyPage;

import melnorme.lang.ide.ui.preferences.LangProjectBuildConfigurationComponent;
import melnorme.util.swt.SWTFactoryUtil;

public abstract class LangBuildConfigurationPropertyPage extends PropertyPage {
	
	public LangBuildConfigurationPropertyPage() {
		super();
		noDefaultAndApplyButton();
	}
	
	protected IProject getProject() {
		IAdaptable adaptable = getElement();
		if(adaptable instanceof IProject) {
			return (IProject) adaptable;
		}
		return (IProject) adaptable.getAdapter(IProject.class);
	}
	
	@Override
	protected Control createContents(Composite parent) {
		IProject project = getProject();
		if(project == null) {
			return SWTFactoryUtil.createLabel(parent, SWT.LEFT, "No project available", null);
		}
		return createContents(parent, project);
	}
	
	/* -----------------  ----------------- */
	
	protected LangProjectBuildConfigurationComponent buildOptionsComponent;
	
	protected Control createContents(Composite parent, IProject project) {
		buildOptionsComponent = createProjectBuildConfigComponent(project);
		return buildOptionsComponent.createComponent(parent);
	}
	
	protected abstract LangProjectBuildConfigurationComponent createProjectBuildConfigComponent(IProject project);
	
	@Override
	public void applyData(Object data) {
		if(data instanceof String) {
			String targetName = (String) data;
			buildOptionsComponent.getBuildTargetField().setFieldValue(targetName);
		}
	}
	
	@Override
	public boolean performOk() {
		return buildOptionsComponent.performOk();
	}
	
	@Override
	protected void performDefaults() {
		buildOptionsComponent.restoreDefaults();
	}
	
}
@@ -95,14 +95,8 @@
  (c)
- 2014,
  201
@@ -1050,16 +1050,93 @@
 e %7B%0D%0A%09%0D%0A
+%09protected LangProjectBuildConfigurationComponent buildOptionsComponent;%0D%0A%09%0D%0A
 %09public 
@@ -1216,32 +1216,204 @@
 tton();%0D%0A%09%7D%0D%0A%09%0D%0A
+%09@Override%0D%0A%09public void setElement(IAdaptable element) %7B%0D%0A%09%09super.setElement(element);%0D%0A%09%09buildOptionsComponent = createProjectBuildConfigComponent(getProject());%0D%0A%09%7D%0D%0A%09%0D%0A
 %09protected IProj
@@ -1844,24 +1844,46 @@
 %09return 
+buildOptionsComponent.
 createCo
 ntents(p
@@ -1878,30 +1878,22 @@
 teCo
+mpo
 n
-t
 ent
-s
 (parent
-, project
 );%0D%0A
@@ -1951,293 +1951,8 @@
 %0A%09%0D%0A
-%09protected LangProjectBuildConfigurationComponent buildOptionsComponent;%0D%0A%09%0D%0A%09protected Control createContents(Composite parent, IProject project) %7B%0D%0A%09%09buildOptionsComponent = createProjectBuildConfigComponent(project);%0D%0A%09%09return buildOptionsComponent.createComponent(parent);%0D%0A%09%7D%0D%0A%09%0D%0A
 %09pro

fix potential NPE depending on how initialization was done.