Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3bafc98665452c09ec686f430f2f52015e367cb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**********************************************************************
 * Copyright (c) 2012, 2015 Ericsson
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Bernd Hufmann - Initial API and implementation
 *   Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
 **********************************************************************/
package org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs;

import static java.text.MessageFormat.format;
import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.remote.core.IRemoteConnection;
import org.eclipse.remote.core.IRemoteConnectionHostService;
import org.eclipse.remote.core.IRemoteConnectionType;
import org.eclipse.remote.core.IRemoteConnectionWorkingCopy;
import org.eclipse.remote.core.IRemoteServicesManager;
import org.eclipse.remote.core.exception.RemoteConnectionException;
import org.eclipse.remote.ui.IRemoteUIConnectionService;
import org.eclipse.remote.ui.IRemoteUIConnectionWizard;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator;
import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
import org.eclipse.tracecompass.tmf.remote.core.proxy.TmfRemoteConnectionFactory;

/**
 * <p>
 * Dialog box for connection information.
 * </p>
 *
 * @author Bernd Hufmann
 */
public class NewConnectionDialog extends Dialog implements INewConnectionDialog {

    private static final int BUTTONS_NUMBER_OF_COLUMNS = 3;
    private static final int LABEL_WIDTH_CHARS = 4;
    private static final int CONNECTIONTREE_HEIGHT_CHARS = 10;
    private static final int CONNECTIONTREE_WIDTH_CHARS = 40;
    // ------------------------------------------------------------------------
    // Constants
    // ------------------------------------------------------------------------
    private static final String TARGET_NEW_CONNECTION_ICON_FILE = "icons/elcl16/target_add.gif"; //$NON-NLS-1$
    private static final String PROVIDERS_ICON_FILE = "icons/obj16/providers.gif"; //$NON-NLS-1$
    private static final String CONNECTION_ICON_FILE = "icons/obj16/target_connected.gif"; //$NON-NLS-1$

    private final class ConnectionTreeLabelProvider extends LabelProvider {
        @Override
        public String getText(Object element) {
            if (element instanceof IRemoteConnection) {
                IRemoteConnection rc = (IRemoteConnection) element;
                return getConnectionLabel(rc);
            } else if (element instanceof IRemoteConnectionType) {
                IRemoteConnectionType rs = (IRemoteConnectionType) element;
                return rs.getName();
            }
            return Messages.TraceControl_UnknownNode;
        }

        @Override
        public Image getImage(Object element) {
            if (element instanceof IRemoteConnection) {
                return Activator.getDefault().loadIcon(CONNECTION_ICON_FILE);
            }
            return Activator.getDefault().loadIcon(PROVIDERS_ICON_FILE);
        }
    }

    private static final class ConnectionContentProvider implements ITreeContentProvider {
        private static final Object[] NO_CHILDREN = {};

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            // Do nothing
        }

        @Override
        public void dispose() {
            // Do nothing
        }

        @Override
        public Object[] getElements(Object inputElement) {
            List<Object> children = new ArrayList<>();
            IRemoteServicesManager manager = TmfRemoteConnectionFactory.getService(IRemoteServicesManager.class);
            if (manager != null) {
                children.addAll(manager.getAllConnectionTypes());
            }
            return children.toArray();
        }

        @Override
        public Object[] getChildren(Object parentElement) {
            if (parentElement instanceof IRemoteConnectionType) {
                return getConnections((IRemoteConnectionType) parentElement);
            }
            return NO_CHILDREN;
        }

        private static IRemoteConnection[] getConnections(IRemoteConnectionType parentElement) {
            List<IRemoteConnection> connectionList = parentElement.getConnections();
            IRemoteConnection[] result = connectionList.toArray(new IRemoteConnection[connectionList.size()]);
            Arrays.sort(result, (o1, o2) -> getConnectionLabel(o1).compareTo(getConnectionLabel(o2)));
            return result;
        }

        @Override
        public Object getParent(Object element) {
            if (element instanceof IRemoteConnection) {
                return ((IRemoteConnection) element).getConnectionType();
            }
            return null;
        }

