lttng: Rename lttng2 feature/plugins to lttng2.control
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.control.ui / src / org / eclipse / linuxtools / internal / lttng2 / control / ui / views / dialogs / NewConnectionDialog.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org.eclipse.linuxtools.internal.lttng2.control.ui.views.dialogs;
13
14 import java.util.Arrays;
15
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.linuxtools.internal.lttng2.control.ui.Activator;
20 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.messages.Messages;
21 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.model.ITraceControlComponent;
22 import org.eclipse.linuxtools.internal.lttng2.control.ui.views.remote.IRemoteSystemProxy;
23 import org.eclipse.rse.core.model.IHost;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.custom.CCombo;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.events.SelectionListener;
28 import org.eclipse.swt.events.VerifyEvent;
29 import org.eclipse.swt.events.VerifyListener;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Group;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Shell;
39 import org.eclipse.swt.widgets.Text;
40
41 /**
42 * <p>
43 * Dialog box for connection information.
44 * </p>
45 *
46 * @author Bernd Hufmann
47 */
48 public class NewConnectionDialog extends Dialog implements INewConnectionDialog {
49
50 // ------------------------------------------------------------------------
51 // Constants
52 // ------------------------------------------------------------------------
53 /**
54 * The icon file for this dialog box.
55 */
56 public static final String TARGET_NEW_CONNECTION_ICON_FILE = "icons/elcl16/target_add.gif"; //$NON-NLS-1$
57
58 // ------------------------------------------------------------------------
59 // Attributes
60 // ------------------------------------------------------------------------
61 /**
62 * The host combo box.
63 */
64 private CCombo fExistingHostsCombo = null;
65 /**
66 * The check box button for enabling/disabling the text input.
67 */
68 private Button fButton = null;
69 /**
70 * The text widget for the node name (alias)
71 */
72 private Text fConnectionNameText = null;
73 /**
74 * The text widget for the node address (IP or DNS name)
75 */
76 private Text fHostNameText = null;
77 /**
78 * The text widget for the IP port
79 */
80 private Text fPortText = null;
81 /**
82 * The parent where the new node should be added.
83 */
84 private ITraceControlComponent fParent;
85 /**
86 * The node name (alias) string.
87 */
88 private String fConnectionName = null;
89 /**
90 * The node address (IP or DNS name) string.
91 */
92 private String fHostName = null;
93 /**
94 * The IP port of the connection.
95 */
96 private int fPort = IRemoteSystemProxy.INVALID_PORT_NUMBER;
97 /**
98 * Input list of existing RSE hosts available for selection.
99 */
100 private IHost[] fExistingHosts = new IHost[0];
101
102 // ------------------------------------------------------------------------
103 // Constructors
104 // ------------------------------------------------------------------------
105 /**
106 * Constructor
107 *
108 * @param shell
109 * The shell
110 */
111 public NewConnectionDialog(Shell shell) {
112 super(shell);
113 setShellStyle(SWT.RESIZE | getShellStyle());
114 }
115
116 // ------------------------------------------------------------------------
117 // Accessors
118 // ------------------------------------------------------------------------
119
120 @Override
121 public String getConnectionName() {
122 return fConnectionName;
123 }
124
125 @Override
126 public String getHostName() {
127 return fHostName;
128 }
129
130 @Override
131 public int getPort() {
132 return fPort;
133 }
134
135 @Override
136 public void setTraceControlParent(ITraceControlComponent parent) {
137 fParent = parent;
138 }
139
140 @Override
141 public void setHosts(IHost[] hosts) {
142 if (hosts != null) {
143 fExistingHosts = Arrays.copyOf(hosts, hosts.length);
144 }
145 }
146
147 @Override
148 public void setPort(int port) {
149 fPort = port;
150 }
151
152 // ------------------------------------------------------------------------
153 // Operations
154 // ------------------------------------------------------------------------
155
156 @Override
157 protected void configureShell(Shell newShell) {
158 super.configureShell(newShell);
159 newShell.setText(Messages.TraceControl_NewDialogTitle);
160 newShell.setImage(Activator.getDefault().loadIcon(TARGET_NEW_CONNECTION_ICON_FILE));
161 }
162
163 @Override
164 protected Control createDialogArea(Composite parent) {
165
166 // Main dialog panel
167 Composite dialogComposite = new Composite(parent, SWT.NONE);
168 GridLayout layout = new GridLayout(1, true);
169 dialogComposite.setLayout(layout);
170 dialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
171
172 // Existing connections group
173 Group comboGroup = new Group(dialogComposite, SWT.SHADOW_NONE);
174 comboGroup.setText(Messages.TraceControl_NewNodeExistingConnectionGroupName);
175 layout = new GridLayout(2, true);
176 comboGroup.setLayout(layout);
177 GridData data = new GridData(GridData.FILL_HORIZONTAL);
178 comboGroup.setLayoutData(data);
179
180 fExistingHostsCombo = new CCombo(comboGroup, SWT.READ_ONLY);
181 fExistingHostsCombo.setToolTipText(Messages.TraceControl_NewNodeComboToolTip);
182 fExistingHostsCombo.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));
183
184 String items[] = new String[fExistingHosts.length];
185 for (int i = 0; i < items.length; i++) {
186 items[i] = String.valueOf(fExistingHosts[i].getAliasName() + " - " + fExistingHosts[i].getHostName()); //$NON-NLS-1$
187 }
188
189 fExistingHostsCombo.setItems(items);
190 fExistingHostsCombo.setEnabled(fExistingHosts.length > 0);
191
192 // Node information grop
193 Group textGroup = new Group(dialogComposite, SWT.SHADOW_NONE);
194 layout = new GridLayout(3, true);
195 textGroup.setLayout(layout);
196 data = new GridData(GridData.FILL_HORIZONTAL);
197 textGroup.setLayoutData(data);
198
199 fButton = new Button(textGroup, SWT.CHECK);
200 fButton.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 3, 1));
201 fButton.setText(Messages.TraceControl_NewNodeEditButtonName);
202 fButton.setEnabled(fExistingHosts.length > 0);
203
204 Label connectionNameLabel = new Label(textGroup, SWT.RIGHT);
205 connectionNameLabel.setText(Messages.TraceControl_NewNodeConnectionNameLabel);
206 fConnectionNameText = new Text(textGroup, SWT.NONE);
207 fConnectionNameText.setToolTipText(Messages.TraceControl_NewNodeConnectionNameTooltip);
208 fConnectionNameText.setEnabled(fExistingHosts.length == 0);
209
210 Label hostNameLabel = new Label(textGroup, SWT.RIGHT);
211 hostNameLabel.setText(Messages.TraceControl_NewNodeHostNameLabel);
212 fHostNameText = new Text(textGroup, SWT.NONE);
213 fHostNameText.setToolTipText(Messages.TraceControl_NewNodeHostNameTooltip);
214 fHostNameText.setEnabled(fExistingHosts.length == 0);
215
216 Label portLabel = new Label(textGroup, SWT.RIGHT);
217 portLabel.setText(Messages.TraceControl_NewNodePortLabel);
218 fPortText = new Text(textGroup, SWT.NONE);
219 fPortText.setToolTipText(Messages.TraceControl_NewNodePortTooltip);
220 fPortText.setEnabled(fExistingHosts.length == 0);
221 fPortText.addVerifyListener(new VerifyListener() {
222 @Override
223 public void verifyText(VerifyEvent e) {
224 // only numbers are allowed.
225 e.doit = e.text.matches("[0-9]*"); //$NON-NLS-1$
226 }
227 });
228
229 fButton.addSelectionListener(new SelectionListener() {
230 @Override
231 public void widgetSelected(SelectionEvent e) {
232 if (fButton.getSelection()) {
233 fExistingHostsCombo.deselectAll();
234 fExistingHostsCombo.setEnabled(false);
235 fConnectionNameText.setEnabled(true);
236 fHostNameText.setEnabled(true);
237 fPortText.setEnabled(true);
238 } else {
239 fExistingHostsCombo.setEnabled(true);
240 fConnectionNameText.setEnabled(false);
241 fHostNameText.setEnabled(false);
242 fPortText.setEnabled(false);
243 }
244 }
245
246 @Override
247 public void widgetDefaultSelected(SelectionEvent e) {
248 }
249 });
250
251 fExistingHostsCombo.addSelectionListener(new SelectionListener() {
252 @Override
253 public void widgetSelected(SelectionEvent e) {
254 int index = fExistingHostsCombo.getSelectionIndex();
255 fConnectionNameText.setText(fExistingHosts[index].getAliasName());
256 fHostNameText.setText(fExistingHosts[index].getHostName());
257 fPortText.setText(""); //$NON-NLS-1$
258 }
259
260 @Override
261 public void widgetDefaultSelected(SelectionEvent e) {
262 }
263 });
264
265 // layout widgets
266 data = new GridData(GridData.FILL_HORIZONTAL);
267 fHostNameText.setText("666.666.666.666"); //$NON-NLS-1$
268 Point minSize = fHostNameText.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
269 int widthHint = minSize.x + 5;
270 data.widthHint = widthHint;
271 data.horizontalSpan = 2;
272 fConnectionNameText.setLayoutData(data);
273
274 data = new GridData(GridData.FILL_HORIZONTAL);
275 data.widthHint = widthHint;
276 data.horizontalSpan = 2;
277 fHostNameText.setLayoutData(data);
278
279 data = new GridData(GridData.FILL_HORIZONTAL);
280 data.widthHint = widthHint;
281 data.horizontalSpan = 2;
282 fPortText.setLayoutData(data);
283
284 fHostNameText.setText(""); //$NON-NLS-1$
285
286 return dialogComposite;
287 }
288
289 @Override
290 protected void createButtonsForButtonBar(Composite parent) {
291 createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", true); //$NON-NLS-1$
292 createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$
293 }
294
295 @Override
296 protected void okPressed() {
297 // Validate input data
298 fConnectionName = fConnectionNameText.getText();
299 fHostName = fHostNameText.getText();
300 fPort = (fPortText.getText().length() > 0) ? Integer.parseInt(fPortText.getText()) : IRemoteSystemProxy.INVALID_PORT_NUMBER;
301
302 if (!"".equals(fHostName)) { //$NON-NLS-1$
303 // If no node name is specified use the node address as name
304 if ("".equals(fConnectionName)) { //$NON-NLS-1$
305 fConnectionName = fHostName;
306 }
307 // Check if node with name already exists in parent
308 if(fParent.containsChild(fConnectionName)) {
309 MessageDialog.openError(getShell(),
310 Messages.TraceControl_NewDialogTitle,
311 Messages.TraceControl_AlreadyExistsError + " (" + fConnectionName + ")"); //$NON-NLS-1$//$NON-NLS-2$
312 return;
313 }
314 }
315 else {
316 return;
317 }
318 // validation successful -> call super.okPressed()
319 super.okPressed();
320 }
321 }
This page took 0.039596 seconds and 5 git commands to generate.