bug 401928: add a link to a grammar on Execution environement
diff --git a/plugins/org.eclipse.ldt.support.lua51/src-ee/lua-5.1/lua-5.1.rockspec b/plugins/org.eclipse.ldt.support.lua51/src-ee/lua-5.1/lua-5.1.rockspec
index a1cef04..28ee97c 100644
--- a/plugins/org.eclipse.ldt.support.lua51/src-ee/lua-5.1/lua-5.1.rockspec
+++ b/plugins/org.eclipse.ldt.support.lua51/src-ee/lua-5.1/lua-5.1.rockspec
@@ -1,5 +1,6 @@
 package = "lua"

 version = "5.1"

+grammar = "lua-5.1"

 flags = { ee = true }

 description = {

    summary = "Lua 5.1 Execution Environment",

diff --git a/plugins/org.eclipse.ldt.support.lua52/src-ee/lua-5.2/lua-5.2.rockspec b/plugins/org.eclipse.ldt.support.lua52/src-ee/lua-5.2/lua-5.2.rockspec
index 8981654..f346627 100644
--- a/plugins/org.eclipse.ldt.support.lua52/src-ee/lua-5.2/lua-5.2.rockspec
+++ b/plugins/org.eclipse.ldt.support.lua52/src-ee/lua-5.2/lua-5.2.rockspec
@@ -1,5 +1,6 @@
 package = "lua"

 version = "5.2"

+grammar = "lua-5.2"

 flags = { ee = true }

 description = {

    summary = "Lua 5.2 Execution Environment",

diff --git a/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/GrammarGroup.java b/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/GrammarGroup.java
index 4e927c3..2d035f7 100644
--- a/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/GrammarGroup.java
+++ b/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/GrammarGroup.java
@@ -12,39 +12,60 @@
 
 import java.util.List;
 
-import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.jface.layout.GridLayoutFactory;
 import org.eclipse.jface.viewers.ComboViewer;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.StructuredSelection;
-import org.eclipse.ldt.core.internal.LuaLanguageToolkit;
-import org.eclipse.ldt.core.internal.PreferenceInitializer;
 import org.eclipse.ldt.core.internal.grammar.LuaGrammarManager;
 import org.eclipse.ldt.ui.internal.grammar.GrammarContentProvider;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Group;
-import org.eclipse.ui.preferences.ScopedPreferenceStore;
+import org.eclipse.swt.widgets.Label;
 
 public class GrammarGroup {
-
 	private ComboViewer availableGrammarComboViewer;
-	private ISelection selection;
+	private Button customGrammarRadio;
+	private Button defaultGrammarRadio;
+	private String grammar;
+
+	private SelectionListener grammarChoiceListener = new SelectionAdapter() {
+		@Override
+		public void widgetSelected(final SelectionEvent e) {
+			availableGrammarComboViewer.getCombo().setEnabled(customGrammarRadio.getSelection());
+			defaultGrammarLabel.setEnabled(defaultGrammarRadio.getSelection());
+		}
+	};
+	private Label defaultGrammarLabel;
 
 	public GrammarGroup(final Composite parent) {
 		// Create group
 		final Group group = new Group(parent, SWT.NONE);
 		group.setText(Messages.GrammarGroup_group_name);
 		GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).applyTo(group);
-		GridLayoutFactory.swtDefaults().numColumns(1).applyTo(group);
+		GridLayoutFactory.swtDefaults().numColumns(2).applyTo(group);
 
 		// Grammar combo viewer
+		defaultGrammarRadio = new Button(group, SWT.RADIO);
+		defaultGrammarRadio.setText(Messages.GrammarGroup_defaultgrammar);
+		defaultGrammarRadio.addSelectionListener(grammarChoiceListener);
+		defaultGrammarLabel = new Label(group, SWT.NONE);
+		GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(defaultGrammarLabel);
+
+		customGrammarRadio = new Button(group, SWT.RADIO);
+		customGrammarRadio.setText(Messages.GrammarGroup_customgrammar);
+		customGrammarRadio.addSelectionListener(grammarChoiceListener);
+
 		availableGrammarComboViewer = new ComboViewer(group, SWT.READ_ONLY | SWT.BORDER);
 		availableGrammarComboViewer.setContentProvider(new GrammarContentProvider());
-		GridDataFactory.swtDefaults().align(SWT.BEGINNING, SWT.BEGINNING).grab(true, false).applyTo(availableGrammarComboViewer.getControl());
+		GridDataFactory.swtDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(availableGrammarComboViewer.getControl());
 
 		initializeGroup();
 	}
@@ -52,36 +73,46 @@
 	public String getSelectedGrammar() {
 		Display.getDefault().syncExec(new Runnable() {
 			public void run() {
-				if (availableGrammarComboViewer != null) {
-					selection = availableGrammarComboViewer.getSelection();
-				} else {
-					selection = null;
+				grammar = null;
+				if (customGrammarRadio != null && availableGrammarComboViewer != null && defaultGrammarLabel != null) {
+					if (customGrammarRadio.getSelection()) {
+						ISelection selection = availableGrammarComboViewer.getSelection();
+						if (selection instanceof IStructuredSelection) {
+							Object firstElement = ((IStructuredSelection) selection).getFirstElement();
+							if (firstElement instanceof String)
+								grammar = (String) firstElement;
+						}
+					} else {
+						grammar = defaultGrammarLabel.getText();
+					}
 				}
 			}
 		});
 
-		if (selection instanceof IStructuredSelection) {
-			Object firstElement = ((IStructuredSelection) selection).getFirstElement();
-			if (firstElement instanceof String)
-				return (String) firstElement;
-		}
-
-		return null;
+		return grammar;
 	}
 
 	private void initializeGroup() {
 		if (availableGrammarComboViewer == null || availableGrammarComboViewer.getControl().isDisposed())
 			return;
 
+		if (defaultGrammarRadio == null || defaultGrammarRadio.isDisposed())
+			return;
+
 		// Refresh list
 		List<String> availableGrammars = LuaGrammarManager.getAvailableGrammars();
 		availableGrammarComboViewer.setInput(availableGrammars);
+		availableGrammarComboViewer.setSelection(new StructuredSelection(LuaGrammarManager.getDefaultGrammar().getName()));
 
-		// Set default interpreter
-		ScopedPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, LuaLanguageToolkit.getDefault()
-				.getPreferenceQualifier());
-		String defaultGrammar = preferenceStore.getString(PreferenceInitializer.GRAMMAR_DEFAULT_ID);
-		if (defaultGrammar != null)
-			availableGrammarComboViewer.setSelection(new StructuredSelection(defaultGrammar));
+		defaultGrammarRadio.setSelection(true);
+		availableGrammarComboViewer.getControl().setEnabled(false);
+	}
+
+	public void setDefaultGrammar(final String defaultGrammar) {
+		Display.getDefault().syncExec(new Runnable() {
+			public void run() {
+				defaultGrammarLabel.setText(defaultGrammar);
+			}
+		});
 	}
 }
diff --git a/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/LuaExecutionEnvironmentGroup.java b/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/LuaExecutionEnvironmentGroup.java
index d03d8f8..396014c 100644
--- a/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/LuaExecutionEnvironmentGroup.java
+++ b/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/LuaExecutionEnvironmentGroup.java
@@ -19,7 +19,9 @@
 import org.eclipse.jface.layout.GridLayoutFactory;

 import org.eclipse.jface.viewers.ComboViewer;

 import org.eclipse.jface.viewers.ISelection;

+import org.eclipse.jface.viewers.ISelectionChangedListener;

 import org.eclipse.jface.viewers.IStructuredSelection;

+import org.eclipse.jface.viewers.SelectionChangedEvent;

 import org.eclipse.jface.viewers.StructuredSelection;

 import org.eclipse.ldt.core.buildpath.LuaExecutionEnvironment;

 import org.eclipse.ldt.core.internal.LuaLanguageToolkit;

@@ -65,6 +67,9 @@
 				isListAvailable = false;

 			}

 			installedEEsComboViewer.getCombo().setEnabled(isListAvailable);

