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
.control
.ui
.views
.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
.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
;
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 // ------------------------------------------------------------------------
64 private CCombo fExistingHostsCombo
= null;
66 * The check box button for enabling/disabling the text input.
68 private Button fButton
= null;
70 * The text widget for the node name (alias)
72 private Text fConnectionNameText
= null;
74 * The text widget for the node address (IP or DNS name)
76 private Text fHostNameText
= null;
78 * The text widget for the IP port
80 private Text fPortText
= null;
82 * The parent where the new node should be added.
84 private ITraceControlComponent fParent
;
86 * The node name (alias) string.
88 private String fConnectionName
= null;
90 * The node address (IP or DNS name) string.
92 private String fHostName
= null;
94 * The IP port of the connection.
96 private int fPort
= IRemoteSystemProxy
.INVALID_PORT_NUMBER
;
98 * Input list of existing RSE hosts available for selection.
100 private IHost
[] fExistingHosts
= new IHost
[0];
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
111 public NewConnectionDialog(Shell shell
) {
113 setShellStyle(SWT
.RESIZE
| getShellStyle());
116 // ------------------------------------------------------------------------
118 // ------------------------------------------------------------------------
121 public String
getConnectionName() {
122 return fConnectionName
;
126 public String
getHostName() {
131 public int getPort() {
136 public void setTraceControlParent(ITraceControlComponent parent
) {
141 public void setHosts(IHost
[] hosts
) {
143 fExistingHosts
= Arrays
.copyOf(hosts
, hosts
.length
);
148 public void setPort(int port
) {
152 // ------------------------------------------------------------------------
154 // ------------------------------------------------------------------------
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
));
164 protected Control
createDialogArea(Composite parent
) {
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
));
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
);
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));
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$
189 fExistingHostsCombo
.setItems(items
);
190 fExistingHostsCombo
.setEnabled(fExistingHosts
.length
> 0);
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
);
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);
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);
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);
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() {
223 public void verifyText(VerifyEvent e
) {
224 // only numbers are allowed.
225 e
.doit
= e
.text
.matches("[0-9]*"); //$NON-NLS-1$
229 fButton
.addSelectionListener(new SelectionListener() {
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);
239 fExistingHostsCombo
.setEnabled(true);
240 fConnectionNameText
.setEnabled(false);
241 fHostNameText
.setEnabled(false);
242 fPortText
.setEnabled(false);
247 public void widgetDefaultSelected(SelectionEvent e
) {
251 fExistingHostsCombo
.addSelectionListener(new SelectionListener() {
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$
261 public void widgetDefaultSelected(SelectionEvent e
) {
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
);
274 data
= new GridData(GridData
.FILL_HORIZONTAL
);
275 data
.widthHint
= widthHint
;
276 data
.horizontalSpan
= 2;
277 fHostNameText
.setLayoutData(data
);
279 data
= new GridData(GridData
.FILL_HORIZONTAL
);
280 data
.widthHint
= widthHint
;
281 data
.horizontalSpan
= 2;
282 fPortText
.setLayoutData(data
);
284 fHostNameText
.setText(""); //$NON-NLS-1$
286 return dialogComposite
;
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$
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
;
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
;
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$
318 // validation successful -> call super.okPressed()