Commit | Line | Data |
---|---|---|
eb1bab5b BH |
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 | **********************************************************************/ | |
115b4a01 | 12 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs; |
eb1bab5b BH |
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; | |
115b4a01 BH |
19 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
20 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.Messages; | |
21 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent; | |
eb1bab5b BH |
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 | /** | |
4775bcbf | 74 | * The check box button for enabling/disabling the text input. |
eb1bab5b BH |
75 | */ |
76 | private Button fButton = null; | |
77 | /** | |
78 | * The text widget for the node name (alias) | |
79 | */ | |
bbb3538a | 80 | private Text fConnectionNameText = null; |
eb1bab5b BH |
81 | /** |
82 | * The text widget for the node address (IP or DNS name) | |
83 | */ | |
bbb3538a | 84 | private Text fHostNameText = null; |
eb1bab5b BH |
85 | /** |
86 | * The parent where the new node should be added. | |
87 | */ | |
88 | private ITraceControlComponent fParent; | |
89 | /** | |
90 | * The node name (alias) string. | |
91 | */ | |
bbb3538a | 92 | private String fConnectionName = null; |
eb1bab5b BH |
93 | /** |
94 | * The node address (IP or DNS name) string. | |
95 | */ | |
bbb3538a | 96 | private String fHostName = null; |
eb1bab5b BH |
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 | // ------------------------------------------------------------------------ | |
d132bcc7 | 106 | public NewConnectionDialog(Shell shell) { |
eb1bab5b | 107 | super(shell); |
498704b3 | 108 | setShellStyle(SWT.RESIZE); |
eb1bab5b BH |
109 | } |
110 | ||
111 | // ------------------------------------------------------------------------ | |
112 | // Accessors | |
113 | // ------------------------------------------------------------------------ | |
114 | /* | |
115 | * (non-Javadoc) | |
115b4a01 | 116 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.INewConnectionDialog#getConnectionName() |
eb1bab5b BH |
117 | */ |
118 | @Override | |
bbb3538a BH |
119 | public String getConnectionName() { |
120 | return fConnectionName; | |
eb1bab5b BH |
121 | } |
122 | ||
123 | /* | |
124 | * (non-Javadoc) | |
115b4a01 | 125 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.INewConnectionDialog#getHostName() |
eb1bab5b BH |
126 | */ |
127 | @Override | |
bbb3538a BH |
128 | public String getHostName() { |
129 | return fHostName; | |
eb1bab5b | 130 | } |
d132bcc7 BH |
131 | |
132 | /* | |
133 | * (non-Javadoc) | |
115b4a01 | 134 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.INewConnectionDialog#setTraceControlParent(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent) |
d132bcc7 BH |
135 | */ |
136 | @Override | |
137 | public void setTraceControlParent(ITraceControlComponent parent) { | |
138 | fParent = parent; | |
139 | } | |
140 | ||
141 | /* | |
142 | * (non-Javadoc) | |
115b4a01 | 143 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.INewConnectionDialog#setHosts(org.eclipse.rse.core.model.IHost[]) |
d132bcc7 BH |
144 | */ |
145 | @Override | |
146 | public void setHosts(IHost[] hosts) { | |
147 | if (hosts != null) { | |
148 | fExistingHosts = Arrays.copyOf(hosts, hosts.length); | |
149 | } | |
150 | } | |
eb1bab5b BH |
151 | |
152 | // ------------------------------------------------------------------------ | |
153 | // Operations | |
154 | // ------------------------------------------------------------------------ | |
155 | /* | |
156 | * (non-Javadoc) | |
157 | * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) | |
158 | */ | |
159 | @Override | |
160 | protected void configureShell(Shell newShell) { | |
161 | super.configureShell(newShell); | |
162 | newShell.setText(Messages.TraceControl_NewDialogTitle); | |
31a6a4e4 | 163 | newShell.setImage(Activator.getDefault().loadIcon(TARGET_NEW_CONNECTION_ICON_FILE)); |
eb1bab5b BH |
164 | } |
165 | ||
166 | /* | |
167 | * (non-Javadoc) | |
168 | * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) | |
169 | */ | |
170 | @Override | |
171 | protected Control createDialogArea(Composite parent) { | |
172 | ||
173 | // Main dialog panel | |
174 | fDialogComposite = new Composite(parent, SWT.NONE); | |
175 | GridLayout layout = new GridLayout(1, true); | |
498704b3 BH |
176 | fDialogComposite.setLayout(layout); |
177 | fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
eb1bab5b BH |
178 | |
179 | // Existing connections group | |
180 | fComboGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); | |
4775bcbf | 181 | fComboGroup.setText(Messages.TraceControl_NewNodeExistingConnectionGroupName); |
eb1bab5b BH |
182 | layout = new GridLayout(2, true); |
183 | fComboGroup.setLayout(layout); | |
498704b3 | 184 | GridData data = new GridData(GridData.FILL_HORIZONTAL); |
eb1bab5b BH |
185 | fComboGroup.setLayoutData(data); |
186 | ||
187 | fExistingHostsCombo = new CCombo(fComboGroup, SWT.READ_ONLY); | |
188 | fExistingHostsCombo.setToolTipText(Messages.TraceControl_NewNodeComboToolTip); | |
498704b3 | 189 | fExistingHostsCombo.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1)); |
eb1bab5b BH |
190 | |
191 | String items[] = new String[fExistingHosts.length]; | |
192 | for (int i = 0; i < items.length; i++) { | |
193 | items[i] = String.valueOf(fExistingHosts[i].getAliasName() + " - " + fExistingHosts[i].getHostName()); //$NON-NLS-1$ | |
194 | } | |
195 | ||
196 | fExistingHostsCombo.setItems(items); | |
197 | fExistingHostsCombo.setEnabled(fExistingHosts.length > 0); | |
198 | ||
199 | // Node information grop | |
200 | fTextGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); | |
498704b3 | 201 | layout = new GridLayout(3, true); |
eb1bab5b | 202 | fTextGroup.setLayout(layout); |
498704b3 | 203 | data = new GridData(GridData.FILL_HORIZONTAL); |
eb1bab5b BH |
204 | fTextGroup.setLayoutData(data); |
205 | ||
206 | fButton = new Button(fTextGroup, SWT.CHECK); | |
498704b3 | 207 | fButton.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 3, 1)); |
eb1bab5b BH |
208 | fButton.setText(Messages.TraceControl_NewNodeEditButtonName); |
209 | fButton.setEnabled(fExistingHosts.length > 0); | |
210 | ||
bbb3538a BH |
211 | Label connectionNameLabel = new Label(fTextGroup, SWT.RIGHT); |
212 | connectionNameLabel.setText(Messages.TraceControl_NewNodeConnectionNameLabel); | |
213 | fConnectionNameText = new Text(fTextGroup, SWT.NONE); | |
214 | fConnectionNameText.setToolTipText(Messages.TraceControl_NewNodeConnectionNameTooltip); | |
215 | fConnectionNameText.setEnabled(fExistingHosts.length == 0); | |
eb1bab5b | 216 | |
bbb3538a BH |
217 | Label hostNameLabel = new Label(fTextGroup, SWT.RIGHT); |
218 | hostNameLabel.setText(Messages.TraceControl_NewNodeHostNameLabel); | |
219 | fHostNameText = new Text(fTextGroup, SWT.NONE); | |
220 | fHostNameText.setToolTipText(Messages.TraceControl_NewNodeHostNameTooltip); | |
221 | fHostNameText.setEnabled(fExistingHosts.length == 0); | |
eb1bab5b BH |
222 | |
223 | fButton.addSelectionListener(new SelectionListener() { | |
224 | @Override | |
225 | public void widgetSelected(SelectionEvent e) { | |
226 | if (fButton.getSelection()) { | |
227 | fExistingHostsCombo.deselectAll(); | |
228 | fExistingHostsCombo.setEnabled(false); | |
bbb3538a BH |
229 | fConnectionNameText.setEnabled(true); |
230 | fHostNameText.setEnabled(true); | |
eb1bab5b BH |
231 | } else { |
232 | fExistingHostsCombo.setEnabled(true); | |
bbb3538a BH |
233 | fConnectionNameText.setEnabled(false); |
234 | fHostNameText.setEnabled(false); | |
eb1bab5b BH |
235 | } |
236 | } | |
237 | ||
238 | @Override | |
239 | public void widgetDefaultSelected(SelectionEvent e) { | |
240 | } | |
241 | }); | |
242 | ||
243 | fExistingHostsCombo.addSelectionListener(new SelectionListener() { | |
244 | @Override | |
245 | public void widgetSelected(SelectionEvent e) { | |
246 | int index = fExistingHostsCombo.getSelectionIndex(); | |
bbb3538a BH |
247 | fConnectionNameText.setText(fExistingHosts[index].getAliasName()); |
248 | fHostNameText.setText(fExistingHosts[index].getHostName()); | |
eb1bab5b BH |
249 | } |
250 | ||
251 | @Override | |
252 | public void widgetDefaultSelected(SelectionEvent e) { | |
253 | } | |
254 | }); | |
255 | ||
256 | // layout widgets | |
498704b3 | 257 | data = new GridData(GridData.FILL_HORIZONTAL); |
bbb3538a BH |
258 | fHostNameText.setText("666.666.666.666"); //$NON-NLS-1$ |
259 | Point minSize = fHostNameText.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); | |
eb1bab5b | 260 | data.widthHint = minSize.x + 5; |
498704b3 | 261 | data.horizontalSpan = 2; |
eb1bab5b | 262 | |
bbb3538a BH |
263 | fConnectionNameText.setLayoutData(data); |
264 | fHostNameText.setLayoutData(data); | |
eb1bab5b | 265 | |
bbb3538a | 266 | fHostNameText.setText(""); //$NON-NLS-1$ |
eb1bab5b BH |
267 | |
268 | return fDialogComposite; | |
269 | } | |
270 | ||
271 | /* | |
272 | * (non-Javadoc) | |
273 | * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) | |
274 | */ | |
275 | @Override | |
276 | protected void createButtonsForButtonBar(Composite parent) { | |
277 | createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$ | |
278 | } | |
279 | ||
280 | /* | |
281 | * (non-Javadoc) | |
282 | * @see org.eclipse.jface.dialogs.Dialog#okPressed() | |
283 | */ | |
284 | @Override | |
285 | protected void okPressed() { | |
286 | // Validate input data | |
bbb3538a BH |
287 | fConnectionName = fConnectionNameText.getText(); |
288 | fHostName = fHostNameText.getText(); | |
eb1bab5b | 289 | |
bbb3538a | 290 | if (!"".equals(fHostName)) { //$NON-NLS-1$ |
eb1bab5b | 291 | // If no node name is specified use the node address as name |
bbb3538a BH |
292 | if ("".equals(fConnectionName)) { //$NON-NLS-1$ |
293 | fConnectionName = fHostName; | |
eb1bab5b BH |
294 | } |
295 | // Check if node with name already exists in parent | |
bbb3538a | 296 | if(fParent.containsChild(fConnectionName)) { |
eb1bab5b BH |
297 | MessageDialog.openError(getShell(), |
298 | Messages.TraceControl_NewDialogTitle, | |
bbb3538a | 299 | Messages.TraceControl_AlreadyExistsError + " (" + fConnectionName + ")"); //$NON-NLS-1$//$NON-NLS-2$ |
eb1bab5b BH |
300 | return; |
301 | } | |
302 | } | |
303 | else { | |
304 | return; | |
305 | } | |
306 | // validation successful -> call super.okPressed() | |
307 | super.okPressed(); | |
308 | } | |
309 | } |