+

+			setChanged();

+			notifyObservers();

 		}

 	};

 

@@ -93,6 +98,13 @@
 		// Execution Environment actual list

 		installedEEsComboViewer = new ComboViewer(group, SWT.READ_ONLY | SWT.BORDER);

 		installedEEsComboViewer.setContentProvider(new LuaExecutionEnvironmentContentProvider());

+		installedEEsComboViewer.addSelectionChangedListener(new ISelectionChangedListener() {

+			@Override

+			public void selectionChanged(SelectionChangedEvent event) {

+				setChanged();

+				notifyObservers();

+			}

+		});

 		GridDataFactory.swtDefaults().align(SWT.FILL, SWT.BEGINNING).grab(true, false).applyTo(installedEEsComboViewer.getControl());

 

 		// Set link to define a new execution environment

diff --git a/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/LuaProjectSettingsPage.java b/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/LuaProjectSettingsPage.java
index 88c8351..09b4418 100644
--- a/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/LuaProjectSettingsPage.java
+++ b/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/LuaProjectSettingsPage.java
@@ -15,6 +15,7 @@
 
 import org.eclipse.dltk.ui.wizards.ProjectWizardFirstPage;
 import org.eclipse.ldt.core.buildpath.LuaExecutionEnvironment;
+import org.eclipse.ldt.core.internal.grammar.LuaGrammarManager;
 import org.eclipse.swt.widgets.Composite;
 
 /**
@@ -57,6 +58,8 @@
 	protected void createCustomGroups(final Composite composite) {
 		luaExecutionEnvironmentGroup = createExecutionEnvironmentGroup(composite);
 		grammarGroup = createGrammarGroup(composite);
+		String defaultGrammar = LuaGrammarManager.getDefaultGrammarFor(getExecutionEnvironment()).getName();
+		grammarGroup.setDefaultGrammar(defaultGrammar);
 	}
 
 	protected GrammarGroup createGrammarGroup(Composite composite) {
@@ -109,6 +112,14 @@
 	public void createControl(Composite parent) {
 		super.createControl(parent);
 		fLocationGroup.addObserver(this);
+		luaExecutionEnvironmentGroup.addObserver(new Observer() {
+
+			@Override
+			public void update(Observable o, Object arg) {
+				LuaExecutionEnvironment ee = getExecutionEnvironment();
+				grammarGroup.setDefaultGrammar(LuaGrammarManager.getDefaultGrammarFor(ee).getName());
+			}
+		});
 	}
 
 	@Override
diff --git a/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/Messages.java b/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/Messages.java
index 337c966..489752d 100644
--- a/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/Messages.java
+++ b/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/Messages.java
@@ -31,6 +31,10 @@
 	/** @since 1.2 */
 	public static String DocLuaFilePage_title;
 
