Bug 548869 - Tabbing in Environment Variable Table unusable


Change-Id: I880bcf5bf73eddf944595df84d2e02875c897f9f
Signed-off-by: Andrew Obuchowicz <aobuchow@redhat.com>
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/EnvironmentTab.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/EnvironmentTab.java
index 6feb4da..aa9e32b 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/EnvironmentTab.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/ui/EnvironmentTab.java
@@ -55,6 +55,7 @@
 import org.eclipse.jface.layout.PixelConverter;
 import org.eclipse.jface.layout.TableColumnLayout;
 import org.eclipse.jface.viewers.ColumnLabelProvider;
+import org.eclipse.jface.viewers.ColumnViewerEditor;
 import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
 import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
 import org.eclipse.jface.viewers.ColumnWeightData;
@@ -405,7 +406,10 @@
 			}
 		};
 
-		TableViewerEditor.create(environmentTable, actSupport, SWT.NONE);
+		int feature = ColumnViewerEditor.TABBING_HORIZONTAL | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
+				| ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION;
+
+		TableViewerEditor.create(environmentTable, actSupport, feature);
 
 		// Setup environment variable name column
 		final TableViewerColumn tcv1 = new TableViewerColumn(environmentTable, SWT.NONE, 0);
@@ -420,12 +424,14 @@
 		tc1.setText(envTableColumnHeaders[0]);
 		tcv1.setEditingSupport(new TextGetSetEditingSupport<>(tcv1.getViewer(), EnvironmentVariable::getName,
 				(EnvironmentVariable envVar, String value) -> {
-					if (value != null && !value.isEmpty()) {
-						if (!value.equals(envVar.getName())) {
-							// Trim environment variable names
-							EnvironmentVariable editedVar = new EnvironmentVariable(value.trim(), envVar.getValue());
-							if (addVariable(editedVar)) {
-								environmentTable.remove(envVar);
+					// Trim environment variable names
+					String newName = value.trim();
+					if (newName != null && !newName.isEmpty()) {
+						if (!newName.equals(envVar.getName())) {
+							if (canRenameVariable(newName)) {
+								envVar.setName(newName);
+								updateAppendReplace();
+								updateLaunchConfigurationDialog();
 							}
 						}
 					}
@@ -444,6 +450,7 @@
 		tc2.setText(envTableColumnHeaders[1]);
 		tcv2.setEditingSupport(
 				new TextGetSetEditingSupport<>(tcv2.getViewer(), EnvironmentVariable::getValue, (envVar, value) -> {
+					// Don't trim environment variable values
 					envVar.setValue(value);
 					updateAppendReplace();
 					updateLaunchConfigurationDialog();
@@ -566,6 +573,34 @@
 	}
 
 	/**
+	 * Returns whether the environment variable can be renamed to the given variable
+	 * name. If the name is already used for another variable, the user decides with
+	 * a dialog whether to overwrite the existing variable
+	 *
+	 * @param newVariableName the chosen name to give to the variable
+	 * @return whether the new name should be used or not
+	 */
+	private boolean canRenameVariable(String newVariableName) {
+		TableItem[] items = environmentTable.getTable().getItems();
+		for (int i = 0; i < items.length; i++) {
+			EnvironmentVariable existingVariable = (EnvironmentVariable) items[i].getData();
+			if (existingVariable.getName().equals(newVariableName)) {
+
+				boolean overWrite = MessageDialog.openQuestion(getShell(),
+						LaunchConfigurationsMessages.EnvironmentTab_12,
+						MessageFormat.format(LaunchConfigurationsMessages.EnvironmentTab_13,
+								new Object[] { newVariableName }));
+				if (!overWrite) {
+					return false;
+				}
+				environmentTable.remove(existingVariable);
+				return true;
+			}
+		}
+		return true;
+	}
+
+	/**
 	 * Attempts to add the given variable. Returns whether the variable
 	 * was added or not (as when the user answers not to overwrite an
 	 * existing variable).