connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/settings/ui/SettingsWizard.java
changeset 0 fb279309251b
child 163 b25acbfc406a
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     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 
       
    25 import org.eclipse.jface.wizard.*;
       
    26 import org.eclipse.swt.widgets.Shell;
       
    27 
       
    28 import java.text.MessageFormat;
       
    29 import java.util.*;
       
    30 
       
    31 /**
       
    32  * Main wizard class for hosting new and edit connection settings UI
       
    33  */
       
    34 public class SettingsWizard extends Wizard {
       
    35 
       
    36 	private ConnectionTypePage connectionTypePage;
       
    37 	private ConnectionSettingsPage connectionSettingsPage;
       
    38 	private IConnection connectionToEdit;
       
    39 	private IService serviceToRestrict;
       
    40 	private Map<IConnectedService, Boolean> savedEnabledStates;
       
    41 	
       
    42 	public SettingsWizard(IConnection connectionToEdit, IService serviceToRestrict) {
       
    43 		this.connectionToEdit = connectionToEdit;
       
    44 		this.serviceToRestrict = serviceToRestrict;
       
    45 		if (connectionToEdit == null)
       
    46 			setWindowTitle(Messages.getString("SettingsWizard.WindowTitle.New")); //$NON-NLS-1$
       
    47 		else {
       
    48 			setWindowTitle(MessageFormat.format(Messages.getString("SettingsWizard.WindowTitle.Edit"), connectionToEdit.getDisplayName())); //$NON-NLS-1$
       
    49 			saveConnectedServicesEnabledState();
       
    50 			enableConnectedServices(false);
       
    51 		}
       
    52 		setNeedsProgressMonitor(true);
       
    53 	}
       
    54 	
       
    55 	public SettingsWizard(IConnection connectionToEdit) {
       
    56 		this(connectionToEdit, null);
       
    57 	}
       
    58 	
       
    59 	public SettingsWizard() {
       
    60 		this(null, null);
       
    61 	}
       
    62 
       
    63 	@Override
       
    64 	public void addPages() {
       
    65 		connectionTypePage = new ConnectionTypePage(this);
       
    66 		addPage(connectionTypePage);
       
    67 		connectionSettingsPage = new ConnectionSettingsPage(this);
       
    68 		addPage(connectionSettingsPage);
       
    69 	}
       
    70 	
       
    71     public IWizardPage getStartingPage() {
       
    72     	if (connectionToEdit == null)
       
    73     		return connectionTypePage;
       
    74     	else
       
    75     		return connectionSettingsPage;
       
    76     }
       
    77     
       
    78     public IConnection getConnectionToEdit() {
       
    79 		return connectionToEdit;
       
    80 	}
       
    81 
       
    82 	public IService getServiceToRestrict() {
       
    83 		return serviceToRestrict;		
       
    84 	}
       
    85 	
       
    86 	public IConnectionType getConnectionType() {
       
    87 		return connectionTypePage.getConnectionType();
       
    88 	}
       
    89 	
       
    90 	@Override
       
    91 	public boolean performFinish() {
       
    92 		String newName = connectionTypePage.getName();
       
    93 		Map<String, String> newSettings = connectionSettingsPage.getSettings();
       
    94 		IConnectionType newConnectionType = connectionTypePage.getConnectionType();
       
    95 		if (connectionToEdit == null || !connectionToEdit.getConnectionType().equals(newConnectionType)) {
       
    96 			String id = null;
       
    97 			if (connectionToEdit != null) {
       
    98 				id = connectionToEdit.getIdentifier();
       
    99 				RemoteConnectionsActivator.getConnectionsManager().removeConnection(connectionToEdit);
       
   100 			}
       
   101 			connectionToEdit = newConnectionType.getConnectionFactory().createConnection(newSettings);
       
   102 			if (id != null)
       
   103 				connectionToEdit.setIdentifier(id);
       
   104 			RemoteConnectionsActivator.getConnectionsManager().addConnection(connectionToEdit);
       
   105 		}
       
   106 		else if (newSettings != null) {
       
   107 			connectionToEdit.updateSettings(newSettings);
       
   108 		}
       
   109 		if (!newName.equals(connectionToEdit.getDisplayName())) {
       
   110 			connectionToEdit.setDisplayName(newName);
       
   111 			RemoteConnectionsActivator.getConnectionsManager().updateDisplays();
       
   112 		}
       
   113 		
       
   114 		enableConnectedServices(true);
       
   115 		return true;
       
   116 	}
       
   117 	
       
   118 	@Override
       
   119 	public boolean performCancel() {
       
   120 		enableConnectedServices(true);
       
   121 		return true;
       
   122 	}
       
   123 
       
   124 	private void saveConnectedServicesEnabledState() {
       
   125 		Collection<IConnectedService> connectedServices = RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(connectionToEdit);
       
   126 		if (connectedServices == null)
       
   127 			return;
       
   128 		if (savedEnabledStates == null)
       
   129 			savedEnabledStates = new HashMap<IConnectedService, Boolean>();
       
   130 		for (IConnectedService connectedService : connectedServices) {
       
   131 			savedEnabledStates.put(connectedService, connectedService.isEnabled());
       
   132 		}
       
   133 	}
       
   134 
       
   135 	private void enableConnectedServices(boolean enabled) {
       
   136 		Collection<IConnectedService> connectedServices = RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(connectionToEdit);
       
   137 		if (connectedServices == null || savedEnabledStates == null)
       
   138 			return;
       
   139 		for (IConnectedService connectedService : connectedServices) {
       
   140 			if (!enabled) // disable when asked
       
   141 				connectedService.setEnabled(false);
       
   142 			else {
       
   143 				Boolean wasEnabled = savedEnabledStates.get(connectedService);
       
   144 				if (wasEnabled != null && wasEnabled) // reenable only if was enabled
       
   145 					connectedService.setEnabled(true);
       
   146 			}
       
   147 		}
       
   148 	}
       
   149 
       
   150 	public int open(Shell shell) {
       
   151 		WizardDialog dialog = new WizardDialog(shell, this);
       
   152 		return dialog.open();
       
   153 	}
       
   154 
       
   155 	public void connectionTypeChanged() {
       
   156 		connectionSettingsPage.updateDynamicUI();
       
   157 		getContainer().updateButtons();
       
   158 	}
       
   159 }