+	public static String GrammarGroup_customgrammar;
+
+	public static String GrammarGroup_defaultgrammar;
+
 	public static String GrammarGroup_group_name;
 
 	/** @since 1.1 */
diff --git a/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/messages.properties b/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/messages.properties
index 7565732..4283fa0 100644
--- a/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/messages.properties
+++ b/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/wizards/pages/messages.properties
@@ -14,6 +14,8 @@
 ConvertToLuaProjectMainPage_migrationMessage=You will migrate a koneki project to new Lua project. This can not be undone.
 DocLuaFilePage_description=Create a DocLua file. A DocLua file is a file describing an API to\nenable tooling, and will not be taken in account at runtime.
 DocLuaFilePage_title=DocLua File
+GrammarGroup_customgrammar=Select one :
+GrammarGroup_defaultgrammar=Default one :
 GrammarGroup_group_name=Target Grammar
 LuaExecutionEnvironmentGroupMainLabel=Create default template project ready to run.
 LuaExecutionEnvironmentGroupTemplateLabel=Create default template project ready to run.
diff --git a/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/buildpath/LuaExecutionEnvironment.java b/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/buildpath/LuaExecutionEnvironment.java
index 616896d..f265de7 100644
--- a/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/buildpath/LuaExecutionEnvironment.java
+++ b/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/buildpath/LuaExecutionEnvironment.java
@@ -26,14 +26,17 @@
 	private final IPath path;

 	private final IPath oldTemplate;

 	private final IPath templates;

+	private final String luaGrammar;

 	private boolean embedded;

 	private Map<?, ?> templatesInfo;

 

-	public LuaExecutionEnvironment(final String identifier, final String eeversion, final Map<?, ?> templatesInfo, final IPath pathToEE) {

+	public LuaExecutionEnvironment(final String identifier, final String eeversion, final Map<?, ?> templatesInfo, final IPath pathToEE,

+			final String grammar) {

 		id = identifier;

 		version = eeversion;

 		path = pathToEE;

 		embedded = false;

+		luaGrammar = grammar;

 		this.templatesInfo = templatesInfo;

 

 		// Old template folder (supported for legacy only)

@@ -164,4 +167,8 @@
 			return null;

 		return templatesPath.append(LuaExecutionEnvironment.DEFAULT_TEMPLATE);

 	}

+

+	public String getLuaGrammar() {

+		return luaGrammar;

+	}

 }

