[431378] [shells] Remote shells not always restored properly on
reconnect
diff --git a/rse/plugins/org.eclipse.rse.shells.ui/src/org/eclipse/rse/internal/shells/ui/view/CommandsViewWorkbook.java b/rse/plugins/org.eclipse.rse.shells.ui/src/org/eclipse/rse/internal/shells/ui/view/CommandsViewWorkbook.java
index 6bc565a..d2f3c8f 100644
--- a/rse/plugins/org.eclipse.rse.shells.ui/src/org/eclipse/rse/internal/shells/ui/view/CommandsViewWorkbook.java
+++ b/rse/plugins/org.eclipse.rse.shells.ui/src/org/eclipse/rse/internal/shells/ui/view/CommandsViewWorkbook.java
@@ -15,6 +15,7 @@
  * David McKnight   (IBM)        - [165680] "Show in Remote Shell View" does not work
  * David McKnight   (IBM)        - [338031] Remote Shell view tabs should have close (x) icon
  * David McKnight (IBM)  -[425014] profile commit job don't always complete during shutdown
+ * David McKnight (IBM)  -[431378] [shells] Remote shells not always restored properly on reconnect
  *******************************************************************************/
 
 package org.eclipse.rse.internal.shells.ui.view;
@@ -180,7 +181,8 @@
 			for (int i = 0; i < _folder.getItemCount(); i++)
 			{
 				CTabItem item = _folder.getItem(i);
-				CommandsViewPage page = (CommandsViewPage) item.getData();
+				Object data = item.getData();
+				CommandsViewPage page = (CommandsViewPage)data;
 				if (page != null && root == page.getInput())
 				{
 					if (!root.isActive())
@@ -192,14 +194,6 @@
 
 					page.updateOutput();
 
-					/* DKM - changing focus can get annoying 
-					 * see defect 142978
-					 * 
-					if (_folder.getSelectionIndex() != i)
-					{
-						_folder.setSelection(item);
-					}
-					*/
 					updateActionStates();
 					//page.setFocus();
 					return;
@@ -220,7 +214,7 @@
 
 		CTabItem titem = new CTabItem(_folder, SWT.CLOSE);	
 		setTabTitle(root, titem);
- 
+
 		titem.setData(commandsViewPage);
 		titem.setControl(commandsViewPage.createTabFolderPage(_folder, _viewPart.getEditorActionHandler()));
 		_folder.setSelection(titem );
diff --git a/rse/plugins/org.eclipse.rse.shells.ui/src/org/eclipse/rse/internal/shells/ui/view/SystemCommandsViewPart.java b/rse/plugins/org.eclipse.rse.shells.ui/src/org/eclipse/rse/internal/shells/ui/view/SystemCommandsViewPart.java
index ca259a9..826db3e 100644
--- a/rse/plugins/org.eclipse.rse.shells.ui/src/org/eclipse/rse/internal/shells/ui/view/SystemCommandsViewPart.java
+++ b/rse/plugins/org.eclipse.rse.shells.ui/src/org/eclipse/rse/internal/shells/ui/view/SystemCommandsViewPart.java
@@ -1,5 +1,5 @@
 /********************************************************************************
- * Copyright (c) 2002, 2011 IBM Corporation and others. All rights reserved.
+ * Copyright (c) 2002, 2014 IBM Corporation and others. 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
@@ -21,6 +21,7 @@
  * Radoslav Gerganov (ProSyst) - [181563] Fix hardcoded Ctrl+Space for remote shell content assist
  * David McKnight   (IBM)        - [294398] [shells] SystemCommandsViewPart always assumes systemResourceChanged() called on Display thread
  * David McKnight   (IBM)        - [351750] [shells] need to check for disposed widget when handling events
+ * David McKnight (IBM)  -[431378] [shells] Remote shells not always restored properly on reconnect
  ********************************************************************************/
 
 package org.eclipse.rse.internal.shells.ui.view;
@@ -726,13 +727,10 @@
 	}
 
 	protected void restoreCommandShells(IRemoteCmdSubSystem cmdSS)
-	{
-	   
-		try
-		{
+	{	   
+		try {
 			IRemoteCommandShell[] cmds = cmdSS.getShells();
-			if (cmds == null || cmds.length == 0)
-			{
+			if (cmds == null || cmds.length == 0){
 				cmds = cmdSS.restoreShellState(getShell());
 				if (cmds!=null)
 				{
@@ -743,11 +741,8 @@
 				}
 			}
 		}
-		catch (Exception e)
-		{
+		catch (Exception e){
 		}
-			
-		
 	}
 	
 	
@@ -778,10 +773,8 @@
 								updateActionStates();
 							}
 						}					
-					});
+					});	
 				}
