debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/newwizard/ConnectToDeviceDialog.java
branchRCL_2_4
changeset 911 81a2e70a37d7
child 953 68b6a294ab01
equal deleted inserted replaced
909:24ba32fc0320 911:81a2e70a37d7
       
     1 /*
       
     2 * Copyright (c) 2010 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 package com.nokia.cdt.internal.debug.launch.newwizard;
       
    19 
       
    20 import java.text.MessageFormat;
       
    21 import java.util.ArrayList;
       
    22 import java.util.Collection;
       
    23 import java.util.HashSet;
       
    24 import java.util.List;
       
    25 import java.util.Set;
       
    26 
       
    27 import org.eclipse.core.runtime.IStatus;
       
    28 import org.eclipse.jface.layout.GridDataFactory;
       
    29 import org.eclipse.jface.layout.GridLayoutFactory;
       
    30 import org.eclipse.jface.viewers.ArrayContentProvider;
       
    31 import org.eclipse.jface.viewers.ComboViewer;
       
    32 import org.eclipse.jface.viewers.ISelection;
       
    33 import org.eclipse.jface.viewers.ISelectionChangedListener;
       
    34 import org.eclipse.jface.viewers.IStructuredSelection;
       
    35 import org.eclipse.jface.viewers.LabelProvider;
       
    36 import org.eclipse.jface.viewers.SelectionChangedEvent;
       
    37 import org.eclipse.jface.viewers.StructuredSelection;
       
    38 import org.eclipse.swt.SWT;
       
    39 import org.eclipse.swt.events.ControlAdapter;
       
    40 import org.eclipse.swt.events.ControlEvent;
       
    41 import org.eclipse.swt.events.SelectionAdapter;
       
    42 import org.eclipse.swt.events.SelectionEvent;
       
    43 import org.eclipse.swt.widgets.Button;
       
    44 import org.eclipse.swt.widgets.Combo;
       
    45 import org.eclipse.swt.widgets.Composite;
       
    46 import org.eclipse.swt.widgets.Control;
       
    47 import org.eclipse.swt.widgets.Display;
       
    48 import org.eclipse.swt.widgets.Label;
       
    49 import org.eclipse.swt.widgets.Shell;
       
    50 
       
    51 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
       
    52 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService;
       
    53 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
       
    54 import com.nokia.carbide.remoteconnections.interfaces.IConnectionType;
       
    55 import com.nokia.carbide.remoteconnections.interfaces.IConnectionTypeProvider;
       
    56 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
       
    57 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener;
       
    58 import com.nokia.carbide.remoteconnections.settings.ui.SettingsWizard;
       
    59 
       
    60 /**
       
    61  *	This dialog allows in-depth configuration of the connection settings.
       
    62  */
       
    63 @SuppressWarnings("restriction")
       
    64 public class ConnectToDeviceDialog extends AbstractLaunchSettingsDialog implements IConnectionListener {
       
    65 	private IConnectionsManager manager;
       
    66 	private IConnectionTypeProvider typeProvider;
       
    67 	private ComboViewer viewer;
       
    68 	private Button editButton;
       
    69 	private Label descriptionLabel;
       
    70 
       
    71 	protected ConnectToDeviceDialog(Shell shell, LaunchOptionsData data) {
       
    72 		super(shell, data);
       
    73 		manager = RemoteConnectionsActivator.getConnectionsManager();
       
    74 		typeProvider = RemoteConnectionsActivator.getConnectionTypeProvider();
       
    75 	}
       
    76 	
       
    77 	@Override
       
    78 	protected Control createDialogArea(Composite parent) {
       
    79 		final Composite composite = initDialogArea(parent, 
       
    80 				"Change connection",
       
    81 				LaunchWizardHelpIds.WIZARD_DIALOG_CHANGE_CONNECTION);
       
    82 		
       
    83 		Composite viewerGroup = new Composite(composite, SWT.NONE);
       
    84 		GridDataFactory.fillDefaults().applyTo(viewerGroup);
       
    85 		GridLayoutFactory.swtDefaults().numColumns(3).applyTo(viewerGroup);
       
    86 		
       
    87 		Label label = new Label(viewerGroup, SWT.NONE);
       
    88 		label.setText("Current connection");
       
    89 		GridDataFactory.defaultsFor(label).applyTo(label);
       
    90 		
       
    91 		viewer = new ComboViewer(viewerGroup, SWT.READ_ONLY);
       
    92 		viewer.setLabelProvider(new LabelProvider() {
       
    93 			@Override
       
    94 			public String getText(Object element) {
       
    95 				if (element instanceof IConnection)
       
    96 					return ((IConnection) element).getDisplayName();
       
    97 				
       
    98 				return "No Current connection";
       
    99 			}
       
   100 		});
       
   101 		viewer.setContentProvider(new ArrayContentProvider());
       
   102 		Combo combo = viewer.getCombo();
       
   103 		GridDataFactory.defaultsFor(combo).grab(true, false).applyTo(combo);
       
   104 		viewer.getControl().setData(UID, "combo_viewer"); //$NON-NLS-1$
       
   105 		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
       
   106 			public void selectionChanged(SelectionChangedEvent event) {
       
   107 				if (getDialogArea() != null)
       
   108 					connectionSelected(getConnectionFromSelection(event.getSelection()));
       
   109 			}
       
   110 		});
       
   111 		manager.addConnectionListener(this);
       
   112 		
       
   113 		editButton = new Button(viewerGroup, SWT.PUSH);
       
   114 		editButton.setText("Edit...");
       
   115 		GridDataFactory.defaultsFor(editButton).applyTo(editButton);
       
   116 		editButton.setData(UID, "edit_button"); //$NON-NLS-1$
       
   117 		editButton.addSelectionListener(new SelectionAdapter() {
       
   118 			@Override
       
   119 			public void widgetSelected(SelectionEvent e) {
       
   120 				IConnection connection = getConnectionFromSelection(viewer.getSelection()); 
       
   121 				if (connection != null) {
       
   122 					SettingsWizard wizard = new SettingsWizard(connection, data.getService());
       
   123 					wizard.open(composite.getShell());
       
   124 				}
       
   125 			}
       
   126 		});
       
   127 
       
   128 		descriptionLabel = new Label(composite, SWT.WRAP);
       
   129 		GridDataFactory.defaultsFor(descriptionLabel).grab(false, true).applyTo(descriptionLabel);
       
   130 		composite.addControlListener(new ControlAdapter() {
       
   131 			@Override
       
   132 			public void controlResized(ControlEvent e) {
       
   133 				descriptionLabel.pack();
       
   134 			}
       
   135 		});
       
   136 		
       
   137 		setViewerInput(data.getConnection());
       
   138 
       
   139 		return composite;
       
   140 	}
       
   141 	
       
   142 	protected void validate() {
       
   143 		IStatus status = ConnectToDeviceSection.revalidate(data);
       
   144 
       
   145 		if (status.isOK()) {
       
   146 			IConnection connection = data.getConnection();
       
   147 			if (connection != null) {
       
   148 				IConnectedService connectedService = null;
       
   149 				Collection<IConnectedService> services = manager.getConnectedServices(connection);
       
   150 				if (services != null) {
       
   151 					for (IConnectedService service : services) {
       
   152 						if (service != null && service.getService().getIdentifier().equals(data.getService().getIdentifier())) {
       
   153 							connectedService = service;
       
   154 						}
       
   155 					}
       
   156 				}
       
   157 				
       
   158 				if (connectedService == null) {
       
   159 					status = error(MessageFormat.format(
       
   160 							"The selected connection does not support {0}",
       
   161 							data.getService().getDisplayName()));
       
   162 				}
       
   163 				else {
       
   164 					com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus serviceStatus = 
       
   165 						connectedService.getStatus();
       
   166 					if (!serviceStatus.getEStatus().equals(
       
   167 							com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus.UP)) {
       
   168 						status = warning("The selected connection may not be usable for debugging:\n {0}", 
       
   169 								serviceStatus.getLongDescription());
       
   170 					}
       
   171 				}
       
   172 			}
       
   173 		}
       
   174 		updateStatus(status);
       
   175 	}
       
   176 
       
   177 	/**
       
   178 	 *	Update for a change in the connection.  We will attempt to connect to the
       
   179 	 *  device (once) to detect what TRK it is running.
       
   180 	 */
       
   181 	private void updateConnection(IConnection connection) {
       
   182 		String standardPNPMessage = ConnectToDeviceSection.getStandardPNPMessage();
       
   183 		data.setConnection(connection);
       
   184 		if (connection != null) {
       
   185 			descriptionLabel.setText(standardPNPMessage);
       
   186 		} else {
       
   187 			descriptionLabel.setText("No connections are detected or defined.  " + standardPNPMessage);
       
   188 		}
       
   189 		
       
   190 	}
       
   191 
       
   192 	public void connectionSelected(IConnection connection) {
       
   193 		updateConnection(connection);
       
   194 		validate();
       
   195 	}
       
   196 	
       
   197 	public void connectionAdded(IConnection connection) {
       
   198 		refreshUI();
       
   199 	}
       
   200 
       
   201 	public void connectionRemoved(IConnection connection) {
       
   202 		refreshUI();		
       
   203 	}
       
   204 
       
   205 	public void currentConnectionSet(IConnection connection) {
       
   206 		refreshUI();		
       
   207 	}
       
   208 
       
   209 	private Set<IConnectionType> getCompatibleConnectionTypes() {
       
   210 		HashSet<IConnectionType> types = new HashSet<IConnectionType>();
       
   211 		Collection<String> compatibleTypeIds =
       
   212 			typeProvider.getCompatibleConnectionTypeIds(data.getService());
       
   213 		for (String typeId : compatibleTypeIds) {
       
   214 			types.add(typeProvider.getConnectionType(typeId));
       
   215 		}
       
   216 		return types;
       
   217 	}
       
   218 
       
   219 	private List<IConnection> getCompatibleConnections() {
       
   220 		Set<IConnectionType> types = getCompatibleConnectionTypes();
       
   221 		
       
   222 		List<IConnection> compatibleConnections = new ArrayList<IConnection>();
       
   223 		for (IConnection connection : manager.getConnections()) {
       
   224 			if (types.contains(connection.getConnectionType()))
       
   225 				compatibleConnections.add(connection);
       
   226 		}
       
   227 		return compatibleConnections;
       
   228 	}
       
   229 	
       
   230 	private void setViewerInput(IConnection connection) {
       
   231 		List<IConnection> connections = getCompatibleConnections();
       
   232 		viewer.setInput(connections);
       
   233 		
       
   234 		if (connections.isEmpty())
       
   235 			viewer.getCombo().setEnabled(false);
       
   236 		else {
       
   237 			viewer.getCombo().setEnabled(true);
       
   238 			if (connection == null) {
       
   239 				viewer.getCombo().select(0);
       
   240 				ISelection selection = viewer.getSelection();
       
   241 				connection = getConnectionFromSelection(selection);
       
   242 				viewer.setSelection(selection);
       
   243 			}
       
   244 			else
       
   245 				viewer.setSelection(new StructuredSelection(connection));
       
   246 		}
       
   247 		editButton.setEnabled(!viewer.getSelection().isEmpty());
       
   248 		
       
   249 		// fire listener in case we selected anew or the current connection changed
       
   250 		connectionSelected(connection);
       
   251 	}
       
   252 	
       
   253 	private IConnection getConnectionFromSelection(ISelection selection) {
       
   254 		return (IConnection) ((IStructuredSelection) selection).getFirstElement();
       
   255 	}
       
   256 
       
   257 	private void refreshUI() {
       
   258 		Display.getDefault().syncExec(new Runnable() {
       
   259 			public void run() {
       
   260 				if (viewer != null && viewer.getContentProvider() != null) {
       
   261 					setViewerInput(getConnectionFromSelection(viewer.getSelection()));
       
   262 				}
       
   263 			}
       
   264 		});
       
   265 	}
       
   266 	
       
   267 	@Override
       
   268 	public boolean close() {
       
   269 		manager.addConnectionListener(this);
       
   270 		return super.close();
       
   271 	}
       
   272 }
       
   273