blob: 12d4dd1fd3448dcf2ab6a78d1cadebd23f20dceb [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2011 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
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ptp.rdt.sync.rsync.ui;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.ptp.rdt.sync.core.serviceproviders.ISyncServiceProvider;
import org.eclipse.ptp.rdt.sync.core.services.IRemoteSyncServiceConstants;
import org.eclipse.ptp.rdt.sync.rsync.core.RSyncServiceProvider;
import org.eclipse.ptp.rdt.sync.rsync.ui.messages.Messages;
import org.eclipse.ptp.rdt.sync.ui.ISynchronizeParticipant;
import org.eclipse.ptp.remote.core.IRemoteConnection;
import org.eclipse.ptp.remote.core.IRemoteFileManager;
import org.eclipse.ptp.remote.core.IRemoteServices;
import org.eclipse.ptp.remote.ui.IRemoteUIConnectionManager;
import org.eclipse.ptp.remote.ui.IRemoteUIConstants;
import org.eclipse.ptp.remote.ui.IRemoteUIFileManager;
import org.eclipse.ptp.remote.ui.IRemoteUIServices;
import org.eclipse.ptp.remote.ui.PTPRemoteUIPlugin;
import org.eclipse.ptp.services.core.IService;
import org.eclipse.ptp.services.core.ServiceModelManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
/**
* Launches a dialog that configures a remote sync target with OK and Cancel
* buttons. Also has a text field to allow the name of the configuration to be
* changed.
*/
public class RSyncParticipant implements ISynchronizeParticipant {
private static final String FILE_SCHEME = "file"; //$NON-NLS-1$
// private IServiceConfiguration fConfig;
private IRemoteConnection fSelectedConnection;
private IRemoteServices fSelectedProvider;
// private final IRunnableContext fContext;
private final String fProjectName = ""; //$NON-NLS-1$
private final Map<Integer, IRemoteServices> fComboIndexToRemoteServicesProviderMap = new HashMap<Integer, IRemoteServices>();
private final Map<Integer, IRemoteConnection> fComboIndexToRemoteConnectionMap = new HashMap<Integer, IRemoteConnection>();
private Control fDialogControl;
private Point fDialogSize;
private Text fNameText;
private Button fBrowseButton;
private Button fNewConnectionButton;
private Combo fProviderCombo;
private Combo fConnectionCombo;
private Text fLocationText;
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ptp.rdt.sync.ui.ISynchronizeParticipant#createConfigurationArea
* (org.eclipse.swt.widgets.Composite,
* org.eclipse.jface.operation.IRunnableContext)
*/
public void createConfigurationArea(Composite parent, IRunnableContext context) {
final Composite configArea = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 3;
configArea.setLayout(layout);
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
configArea.setLayoutData(gd);
// Label for "Provider:"
Label providerLabel = new Label(configArea, SWT.LEFT);
providerLabel.setText(Messages.RSyncParticipant_remoteProvider);
// combo for providers
fProviderCombo = new Combo(configArea, SWT.DROP_DOWN | SWT.READ_ONLY);
gd = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
gd.horizontalSpan = 2;
fProviderCombo.setLayoutData(gd);
fProviderCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
handleServicesSelected();
}
});
// attempt to restore settings from saved state
// IRemoteServices providerSelected = fProvider.getRemoteServices();
// populate the combo with a list of providers
IRemoteServices[] providers = PTPRemoteUIPlugin.getDefault().getRemoteServices(context);
int toSelect = 0;
for (int k = 0; k < providers.length; k++) {
fProviderCombo.add(providers[k].getName(), k);
fComboIndexToRemoteServicesProviderMap.put(k, providers[k]);
}
// set selected host to be the first one if we're not restoring from
// settings
fProviderCombo.select(toSelect);
fSelectedProvider = fComboIndexToRemoteServicesProviderMap.get(toSelect);
// connection combo
// Label for "Connection:"
Label connectionLabel = new Label(configArea, SWT.LEFT);
connectionLabel.setText(Messages.RSyncParticipant_connection);
// combo for providers
fConnectionCombo = new Combo(configArea, SWT.DROP_DOWN | SWT.READ_ONLY);
// set layout to grab horizontal space
fConnectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
fConnectionCombo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
handleConnectionSelected();
}
});
// populate the combo with a list of providers
populateConnectionCombo(fConnectionCombo);
// new connection button
fNewConnectionButton = new Button(configArea, SWT.PUSH);
fNewConnectionButton.setText(Messages.RSyncParticipant_new);
updateNewConnectionButtonEnabled(fNewConnectionButton);
fNewConnectionButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
IRemoteUIConnectionManager connectionManager = getUIConnectionManager();
if (connectionManager != null) {
connectionManager.newConnection(fNewConnectionButton.getShell());
}
// refresh list of connections
populateConnectionCombo(fConnectionCombo);
}
});
Label locationLabel = new Label(configArea, SWT.LEFT);
locationLabel.setText(Messages.RSyncParticipant_location);
fLocationText = new Text(configArea, SWT.SINGLE | SWT.BORDER);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 1;
gd.grabExcessHorizontalSpace = true;
gd.widthHint = 250;
fLocationText.setLayoutData(gd);
fLocationText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
// MBSCustomPageManager.addPageProperty(REMOTE_SYNC_WIZARD_PAGE_ID,
// PATH_PROPERTY, fLocationText.getText());
}
});
// new connection button
fBrowseButton = new Button(configArea, SWT.PUSH);
fBrowseButton.setText(Messages.RSyncParticipant_browse);
fBrowseButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (fSelectedConnection != null) {
checkConnection();
if (fSelectedConnection.isOpen()) {
IRemoteUIServices remoteUIServices = PTPRemoteUIPlugin.getDefault().getRemoteUIServices(fSelectedProvider);
if (remoteUIServices != null) {
IRemoteUIFileManager fileMgr = remoteUIServices.getUIFileManager();
if (fileMgr != null) {
fileMgr.setConnection(fSelectedConnection);
String correctPath = fLocationText.getText();
String selectedPath = fileMgr.browseDirectory(
fLocationText.getShell(),
"Project Location (" + fSelectedConnection.getName() + ")", correctPath, IRemoteUIConstants.NONE); //$NON-NLS-1$ //$NON-NLS-2$
if (selectedPath != null) {
fLocationText.setText(selectedPath);
}
}
}
}
}
}
});
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ptp.rdt.sync.ui.ISynchronizeParticipant#getProvider(org.eclipse
* .core.resources.IProject)
*/
public ISyncServiceProvider getProvider(IProject project) {
ServiceModelManager smm = ServiceModelManager.getInstance();
IService syncService = smm.getService(IRemoteSyncServiceConstants.SERVICE_SYNC);
RSyncServiceProvider provider = (RSyncServiceProvider) smm.getServiceProvider(syncService
.getProviderDescriptor(RSyncServiceProvider.ID));
provider.setLocation(fLocationText.getText());
provider.setRemoteConnection(fSelectedConnection);
provider.setRemoteServices(fSelectedProvider);
provider.setProject(project);
return provider;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ptp.rdt.sync.ui.ISynchronizeParticipant#isConfigComplete()
*/
public boolean isConfigComplete() {
// TODO Auto-generated method stub
return false;
}
/**
* Attempt to open a connection.
*/
private void checkConnection() {
IRemoteUIConnectionManager mgr = getUIConnectionManager();
if (mgr != null) {
mgr.openConnectionWithProgress(fConnectionCombo.getShell(), null, fSelectedConnection);
}
}
/**
* Return the path we are going to display. If it is a file URI then remove
* the file prefix.
*
* Only do this if the connection is open. Otherwise we will attempt to
* connect to the first machine in the list, which is annoying.
*
* @return String
*/
private String getDefaultPathDisplayString() {
String projectName = ""; //$NON-NLS-1$
// IWizardPage page = getWizard().getStartingPage();
// if (page instanceof CDTMainWizardPage) {
// projectName = ((CDTMainWizardPage) page).getProjectName();
// }
if (fSelectedConnection != null && fSelectedConnection.isOpen()) {
IRemoteFileManager fileMgr = fSelectedProvider.getFileManager(fSelectedConnection);
URI defaultURI = fileMgr.toURI(fSelectedConnection.getWorkingDirectory());
// Handle files specially. Assume a file if there is no project to
// query
if (defaultURI != null && defaultURI.getScheme().equals(FILE_SCHEME)) {
return Platform.getLocation().append(fProjectName).toOSString();
}
if (defaultURI == null) {
return ""; //$NON-NLS-1$
}
return new Path(defaultURI.getPath()).append(fProjectName).toOSString();
}
return ""; //$NON-NLS-1$
}
/**
* @return
*/
private IRemoteUIConnectionManager getUIConnectionManager() {
IRemoteUIConnectionManager connectionManager = PTPRemoteUIPlugin.getDefault().getRemoteUIServices(fSelectedProvider)
.getUIConnectionManager();
return connectionManager;
}
/**
* Handle new connection selected
*/
private void handleConnectionSelected() {
int selectionIndex = fConnectionCombo.getSelectionIndex();
fSelectedConnection = fComboIndexToRemoteConnectionMap.get(selectionIndex);
updateNewConnectionButtonEnabled(fNewConnectionButton);
fLocationText.setText(getDefaultPathDisplayString());
}
/**
* Handle new remote services selected
*/
private void handleServicesSelected() {
int selectionIndex = fProviderCombo.getSelectionIndex();
fSelectedProvider = fComboIndexToRemoteServicesProviderMap.get(selectionIndex);
populateConnectionCombo(fConnectionCombo);
updateNewConnectionButtonEnabled(fNewConnectionButton);
handleConnectionSelected();
}
/**
* @param connectionCombo
*/
private void populateConnectionCombo(final Combo connectionCombo) {
connectionCombo.removeAll();
IRemoteConnection[] connections = fSelectedProvider.getConnectionManager().getConnections();
for (int k = 0; k < connections.length; k++) {
connectionCombo.add(connections[k].getName(), k);
fComboIndexToRemoteConnectionMap.put(k, connections[k]);
}
connectionCombo.select(0);
fSelectedConnection = fComboIndexToRemoteConnectionMap.get(0);
}
/**
* @param button
*/
private void updateNewConnectionButtonEnabled(Button button) {
IRemoteUIConnectionManager connectionManager = getUIConnectionManager();
button.setEnabled(connectionManager != null);
}
}