Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6f40a95438dbcd81a57925b914afa022628f9b8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/********************************************************************************
 * Copyright (c) 2016 IBM Corporation and others.
 * 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:
 * Wainer S. Moschetta (IBM) - initial API and implementation
 ********************************************************************************/
package org.eclipse.cdt.launch.remote;

import java.util.ArrayList;

import org.eclipse.cdt.internal.launch.remote.Activator;
import org.eclipse.cdt.internal.launch.remote.Messages;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.remote.core.IRemoteCommandShellService;
import org.eclipse.remote.core.IRemoteConnection;
import org.eclipse.remote.core.IRemoteConnectionType;
import org.eclipse.remote.core.IRemoteConnectionWorkingCopy;
import org.eclipse.remote.core.IRemoteServicesManager;
import org.eclipse.remote.core.exception.RemoteConnectionException;
import org.eclipse.remote.ui.IRemoteUIConnectionService;
import org.eclipse.remote.ui.IRemoteUIConnectionWizard;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
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.Shell;

public class RemoteUIHelper {
	/**
	 * Open dialog to edit a remote connection.
	 *
	 * @param connection - the remote connection
	 * @param shell - the shell
	 */
	public static void editConnectionDialog(IRemoteConnection connection, Shell shell) {
		if (connection == null) {
			return;
		}
		IRemoteUIConnectionService uiConnServices = connection.getConnectionType().getService(IRemoteUIConnectionService.class);
		IRemoteUIConnectionWizard wizard = uiConnServices.getConnectionWizard(shell);
		wizard.setConnection(connection.getWorkingCopy());
		IRemoteConnectionWorkingCopy connCopy = wizard.open();
		if (connCopy != null) {
			try {
				connCopy.save();
			} catch (RemoteConnectionException e) {
				logError(e);
			}
		}
	}

	/**
	 * Open dialog for user to create a new connection.
	 *
	 * @param shell - the shell
	 */
	public static void newConnectionDialog(Shell shell) {
		/*
		 * Evoke native new connection wizard and save connection
		 * 	if created by the user.
		 */
		class NewRemoteConnectionDialog extends Dialog {
			private String title;
			private Combo fConnSelection;
			private IRemoteServicesManager manager;

			protected NewRemoteConnectionDialog(Shell parentShell, String windowTitle) {
				super(parentShell);
				title = windowTitle;
				manager = Activator.getService(IRemoteServicesManager.class);
			}
			@Override
			protected void configureShell(Shell newShell) {
				super.configureShell(newShell);
				newShell.setText(title);
			}
			@Override
			protected Control createDialogArea(Composite parent) {
				Composite composite = (Composite) super.createDialogArea(parent);

				Label label = new Label(composite, SWT.WRAP);
				label.setText(Messages.RemoteCMainTab_New_conntype_combo_label);
				GridData data = new GridData(GridData.GRAB_HORIZONTAL
						| GridData.GRAB_VERTICAL
						| GridData.HORIZONTAL_ALIGN_FILL
						| GridData.VERTICAL_ALIGN_CENTER);
				data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
				label.setLayoutData(data);
				label.setFont(parent.getFont());
				fConnSelection = new Combo (composite, SWT.DROP_DOWN | SWT.READ_ONLY);
				ArrayList<String> suitableConnections = new ArrayList<>();
				for (IRemoteConnectionType type: manager.getAllConnectionTypes()) {
					if (type.canAdd() &&
							type.getConnectionServices().contains(
									IRemoteCommandShellService.class.getName())) {
						fConnSelection.setData(type.getName(), type.getId());
						suitableConnections.add(type.getName());
					}
				}
				fConnSelection.setItems(suitableConnections.toArray(new String[0]));
				fConnSelection.select(0);

				applyDialogFont(composite);
				return composite;
			}

			@Override
			protected void buttonPressed(int buttonId) {
				if (buttonId == IDialogConstants.OK_ID) {
				String connTypeId = (String) fConnSelection.getData(fConnSelection.getText());
				IRemoteConnectionType connType = manager.getConnectionType(connTypeId);
				IRemoteUIConnectionService fUIConnectionManager = connType.getService(IRemoteUIConnectionService.class);
				IRemoteUIConnectionWizard wizard = fUIConnectionManager.getConnectionWizard(this.getShell());

				IRemoteConnectionWorkingCopy wc = wizard.open();
				if (wc != null) {
					try {
						wc.save();
					} catch (RemoteConnectionException e) {
						logError(e);
					}
				}
				}
				super.buttonPressed(buttonId);
			}
		}

		NewRemoteConnectionDialog dlg = new NewRemoteConnectionDialog(shell,
				Messages.RemoteCMainTab_New_title);
		dlg.setBlockOnOpen(true);
		dlg.open();
	}

	/**
	 * Get a property associated with the connection.
	 *
	 * @param connection - the connection
	 * @param property - the property's name
	 * @return the property's value or empty string if it is not set.
	 */
	public static String getConnectionProperty(IRemoteConnection connection, String property) {
		String key = IRemoteConnectionHostConstants.PI_REMOTE_CDT + "." //$NON-NLS-1$
				+ property;
		return connection.getAttribute(key);
	}

	/**
	 * Associate a property with the connection.
	 *
	 * @param connection - the connection
	 * @param property - the property's name
	 * @param value the property's value
	 */
	public static void setConnectionProperty(IRemoteConnection connection, String property, String value) {
		String key = IRemoteConnectionHostConstants.PI_REMOTE_CDT + "." //$NON-NLS-1$
				+ property;
		IRemoteConnectionWorkingCopy wc = connection.getWorkingCopy();
		wc.setAttribute(key, value);
		try {
			wc.save();
		} catch (RemoteConnectionException e) {
			logError(e);
		}
	}

	private static void logError(Exception e) {
		Plugin plugin = Activator.getDefault();
		ILog logger = plugin.getLog();
		logger.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
				e.getMessage(), e));
	}
}

Back to the top