Bug 573646: Open primary terminal view when there is no last available

Change-Id: I286d039d6cb3eb1e73fdb5b76c8743a8e0870d94
diff --git a/terminal/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/manager/ConsoleManager.java b/terminal/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/manager/ConsoleManager.java
index 428ebf5..8dec9dc 100644
--- a/terminal/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/manager/ConsoleManager.java
+++ b/terminal/plugins/org.eclipse.tm.terminal.view.ui/src/org/eclipse/tm/terminal/view/ui/manager/ConsoleManager.java
@@ -237,8 +237,9 @@
 
 		for (IViewReference ref : getActiveWorkbenchPage().getViewReferences()) {
 			if (ref.getId().equals(id)) {
+				String refSecondaryId = ref.getSecondaryId();
 				if (ITerminalsConnectorConstants.ANY_ACTIVE_SECONDARY_ID.equals(secondaryId)
-						|| Objects.equals(secondaryId, ref.getSecondaryId())) {
+						|| Objects.equals(secondaryId, refSecondaryId)) {
 					return ref.getView(restore);
 				}
 			}
@@ -268,7 +269,14 @@
 		}
 
 		if (part == null) {
-			part = getTerminalsViewWithSecondaryId(id, secondaryId, true);
+			String finalSecondaryId;
+			if (ITerminalsConnectorConstants.LAST_ACTIVE_SECONDARY_ID.equals(secondaryId)) {
+				// There is no last available, so get any available instead
+				finalSecondaryId = ITerminalsConnectorConstants.ANY_ACTIVE_SECONDARY_ID;
+			} else {
+				finalSecondaryId = secondaryId;
+			}
+			part = getTerminalsViewWithSecondaryId(id, finalSecondaryId, true);
 			if (part != null) {
 				lastActiveViewId = part.getViewSite().getId();
 				lastActiveSecondaryViewId = part.getViewSite().getSecondaryId();
@@ -339,8 +347,19 @@
 			try {
 				// show the view
 				IViewPart part = getActiveTerminalsView(id != null ? id : IUIConstants.ID, secondaryId);
-				if (part == null)
-					part = page.showView(id != null ? id : IUIConstants.ID, secondaryId, IWorkbenchPage.VIEW_ACTIVATE);
+				if (part == null) {
+					String finalSecondaryId;
+					if (ITerminalsConnectorConstants.LAST_ACTIVE_SECONDARY_ID.equals(secondaryId)
+							|| ITerminalsConnectorConstants.ANY_ACTIVE_SECONDARY_ID.equals(secondaryId)) {
+						// We have already checked all open views, so since none of the special flags work
+						// we are opening the first view, which means no secondary id.
+						finalSecondaryId = null;
+					} else {
+						finalSecondaryId = secondaryId;
+					}
+					part = page.showView(id != null ? id : IUIConstants.ID, finalSecondaryId,
+							IWorkbenchPage.VIEW_ACTIVATE);
+				}
 				// and force the view to the foreground
 				page.bringToTop(part);
 				return part;