        @Override
        public boolean hasChildren(Object element) {
            return getChildren(element).length > 0;
        }

    }

    // ------------------------------------------------------------------------
    // Attributes
    // ------------------------------------------------------------------------
    /**
     * The host combo box.
     */
    private TreeViewer fConnectionTree = null;
    /**
     * The push button for creating a new connection.
     */
    private Button fNewButton = null;
    /**
     * The push button for editing a connection.
     */
    private Button fEditButton = null;

    private IRemoteConnection fConnection;

    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------
    /**
     * Constructor
     *
     * @param shell
     *            The shell
     */
    public NewConnectionDialog(Shell shell) {
        super(shell);
        setShellStyle(SWT.RESIZE | getShellStyle());
    }

    // ------------------------------------------------------------------------
    // Operations
    // ------------------------------------------------------------------------

    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText(Messages.TraceControl_NewDialogTitle);
        newShell.setImage(Activator.getDefault().loadIcon(TARGET_NEW_CONNECTION_ICON_FILE));
    }

    @Override
    protected Control createContents(Composite parent) {
        Control result = super.createContents(parent);
        fConnectionTree.setAutoExpandLevel(2);
        fConnectionTree.setInput(this);

        IRemoteServicesManager manager = TmfRemoteConnectionFactory.getService(IRemoteServicesManager.class);
        if (manager == null) {
            return result;
        }
        List<IRemoteConnectionType> providers = manager.getAllConnectionTypes();
        if (!providers.isEmpty()) {
            IRemoteConnectionType provider = providers.get(0);
            IRemoteConnection[] connections = ConnectionContentProvider.getConnections(provider);
            if (connections.length > 0) {
                fConnectionTree.setSelection(new StructuredSelection(connections[0]));
            } else {
                fConnectionTree.setSelection(new StructuredSelection(provider));
            }
        } else {
            onSelectionChanged();
        }
        return result;
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        // Main dialog panel
        GridData gd;
        Composite dialogComposite = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout(1, true);
        dialogComposite.setLayout(layout);
        dialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH));

        Label label = new Label(dialogComposite, SWT.NONE);
        label.setText(Messages.TraceControl_NewNodeExistingConnectionGroupName);
        gd = new GridData();
        label.setLayoutData(gd );
        gd.widthHint = label.computeSize(-1, -1).x + convertWidthInCharsToPixels(LABEL_WIDTH_CHARS);
        // Existing connections group
        fConnectionTree = new TreeViewer(dialogComposite);
        gd = new GridData(SWT.FILL, SWT.FILL, true, true);
        fConnectionTree.getTree().setLayoutData(gd);
        gd.widthHint = convertWidthInCharsToPixels(CONNECTIONTREE_WIDTH_CHARS);
        gd.heightHint = convertHeightInCharsToPixels(CONNECTIONTREE_HEIGHT_CHARS);
        fConnectionTree.setLabelProvider(new ConnectionTreeLabelProvider());
        fConnectionTree.setContentProvider(new ConnectionContentProvider());
        fConnectionTree.addSelectionChangedListener(event -> onSelectionChanged());
        fConnectionTree.addDoubleClickListener(event -> okPressed());

        Composite buttons = new Composite(dialogComposite, SWT.NONE);
        layout = new GridLayout(BUTTONS_NUMBER_OF_COLUMNS, true);
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        buttons.setLayout(layout);
        buttons.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));

        new Label(buttons, SWT.NONE);

        fEditButton = new Button(buttons, SWT.PUSH);
        fEditButton.setText(Messages.TraceControl_NewNodeEditButtonName);
        setButtonLayoutData(fEditButton);
        fEditButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                onEditConnection();
            }
        });

        fNewButton = new Button(buttons, SWT.PUSH);
        fNewButton.setText(Messages.TraceControl_NewNodeCreateButtonText);
        setButtonLayoutData(fNewButton);
        fNewButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                onNewConnection();
            }
        });

        return dialogComposite;
    }

    private void onSelectionChanged() {
        setConnection();
        getButton(OK).setEnabled(fConnection != null);
        fEditButton.setEnabled(canEdit(fConnection));
        fNewButton.setEnabled(getServiceForCreation() != null);
    }

    private IRemoteConnectionType getServiceForCreation() {
        Object o = ((IStructuredSelection) fConnectionTree.getSelection()).getFirstElement();
        IRemoteConnectionType result = null;
        if (o instanceof IRemoteConnectionType) {
            result = (IRemoteConnectionType) o;
        } else if (o instanceof IRemoteConnection) {
            result = ((IRemoteConnection) o).getConnectionType();
        } else {
            return null;
        }

        if (!result.canAdd()) {
            return null;
        }

        return result;
    }

    private static boolean canEdit(IRemoteConnection conn) {
        if (conn == null) {
            return false;
        }
        return conn.getConnectionType().canEdit();
    }

    private void onNewConnection() {
        IRemoteConnectionType rs = getServiceForCreation();
        if (rs != null) {
            IRemoteUIConnectionService uiService = rs.getService(IRemoteUIConnectionService.class);
            if (uiService != null) {
                IRemoteUIConnectionWizard wiz = uiService.getConnectionWizard(getShell());
                if (wiz != null) {
                    IRemoteConnectionWorkingCopy wc = wiz.open();
                    if (wc != null) {
                        IRemoteConnection conn = null;
                        try {
                            conn = wc.save();
                        } catch (RemoteConnectionException e) {
                            Activator.getDefault().logWarning("Connection configuration could not be saved for " + fConnection.getName() , e); //$NON-NLS-1$
                        }
                        if (conn != null) {
                            fConnectionTree.refresh();
                            fConnectionTree.setSelection(new StructuredSelection(conn), true);
                        }
                    }
                }
            }
        }
    }

    private void onEditConnection() {
        setConnection();
        if (fConnection != null) {
            IRemoteUIConnectionService ui = fConnection.getConnectionType().getService(IRemoteUIConnectionService.class);
            if (ui != null) {
                    IRemoteUIConnectionWizard wiz = ui.getConnectionWizard(getShell());
                    wiz.setConnection(fConnection.getWorkingCopy());
                    IRemoteConnectionWorkingCopy result = wiz.open();
                    if (result != null) {
                        try {
                            result.save();
                        } catch (RemoteConnectionException e) {
                            Activator.getDefault().logWarning("Connection configuration could not be saved for " + fConnection.getName() , e); //$NON-NLS-1$
                        }
                        fConnectionTree.refresh();
                    }
            }
        }
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", true); //$NON-NLS-1$
        createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$
    }

    @Override
    protected void okPressed() {
        setConnection();
        if (fConnection != null) {
            super.okPressed();
        }
    }

    private void setConnection() {
        Object o = ((IStructuredSelection) fConnectionTree.getSelection()).getFirstElement();
        fConnection = o instanceof IRemoteConnection ? (IRemoteConnection) o : null;
    }

    @Override
    public IRemoteConnection getConnection() {
        return fConnection;
    }

    private static String getConnectionLabel(IRemoteConnection rc) {
        StringBuffer label = new StringBuffer();
        label.append(rc.getName());
        if (rc.hasService(IRemoteConnectionHostService.class)) {
            IRemoteConnectionHostService service = checkNotNull(rc.getService(IRemoteConnectionHostService.class));
            label.append(format(" [{0}]", service.getHostname())); //$NON-NLS-1$
        }
        return label.toString();
    }
}

Back to the top