/*******************************************************************************
 * Copyright (c) 2009, 2013 Wind River Systems, Inc. and others.
 * 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:
 *     Doug Schaefer - initial API and implementation
 *     Marc-Andre Laperle - Moved to an operation for a custom wizard page
 *     Liviu Ionescu - ARM version
 *******************************************************************************/

package ilg.gnuarmeclipse.managedbuild.cross.ui;

import ilg.gnuarmeclipse.managedbuild.cross.ToolchainDefinition;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.cdt.build.core.scannerconfig.ScannerConfigBuilder;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.ui.wizards.MBSCustomPageManager;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;

/**
 * An operation that runs when the new project wizard finishes for the Cross GCC
 * toolchain. It reuses the information from {@link SetCrossCommandWizardPage}
 * to store options (index and path) in persistent storage.
 * 
 */
public class SetCrossCommandWizardOperation implements IRunnableWithProgress {

	String fProjectName;
	String fToolchainName;
	String fPath;

	public SetCrossCommandWizardOperation() {
	}

	public SetCrossCommandWizardOperation(String projectName,
			String toolchainName, String path) {
		fProjectName = projectName;
		fToolchainName = toolchainName;
		fPath = path;
	}

	public void run(IProgressMonitor monitor) throws InvocationTargetException,
			InterruptedException {

		// System.out.println("SetCrossCommandOperation.run() begin");

		// get local properties
		fProjectName = (String) MBSCustomPageManager.getPageProperty(
				SetCrossCommandWizardPage.PAGE_ID,
				SetCrossCommandWizardPage.CROSS_PROJECT_NAME);

		fToolchainName = (String) MBSCustomPageManager.getPageProperty(
				SetCrossCommandWizardPage.PAGE_ID,
				SetCrossCommandWizardPage.CROSS_TOOLCHAIN_NAME);
		fPath = (String) MBSCustomPageManager.getPageProperty(
				SetCrossCommandWizardPage.PAGE_ID,
				SetCrossCommandWizardPage.CROSS_TOOLCHAIN_PATH);

		// store them on the permanent storage in
		// workspace/.plugins/org.eclipse.cdt.core/shareddefaults.xml
		runWithoutWizard();

		// System.out.println("SetCrossCommandOperation.run() end");
	}

	public void runWithoutWizard() {
		
		SharedStorage.putToolchainPath(fToolchainName, fPath);
		SharedStorage.putToolchainName(fToolchainName);

		SharedStorage.update();

		IProject project = ResourcesPlugin.getWorkspace().getRoot()
				.getProject(fProjectName);
		if (!project.exists())
			return;

		IManagedBuildInfo buildInfo = ManagedBuildManager.getBuildInfo(project);
		if (buildInfo == null)
			return;

		IConfiguration[] configs = buildInfo.getManagedProject()
				.getConfigurations();
		for (IConfiguration config : configs) {

			try {
				updateOptions(config);
			} catch (BuildException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("SetCrossCommandWizardOperation "
						+ e.getMessage());
			}
		}

		ManagedBuildManager.saveBuildInfo(project, true);

		if (true) {
			for (IConfiguration config : configs) {
				ScannerConfigBuilder.build(config,
						ScannerConfigBuilder.PERFORM_CORE_UPDATE,
						new NullProgressMonitor());
			}
		}

		// System.out.println("SetCrossCommandOperation.run() end");

	}

	private void updateOptions(IConfiguration config) throws BuildException {

		String sToolchainName = (String) MBSCustomPageManager.getPageProperty(
				SetCrossCommandWizardPage.PAGE_ID,
				SetCrossCommandWizardPage.CROSS_TOOLCHAIN_NAME);

		int toolchainIndex;
		try {
			toolchainIndex = ToolchainDefinition
					.findToolchainByName(sToolchainName);
		} catch (IndexOutOfBoundsException e) {
			toolchainIndex = ToolchainDefinition.getDefault();
		}

		TabToolchains.setOptionsForToolchain(config, toolchainIndex);
	}

}

@@ -1819,314 +1819,8 @@
 %0D%0A%0D%0A
-%09String fProjectName;%0D%0A%09String fToolchainName;%0D%0A%09String fPath;%0D%0A%0D%0A%09public SetCrossCommandWizardOperation() %7B%0D%0A%09%7D%0D%0A%0D%0A%09public SetCrossCommandWizardOperation(String projectName,%0D%0A%09%09%09String toolchainName, String path) %7B%0D%0A%09%09fProjectName = projectName;%0D%0A%09%09fToolchainName = toolchainName;%0D%0A%09%09fPath = path;%0D%0A%09%7D%0D%0A%0D%0A
 %09pub
@@ -2019,18 +2019,24 @@
 ties%0D%0A%09%09
-fP
+String p
 rojectNa
@@ -2184,18 +2184,24 @@
 );%0D%0A%0D%0A%09%09
-fT
+String t
 oolchain
@@ -2351,18 +2351,24 @@
 ME);%0D%0A%09%09
-fP
+String p
 ath = (S
@@ -2623,140 +2623,8 @@
 ml%0D%0A
-%09%09runWithoutWizard();%0D%0A%0D%0A%09%09// System.out.println(%22SetCrossCommandOperation.run() end%22);%0D%0A%09%7D%0D%0A%0D%0A%09public void runWithoutWizard() %7B%0D%0A%09%09
 %0D%0A%09%09
@@ -2654,18 +2654,17 @@
 ainPath(
-fT
+t
 oolchain
@@ -2669,18 +2669,17 @@
 inName, 
-fP
+p
 ath);%0D%0A%09
@@ -2714,10 +2714,9 @@
 ame(
-fT
+t
 oolc
@@ -2841,10 +2841,9 @@
 ect(
-fP
+p
 roje
@@ -3333,24 +3333,17 @@
 ration %22
-%0D%0A%09%09%09%09%09%09
+ 
 + e.getM

Revert "cross: cross command wizard runWithoutWizard"