| 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 | **********************************************************************/ |
| 12 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs; |
| 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; |
| 19 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; |
| 20 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages; |
| 21 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceChannelComponent; |
| 22 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceDomainComponent; |
| 23 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent; |
| 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.graphics.Point; |
| 29 | import org.eclipse.swt.layout.GridData; |
| 30 | import org.eclipse.swt.layout.GridLayout; |
| 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.Shell; |
| 35 | |
| 36 | /** |
| 37 | * <p> |
| 38 | * Dialog box for collecting information about the events to enable. |
| 39 | * </p> |
| 40 | * |
| 41 | * @author Bernd Hufmann |
| 42 | */ |
| 43 | public class GetEventInfoDialog extends Dialog implements IGetEventInfoDialog { |
| 44 | |
| 45 | // ------------------------------------------------------------------------ |
| 46 | // Constants |
| 47 | // ------------------------------------------------------------------------ |
| 48 | /** |
| 49 | * The icon file for this dialog box. |
| 50 | */ |
| 51 | public static final String TARGET_NEW_CONNECTION_ICON_FILE = "icons/elcl16/enable_event.gif"; //$NON-NLS-1$ |
| 52 | |
| 53 | // ------------------------------------------------------------------------ |
| 54 | // Attributes |
| 55 | // ------------------------------------------------------------------------ |
| 56 | /** |
| 57 | * The dialog composite. |
| 58 | */ |
| 59 | private Composite fDialogComposite = null; |
| 60 | /** |
| 61 | * The Group for the session combo box. |
| 62 | */ |
| 63 | private Group fSessionsGroup = null; |
| 64 | /** |
| 65 | * The Group for the channel combo box. |
| 66 | */ |
| 67 | private Group fChannelsGroup = null; |
| 68 | /** |
| 69 | * The session combo box. |
| 70 | */ |
| 71 | private CCombo fSessionsCombo = null; |
| 72 | /** |
| 73 | * The channel combo box. |
| 74 | */ |
| 75 | private CCombo fChannelsCombo = null; |
| 76 | /** |
| 77 | * The list of available sessions. |
| 78 | */ |
| 79 | private TraceSessionComponent[] fSessions; |
| 80 | /** |
| 81 | * True for kernel, false for UST. |
| 82 | */ |
| 83 | private boolean fIsKernel; |
| 84 | /** |
| 85 | * Index in session array (selected session). |
| 86 | */ |
| 87 | private int fSessionIndex = 0; |
| 88 | /** |
| 89 | * The Channel where the events should be enabled. |
| 90 | */ |
| 91 | private TraceChannelComponent fChannel; |
| 92 | /** |
| 93 | * List of available channels of the selected session. |
| 94 | */ |
| 95 | private TraceChannelComponent[] fChannels; |
| 96 | |
| 97 | // ------------------------------------------------------------------------ |
| 98 | // Constructors |
| 99 | // ------------------------------------------------------------------------ |
| 100 | /** |
| 101 | * Constructor of dialog box. |
| 102 | * @param shell - the shell for the dialog box |
| 103 | */ |
| 104 | public GetEventInfoDialog(Shell shell) { |
| 105 | super(shell); |
| 106 | setShellStyle(SWT.RESIZE); |
| 107 | } |
| 108 | |
| 109 | // ------------------------------------------------------------------------ |
| 110 | // Accessors |
| 111 | // ------------------------------------------------------------------------ |
| 112 | /* |
| 113 | * (non-Javadoc) |
| 114 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IEnableEventsDialog#getSession() |
| 115 | */ |
| 116 | @Override |
| 117 | public TraceSessionComponent getSession() { |
| 118 | return fSessions[fSessionIndex]; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * (non-Javadoc) |
| 123 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IEnableEventsDialog#getChannel() |
| 124 | */ |
| 125 | @Override |
| 126 | public TraceChannelComponent getChannel() { |
| 127 | return fChannel; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * (non-Javadoc) |
| 132 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IGetEventInfoDialog#setIsKernel(boolean) |
| 133 | */ |
| 134 | @Override |
| 135 | public void setIsKernel(boolean isKernel) { |
| 136 | fIsKernel = isKernel; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * (non-Javadoc) |
| 141 | * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.dialogs.IGetEventInfoDialog#setSessions(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent[]) |
| 142 | */ |
| 143 | @Override |
| 144 | public void setSessions(TraceSessionComponent[] sessions) { |
| 145 | fSessions = Arrays.copyOf(sessions, sessions.length); |
| 146 | } |
| 147 | |
| 148 | // ------------------------------------------------------------------------ |
| 149 | // Operations |
| 150 | // ------------------------------------------------------------------------ |
| 151 | /* |
| 152 | * (non-Javadoc) |
| 153 | * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) |
| 154 | */ |
| 155 | @Override |
| 156 | protected void configureShell(Shell newShell) { |
| 157 | super.configureShell(newShell); |
| 158 | newShell.setText(Messages.TraceControl_EnableEventsDialogTitle); |
| 159 | newShell.setImage(Activator.getDefault().loadIcon(TARGET_NEW_CONNECTION_ICON_FILE)); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * (non-Javadoc) |
| 164 | * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) |
| 165 | */ |
| 166 | @Override |
| 167 | protected Control createDialogArea(Composite parent) { |
| 168 | |
| 169 | // Main dialog panel |
| 170 | fDialogComposite = new Composite(parent, SWT.NONE); |
| 171 | GridLayout layout = new GridLayout(1, true); |
| 172 | fDialogComposite.setLayout(layout); |
| 173 | fDialogComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 174 | |
| 175 | fSessionsGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); |
| 176 | fSessionsGroup.setText(Messages.TraceControl_EnableEventsSessionGroupName); |
| 177 | layout = new GridLayout(1, true); |
| 178 | fSessionsGroup.setLayout(layout); |
| 179 | GridData data = new GridData(GridData.FILL_HORIZONTAL); |
| 180 | fSessionsGroup.setLayoutData(data); |
| 181 | |
| 182 | fSessionsCombo = new CCombo(fSessionsGroup, SWT.READ_ONLY); |
| 183 | fSessionsCombo.setToolTipText(Messages.TraceControl_EnableEventsSessionsTooltip); |
| 184 | fSessionsCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 185 | |
| 186 | String items[] = new String[fSessions.length]; |
| 187 | for (int i = 0; i < items.length; i++) { |
| 188 | items[i] = String.valueOf(fSessions[i].getName()); |
| 189 | } |
| 190 | |
| 191 | fSessionsCombo.setItems(items); |
| 192 | fSessionsCombo.setEnabled(fSessions.length > 0); |
| 193 | |
| 194 | fChannelsGroup = new Group(fDialogComposite, SWT.SHADOW_NONE); |
| 195 | fChannelsGroup.setText(Messages.TraceControl_EnableEventsChannelGroupName); |
| 196 | layout = new GridLayout(1, true); |
| 197 | fChannelsGroup.setLayout(layout); |
| 198 | data = new GridData(GridData.FILL_HORIZONTAL); |
| 199 | fChannelsGroup.setLayoutData(data); |
| 200 | |
| 201 | fChannelsCombo = new CCombo(fChannelsGroup, SWT.READ_ONLY); |
| 202 | fChannelsCombo.setToolTipText(Messages.TraceControl_EnableEventsChannelsTooltip); |
| 203 | fChannelsCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 204 | fChannelsCombo.setEnabled(false); |
| 205 | |
| 206 | fSessionsCombo.addSelectionListener(new SelectionListener() { |
| 207 | @Override |
| 208 | public void widgetSelected(SelectionEvent e) { |
| 209 | fSessionIndex = fSessionsCombo.getSelectionIndex(); |
| 210 | |
| 211 | if (fSessionIndex >= 0) { |
| 212 | TraceDomainComponent domain = null; |
| 213 | TraceDomainComponent[] domains = fSessions[fSessionIndex].getDomains(); |
| 214 | for (int i = 0; i < domains.length; i++) { |
| 215 | |
| 216 | if (domains[i].isKernel() == fIsKernel) { |
| 217 | domain = domains[i]; |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (domain != null) { |
| 223 | fChannels = domain.getChannels(); |
| 224 | String items[] = new String[fChannels.length]; |
| 225 | for (int i = 0; i < items.length; i++) { |
| 226 | items[i] = String.valueOf(fChannels[i].getName()); |
| 227 | } |
| 228 | fChannelsCombo.setItems(items); |
| 229 | fChannelsCombo.setEnabled(fChannels.length > 0); |
| 230 | } else { |
| 231 | fChannelsCombo.setItems(new String[0]); |
| 232 | fChannelsCombo.setEnabled(false); |
| 233 | fChannels = null; |
| 234 | } |
| 235 | fChannelsCombo.getParent().getParent().layout(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public void widgetDefaultSelected(SelectionEvent e) { |
| 241 | } |
| 242 | }); |
| 243 | |
| 244 | getShell().setMinimumSize(new Point(300, 200)); |
| 245 | |
| 246 | return fDialogComposite; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * (non-Javadoc) |
| 251 | * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) |
| 252 | */ |
| 253 | @Override |
| 254 | protected void createButtonsForButtonBar(Composite parent) { |
| 255 | createButton(parent, IDialogConstants.CANCEL_ID, "&Cancel", true); //$NON-NLS-1$ |
| 256 | createButton(parent, IDialogConstants.OK_ID, "&Ok", true); //$NON-NLS-1$ |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * (non-Javadoc) |
| 261 | * @see org.eclipse.jface.dialogs.Dialog#okPressed() |
| 262 | */ |
| 263 | @Override |
| 264 | protected void okPressed() { |
| 265 | |
| 266 | if (fSessionsCombo.getSelectionIndex() < 0) { |
| 267 | MessageDialog.openError(getShell(), |
| 268 | Messages.TraceControl_EnableEventsDialogTitle, |
| 269 | Messages.TraceControl_EnableEventsNoSessionError); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | fSessionIndex = fSessionsCombo.getSelectionIndex(); |
| 274 | |
| 275 | if ((fChannels != null) && (fChannels.length > 0) && (fChannelsCombo.getSelectionIndex() < 0)) { |
| 276 | MessageDialog.openError(getShell(), |
| 277 | Messages.TraceControl_EnableEventsDialogTitle, |
| 278 | Messages.TraceControl_EnableEventsNoChannelError); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | if ((fChannels != null) && (fChannels.length > 0)) { |
| 283 | fChannel = fChannels[fChannelsCombo.getSelectionIndex()]; |
| 284 | } |
| 285 | |
| 286 | super.okPressed(); |
| 287 | } |
| 288 | } |