diff --git a/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/buildpath/LuaExecutionEnvironmentManager.java b/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/buildpath/LuaExecutionEnvironmentManager.java
index d5f82e8..4027713 100644
--- a/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/buildpath/LuaExecutionEnvironmentManager.java
+++ b/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/buildpath/LuaExecutionEnvironmentManager.java
@@ -62,6 +62,7 @@
 

 	private static final String MANIFEST_NAME = "package"; //$NON-NLS-1$

 	private static final String MANIFEST_VERSION = "version"; //$NON-NLS-1$

+	private static final String MANIFEST_GRAMMAR = "grammar"; //$NON-NLS-1$

 	private static final String MANIFEST_TEMPLATES = "templates"; //$NON-NLS-1$

 

 	private static final String INSTALLATION_FOLDER = "ee"; //$NON-NLS-1$

@@ -231,6 +232,9 @@
 			luaState.getGlobal(MANIFEST_VERSION);

 			String version = luaState.toString(-1);

 

+			luaState.getGlobal(MANIFEST_GRAMMAR);

+			String grammar = luaState.toString(-1);

+

 			// Create object representing a valid Execution Environment

 			if (name == null || version == null) {

 				throwException("Manifest from given file has no package name or version.", null, IStatus.ERROR); //$NON-NLS-1$

@@ -242,7 +246,7 @@
 			if (luatemplates != null)

 				templates = createJavaCopy(luatemplates);

 

-			return new LuaExecutionEnvironment(name, version, templates, installDirectory);

+			return new LuaExecutionEnvironment(name, version, templates, installDirectory, grammar);

 		} catch (LuaException e) {

 			luaState.close();

 			throwException("Error while loading the manifest", e, IStatus.ERROR); //$NON-NLS-1$

diff --git a/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/grammar/LuaGrammarManager.java b/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/grammar/LuaGrammarManager.java
index 1e1f5be..1aea8ae 100644
--- a/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/grammar/LuaGrammarManager.java
+++ b/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/grammar/LuaGrammarManager.java
@@ -16,8 +16,14 @@
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IConfigurationElement;
 import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.ldt.core.buildpath.LuaExecutionEnvironment;
 import org.eclipse.ldt.core.grammar.IGrammar;
 import org.eclipse.ldt.core.grammar.ILuaSourceValidator;
+import org.eclipse.ldt.core.internal.Activator;
+import org.eclipse.ldt.core.internal.LuaLanguageToolkit;
+import org.eclipse.ldt.core.internal.PreferenceInitializer;
+import org.eclipse.ui.preferences.ScopedPreferenceStore;
 
 public final class LuaGrammarManager {
 
@@ -104,4 +110,49 @@
 
 		return grammars;
 	}
+
+	public static IGrammar getDefaultGrammar() {
+		// get in preference the default grammar
+		ScopedPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, LuaLanguageToolkit.getDefault()
+				.getPreferenceQualifier());
+		String defaultGrammarID = preferenceStore.getString(PreferenceInitializer.GRAMMAR_DEFAULT_ID);
+
+		// check the grammar is available
+		try {
+			IGrammar defaultGrammar = getAvailableGrammar(defaultGrammarID);
+			if (defaultGrammar != null)
+				return defaultGrammar;
+		} catch (CoreException e) {
+			String message = String.format("The default grammar %s is not available.", defaultGrammarID);//$NON-NLS-1$
+			Activator.logWarning(message, e);
+		}
+
+		// use grammar 5.1 by default
+		try {
+			return getAvailableGrammar("lua-5.1"); //$NON-NLS-1$
+		} catch (CoreException e) {
+			String message = "The lua 5.1 grammar must be available. Check if you have the org.eclise.ldt.support.lua51 installed!"; //$NON-NLS-1$
+			Activator.logError(message, e);
+			throw new RuntimeException(message, e);
+		}
+	}
+
+	public static IGrammar getDefaultGrammarFor(LuaExecutionEnvironment ee) {
+		// if ee has no grammar defined use the default one
+		if (ee == null || ee.getLuaGrammar() == null)
+			return getDefaultGrammar();
+
+		// check the grammar is available
+		try {
+			IGrammar availableGrammar = getAvailableGrammar(ee.getLuaGrammar());
+			if (availableGrammar != null)
+				return availableGrammar;
+		} catch (CoreException e) {
+			String message = String.format(
+					"The default grammar %s for the execution environment %s is not available.", ee.getLuaGrammar(), ee.getEEIdentifier());//$NON-NLS-1$
+			Activator.logWarning(message, e);
+			// use default grammar instead...
+		}
+		return getDefaultGrammar();
+	}
 }