/**
 *  Copyright (c) 2015-2016 Angelo ZERR.
 *  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:
 *  Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
 * 
 */
package ts.eclipse.ide.angular2.internal.cli.jobs;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.progress.UIJob;

import ts.eclipse.ide.angular2.cli.AngularCLIPlugin;
import ts.eclipse.ide.angular2.internal.cli.AngularCLIMessages;
import ts.eclipse.ide.terminal.interpreter.UIInterpreterHelper;

/**
 * Refresh generated files and select it in the Project Explorer.
 *
 */
public class NgGenerateJob extends UIJob {

	private final Collection<String> fileNames;
	private final IContainer parent;
	private final String blueprint;

	public NgGenerateJob(Collection<String> fileNames, IContainer parent, String blueprint) {
		super(AngularCLIMessages.NgGenerateJob_jobName);
		this.fileNames = fileNames;
		this.parent = parent;
		this.blueprint = blueprint;
	}

	@Override
	public IStatus runInUIThread(IProgressMonitor monitor) {
		try {
			List<IResource> resources = getResources(parent, monitor);
			if (resources.size() > 0) {
				// Open the blueprint file in an editor
				IFile fileToOpen = getFileToOpen(resources);
				UIInterpreterHelper.openFile(fileToOpen);
				// Select in the Project Explorer the generated files.
				UIInterpreterHelper.selectRevealInDefaultViews(resources);
			}
		} catch (CoreException e) {
			return new Status(IStatus.ERROR, AngularCLIPlugin.PLUGIN_ID, AngularCLIMessages.NgGenerateJob_error, e);
		}
		return Status.OK_STATUS;
	}

	/**
	 * Collect the generated resources.
	 * 
	 * @param parents
	 * @param monitor
	 * @return the generated resources.
	 * @throws CoreException
	 */
	private List<IResource> getResources(IContainer parent, IProgressMonitor monitor) throws CoreException {
		List<IResource> resources = new ArrayList<IResource>();
		for (String filename : fileNames) {
			IPath path = new Path(filename);
			IFile file = parent.getFile(path);
			file.refreshLocal(IResource.DEPTH_INFINITE, monitor);
			if (parent.exists(path)) {
				resources.add(file);
			}
		}
		return resources;
	}

	private IFile getFileToOpen(List<IResource> resources) {
		String suffix = "." + blueprint.toLowerCase() + ".ts";
		for (IResource resource : resources) {
			if (resource.getName().endsWith(suffix)) {
				return (IFile) resource;
			}
		}
		return (IFile) resources.get(resources.size() - 1);
	}
}

@@ -2674,16 +2674,528 @@
 ame);%0D%0A%09
+%09%09// new version of angular-cli display in the console %0D%0A%09%09%09// create src%5Capp%5Ca.ts =%3E search in the project%0D%0A%09%09%09if (!addResource(parent.getParent(), path, resources, monitor)) %7B%0D%0A%09%09%09%09// old version of angular-cli display in the console%0D%0A%09%09%09%09// create a.ts =%3E search in the folder%0D%0A%09%09%09%09addResource(parent, path, resources, monitor);%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09return resources;%0D%0A%09%7D%0D%0A%0D%0A%09private boolean addResource(IContainer parent, IPath path, List%3CIResource%3E resources, IProgressMonitor monitor)%0D%0A%09%09%09throws CoreException %7B%0D%0A
 %09%09IFile 
@@ -3224,17 +3224,16 @@
 path);%0D%0A
-%09
 %09%09file.r
@@ -3277,25 +3277,24 @@
  monitor);%0D%0A
-%09
 %09%09if (parent
@@ -3314,17 +3314,16 @@
 ) %7B%0D%0A%09%09%09
-%09
 resource
@@ -3335,25 +3335,36 @@
 (file);%0D%0A%09%09%09
-%7D
+return true;
 %0D%0A%09%09%7D%0D%0A%09%09ret
@@ -3363,33 +3363,29 @@
 %7D%0D%0A%09%09return 
-resources
+false
 ;%0D%0A%09%7D%0D%0A%0D%0A%09pr

Refresh generated ts files with last angular-cli.