/**
 *  Copyright (c) 2015-2017 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.terminal;

import ts.eclipse.ide.angular2.internal.cli.jobs.NgServeJob;
import ts.eclipse.ide.terminal.interpreter.AbstractCommandInterpreter;

/**
 * "ng serve" interpreter to open a WebBrowser when trace "NG Live Development
 * Server is running on http://localhost:4200. **" appears.
 *
 */
public class NgServeCommandInterpreter extends AbstractCommandInterpreter {

	private static final String HTTP = "http";
	
	public NgServeCommandInterpreter(String workingDir) {
		super(workingDir);
	}

	@Override
	public void execute(String newWorkingDir) {
		// Do nothing, the open of web browser is done when trace "Serving on
		// http://localhost:4200/"
		// is found.
	}

	@Override
	public void onTrace(String line) {
		// track 
		// ** NG Live Development Server is running on http://localhost:4200. **
		// ** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200 **
		int startIndex = line.indexOf(HTTP);
		if (startIndex != -1) {			
			int endIndex = line.indexOf(".", startIndex);
			if (endIndex == -1) {
				endIndex = line.indexOf(" ", startIndex);
			}
			final String serverURL = line.substring(startIndex, endIndex != -1 ? endIndex : line.length())
					.trim();
			// Open a Web Browser with the given server URL
			new NgServeJob(serverURL).schedule();
		}
	}

}

@@ -397,12 +397,10 @@
 %0D%0A *
-  
 %0D%0A
+
  */%0D
@@ -458,16 +458,40 @@
 nal;%0D%0A%0D%0A
+import java.net.URL;%0D%0A%0D%0A
 import t
@@ -897,17 +897,87 @@
 http%22;%0D%0A
-%09
+%0D%0A%09private boolean open;%09%09%09%09%09%09// Has the browser already been opened?%0D%0A
 %0D%0A%09publi
@@ -1296,17 +1296,16 @@
 // track
- 
 %0D%0A%09%09// *
@@ -1488,16 +1488,33 @@
 200 **%0D%0A
+%09%09if (!open) %7B%0D%0A%09
 %09%09int st
@@ -1545,16 +1545,17 @@
 HTTP);%0D%0A
+%09
 %09%09if (st
@@ -1575,13 +1575,11 @@
 1) %7B
-%09%09%09
 %0D%0A
+%09
 %09%09%09i
@@ -1620,24 +1620,25 @@
 artIndex);%0D%0A
+%09
 %09%09%09if (endIn
@@ -1655,16 +1655,17 @@
  %7B%0D%0A%09%09%09%09
+%09
 endIndex
@@ -1702,19 +1702,21 @@
 x);%0D%0A%09%09%09
+%09
 %7D%0D%0A
+%09
 %09%09%09final
@@ -1804,23 +1804,16 @@
 ength())
-%0D%0A%09%09%09%09%09
 .trim();
@@ -1814,16 +1814,213 @@
 rim();%0D%0A
+%0D%0A%09%09%09%09// Basic URL-Validation%0D%0A%09%09%09%09URL u;%0D%0A%09%09%09%09try %7B%0D%0A%09%09%09%09%09u = new URL(serverURL);%09%09// this would check for the protocol%0D%0A%09%09%09%09%09u.toURI(); %09%09%09%09%09// extra checking required for validation of URI%0D%0A%0D%0A%09%09
 %09%09%09// Op
@@ -2066,16 +2066,18 @@
 URL%0D%0A%09%09%09
+%09%09
 new NgSe
@@ -2107,16 +2107,82 @@
 ule();%0D%0A
+%09%09%09%09%09open = true;%0D%0A%09%09%09%09%7D catch (Exception ignore) %7B%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%09%7D%0D

Change NgServeCommandInterpreter to open Browser only once