-				
-			
 			}
 		} 
 		
diff --git a/rse/plugins/org.eclipse.rse.shells.ui/src/org/eclipse/rse/shells/ui/view/SystemViewRemoteOutputAdapter.java b/rse/plugins/org.eclipse.rse.shells.ui/src/org/eclipse/rse/shells/ui/view/SystemViewRemoteOutputAdapter.java
index 3970ca6..d2c1b2f 100644
--- a/rse/plugins/org.eclipse.rse.shells.ui/src/org/eclipse/rse/shells/ui/view/SystemViewRemoteOutputAdapter.java
+++ b/rse/plugins/org.eclipse.rse.shells.ui/src/org/eclipse/rse/shells/ui/view/SystemViewRemoteOutputAdapter.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2002, 2013 IBM Corporation and others.
+ * Copyright (c) 2002, 2014 IBM Corporation and others.
  * 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
@@ -32,6 +32,7 @@
  * Martin Oberhuber (Wind River) - [227135] Cryptic exception when sftp-server is missing
  * David McKnight   (IBM)        - [336640] SystemViewRemoteOutputAdapter should not use hard-coded editor id
  * David McKnight   (IBM)        - [404310] [shells][performance] view adapter should not query file in getSubSystem()
+ * David McKnight (IBM)  -[431378] [shells] Remote shells not always restored properly on reconnect
  *******************************************************************************/
 
 package org.eclipse.rse.shells.ui.view;
@@ -825,11 +826,21 @@
 			ImageDescriptor imageDescriptor = null;
 			if (command.isActive())
 			{
-				imageDescriptor = factoryAdapter.getActiveCommandShellImageDescriptor();
+				if (factoryAdapter == null){ // handle case where adapter not loaded yet
+					imageDescriptor = ShellsUIPlugin.getDefault().getImageDescriptor(ShellsUIPlugin.ICON_SYSTEM_SHELLLIVE_ID);
+				}
+				else {
+					imageDescriptor = factoryAdapter.getActiveCommandShellImageDescriptor();
+				}
 			}
 			else
 			{
-			    imageDescriptor = factoryAdapter.getInactiveCommandShellImageDescriptor();
+				if (factoryAdapter == null){  // handle case where adapter not loaded yet
+					imageDescriptor = ShellsUIPlugin.getDefault().getImageDescriptor(ShellsUIPlugin.ICON_SYSTEM_SHELL_ID);
+				}
+				else {
+					imageDescriptor = factoryAdapter.getInactiveCommandShellImageDescriptor();
+				}
 			}
 			return imageDescriptor;
 		}
diff --git a/rse/plugins/org.eclipse.rse.subsystems.shells.core/src/org/eclipse/rse/subsystems/shells/core/subsystems/RemoteCmdSubSystem.java b/rse/plugins/org.eclipse.rse.subsystems.shells.core/src/org/eclipse/rse/subsystems/shells/core/subsystems/RemoteCmdSubSystem.java
index 48416db..fb599fe 100644
--- a/rse/plugins/org.eclipse.rse.subsystems.shells.core/src/org/eclipse/rse/subsystems/shells/core/subsystems/RemoteCmdSubSystem.java
+++ b/rse/plugins/org.eclipse.rse.subsystems.shells.core/src/org/eclipse/rse/subsystems/shells/core/subsystems/RemoteCmdSubSystem.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2002, 2011 IBM Corporation and others.
+ * Copyright (c) 2002, 2014 IBM Corporation and others.
  * 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
@@ -25,6 +25,7 @@
  * David McKnight     (IBM)   [302724] problems with environment variable substitution
  * David McKnight   (IBM)        - [338031] Remote Shell view tabs should have close (x) icon
  * David McKnight   (IBM)        - [349491] possible NPE on shutdown due to event firing
+ * David McKnight (IBM)  -[431378] [shells] Remote shells not always restored properly on reconnect
  *******************************************************************************/
 
 package org.eclipse.rse.subsystems.shells.core.subsystems;
@@ -79,6 +80,7 @@
 	protected IRemoteCommandShell _defaultShell;
 
 	protected IRemoteFileSubSystem _fileSubSystem;
+	private boolean _hasRestoredState = false;
 
 	public RemoteCmdSubSystem(IHost host, IConnectorService connectorService)
 	{
@@ -88,10 +90,11 @@
 
 	public void initializeSubSystem(IProgressMonitor monitor) throws SystemMessageException {
 		super.initializeSubSystem(monitor);
+		_hasRestoredState = false; // reset so shells are restored after this connect
 		// load UI plugin for adapters right after successful connect
 		Platform.getAdapterManager().loadAdapter(new RemoteOutput(null, ""), "org.eclipse.rse.ui.view.ISystemViewElementAdapter"); //$NON-NLS-1$ //$NON-NLS-2$
 	}
-
+	
 	/**
 	 * Return parent subsystem factory, cast to a RemoteCmdSubSystemConfiguration
 	 */
@@ -564,7 +567,6 @@
 	{
 		// DKM: changing this so that only first active shell is saved
 		StringBuffer shellBuffer = new StringBuffer();
-		boolean gotShell = false;
 		for (int i = 0; i < cmdShells.size() /*&& !gotShell*/; i++)
 		{
 		
@@ -573,18 +575,16 @@
 			}
 
 			IRemoteCommandShell cmd = (IRemoteCommandShell) cmdShells.get(i);
-			if (cmd.isActive())
+			if (cmd != null && cmd.isActive())
 			{
 				Object context = cmd.getContextString();
 				if (context instanceof String)
 				{
 					shellBuffer.append(context);
-					gotShell = true;
 				}
 				else
 				{
 					shellBuffer.append(cmd.getType());
-					gotShell = true;
 				}
 			}
 		}
@@ -624,14 +624,15 @@
 			_cmdShells.remove(command);
 			Display.getDefault().asyncExec(new RefreshRemovedShell(this, cmdShell));
 		}
-
-
-
 	}
 
+
 	// called to restore running shells - behaviour determined by UI
 	public IRemoteCommandShell[] restoreShellState(Shell shellWindow)
 	{
+		if (_hasRestoredState){
+			return null; // already did this, don't do it again! Returning null just means shells view won't restore again
+		}
 		this.shell = shellWindow;
 		IRemoteCommandShell[] results = null;
 
@@ -665,11 +666,11 @@
 				}
 			}
 		}
-
-		//ISystemRegistry registry = RSECorePlugin.getTheSystemRegistry();
-//		registry.fireEvent(new SystemResourceChangeEvent(this, ISystemResourceChangeEvents.EVENT_REFRESH, this));
-
-		Display.getDefault().asyncExec(new Refresh(this));
+		
+		if (numShells > 0){
+			Display.getDefault().asyncExec(new Refresh(this));
+		}
+		_hasRestoredState = true;
 		return results;
 	}