debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/newwizard/ConnectToDeviceSection.java
branchRCL_2_4
changeset 911 81a2e70a37d7
child 912 1d96f83e60ef
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 
       
    22 import org.eclipse.core.runtime.IStatus;
       
    23 import org.eclipse.core.runtime.Status;
       
    24 import org.eclipse.swt.widgets.Composite;
       
    25 import org.eclipse.swt.widgets.Display;
       
    26 import org.eclipse.swt.widgets.Shell;
       
    27 
       
    28 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
       
    29 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
       
    30 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
       
    31 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener;
       
    32 
       
    33 /**
       
    34  * Present the "Connect to device" section with a short description.
       
    35  */
       
    36 public class ConnectToDeviceSection extends AbstractLaunchWizardSection implements IConnectionListener {
       
    37 
       
    38 	private static final String NO_CURRENT_CONNECTION_MSG = "No current connection is defined or detected.";
       
    39 	private final UnifiedLaunchOptionsPage launchOptionsPage;
       
    40 	private final IConnectionsManager manager;
       
    41 
       
    42 	/**
       
    43 	 * @param unifiedLaunchOptionsPage 
       
    44 	 * 
       
    45 	 */
       
    46 	public ConnectToDeviceSection(LaunchOptionsData data, UnifiedLaunchOptionsPage launchOptionsPage) {
       
    47 		super(data, "Connect to device");
       
    48 		this.launchOptionsPage = launchOptionsPage;
       
    49 		manager = RemoteConnectionsActivator.getConnectionsManager();
       
    50 	}
       
    51 	
       
    52 	/* (non-Javadoc)
       
    53 	 * @see com.nokia.cdt.internal.debug.launch.wizard2.IWizardSection#createComposite(org.eclipse.swt.widgets.Composite)
       
    54 	 */
       
    55 	public void createControl(Composite parent) {
       
    56 		createSection(parent, 0);
       
    57 		manager.addConnectionListener(this);
       
    58 	}
       
    59 	
       
    60 	/* (non-Javadoc)
       
    61 	 * @see com.nokia.cdt.internal.debug.launch.wizard2.AbstractLaunchWizardSection#dispose()
       
    62 	 */
       
    63 	@Override
       
    64 	protected void dispose() {
       
    65 		manager.removeConnectionListener(this);
       
    66 	}
       
    67 	
       
    68 	public void initializeSettings() {
       
    69 		data.setConnection(manager.getCurrentConnection());
       
    70 	}
       
    71 
       
    72 	@Override
       
    73 	protected void validate() {
       
    74 		status = revalidate(data);
       
    75 	}
       
    76 
       
    77 	/** Get the simple status for the connection state */
       
    78 	static IStatus revalidate(LaunchOptionsData data) {
       
    79 		IStatus status = Status.OK_STATUS;
       
    80 		
       
    81 		if (data.getConnection() == null) {
       
    82 			status = error(NO_CURRENT_CONNECTION_MSG);
       
    83 		}
       
    84 		
       
    85 		return status;
       
    86 	}
       
    87 	
       
    88 	static String getStandardPNPMessage() {
       
    89 		return "You may plug in a device over USB or activate it over WLAN, or create a new connection now for your device or simulator.";
       
    90 	}
       
    91 
       
    92 	@Override
       
    93 	protected void updateUI() {
       
    94 		if (control == null || control.isDisposed())
       
    95 			return;
       
    96 		
       
    97 		String msg;
       
    98 		if (data.getConnection() != null)
       
    99 			msg = MessageFormat.format("The current connection is now ''{0}''.", data.getConnectionName());
       
   100 		else
       
   101 			msg = MessageFormat.format("{0} {1}", NO_CURRENT_CONNECTION_MSG, getStandardPNPMessage());
       
   102 			
       
   103 		descriptionLabel.setText(msg);
       
   104 		launchOptionsPage.changed();
       
   105 	}
       
   106 	
       
   107 	/* (non-Javadoc)
       
   108 	 * @see com.nokia.cdt.internal.debug.launch.wizard2.AbstractLaunchWizardSection#createChangeSettingsDialog(org.eclipse.swt.widgets.Shell, com.nokia.cdt.internal.debug.launch.wizard2.LaunchOptionsData)
       
   109 	 */
       
   110 	@Override
       
   111 	protected AbstractLaunchSettingsDialog createChangeSettingsDialog(Shell shell, LaunchOptionsData dialogData) {
       
   112 		return new ConnectToDeviceDialog(shell, dialogData);
       
   113 	}
       
   114 	
       
   115 	protected void refresh() {
       
   116 		Display.getDefault().asyncExec(new Runnable() {
       
   117 			@Override
       
   118 			public void run() {
       
   119 				validate();
       
   120 				updateUI();
       
   121 			}
       
   122 		});
       
   123 	}
       
   124 	
       
   125 	private void doConnectionsChanged() {
       
   126 		data.setConnection(manager.getCurrentConnection());
       
   127 		refresh();
       
   128 	}
       
   129 	
       
   130 	public void connectionAdded(IConnection connection) {
       
   131 		doConnectionsChanged();
       
   132 	}
       
   133 	
       
   134 	public void connectionRemoved(IConnection connection) {
       
   135 		doConnectionsChanged();
       
   136 	}
       
   137 	
       
   138 	public void currentConnectionSet(IConnection connection) {
       
   139 		doConnectionsChanged();
       
   140 	}
       
   141 	
       
   142 	@Override
       
   143 	protected void doChange() {
       
   144 		super.doChange();
       
   145 		IConnection connection = data.getConnection();
       
   146 		if (connection != null && !connection.equals(manager.getCurrentConnection()))
       
   147 			manager.setCurrentConnection(connection);
       
   148 	}
       
   149 }