| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2012, 2013 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.handlers; |
| 13 | |
| 14 | import java.util.Iterator; |
| 15 | import java.util.List; |
| 16 | |
| 17 | import org.eclipse.core.commands.ExecutionException; |
| 18 | import org.eclipse.core.runtime.IProgressMonitor; |
| 19 | import org.eclipse.jface.viewers.ISelection; |
| 20 | import org.eclipse.jface.viewers.StructuredSelection; |
| 21 | import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceSessionState; |
| 22 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView; |
| 23 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceDomainComponent; |
| 24 | import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent; |
| 25 | import org.eclipse.ui.IWorkbenchPage; |
| 26 | |
| 27 | /** |
| 28 | * <p> |
| 29 | * Command handler implementation to add contexts to all channels and all events. |
| 30 | * </p> |
| 31 | * |
| 32 | * @author Bernd Hufmann |
| 33 | */ |
| 34 | public class AddContextOnDomainHandler extends BaseAddContextHandler { |
| 35 | |
| 36 | // ------------------------------------------------------------------------ |
| 37 | // Attributes |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | // ------------------------------------------------------------------------ |
| 41 | // Operations |
| 42 | // ------------------------------------------------------------------------ |
| 43 | |
| 44 | @Override |
| 45 | public void addContexts(CommandParameter param, List<String> contextNames, IProgressMonitor monitor) throws ExecutionException { |
| 46 | if (param instanceof DomainCommandParameter) { |
| 47 | TraceDomainComponent domain = ((DomainCommandParameter)param).getDomain(); |
| 48 | domain.addContexts(contextNames, monitor); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public boolean isEnabled() { |
| 54 | |
| 55 | // Get workbench page for the Control View |
| 56 | IWorkbenchPage page = getWorkbenchPage(); |
| 57 | if (page == null) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | TraceDomainComponent domain = null; |
| 62 | TraceSessionComponent session = null; |
| 63 | |
| 64 | // Check if one domain is selected |
| 65 | ISelection selection = page.getSelection(ControlView.ID); |
| 66 | if (selection instanceof StructuredSelection) { |
| 67 | StructuredSelection structered = ((StructuredSelection) selection); |
| 68 | for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) { |
| 69 | Object element = iterator.next(); |
| 70 | if (element instanceof TraceDomainComponent) { |
| 71 | TraceDomainComponent tmpDomain = (TraceDomainComponent) element; |
| 72 | session = (TraceSessionComponent) tmpDomain.getParent(); |
| 73 | |
| 74 | // Add only TraceDomainComponent whose TraceSessionComponent parent is inactive and not destroyed |
| 75 | if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) { |
| 76 | domain = tmpDomain; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | boolean isEnabled = domain != null; |
| 83 | |
| 84 | fLock.lock(); |
| 85 | try { |
| 86 | fParam = null; |
| 87 | if (isEnabled) { |
| 88 | fParam = new DomainCommandParameter(session, domain); |
| 89 | } |
| 90 | } finally { |
| 91 | fLock.unlock(); |
| 92 | } |
| 93 | |
| 94 | return isEnabled; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | } |