| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2015 École Polytechnique de Montréal and others |
| 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 | * Patrick-Jeffrey Pollo Guilbert - Extracted from NewConnectionDialog |
| 11 | **********************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.internal.lttng2.control.ui.views.handlers; |
| 14 | |
| 15 | import org.eclipse.jface.viewers.DoubleClickEvent; |
| 16 | import org.eclipse.jface.viewers.IDoubleClickListener; |
| 17 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 18 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 19 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 20 | import org.eclipse.jface.viewers.StructuredSelection; |
| 21 | import org.eclipse.jface.viewers.TreeViewer; |
| 22 | import org.eclipse.jface.wizard.WizardPage; |
| 23 | import org.eclipse.remote.core.IRemoteConnection; |
| 24 | import org.eclipse.remote.core.IRemoteConnectionType; |
| 25 | import org.eclipse.remote.core.IRemoteConnectionWorkingCopy; |
| 26 | import org.eclipse.remote.core.exception.RemoteConnectionException; |
| 27 | import org.eclipse.remote.ui.IRemoteUIConnectionService; |
| 28 | import org.eclipse.remote.ui.IRemoteUIConnectionWizard; |
| 29 | import org.eclipse.swt.SWT; |
| 30 | import org.eclipse.swt.events.SelectionAdapter; |
| 31 | import org.eclipse.swt.events.SelectionEvent; |
| 32 | import org.eclipse.swt.layout.GridData; |
| 33 | import org.eclipse.swt.layout.GridLayout; |
| 34 | import org.eclipse.swt.widgets.Button; |
| 35 | import org.eclipse.swt.widgets.Composite; |
| 36 | import org.eclipse.swt.widgets.Label; |
| 37 | import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator; |
| 38 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.ConnectionContentProvider; |
| 39 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.ConnectionTreeLabelProvider; |
| 40 | import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages; |
| 41 | |
| 42 | final class ConnectionPage extends WizardPage { |
| 43 | /** |
| 44 | * |
| 45 | * |
| 46 | * |
| 47 | */ |
| 48 | |
| 49 | private static final int BUTTONS_NUMBER_OF_COLUMNS = 3; |
| 50 | private Button fNewButton; |
| 51 | private Button fEditButton; |
| 52 | private IRemoteConnection fConnection; |
| 53 | private TreeViewer fConnectionTree = null; |
| 54 | |
| 55 | ConnectionPage(String pageName) { |
| 56 | super(pageName); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void createControl(Composite parent) { |
| 61 | GridData gd; |
| 62 | Composite dialogComposite = new Composite(parent, SWT.NONE); |
| 63 | GridLayout layout = new GridLayout(1, true); |
| 64 | dialogComposite.setLayout(layout); |
| 65 | dialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 66 | |
| 67 | Label label = new Label(dialogComposite, SWT.NONE); |
| 68 | label.setText(Messages.TraceControl_NewNodeExistingConnectionGroupName); |
| 69 | gd = new GridData(); |
| 70 | label.setLayoutData(gd); |
| 71 | gd.widthHint = label.computeSize(-1, -1).x + convertWidthInCharsToPixels(NewConnectionWizard.LABEL_WIDTH_CHARS); |
| 72 | // Existing connections group |
| 73 | this.fConnectionTree = new TreeViewer(dialogComposite); |
| 74 | gd = new GridData(SWT.FILL, SWT.FILL, true, true); |
| 75 | this.fConnectionTree.getTree().setLayoutData(gd); |
| 76 | gd.widthHint = convertWidthInCharsToPixels(NewConnectionWizard.CONNECTIONTREE_WIDTH_CHARS); |
| 77 | gd.heightHint = convertHeightInCharsToPixels(NewConnectionWizard.CONNECTIONTREE_HEIGHT_CHARS); |
| 78 | fConnectionTree.setLabelProvider(new ConnectionTreeLabelProvider()); |
| 79 | fConnectionTree.setContentProvider(new ConnectionContentProvider()); |
| 80 | fConnectionTree.addSelectionChangedListener(new ISelectionChangedListener() { |
| 81 | @Override |
| 82 | public void selectionChanged(SelectionChangedEvent event) { |
| 83 | ConnectionPage.this.onSelectionChanged(); |
| 84 | } |
| 85 | }); |
| 86 | fConnectionTree.addDoubleClickListener(new IDoubleClickListener() { |
| 87 | @Override |
| 88 | public void doubleClick(DoubleClickEvent event) { |
| 89 | getWizard().performFinish(); |
| 90 | } |
| 91 | }); |
| 92 | |
| 93 | Composite buttons = new Composite(dialogComposite, SWT.NONE); |
| 94 | layout = new GridLayout(BUTTONS_NUMBER_OF_COLUMNS, true); |
| 95 | layout.marginHeight = 0; |
| 96 | layout.marginWidth = 0; |
| 97 | buttons.setLayout(layout); |
| 98 | buttons.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false)); |
| 99 | |
| 100 | new Label(buttons, SWT.NONE); |
| 101 | |
| 102 | fEditButton = new Button(buttons, SWT.PUSH); |
| 103 | fEditButton.setText(Messages.TraceControl_NewNodeEditButtonName); |
| 104 | setButtonLayoutData(fEditButton); |
| 105 | fEditButton.addSelectionListener(new SelectionAdapter() { |
| 106 | @Override |
| 107 | public void widgetSelected(SelectionEvent e) { |
| 108 | onEditConnection(); |
| 109 | } |
| 110 | }); |
| 111 | |
| 112 | fNewButton = new Button(buttons, SWT.PUSH); |
| 113 | fNewButton.setText(Messages.TraceControl_NewNodeCreateButtonText); |
| 114 | setButtonLayoutData(fNewButton); |
| 115 | fNewButton.addSelectionListener(new SelectionAdapter() { |
| 116 | @Override |
| 117 | public void widgetSelected(SelectionEvent e) { |
| 118 | onNewConnection(); |
| 119 | } |
| 120 | }); |
| 121 | setControl(dialogComposite); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | private void onSelectionChanged() { |
| 126 | setConnection(); |
| 127 | setPageComplete(true); |
| 128 | fEditButton.setEnabled(canEdit(fConnection)); |
| 129 | fNewButton.setEnabled(getServiceForCreation() != null); |
| 130 | } |
| 131 | |
| 132 | private void onEditConnection() { |
| 133 | setConnection(); |
| 134 | if (fConnection != null) { |
| 135 | IRemoteConnectionType connectionType = fConnection.getConnectionType(); |
| 136 | if (connectionType != null) { |
| 137 | IRemoteUIConnectionService connManager = connectionType.getService(IRemoteUIConnectionService.class); |
| 138 | if (connManager != null) { |
| 139 | IRemoteUIConnectionWizard wiz = connManager.getConnectionWizard(getShell()); |
| 140 | wiz.setConnection(fConnection.getWorkingCopy()); |
| 141 | IRemoteConnectionWorkingCopy result = wiz.open(); |
| 142 | if (result != null) { |
| 143 | try { |
| 144 | result.save(); |
| 145 | } catch (RemoteConnectionException e) { |
| 146 | Activator.getDefault().logError(e.toString(), e); |
| 147 | } |
| 148 | fConnectionTree.refresh(); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private void setConnection() { |
| 156 | Object o = ((IStructuredSelection) fConnectionTree.getSelection()).getFirstElement(); |
| 157 | fConnection = o instanceof IRemoteConnection ? (IRemoteConnection) o : null; |
| 158 | } |
| 159 | |
| 160 | private static boolean canEdit(IRemoteConnection conn) { |
| 161 | if (conn == null) { |
| 162 | return false; |
| 163 | } |
| 164 | return conn.getConnectionType().canEdit(); |
| 165 | } |
| 166 | |
| 167 | private IRemoteConnectionType getServiceForCreation() { |
| 168 | Object o = ((IStructuredSelection) fConnectionTree.getSelection()).getFirstElement(); |
| 169 | IRemoteConnectionType result = null; |
| 170 | if (o instanceof IRemoteConnectionType) { |
| 171 | result = (IRemoteConnectionType) o; |
| 172 | } else if (o instanceof IRemoteConnection) { |
| 173 | IRemoteConnection iRemoteConnection = (IRemoteConnection) o; |
| 174 | result = iRemoteConnection.getConnectionType(); |
| 175 | } else { |
| 176 | return null; |
| 177 | } |
| 178 | if (!result.canAdd()) { |
| 179 | return null; |
| 180 | } |
| 181 | |
| 182 | return result; |
| 183 | } |
| 184 | |
| 185 | public void onNewConnection() { |
| 186 | IRemoteConnectionType rs = getServiceForCreation(); |
| 187 | if (rs != null) { |
| 188 | IRemoteUIConnectionService uiService = rs.getService(IRemoteUIConnectionService.class); |
| 189 | if (uiService != null) { |
| 190 | IRemoteUIConnectionWizard wiz = uiService.getConnectionWizard(getShell()); |
| 191 | if (wiz != null) { |
| 192 | IRemoteConnectionWorkingCopy wc = wiz.open(); |
| 193 | if (wc != null) { |
| 194 | IRemoteConnection conn = null; |
| 195 | try { |
| 196 | conn = wc.save(); |
| 197 | fConnectionTree.refresh(); |
| 198 | fConnectionTree.setSelection(new StructuredSelection(conn), true); |
| 199 | } catch (RemoteConnectionException e) { |
| 200 | Activator.getDefault().logError(e.toString(), e); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |