connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/settings/ui/ConnectionSettingsPage.java
changeset 2 d760517a8095
child 44 e4eb00aa1a3f
equal deleted inserted replaced
-1:000000000000 2:d760517a8095
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.carbide.remoteconnections.settings.ui;
       
    20 
       
    21 import com.nokia.carbide.remoteconnections.Messages;
       
    22 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
       
    23 import com.nokia.carbide.remoteconnections.interfaces.*;
       
    24 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
       
    25 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener;
       
    26 import com.nokia.carbide.remoteconnections.interfaces.IConnectionFactory.IValidationErrorReporter;
       
    27 import com.nokia.carbide.remoteconnections.interfaces.IRemoteAgentInstallerProvider.IRemoteAgentInstaller;
       
    28 import com.nokia.carbide.remoteconnections.interfaces.IRemoteAgentInstallerProvider.IRemoteAgentInstaller.IPackageContents;
       
    29 import com.nokia.cpp.internal.api.utils.core.*;
       
    30 
       
    31 import org.eclipse.core.runtime.IPath;
       
    32 import org.eclipse.core.runtime.Path;
       
    33 import org.eclipse.jface.dialogs.IDialogConstants;
       
    34 import org.eclipse.jface.viewers.*;
       
    35 import org.eclipse.jface.wizard.IWizardContainer2;
       
    36 import org.eclipse.jface.wizard.WizardPage;
       
    37 import org.eclipse.swt.SWT;
       
    38 import org.eclipse.swt.custom.CLabel;
       
    39 import org.eclipse.swt.custom.SashForm;
       
    40 import org.eclipse.swt.events.*;
       
    41 import org.eclipse.swt.graphics.Image;
       
    42 import org.eclipse.swt.graphics.Point;
       
    43 import org.eclipse.swt.layout.GridData;
       
    44 import org.eclipse.swt.layout.GridLayout;
       
    45 import org.eclipse.swt.program.Program;
       
    46 import org.eclipse.swt.widgets.*;
       
    47 import org.eclipse.ui.ISharedImages;
       
    48 import org.eclipse.ui.PlatformUI;
       
    49 import org.osgi.framework.Version;
       
    50 
       
    51 import java.io.File;
       
    52 import java.io.InputStream;
       
    53 import java.text.MessageFormat;
       
    54 import java.util.*;
       
    55 import java.util.List;
       
    56 
       
    57 public class ConnectionSettingsPage extends WizardPage {
       
    58 
       
    59 	private final static TreeNode LOADING_CONTENT_TREENODE = 
       
    60 		new TreeNode(Messages.getString("ConnectionSettingsPage.GettingDataMessage")); //$NON-NLS-1$
       
    61 	private static final String STATUS_NOT_TESTED = 
       
    62 		Messages.getString("ConnectionSettingsPage.NotTestedStatusString"); //$NON-NLS-1$
       
    63 	private final static Image FOLDER_ICON_IMG = 
       
    64 		PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
       
    65 				ISharedImages.IMG_OBJ_FOLDER).createImage();
       
    66 	private static final String UID = ".uid"; //$NON-NLS-1$
       
    67 	private final SettingsWizard settingsWizard;
       
    68 	private IConnectionType connectionType;
       
    69 	private ComboViewer deviceOSComboViewer;
       
    70 	private Group settingsGroup;
       
    71 	private Composite agentTestTabComposite;
       
    72 	private ListViewer servicesListViewer;
       
    73 	private Text serviceTestInfo;
       
    74 	private Button serviceTestButton;
       
    75 	private boolean isTesting;
       
    76 	private Label statusLabel;
       
    77 	private Text statusText;
       
    78 	private IConnectionFactory connectionFactory;
       
    79 	private IConnection connection;
       
    80 	private IService service;
       
    81 	private IConnectedService connectedService;
       
    82 	private IStatusChangedListener statusListener;
       
    83 	private SashForm installerSashForm;
       
    84 	private TreeViewer installerTreeViewer;
       
    85 	private Text installerInfoText;
       
    86 	private Button installerSaveButton;
       
    87 	private Button installButton;
       
    88 	private String saveAsParent;
       
    89 	private List<IRemoteAgentInstallerProvider> installerProviders;
       
    90 
       
    91 	protected ConnectionSettingsPage(SettingsWizard settingsWizard) {
       
    92 		super("settingspage"); //$NON-NLS-1$
       
    93 		this.settingsWizard = settingsWizard;
       
    94 	}
       
    95 
       
    96 	/* (non-Javadoc)
       
    97 	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
       
    98 	 */
       
    99 	public void createControl(Composite parent) {
       
   100 		final TabFolder tabFolder = new TabFolder(parent, SWT.NONE);
       
   101 		tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
       
   102 		tabFolder.setData(UID, "ConnectionSettingsPage"); //$NON-NLS-1$
       
   103 		
       
   104 		createAgentTestTabComposite(tabFolder);
       
   105 		createInstallTabComposite(tabFolder);
       
   106 		
       
   107 		RemoteConnectionsActivator.setHelp(tabFolder, ".connection_settings_page"); //$NON-NLS-1$
       
   108 		
       
   109 		setControl(tabFolder);
       
   110 	}
       
   111 	
       
   112 	private void createAgentTestTabComposite(TabFolder tabFolder) {
       
   113 		TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
       
   114 		tabItem.setText(Messages.getString("ConnectionSettingsPage.AgentTestTabLabel")); //$NON-NLS-1$
       
   115 		tabItem.setData(UID, "testTab"); //$NON-NLS-1$
       
   116 		agentTestTabComposite = new Composite(tabFolder, SWT.NONE);
       
   117 		GridLayout gridLayout = new GridLayout();
       
   118 		gridLayout.numColumns = 2;
       
   119 		agentTestTabComposite.setLayout(gridLayout);
       
   120 		tabItem.setControl(agentTestTabComposite);
       
   121 
       
   122 		createSettingsGroup(agentTestTabComposite);
       
   123 		
       
   124 		createDeviceOSCombo(agentTestTabComposite);
       
   125 
       
   126 		createServiceTestComposite(agentTestTabComposite);
       
   127 	}
       
   128 
       
   129 	private void createDeviceOSCombo(Composite parent) {
       
   130 		Composite comboComposite = new Composite(parent, SWT.NONE);
       
   131 		GridData gd_composite = new GridData(SWT.FILL, SWT.CENTER, true, false);
       
   132 		gd_composite.horizontalSpan = 2;
       
   133 		comboComposite.setLayoutData(gd_composite);
       
   134 		GridLayout gridLayout = new GridLayout();
       
   135 		gridLayout.numColumns = 2;
       
   136 		comboComposite.setLayout(gridLayout);
       
   137 
       
   138 		Label deviceOSLabel = new Label(comboComposite, SWT.NONE);
       
   139 		GridData gd_sdkLabel = new GridData();
       
   140 		deviceOSLabel.setLayoutData(gd_sdkLabel);
       
   141 		deviceOSLabel.setText(Messages.getString("ConnectionSettingsPage.DeviceOSLabel")); //$NON-NLS-1$
       
   142 		
       
   143 		deviceOSComboViewer = new ComboViewer(comboComposite, SWT.READ_ONLY);
       
   144 		GridData gd_sdkcombo = new GridData(SWT.LEFT, SWT.CENTER, true, false);
       
   145 		gd_sdkcombo.widthHint = 150;
       
   146 		deviceOSComboViewer.getCombo().setLayoutData(gd_sdkcombo);
       
   147 		deviceOSComboViewer.addSelectionChangedListener(new ISelectionChangedListener() {
       
   148 			public void selectionChanged(SelectionChangedEvent event) {
       
   149 				IStructuredSelection selection = (IStructuredSelection) deviceOSComboViewer.getSelection();
       
   150 				Pair<String, Version> pair = (Pair<String, Version>) selection.getFirstElement();
       
   151 				setSelectionToInstallComposite(pair);
       
   152 				if (connectedService != null)
       
   153 					connectedService.setDeviceOS(pair.first, pair.second);
       
   154 			}
       
   155 		});
       
   156 		deviceOSComboViewer.setContentProvider(new ArrayContentProvider());
       
   157 		deviceOSComboViewer.setLabelProvider(new LabelProvider() {
       
   158 			@Override
       
   159 			public String getText(Object element) {
       
   160 				Check.checkState(element instanceof Pair);
       
   161 				Pair pair = (Pair) element;
       
   162 				return MessageFormat.format("{0} {1}", pair.first, pair.second); //$NON-NLS-1$
       
   163 			}
       
   164 		});
       
   165 		deviceOSComboViewer.getControl().setToolTipText(Messages.getString("ConnectionSettingsPage.DeviceOSComboToolTip")); //$NON-NLS-1$
       
   166 		deviceOSComboViewer.getControl().setData(UID, "deviceOSComboViewer"); //$NON-NLS-1$
       
   167 	}
       
   168 	
       
   169 	private void createSettingsGroup(Composite parent) {
       
   170 		settingsGroup = new Group(parent, SWT.NONE);
       
   171 		GridLayout settingsLayout = new GridLayout();
       
   172 		settingsGroup.setLayout(settingsLayout);
       
   173 		GridData gd_settings = new GridData(GridData.FILL_HORIZONTAL);
       
   174 		gd_settings.horizontalSpan = 2;
       
   175 		settingsGroup.setLayoutData(gd_settings);
       
   176 		settingsGroup.setText(Messages.getString("ConnectionSettingsPage.ConnectionSettingsGroupLabel")); //$NON-NLS-1$
       
   177 	}
       
   178 
       
   179 	private void createServiceTestComposite(Composite parent) {
       
   180 		Composite serviceSelectionComposite = new Composite(parent, SWT.NONE);
       
   181 		serviceSelectionComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
       
   182 		serviceSelectionComposite.setLayout(new GridLayout());
       
   183 		Label label = new Label(serviceSelectionComposite, SWT.NONE);
       
   184 		label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
   185 		label.setText(Messages.getString("ConnectionSettingsPage.ServicesListLabel")); //$NON-NLS-1$
       
   186 		servicesListViewer = new ListViewer(serviceSelectionComposite, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
       
   187 		GridData gd_viewer = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
       
   188 		servicesListViewer.getControl().setLayoutData(gd_viewer);
       
   189 		servicesListViewer.setContentProvider(new ArrayContentProvider());
       
   190 		servicesListViewer.setLabelProvider(new LabelProvider() {
       
   191 			@Override
       
   192 			public String getText(Object element) {
       
   193 				if (element instanceof IService)
       
   194 					return ((IService) element).getDisplayName();
       
   195 				return null;
       
   196 			}
       
   197 		});
       
   198 		servicesListViewer.addSelectionChangedListener(new ISelectionChangedListener() {
       
   199 			public void selectionChanged(SelectionChangedEvent event) {
       
   200 				IStructuredSelection selection = (IStructuredSelection) servicesListViewer.getSelection();
       
   201 				IService curService = (IService) selection.getFirstElement();
       
   202 				if (!curService.equals(service)) {
       
   203 					service = curService;
       
   204 					resetServiceTesting();
       
   205 				}
       
   206 			}
       
   207 		});
       
   208 		servicesListViewer.getControl().setToolTipText(Messages.getString("ConnectionSettingsPage.ServicesListToolTip")); //$NON-NLS-1$
       
   209 		servicesListViewer.getControl().setData(UID, "servicesListViewer"); //$NON-NLS-1$
       
   210 		
       
   211 		Composite testButtonComposite = new Composite(parent, SWT.NONE);
       
   212 		testButtonComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
       
   213 		testButtonComposite.setLayout(new GridLayout());
       
   214 		serviceTestInfo = new Text(testButtonComposite, SWT.READ_ONLY | SWT.WRAP);
       
   215 		serviceTestButton = new Button(testButtonComposite, SWT.PUSH);
       
   216 		GridData gd_button = new GridData(SWT.CENTER, SWT.CENTER, false, false);
       
   217 		int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
       
   218 		serviceTestButton.setText(Messages.getString("ConnectionSettingsPage.StartServiceTestButtonLabel")); //$NON-NLS-1$
       
   219 		Point minSize = serviceTestButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
       
   220 		gd_button.widthHint = Math.max(widthHint, minSize.x);
       
   221 		GridData gd_text = new GridData(SWT.CENTER, SWT.FILL, true, true);
       
   222 		gd_text.widthHint = gd_button.widthHint;
       
   223 		serviceTestInfo.setLayoutData(gd_text);
       
   224 		serviceTestInfo.setData(UID, "serviceTestInfo"); //$NON-NLS-1$
       
   225 		serviceTestButton.setLayoutData(gd_button);
       
   226 		serviceTestButton.setToolTipText(Messages.getString("ConnectionSettingsPage.ServiceTestButtonToolTip")); //$NON-NLS-1$
       
   227 		serviceTestButton.setData(UID, "serviceTestButton"); //$NON-NLS-1$
       
   228 		serviceTestButton.addSelectionListener(new SelectionAdapter() {
       
   229 			@Override
       
   230 			public void widgetSelected(SelectionEvent e) {
       
   231 				if (isTesting) {
       
   232 					serviceTestButton.setText(Messages.getString("ConnectionSettingsPage.StartServiceTestButtonLabel")); //$NON-NLS-1$
       
   233 					resetServiceTesting();
       
   234 				}
       
   235 				else {
       
   236 					serviceTestButton.setText(Messages.getString("ConnectionSettingsPage.StopServiceTestButtonLabel")); //$NON-NLS-1$
       
   237 					testService();
       
   238 				}
       
   239 			}
       
   240 		});
       
   241 		
       
   242 		Composite statusComposite = new Composite(parent, SWT.NONE);
       
   243 		statusComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
       
   244 		statusComposite.setLayout(new GridLayout());
       
   245 		statusLabel = new Label(statusComposite, SWT.NONE);
       
   246 		statusLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
       
   247 		statusLabel.setText(Messages.getString("ConnectionSettingsPage.StatusLabel")); //$NON-NLS-1$
       
   248 		statusText = new Text(statusComposite, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER | SWT.WRAP);
       
   249 		statusText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
       
   250 		statusText.setText(STATUS_NOT_TESTED);
       
   251 		statusText.setData(UID, "statusText"); //$NON-NLS-1$
       
   252 	}
       
   253 
       
   254 	private void createInstallTabComposite(TabFolder tabFolder) {
       
   255 		TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
       
   256 		tabItem.setText(Messages.getString("ConnectionSettingsPage.InstallTabLabel")); //$NON-NLS-1$
       
   257 		tabItem.setData(UID, "installTab"); //$NON-NLS-1$
       
   258 		Composite composite = new Composite(tabFolder, SWT.NONE);
       
   259 		GridLayout gridLayout = new GridLayout();
       
   260 		composite.setLayout(gridLayout);
       
   261 		tabItem.setControl(composite);
       
   262 		
       
   263 		Composite installDebugAgentComposite = new Composite(tabFolder, SWT.NONE);
       
   264 		installDebugAgentComposite.setLayout(new GridLayout(1, false));
       
   265 		tabItem.setControl(installDebugAgentComposite);
       
   266 
       
   267 		installerSashForm = new SashForm(installDebugAgentComposite, SWT.HORIZONTAL);
       
   268 		GridData gd_sash = new GridData(SWT.FILL, SWT.FILL, true, true);
       
   269 		gd_sash.widthHint = 420;
       
   270 		gd_sash.heightHint = 280;
       
   271 		installerSashForm.setLayoutData(gd_sash);
       
   272 
       
   273 		installerTreeViewer = new TreeViewer(installerSashForm, SWT.BORDER);
       
   274 		GridData gd_tree = new GridData(SWT.CENTER, SWT.CENTER, false, false);
       
   275 		installerTreeViewer.getTree().setLayoutData(gd_tree);
       
   276 		installerTreeViewer.getControl().setData(UID, "installerTreeViewer"); //$NON-NLS-1$
       
   277 		installerTreeViewer.setContentProvider(new TreeNodeContentProvider());
       
   278 		installerTreeViewer.setLabelProvider(new LabelProvider() {
       
   279 			@Override
       
   280 			public String getText(Object element) {
       
   281 				TreeNode node = (TreeNode) element;
       
   282 				Object value = node.getValue();
       
   283 				if (value instanceof IRemoteAgentInstaller) {
       
   284 					String label = ((IRemoteAgentInstaller) value).getLabel();
       
   285 					return label == null ? Messages.getString("ConnectionSettingsPage.UnlabeledPackageLabel") : label; //$NON-NLS-1$
       
   286 				}
       
   287 					
       
   288 				return value.toString();
       
   289 			}
       
   290 			
       
   291 			@Override
       
   292 			public Image getImage(Object element) {
       
   293 				if (element.equals(LOADING_CONTENT_TREENODE))
       
   294 					return null;
       
   295 				
       
   296 				TreeNode node = (TreeNode) element;
       
   297 				Object value = node.getValue();
       
   298 				if (value instanceof IRemoteAgentInstaller)
       
   299 					return ((IRemoteAgentInstaller) value).getImage();
       
   300 				
       
   301 				return FOLDER_ICON_IMG;
       
   302 			}
       
   303 		});
       
   304 		installerTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
       
   305 			public void selectionChanged(SelectionChangedEvent event) {
       
   306 				TreeNode node = (TreeNode) ((StructuredSelection) event.getSelection()).getFirstElement();
       
   307 				if (node == null)
       
   308 					return;
       
   309 				Object value = node.getValue();
       
   310 				boolean isPackage = value instanceof IRemoteAgentInstaller;
       
   311 				boolean installable = false;
       
   312 				String information = null;
       
   313 				if (isPackage) {
       
   314 					IRemoteAgentInstaller installer = (IRemoteAgentInstaller) value;
       
   315 					installable = installer.fileSupportsInstall();
       
   316 					information = installer.getInformation();
       
   317 				}
       
   318 				if (information != null)
       
   319 					installerInfoText.setText(information);
       
   320 				else
       
   321 					installerInfoText.setText(""); //$NON-NLS-1$
       
   322 				installButton.setEnabled(isPackage && installable);
       
   323 				installerSaveButton.setEnabled(isPackage);
       
   324 			}
       
   325 		});
       
   326 
       
   327 		installerInfoText = new Text(installerSashForm, SWT.READ_ONLY | SWT.BORDER | SWT.WRAP);
       
   328 		String errorText = Messages.getString("ConnectionSettingsPage.NoInstallerDataInfoString"); //$NON-NLS-1$
       
   329 		errorText += "\n" + Messages.getString("ConnectionSettingsPage.NoInstallerDataInfoString2"); //$NON-NLS-1$ //$NON-NLS-2$
       
   330 		installerInfoText.setText(errorText);
       
   331 		installerInfoText.setData(UID, "installerInfoText"); //$NON-NLS-1$
       
   332 		installerSashForm.setWeights(new int[] {160, 100 });
       
   333 
       
   334 		Composite buttonsArea = new Composite(installDebugAgentComposite, SWT.NONE);
       
   335 		buttonsArea.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
       
   336 		gridLayout = new GridLayout();
       
   337 		gridLayout.numColumns = 2;
       
   338 		buttonsArea.setLayout(gridLayout);
       
   339 
       
   340 		installerSaveButton = new Button(buttonsArea, SWT.NONE);
       
   341 		final GridData gd_saveButton = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
       
   342 		gd_saveButton.widthHint = 125;
       
   343 		installerSaveButton.setLayoutData(gd_saveButton);
       
   344 		installerSaveButton.setText(Messages.getString("ConnectionSettingsPage.SaveButtonLabel")); //$NON-NLS-1$
       
   345 		installerSaveButton.setEnabled(false);
       
   346 		installerSaveButton.setToolTipText(Messages.getString("ConnectionSettingsPage.SaveButtonToolTip")); //$NON-NLS-1$
       
   347 		installerSaveButton.setData(UID, "installerSaveButton"); //$NON-NLS-1$
       
   348 		installerSaveButton.addSelectionListener(new SelectionAdapter() {
       
   349 			public void widgetSelected(SelectionEvent event) {
       
   350 				Display.getDefault().syncExec(new Runnable() {
       
   351 					public void run() {
       
   352 						try {
       
   353 							IRemoteAgentInstaller installer = getCurrentInstaller();
       
   354 							saveAs(installer.getPackageContents(getContainer()));
       
   355 						}
       
   356 						catch (Exception e) {
       
   357 							RemoteConnectionsActivator.logError(e);
       
   358 						}
       
   359 					}
       
   360 				});
       
   361 			}
       
   362 		});
       
   363 
       
   364 		installButton = new Button(buttonsArea, SWT.NONE);
       
   365 		final GridData gd_installButton = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
       
   366 		gd_installButton.widthHint = 125;
       
   367 		installButton.setLayoutData(gd_installButton);
       
   368 		installButton.setText(Messages.getString("ConnectionSettingsPage.InstallButtonLabel")); //$NON-NLS-1$
       
   369 		installButton.setEnabled(false);
       
   370 		installButton.setToolTipText(Messages.getString("ConnectionSettingsPage.InstallButtonToolTip")); //$NON-NLS-1$
       
   371 		installButton.setData(UID, "installButton"); //$NON-NLS-1$
       
   372 		installButton.addSelectionListener(new SelectionAdapter() {
       
   373 			public void widgetSelected(SelectionEvent event) {
       
   374 				Display.getDefault().syncExec(new Runnable() {
       
   375 					public void run() {
       
   376 						try {
       
   377 							IRemoteAgentInstaller installer = getCurrentInstaller();
       
   378 							attemptInstall(installer.getPackageContents(getContainer()));
       
   379 						}
       
   380 						catch (Exception e) {
       
   381 							RemoteConnectionsActivator.logError(e);
       
   382 						}
       
   383 					}
       
   384 				});
       
   385 			}
       
   386 		});
       
   387 	}
       
   388 
       
   389 	
       
   390 	public void setVisible(boolean visible) {
       
   391 		super.setVisible(visible);
       
   392 		if (visible)
       
   393 			updateDynamicUI();
       
   394 	}
       
   395 
       
   396 	public void updateDynamicUI() {
       
   397 		IConnectionType currentConnectionType = settingsWizard.getConnectionType();
       
   398 		if (currentConnectionType != null && !currentConnectionType.equals(connectionType)) {
       
   399 			setErrorMessage(null);
       
   400 			setPageComplete(true);
       
   401 			String buttonLabel = Messages.getString("ConnectionSettingsPage.StartServiceTestButtonLabel"); //$NON-NLS-1$
       
   402 			serviceTestButton.setText(buttonLabel);
       
   403 			connectionType = currentConnectionType;
       
   404 			setTitle(MessageFormat.format(Messages.getString("ConnectionSettingsPage.PageTitleFmt"), connectionType.getDisplayName())); //$NON-NLS-1$
       
   405 			setDescription(connectionType.getDescription());
       
   406 			
       
   407 			// update settings editing UI
       
   408 			Control[] settingsUI = settingsGroup.getChildren();
       
   409 			if (settingsUI.length > 0) {
       
   410 				for (Control control : settingsUI) {
       
   411 					control.dispose();
       
   412 				}
       
   413 			}
       
   414 			connectionFactory = connectionType.getConnectionFactory();
       
   415 			Map<String, String> initialSettings = null;
       
   416 			IConnection connectionToEdit = settingsWizard.getConnectionToEdit();
       
   417 			if (connectionToEdit != null && connectionToEdit.getConnectionType().equals(connectionType)) // show existing settings
       
   418 				initialSettings = connectionToEdit.getSettings();
       
   419 			connectionFactory.createEditingUI(settingsGroup, new IValidationErrorReporter() {
       
   420 				public void setErrorMessage(String newMessage) {
       
   421 					ConnectionSettingsPage.this.setErrorMessage(newMessage);
       
   422 					boolean noError = newMessage == null;
       
   423 					setPageComplete(noError);
       
   424 					String buttonLabel = Messages.getString("ConnectionSettingsPage.StartServiceTestButtonLabel"); //$NON-NLS-1$
       
   425 					serviceTestButton.setText(buttonLabel);
       
   426 				}
       
   427 			}, initialSettings);
       
   428 			settingsUI = settingsGroup.getChildren();
       
   429 			if (settingsUI.length == 0) {
       
   430 				CLabel label = new CLabel(settingsGroup, SWT.NONE);
       
   431 				label.setText(MessageFormat.format(Messages.getString("ConnectionSettingsPage.NoSettingsString"), connectionType.getDisplayName())); //$NON-NLS-1$
       
   432 				GridData gd = new GridData(SWT.LEFT, SWT.TOP, true, true);
       
   433 				label.setLayoutData(gd);
       
   434 			}
       
   435 	
       
   436 			// update services list
       
   437 			Collection<IService> compatibleServices = 
       
   438 				RemoteConnectionsActivator.getConnectionTypeProvider().getCompatibleServices(connectionType);
       
   439 			servicesListViewer.setInput(compatibleServices);
       
   440 			if (!compatibleServices.isEmpty()) {
       
   441 				servicesListViewer.getList().select(0);
       
   442 				servicesListViewer.setSelection(servicesListViewer.getSelection());
       
   443 			}
       
   444 			servicesListViewer.getList().addFocusListener(new FocusAdapter() {
       
   445 				public void focusGained(FocusEvent e) {
       
   446 					resetServiceTesting();
       
   447 				}
       
   448 			});
       
   449 			
       
   450 			Thread t = new Thread() {
       
   451 				@Override
       
   452 				public void run() {
       
   453 					initializeInstallerData();
       
   454 				}
       
   455 			};
       
   456 			t.start();
       
   457 		}
       
   458 		
       
   459 		if (getControl().isVisible())
       
   460 			agentTestTabComposite.layout(true, true);
       
   461 		
       
   462 		if (getControl().isVisible())
       
   463 			((IWizardContainer2) getWizard().getContainer()).updateSize();
       
   464 	}
       
   465 
       
   466 	private synchronized void initializeInstallerData() {
       
   467 		Display.getDefault().asyncExec(new Runnable() {
       
   468 			public void run() {
       
   469 				installerTreeViewer.setInput(new TreeNode[] { LOADING_CONTENT_TREENODE });
       
   470 				installerInfoText.setText(""); //$NON-NLS-1$
       
   471 				deviceOSComboViewer.setInput(Collections.EMPTY_LIST);
       
   472 			}
       
   473 		});
       
   474 
       
   475 		IConnectionTypeProvider connectionTypeProvider = RemoteConnectionsActivator.getConnectionTypeProvider();
       
   476 		Collection<IService> services = connectionTypeProvider.getCompatibleServices(connectionType);
       
   477 		getInstallerProviders(services);
       
   478 		final TreeNode[] treeNodes = createTreeNodes(); // gets actual data from server
       
   479 		
       
   480 		Display.getDefault().asyncExec(new Runnable() {
       
   481 			public void run() {
       
   482 				// update installer tree
       
   483 				installerTreeViewer.setInput(treeNodes);
       
   484 				installerTreeViewer.refresh(true);
       
   485 				installerTreeViewer.expandAll();
       
   486 				
       
   487 				if (treeNodes.length == 0) {
       
   488 					String errorText = Messages.getString("ConnectionSettingsPage.NoInstallerDataInfoString"); //$NON-NLS-1$
       
   489 					errorText += "\n" + Messages.getString("ConnectionSettingsPage.NoInstallerDataInfoString2"); //$NON-NLS-1$ //$NON-NLS-2$
       
   490 					installerInfoText.setText(errorText);
       
   491 				}
       
   492 				
       
   493 				// update sdk combo
       
   494 				List<Pair<String, Version>> deviceOSPairs = createDeviceOSPairs();
       
   495 				deviceOSComboViewer.setInput(deviceOSPairs);
       
   496 				if (!deviceOSPairs.isEmpty()) {
       
   497 					deviceOSComboViewer.getCombo().select(0);
       
   498 					deviceOSComboViewer.setSelection(deviceOSComboViewer.getSelection());		
       
   499 				}
       
   500 				else {
       
   501 					String errorLabel = Messages.getString("ConnectionSettingsPage.NoInstallerDataInfoString"); //$NON-NLS-1$
       
   502 					deviceOSComboViewer.getCombo().setItems(new String[] {errorLabel});
       
   503 					deviceOSComboViewer.getCombo().select(0);
       
   504 					deviceOSComboViewer.getCombo().setEnabled(false);
       
   505 				}
       
   506 				deviceOSComboViewer.getCombo().addFocusListener(new FocusAdapter() {
       
   507 					public void focusGained(FocusEvent e) {
       
   508 						resetServiceTesting();
       
   509 					}
       
   510 				});
       
   511 			}
       
   512 		});
       
   513 
       
   514 	}
       
   515 
       
   516 	protected void setSelectionToInstallComposite(Pair<String, Version> pair) {
       
   517 		Object input = installerTreeViewer.getInput();
       
   518 		if (input instanceof TreeNode[]) {
       
   519 			TreeNode node = findTreeNodeForPair((TreeNode[]) input, pair);
       
   520 			if (node != null) {
       
   521 				installerTreeViewer.setSelection(new StructuredSelection(node));
       
   522 			}
       
   523 		}
       
   524 	}
       
   525 	
       
   526 	private TreeNode findTreeNodeForPair(TreeNode[] treeNodes, Pair<String, Version> pair) {
       
   527 		for (TreeNode treeNode : treeNodes) {
       
   528 			Object value = treeNode.getValue();
       
   529 			if (value instanceof IRemoteAgentInstaller) {
       
   530 				TreeNode versionNode = treeNode.getParent();
       
   531 				TreeNode familyNode = versionNode.getParent();
       
   532 				if (pair.equals(new Pair(familyNode.getValue(), versionNode.getValue())))
       
   533 					return treeNode;
       
   534 			}
       
   535 			TreeNode[] children = treeNode.getChildren();
       
   536 			if (children != null) {
       
   537 				TreeNode treeNodeFromChildren = findTreeNodeForPair(children, pair);
       
   538 				if (treeNodeFromChildren != null)
       
   539 					return treeNodeFromChildren;
       
   540 			}
       
   541 		}
       
   542 		return null;
       
   543 	}
       
   544 
       
   545 	protected void testService() {
       
   546 		Map<String, String> settings = connectionFactory.getSettingsFromUI();
       
   547 		boolean newConnection = connection == null || !connectionType.equals(connection.getConnectionType());
       
   548 		if (newConnection) {
       
   549 			if (connection != null)
       
   550 				connection.dispose();
       
   551 			connection = connectionFactory.createConnection(settings);
       
   552 			connection.setDisplayName("TestConnection:"+connectionType.getDisplayName()); //$NON-NLS-1$
       
   553 		}
       
   554 		else {
       
   555 			connection.updateSettings(settings);
       
   556 		}
       
   557 		if (newConnection || connectedService == null || !connectedService.getService().equals(service)) {
       
   558 			disposeConnectedService();
       
   559 			connectedService = 
       
   560 				RemoteConnectionsActivator.getConnectionsManager().createConnectedService(service, connection);
       
   561 			IStructuredSelection selection = (IStructuredSelection) deviceOSComboViewer.getSelection();
       
   562 			Pair<String, Version> pair = (Pair<String, Version>) selection.getFirstElement();
       
   563 			if (pair != null)
       
   564 				connectedService.setDeviceOS(pair.first, pair.second);
       
   565 			connectedService.addStatusChangedListener(statusListener = new IStatusChangedListener() {
       
   566 				public void statusChanged(final IStatus status) {
       
   567 					Display.getDefault().asyncExec(new Runnable() {
       
   568 						public void run() {
       
   569 							if (!statusText.isDisposed())
       
   570 								statusText.setText(status.getLongDescription());
       
   571 						}
       
   572 					});
       
   573 				}
       
   574 			});
       
   575 			if (connectedService instanceof AbstractConnectedService)
       
   576 				((AbstractConnectedService) connectedService).setRunnableContext(getContainer());
       
   577 			connectedService.setEnabled(true);
       
   578 			isTesting = true;
       
   579 		}
       
   580 	}
       
   581 
       
   582 	@Override
       
   583 	public void dispose() {
       
   584 		disposeInstallerProviders();
       
   585 		disposeConnectedService();
       
   586 		if (connection != null)
       
   587 			connection.dispose();
       
   588 		super.dispose();
       
   589 	}
       
   590 
       
   591 	private void disposeConnectedService() {
       
   592 		if (connectedService != null) {
       
   593 			connectedService.removeStatusChangedListener(statusListener);
       
   594 			connectedService.dispose();
       
   595 			connectedService = null;
       
   596 		}
       
   597 	}
       
   598 	
       
   599 	public Map<String, String> getSettings() {
       
   600 		if (connectionFactory == null) {
       
   601 			IConnection connectionToEdit = settingsWizard.getConnectionToEdit();
       
   602 			if (connectionToEdit == null || !connectionToEdit.getConnectionType().equals(settingsWizard.getConnectionType())) {
       
   603 				return null;
       
   604 			}
       
   605 			return connectionToEdit.getSettings();
       
   606 		}
       
   607 		return connectionFactory.getSettingsFromUI();
       
   608 	}
       
   609 	
       
   610 	private void saveAs(IPackageContents packageContents) throws Exception {
       
   611 		InputStream is = packageContents.getInputStream();
       
   612 		if (is != null) {
       
   613 			Shell shell = getShell();
       
   614 			FileDialog dialog =  new FileDialog(shell, SWT.SAVE);
       
   615 			dialog.setText(Messages.getString("ConnectionSettingsPage.SaveAsDialogTitle"));  //$NON-NLS-1$
       
   616 			if (saveAsParent == null)
       
   617 				saveAsParent = System.getProperty("user.home");  //$NON-NLS-1$
       
   618 			dialog.setFilterPath(saveAsParent);
       
   619 			dialog.setFileName(packageContents.getDefaultNameFileName());
       
   620 			dialog.setOverwrite(true); // prompt for overwrite
       
   621 			String path = dialog.open();
       
   622 			if (path != null) {
       
   623 				IPath saveAsPath = new Path(path);
       
   624 				saveAsParent = saveAsPath.removeLastSegments(1).toString();
       
   625 				File file = saveAsPath.toFile();
       
   626 				FileUtils.copyFile(is, file);
       
   627 			}
       
   628 		}
       
   629 	}
       
   630 	
       
   631 	private void attemptInstall(IPackageContents packageContents) throws Exception {
       
   632 		File tempDir = FileUtils.getTemporaryDirectory();
       
   633 		IPath path = new Path(tempDir.getAbsolutePath());
       
   634 		IPath tempFilePath = path.append(packageContents.getDefaultNameFileName());
       
   635 		File tempFile = tempFilePath.toFile();
       
   636 		if (tempFile.exists())
       
   637 			tempFile.delete();
       
   638 		InputStream is = packageContents.getInputStream();
       
   639 		if (is != null) {
       
   640 			FileUtils.copyFile(is, tempFile);
       
   641 			Program.launch(tempFile.getAbsolutePath());
       
   642 		}
       
   643 	}
       
   644 	
       
   645 
       
   646 	private IRemoteAgentInstaller getCurrentInstaller() {
       
   647 		TreeNode node = (TreeNode) ((StructuredSelection) installerTreeViewer.getSelection()).getFirstElement();
       
   648 		if (node != null) {
       
   649 			Object value = node.getValue();
       
   650 			if (value instanceof IRemoteAgentInstaller)
       
   651 				return (IRemoteAgentInstaller) value;
       
   652 		}
       
   653 		return null;
       
   654 	}
       
   655 	
       
   656 	private TreeNode[] createTreeNodes() {
       
   657 		Map<String, TreeNode> sdkFamilyToNodes = new HashMap<String, TreeNode>();
       
   658 		for (IRemoteAgentInstallerProvider installerProvider : installerProviders) {
       
   659 			List<String> familyNames = installerProvider.getSDKFamilyNames(null);
       
   660 			for (String familyName : familyNames) {
       
   661 				if (!sdkFamilyToNodes.containsKey(familyName))
       
   662 					sdkFamilyToNodes.put(familyName, new TreeNode(familyName));
       
   663 				TreeNode familyNode = sdkFamilyToNodes.get(familyName);
       
   664 				createFamilySubNodes(familyNode, installerProvider);
       
   665 			}
       
   666 		}
       
   667 		return (TreeNode[]) sdkFamilyToNodes.values().toArray(new TreeNode[sdkFamilyToNodes.values().size()]);
       
   668 	}
       
   669 
       
   670 	private void createFamilySubNodes(TreeNode familyNode, IRemoteAgentInstallerProvider installerProvider) {
       
   671 		String familyName = familyNode.getValue().toString();
       
   672 		List<Version> versions = installerProvider.getVersions(familyName);
       
   673 		List<TreeNode> childList = new ArrayList<TreeNode>();
       
   674 		TreeNode[] children = familyNode.getChildren();
       
   675 		if (children != null)
       
   676 			childList.addAll(Arrays.asList(children));
       
   677 		for (Version version : versions) {
       
   678 			TreeNode versionNode = getVersionNode(familyNode, version);
       
   679 			if (versionNode == null) {
       
   680 				versionNode = new TreeNode(version);
       
   681 				versionNode.setParent(familyNode);
       
   682 				childList.add(versionNode);
       
   683 			}
       
   684 			createInstallerNodes(versionNode, installerProvider);
       
   685 		}
       
   686 		familyNode.setChildren((TreeNode[]) childList.toArray(new TreeNode[childList.size()]));
       
   687 	}
       
   688 	
       
   689 	private TreeNode getVersionNode(TreeNode familyNode, Version version) {
       
   690 		TreeNode[] children = familyNode.getChildren();
       
   691 		if (children != null) {
       
   692 			for (TreeNode node : children) {
       
   693 				if (node.getValue().equals(version))
       
   694 					return node;
       
   695 			}
       
   696 		}
       
   697 		return null;
       
   698 	}	
       
   699 
       
   700 	private void createInstallerNodes(TreeNode versionNode, IRemoteAgentInstallerProvider installerProvider) {
       
   701 		String familyName = versionNode.getParent().getValue().toString();
       
   702 		Version version = (Version) versionNode.getValue();
       
   703 		List<IRemoteAgentInstaller> installers = 
       
   704 			installerProvider.getRemoteAgentInstallers(familyName, version);
       
   705 		List<TreeNode> childList = new ArrayList<TreeNode>();
       
   706 		TreeNode[] children = versionNode.getChildren();
       
   707 		if (children != null)
       
   708 			childList.addAll(Arrays.asList(children));
       
   709 		for (IRemoteAgentInstaller installer : installers) {
       
   710 			TreeNode installerNode = new TreeNode(installer);
       
   711 			installerNode.setParent(versionNode);
       
   712 			childList.add(installerNode);
       
   713 		}
       
   714 		versionNode.setChildren((TreeNode[]) childList.toArray(new TreeNode[childList.size()]));
       
   715 	}
       
   716 	
       
   717 	private List<Pair<String, Version>> createDeviceOSPairs() {
       
   718 		List<Pair<String, Version>> deviceOSPairs = new ArrayList<Pair<String, Version>>();
       
   719 		for (IRemoteAgentInstallerProvider installerProvider : installerProviders) {
       
   720 			List<String> familyNames = installerProvider.getSDKFamilyNames(null);
       
   721 			for (String familyName : familyNames) {
       
   722 				List<Version> versions = installerProvider.getVersions(familyName);
       
   723 				for (Version version : versions) {
       
   724 					Pair<String, Version> pair = new Pair(familyName, version);
       
   725 					if (!deviceOSPairs.contains(pair))
       
   726 						deviceOSPairs.add(pair);
       
   727 				}
       
   728 			}
       
   729 		}
       
   730 		return deviceOSPairs;
       
   731 	}
       
   732 	
       
   733 	private void getInstallerProviders(Collection<IService> services) {
       
   734 		if (installerProviders != null) {
       
   735 			// check to see if we already have this set of installer providers
       
   736 			Set<String> serviceIds = new TreeSet<String>();
       
   737 			for (IService service : services) {
       
   738 				if (service.getInstallerProvider() != null)
       
   739 					serviceIds.add(service.getIdentifier());
       
   740 			}
       
   741 			Set<String> existingServiceIds = new TreeSet<String>();
       
   742 			for (IRemoteAgentInstallerProvider installerProvider : installerProviders) {
       
   743 				existingServiceIds.add(installerProvider.getService().getIdentifier());
       
   744 			}
       
   745 			if (serviceIds.equals(existingServiceIds))
       
   746 				return;
       
   747 		}
       
   748 		disposeInstallerProviders();
       
   749 		installerProviders = new ArrayList<IRemoteAgentInstallerProvider>();
       
   750 		for (IService service : services) {
       
   751 			IRemoteAgentInstallerProvider installerProvider = service.getInstallerProvider();
       
   752 			if (installerProvider != null)
       
   753 				installerProviders.add(installerProvider);
       
   754 		}
       
   755 	}
       
   756 
       
   757 	private void disposeInstallerProviders() {
       
   758 		if (installerProviders != null) {
       
   759 			for (IRemoteAgentInstallerProvider installerProvider : installerProviders) {
       
   760 				installerProvider.dispose();
       
   761 			}
       
   762 			installerProviders.clear();
       
   763 			installerProviders = null;
       
   764 		}
       
   765 	}
       
   766 	
       
   767 	private void resetServiceTesting() {
       
   768 		isTesting = false;
       
   769 		if (service == null)
       
   770 			return;
       
   771 		serviceTestInfo.setText(service.getAdditionalServiceInfo());
       
   772 		agentTestTabComposite.layout(true, true);
       
   773 		statusText.setText(STATUS_NOT_TESTED);
       
   774 		disposeConnectedService();
       
   775 		String buttonLabel = Messages.getString("ConnectionSettingsPage.StartServiceTestButtonLabel"); //$NON-NLS-1$
       
   776 		serviceTestButton.setText(buttonLabel);
       
   777 	}
       
   778 
       
   779 }