1 /**********************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.dialogs
;
14 import java
.util
.Arrays
;
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
.ui
.Activator
;
20 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.messages
.Messages
;
21 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.model
.ITraceControlComponent
;
22 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.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
;
43 * Dialog box for connection information.
46 * @author Bernd Hufmann
48 public class NewConnectionDialog
extends Dialog
implements INewConnectionDialog
{
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 * The icon file for this dialog box.
56 public static final String TARGET_NEW_CONNECTION_ICON_FILE
= "icons/elcl16/target_add.gif"; //$NON-NLS-1$
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 * The dialog composite.
64 private Composite fDialogComposite
= null;
66 * The Group for the host combo box.
68 private Group fComboGroup
= null;
70 * The Group for the text input.
72 private Group fTextGroup
= null;
76 private CCombo fExistingHostsCombo
= null;
78 * The check box button for enabling/disabling the text input.
80 private Button fButton
= null;
82 * The text widget for the node name (alias)
84 private Text fConnectionNameText
= null;
86 * The text widget for the node address (IP or DNS name)
88 private Text fHostNameText
= null;
90 * The text widget for the IP port
92 private Text fPortText
= null;
94 * The parent where the new node should be added.
96 private ITraceControlComponent fParent
;
98 * The node name (alias) string.
100 private String fConnectionName
= null;
102 * The node address (IP or DNS name) string.
104 private String fHostName
= null;
106 * The IP port of the connection.
108 private int fPort
= IRemoteSystemProxy
.INVALID_PORT_NUMBER
;
110 * Input list of existing RSE hosts available for selection.
112 private IHost
[] fExistingHosts
= new IHost
[0];
114 // ------------------------------------------------------------------------
116 // ------------------------------------------------------------------------
123 public NewConnectionDialog(Shell shell
) {
125 setShellStyle(SWT
.RESIZE
);
128 // ------------------------------------------------------------------------
130 // ------------------------------------------------------------------------
133 public String
getConnectionName() {
134 return fConnectionName
;
138 public String
getHostName() {
143 public int getPort() {
148 public void setTraceControlParent(ITraceControlComponent parent
) {
153 public void setHosts(IHost
[] hosts
) {
155 fExistingHosts
= Arrays
.copyOf(hosts
, hosts
.length
);
160 public void setPort(int port
) {
164 // ------------------------------------------------------------------------
166 // ------------------------------------------------------------------------
169 protected void configureShell(Shell newShell
) {
170 super.configureShell(newShell
);
171 newShell
.setText(Messages
.TraceControl_NewDialogTitle
);
172 newShell
.setImage(Activator
.getDefault().loadIcon(TARGET_NEW_CONNECTION_ICON_FILE
));
176 protected Control
createDialogArea(Composite parent
) {
179 fDialogComposite
= new Composite(parent
, SWT
.NONE
);
180 GridLayout layout
= new GridLayout(1, true);
181 fDialogComposite
.setLayout(layout
);
182 fDialogComposite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
184 // Existing connections group
185 fComboGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
186 fComboGroup
.setText(Messages
.TraceControl_NewNodeExistingConnectionGroupName
);
187 layout
= new GridLayout(2, true);
188 fComboGroup
.setLayout(layout
);
189 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
190 fComboGroup
.setLayoutData(data
);
192 fExistingHostsCombo
= new CCombo(fComboGroup
, SWT
.READ_ONLY
);
193 fExistingHostsCombo
.setToolTipText(Messages
.TraceControl_NewNodeComboToolTip
);
194 fExistingHostsCombo
.setLayoutData(new GridData(GridData
.FILL
, GridData
.CENTER
, true, false, 2, 1));
196 String items
[] = new String
[fExistingHosts
.length
];
197 for (int i
= 0; i
< items
.length
; i
++) {
198 items
[i
] = String
.valueOf(fExistingHosts
[i
].getAliasName() + " - " + fExistingHosts
[i
].getHostName()); //$NON-NLS-1$
201 fExistingHostsCombo
.setItems(items
);
202 fExistingHostsCombo
.setEnabled(fExistingHosts
.length
> 0);
204 // Node information grop
205 fTextGroup
= new Group(fDialogComposite
, SWT
.SHADOW_NONE
);
206 layout
= new GridLayout(3, true);
207 fTextGroup
.setLayout(layout
);
208 data
= new GridData(GridData
.FILL_HORIZONTAL
);
209 fTextGroup
.setLayoutData(data
);
211 fButton
= new Button(fTextGroup
, SWT
.CHECK
);
212 fButton
.setLayoutData(new GridData(GridData
.FILL
, GridData
.CENTER
, true, false, 3, 1));
213 fButton
.setText(Messages
.TraceControl_NewNodeEditButtonName
);
214 fButton
.setEnabled(fExistingHosts
.length
> 0);
216 Label connectionNameLabel
= new Label(fTextGroup
, SWT
.RIGHT
);
217 connectionNameLabel
.setText(Messages
.TraceControl_NewNodeConnectionNameLabel
);
218 fConnectionNameText
= new Text(fTextGroup
, SWT
.NONE
);
219 fConnectionNameText
.setToolTipText(Messages
.TraceControl_NewNodeConnectionNameTooltip
);
220 fConnectionNameText
.setEnabled(fExistingHosts
.length
== 0);
222 Label hostNameLabel
= new Label(fTextGroup
, SWT
.RIGHT
);
223 hostNameLabel
.setText(Messages
.TraceControl_NewNodeHostNameLabel
);
224 fHostNameText
= new Text(fTextGroup
, SWT
.NONE
);
225 fHostNameText
.setToolTipText(Messages
.TraceControl_NewNodeHostNameTooltip
);
226 fHostNameText
.setEnabled(fExistingHosts
.length
== 0);
228 Label portLabel
= new Label(fTextGroup
, SWT
.RIGHT
);
229 portLabel
.setText(Messages
.TraceControl_NewNodePortLabel
);
230 fPortText
= new Text(fTextGroup
, SWT
.NONE
);
231 fPortText
.setToolTipText(Messages
.TraceControl_NewNodePortTooltip
);
232 fPortText
.setEnabled(fExistingHosts
.length
== 0);
233 fPortText
.addVerifyListener(new VerifyListener() {
235 public void verifyText(VerifyEvent e
) {
236 // only numbers are allowed.
237 e
.doit
= e
.text
.matches("[0-9]*"); //$NON-NLS-1$
241 fButton
.addSelectionListener(new SelectionListener() {
243 public void widgetSelected(SelectionEvent e
) {
244 if (fButton
.getSelection()) {
245 fExistingHostsCombo
.deselectAll();
246 fExistingHostsCombo
.setEnabled(false);
247 fConnectionNameText
.setEnabled(true);
248 fHostNameText
.setEnabled(true);
249 fPortText
.setEnabled(true);
251 fExistingHostsCombo
.setEnabled(true);
252 fConnectionNameText
.setEnabled(false);
253 fHostNameText
.setEnabled(false);
254 fPortText
.setEnabled(false);
259 public void widgetDefaultSelected(SelectionEvent e
) {
263 fExistingHostsCombo
.addSelectionListener(new SelectionListener() {
265 public void widgetSelected(SelectionEvent e
) {
266 int index
= fExistingHostsCombo
.getSelectionIndex();
267 fConnectionNameText
.setText(fExistingHosts
[index
].getAliasName());
268 fHostNameText
.setText(fExistingHosts
[index
].getHostName());
269 fPortText
.setText(""); //$NON-NLS-1$
273 public void widgetDefaultSelected(SelectionEvent e
) {
278 data
= new GridData(GridData
.FILL_HORIZONTAL
);
279 fHostNameText
.setText("666.666.666.666"); //$NON-NLS-1$
280 Point minSize
= fHostNameText
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
, true);
281 int widthHint
= minSize
.x
+ 5;
282 data
.widthHint
= widthHint
;
283 data
.horizontalSpan
= 2;
284 fConnectionNameText
.setLayoutData(data
);
286 data
= new GridData(GridData
.FILL_HORIZONTAL
);
287 data
.widthHint
= widthHint
;
288 data
.horizontalSpan
= 2;
289 fHostNameText
.setLayoutData(data
);
291 data
= new GridData(GridData
.FILL_HORIZONTAL
);
292 data
.widthHint
= widthHint
;
293 data
.horizontalSpan
= 2;
294 fPortText
.setLayoutData(data
);
296 fHostNameText
.setText(""); //$NON-NLS-1$
298 return fDialogComposite
;
302 protected void createButtonsForButtonBar(Composite parent
) {
303 createButton(parent
, IDialogConstants
.CANCEL_ID
, "&Cancel", true); //$NON-NLS-1$
304 createButton(parent
, IDialogConstants
.OK_ID
, "&Ok", true); //$NON-NLS-1$
308 protected void okPressed() {
309 // Validate input data
310 fConnectionName
= fConnectionNameText
.getText();
311 fHostName
= fHostNameText
.getText();
312 fPort
= (fPortText
.getText().length() > 0) ? Integer
.parseInt(fPortText
.getText()) : IRemoteSystemProxy
.INVALID_PORT_NUMBER
;
314 if (!"".equals(fHostName
)) { //$NON-NLS-1$
315 // If no node name is specified use the node address as name
316 if ("".equals(fConnectionName
)) { //$NON-NLS-1$
317 fConnectionName
= fHostName
;
319 // Check if node with name already exists in parent
320 if(fParent
.containsChild(fConnectionName
)) {
321 MessageDialog
.openError(getShell(),
322 Messages
.TraceControl_NewDialogTitle
,
323 Messages
.TraceControl_AlreadyExistsError
+ " (" + fConnectionName
+ ")"); //$NON-NLS-1$//$NON-NLS-2$
330 // validation successful -> call super.okPressed()