Merge branch 'master' into lttng_2_0_control_dev
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / control / dialogs / NewConnectionDialog.java
1 /**********************************************************************
2 * Copyright (c) 2012 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.lttng.ui.views.control.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.lttng.ui.LTTngUiPlugin;
20 import org.eclipse.linuxtools.lttng.ui.views.control.Messages;
21 import org.eclipse.linuxtools.lttng.ui.views.control.model.ITraceControlComponent;
22 import org.eclipse.rse.core.model.IHost;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.CCombo;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Group;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.Text;
37
38 /**
39 * <b><u>NewConnectionDialog</u></b>
40 * <p>
41 * Dialog box for connection information.
42 * </p>
43 */
44 public class NewConnectionDialog extends Dialog implements INewConnectionDialog {
45
46 // ------------------------------------------------------------------------
47 // Constants
48 // ------------------------------------------------------------------------
49 /**
50 * The icon file for this dialog box.
51 */
52 public static final String TARGET_NEW_CONNECTION_ICON_FILE = "icons/elcl16/target_add.gif"; //$NON-NLS-1$
53
54 // ------------------------------------------------------------------------
55 // Attributes
56 // ------------------------------------------------------------------------
57 /**
58 * The dialog composite.
59 */
60 private Composite fDialogComposite = null;
61 /**
62 * The Group for the host combo box.
63 */
64 private Group fComboGroup = null;
65 /**
66 * The Group for the text input.
67 */
68 private Group fTextGroup = null;
69 /**
70 * The host combo box.
71 */
72 private CCombo fExistingHostsCombo = null;
73 /**
74 * The check box button for enabling/disabling the text input.
75 */
76 private Button fButton = null;
77 /**
78 * The text widget for the node name (alias)
79 */
80 private Text fConnectionNameText = null;
81 /**
82 * The text widget for the node address (IP or DNS name)
83 */
84 private Text fHostNameText = null;
85 /**
86 * The parent where the new node should be added.
87 */
88 private ITraceControlComponent fParent;
89 /**
90 * The node name (alias) string.
91 */
92 private String fConnectionName = null;
93 /**
94 * The node address (IP or DNS name) string.
95 */
96 private String fHostName = null;
97
98 /**
99 * Input list of existing RSE hosts available for selection.
100 */
101 private IHost[] fExistingHosts = new IHost[0];
102
103 // ------------------------------------------------------------------------
104 // Constructors
105 // ------------------------------------------------------------------------
106 public NewConnectionDialog(Shell shell, ITraceControlComponent parent, IHost[] hosts) {
107 super(shell);
108 fParent = parent;
109 if (hosts != null) {
110 fExistingHosts = Arrays.copyOf(hosts, hosts.length);
111 }
112 setShellStyle(SWT.RESIZE);
113 }
114
115 // ------------------------------------------------------------------------
116 // Accessors
117 // ------------------------------------------------------------------------
118 /*
119 * (non-Javadoc)
120 * @see org.eclipse.linuxtools.lttng.ui.views.control.dialogs.INewConnectionDialog#getConnectionName()
121 */
122 @Override
123 public String getConnectionName() {
124 return fConnectionName;
125 }
126
127 /*
128 * (non-Javadoc)
129 * @see org.eclipse.linuxtools.lttng.ui.views.control.dialogs.INewConnectionDialog#getHostName()
130 */
131 @Override
132 public String getHostName() {
133 return fHostName;
134 }
135
136 // ------------------------------------------------------------------------
137 // Operations
138 // ------------------------------------------------------------------------
139 /*
140 * (non-Javadoc)
141 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
142 */
143 @Override
144 protected void configureShell(Shell newShell) {
145 super.configureShell(newShell);
146 newShell.setText(Messages.TraceControl_NewDialogTitle);
147 newShell.setImage(LTTngUiPlugin.getDefault().loadIcon(TARGET_NEW_CONNECTION_ICON_FILE));
148 }
149
150 /*
151 * (non-Javadoc)
152 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
153 */
154 @Override
155 protected Control createDialogArea(Composite parent) {
156
157 // Main dialog panel
158 fDialogComposite = new Composite(parent, SWT.NONE);
159 GridLayout layout = new GridLayout(1, true);
160 fDialogComposite.setLayout(layout);
161 fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
162
163 // Existing connections group
164 fComboGroup = new Group(fDialogComposite, SWT.SHADOW_NONE);
165 fComboGroup.setText(Messages.TraceControl_NewNodeExistingConnectionGroupName);
166 layout = new GridLayout(2, true);
167 fComboGroup.setLayout(layout);
168 GridData data = new GridData(GridData.FILL_HORIZONTAL);
169 fComboGroup.setLayoutData(data);
170
171 fExistingHostsCombo = new CCombo(fComboGroup, SWT.READ_ONLY);
172 fExistingHostsCombo.setToolTipText(Messages.TraceControl_NewNodeComboToolTip);
173 fExistingHostsCombo.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));
174
175 String items[] = new String[fExistingHosts.length];
176 for (int i = 0; i < items.length; i++) {
177 items[i] = String.valueOf(fExistingHosts[i].getAliasName() + " - " + fExistingHosts[i].getHostName()); //$NON-NLS-1$
178 }
179
180 fExistingHostsCombo.setItems(items);
181 fExistingHostsCombo.setEnabled(fExistingHosts.length > 0);
182
183 // Node information grop
184 fTextGroup = new Group(fDialogComposite, SWT.SHADOW_NONE);
185 layout = new GridLayout(3, true);
186 fTextGroup.setLayout(layout);
187 data = new GridData(GridData.FILL_HORIZONTAL);
188 fTextGroup.setLayoutData(data);
189
190 fButton = new Button(fTextGroup, SWT.CHECK);
191 fButton.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 3, 1));
192 fButton.setText(Messages.TraceControl_NewNodeEditButtonName);
193 fButton.setEnabled(fExistingHosts.length > 0);
194
195 Label connectionNameLabel = new Label(fTextGroup, SWT.RIGHT);
196 connectionNameLabel.setText(Messages.TraceControl_NewNodeConnectionNameLabel);
197 fConnectionNameText = new Text(fTextGroup, SWT.NONE);
198 fConnectionNameText.setToolTipText(Messages.TraceControl_NewNodeConnectionNameTooltip);
199 fConnectionNameText.setEnabled(fExistingHosts.length == 0);
200
201 Label hostNameLabel = new Label(fTextGroup, SWT.RIGHT);
202 hostNameLabel.setText(Messages.TraceControl_NewNodeHostNameLabel);
203 fHostNameText = new Text(fTextGroup, SWT.NONE);
204 fHostNameText.setToolTipText(Messages.TraceControl_NewNodeHostNameTooltip);
205 fHostNameText.setEnabled(fExistingHosts.length == 0);
206
207 fButton.addSelectionListener(new SelectionListener() {
208 @Override
209 public void widgetSelected(SelectionEvent e) {
210 if (fButton.getSelection()) {
211 fExistingHostsCombo.deselectAll();
212 fExistingHostsCombo.setEnabled(false);
213 fConnectionNameText.setEnabled(true);
214 fHostNameText.setEnabled(true);
215 } else {
216 fExistingHostsCombo.setEnabled(true);
217 fConnectionNameText.setEnabled(false);
218 fHostNameText.setEnabled(false);
219 }
220 }
221
222 @Override
223 public void widgetDefaultSelected(SelectionEvent e) {
224 }
225 });
226
227 fExistingHostsCombo.addSelectionListener(new SelectionListener() {
228 @Override
229 public void widgetSelected(SelectionEvent e) {
230 int index = fExistingHostsCombo.getSelectionIndex();
231 fConnectionNameText.setText(fExistingHosts[index].getAliasName());
232 fHostNameText.setText(fExistingHosts[index].getHostName());
233 }
234
235 @Override
236 public void widgetDefaultSelected(SelectionEvent e) {
237 }
238 });
239
240 // layout widgets
241 data = new GridData(GridData.FILL_HORIZONTAL);
242 fHostNameText.setText("666.666.666.666"); //$NON-NLS-1$
243 Point minSize = fHostNameText.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
244 data.widthHint = minSize.x + 5;
245 data.horizontalSpan = 2;
246
247 fConnectionNameText.setLayoutData(data);
248 fHostNameText.setLayoutData(data);
249
250 fHostNameText.setText(""); //$NON-NLS-1$
251
252 return fDialogComposite;
253 }
254
255 /*
256 * (non-Javadoc)
257 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
258 */
259 @Override
260 protected void createButtonsForButtonBar(Composite parent) {
261 createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$
262 }
263
264 /*
265 * (non-Javadoc)
266 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
267 */
268 @Override
269 protected void okPressed() {
270 // Validate input data
271 fConnectionName = fConnectionNameText.getText();
272 fHostName = fHostNameText.getText();
273
274 if (!"".equals(fHostName)) { //$NON-NLS-1$
275 // If no node name is specified use the node address as name
276 if ("".equals(fConnectionName)) { //$NON-NLS-1$
277 fConnectionName = fHostName;
278 }
279 // Check if node with name already exists in parent
280 if(fParent.containsChild(fConnectionName)) {
281 MessageDialog.openError(getShell(),
282 Messages.TraceControl_NewDialogTitle,
283 Messages.TraceControl_AlreadyExistsError + " (" + fConnectionName + ")"); //$NON-NLS-1$//$NON-NLS-2$
284 return;
285 }
286 }
287 else {
288 return;
289 }
290 // validation successful -> call super.okPressed()
291 super.okPressed();
292 }
293 }
This page took 0.036658 seconds and 5 git commands to generate.