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