Fix for bug 382684 (connection re-use)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / handlers / ChangeEventStateHandler.java
index b0647a4a6dca8a30a7914528e0ea89e528bae353..7ec0916be81fb4cb8ddf63d5f935042a7d1b2878 100644 (file)
@@ -1,12 +1,12 @@
 /**********************************************************************
  * Copyright (c) 2012 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: 
+ *
+ * Contributors:
  *   Bernd Hufmann - Initial API and implementation
  **********************************************************************/
 package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers;
@@ -23,10 +23,10 @@ import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.linuxtools.internal.lttng2.core.control.model.TraceEnablement;
 import org.eclipse.linuxtools.internal.lttng2.ui.Activator;
 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
-import org.eclipse.linuxtools.internal.lttng2.ui.views.control.Messages;
-import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.TraceEnablement;
+import org.eclipse.linuxtools.internal.lttng2.ui.views.control.messages.Messages;
 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceChannelComponent;
 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceEventComponent;
 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceSessionComponent;
@@ -35,10 +35,11 @@ import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
 
 /**
- * <b><u>EnableChannelHandler</u></b>
  * <p>
  * Base Command handler implementation to enable or disabling a trace channel.
  * </p>
+ *
+ * @author Bernd Hufmann
  */
 abstract public class ChangeEventStateHandler extends BaseControlViewHandler {
 
@@ -46,21 +47,17 @@ abstract public class ChangeEventStateHandler extends BaseControlViewHandler {
     // Attributes
     // ------------------------------------------------------------------------
     /**
-     * Channel component reference.
+     * The command execution parameter.
      */
-    protected TraceChannelComponent fChannel = null;
-    /**
-     * The list of kernel channel components the command is to be executed on. 
-     */
-    protected List<TraceEventComponent> fEvents = new ArrayList<TraceEventComponent>();
-    
+    protected Parameter fParam;
+
     // ------------------------------------------------------------------------
     // Accessors
     // ------------------------------------------------------------------------
     /**
      * @return the new state to set
      */
-    abstract protected TraceEnablement getNewState(); 
+    abstract protected TraceEnablement getNewState();
 
     // ------------------------------------------------------------------------
     // Operations
@@ -68,11 +65,11 @@ abstract public class ChangeEventStateHandler extends BaseControlViewHandler {
     /**
      * Change the state
      * @param channel - channel of events to be enabled
-     * @param eventNames - list event names  
+     * @param eventNames - list event names
      * @param monitor - a progress monitor
      * @throws ExecutionException
      */
-    abstract protected void changeState(TraceChannelComponent channel, List<String> eventNames, IProgressMonitor monitor) throws ExecutionException; 
+    abstract protected void changeState(TraceChannelComponent channel, List<String> eventNames, IProgressMonitor monitor) throws ExecutionException;
 
     /*
      * (non-Javadoc)
@@ -87,60 +84,71 @@ abstract public class ChangeEventStateHandler extends BaseControlViewHandler {
             return false;
         }
 
-        Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
-            @Override
-            protected IStatus run(IProgressMonitor monitor) {
-                String errorString = null;
-
-                TraceSessionComponent session = null;
-                
-                try {
-                    boolean isAll = false;
-                    if (fChannel != null) {
-                        session = fChannel.getSession();
-                        List<String> eventNames = new ArrayList<String>();
-                        for (Iterator<TraceEventComponent> iterator = fEvents.iterator(); iterator.hasNext();) {
-                            // Enable/disable all selected channels which are disabled
-                            TraceEventComponent event = (TraceEventComponent) iterator.next();
-                            
-                            // Workaround for wildcard handling in lttng-tools
-                            if ("*".equals(event.getName())) { //$NON-NLS-1$
-                                isAll = true;
-                            } else { 
-                                eventNames.add(event.getName());
+        fLock.lock();
+        try {
+
+            final Parameter param = new Parameter(fParam);
+
+            Job job = new Job(Messages.TraceControl_ChangeChannelStateJob) {
+                @Override
+                protected IStatus run(IProgressMonitor monitor) {
+                    Exception error = null;
+
+                    TraceSessionComponent session = null;
+
+                    try {
+                        boolean isAll = false;
+                        if (param.getChannel() != null) {
+                            session = param.getChannel().getSession();
+                            List<String> eventNames = new ArrayList<String>();
+                            List<TraceEventComponent> events = param.getEvents();
+
+                            for (Iterator<TraceEventComponent> iterator = events.iterator(); iterator.hasNext();) {
+                                // Enable/disable all selected channels which are disabled
+                                TraceEventComponent event = iterator.next();
+
+                                // Workaround for wildcard handling in lttng-tools
+                                if ("*".equals(event.getName())) { //$NON-NLS-1$
+                                    isAll = true;
+                                } else {
+                                    eventNames.add(event.getName());
+                                }
+                            }
+                            if (isAll) {
+                                changeState(param.getChannel(), null, monitor);
                             }
-                        }
-                        if (isAll) {
-                            changeState(fChannel, null, monitor);
-                        }
 
-                        if (eventNames.size() > 0) {
-                            changeState(fChannel, eventNames, monitor);
-                        }
+                            if (!eventNames.isEmpty()) {
+                                changeState(param.getChannel(), eventNames, monitor);
+                            }
 
-                        for (Iterator<TraceEventComponent> iterator = fEvents.iterator(); iterator.hasNext();) {
-                            // Enable all selected channels which are disabled
-                            TraceEventComponent ev = (TraceEventComponent) iterator.next();
-                            ev.setState(getNewState());
+                            for (Iterator<TraceEventComponent> iterator = events.iterator(); iterator.hasNext();) {
+                                // Enable all selected channels which are disabled
+                                TraceEventComponent ev = iterator.next();
+                                ev.setState(getNewState());
+                            }
                         }
+                    } catch (ExecutionException e) {
+                        error = e;
                     }
-                } catch (ExecutionException e) {
-                    errorString = e.toString() + "\n"; //$NON-NLS-1$
-                }
 
-                // In all cases notify listeners  
-                session.fireComponentChanged(session);
-
-                if (errorString != null) {
-                    return new Status(Status.ERROR, Activator.PLUGIN_ID, errorString);
-                }
+                    if (session != null) {
+                        // In all cases notify listeners
+                        session.fireComponentChanged(session);
+                    }
 
-                return Status.OK_STATUS;
-            }
-        };
-        job.setUser(true);
-        job.schedule();
+                    if (error != null) {
+                        return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_ChangeEventStateFailure, error);
+                    }
 
+                    return Status.OK_STATUS;
+                }
+            };
+            job.setUser(true);
+            job.schedule();
+        } finally {
+            fLock.unlock();
+        }
         return null;
     }
 
@@ -156,27 +164,29 @@ abstract public class ChangeEventStateHandler extends BaseControlViewHandler {
             return false;
         }
 
-        reset();
-
         // Check if one or more session are selected
         ISelection selection = page.getSelection(ControlView.ID);
+
+        TraceChannelComponent channel = null;
+        List<TraceEventComponent> events = new ArrayList<TraceEventComponent>();
+
         if (selection instanceof StructuredSelection) {
             StructuredSelection structered = ((StructuredSelection) selection);
             String sessionName = null;
             String channelName = null;
-            
+
             for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
-                Object element = (Object) iterator.next();
-                 
+                Object element = iterator.next();
+
                 if (element instanceof TraceEventComponent) {
-                    
+
                     TraceEventComponent event = (TraceEventComponent) element;
                     if (sessionName == null) {
                         sessionName = String.valueOf(event.getSessionName());
                     }
-                    
-                    if (fChannel == null) {
-                        fChannel = (TraceChannelComponent)event.getParent();
+
+                    if (channel == null) {
+                        channel = (TraceChannelComponent)event.getParent();
                     }
 
                     if (channelName == null) {
@@ -186,25 +196,74 @@ abstract public class ChangeEventStateHandler extends BaseControlViewHandler {
                     // Enable command only for events of same session, same channel and domain
                     if ((!sessionName.equals(event.getSessionName())) ||
                         (!channelName.equals(event.getChannelName())) ||
-                        (fChannel.isKernel() != event.isKernel())) {
-                        reset();
+                        (channel.isKernel() != event.isKernel())) {
+                        events.clear();
                         break;
                     }
 
                     if ((event.getState() != getNewState())) {
-                        fEvents.add(event);
+                        events.add(event);
                     }
                 }
             }
         }
-        return fEvents.size() > 0;
+        boolean isEnabled = !events.isEmpty();
+
+        fLock.lock();
+        try {
+            fParam = null;
+            if (isEnabled) {
+                fParam = new Parameter(channel, events);
+            }
+        } finally {
+            fLock.unlock();
+        }
+        return isEnabled;
     }
 
     /**
-     * Reset members
+     *  Class containing parameter for the command execution.
      */
-    private void reset() {
-        fChannel = null;
-        fEvents.clear();
+    static protected class Parameter {
+        /**
+         * Channel component reference.
+         */
+        final private TraceChannelComponent fChannel;
+        /**
+         * The list of kernel channel components the command is to be executed on.
+         */
+        final private List<TraceEventComponent> fEvents = new ArrayList<TraceEventComponent>();
+
+        /**
+         * Constructor
+         * @param channel - a channel component
+         * @param events - a list of event components
+         */
+        public Parameter(TraceChannelComponent channel, List<TraceEventComponent> events) {
+            fChannel = channel;
+            fEvents.addAll(events);
+        }
+
+        /**
+         * Copy constructor
+         * @param other - a parameter to copy
+         */
+        public Parameter(Parameter other) {
+            this(other.fChannel, other.fEvents);
+        }
+
+        /**
+         * @return the trace channel component.
+         */
+        public TraceChannelComponent getChannel() {
+            return fChannel;
+        }
+
+        /**
+         * @return a list of trace event components.
+         */
+        public List<TraceEventComponent> getEvents() {
+            return fEvents;
+        }
     }
 }
This page took 0.028001 seconds and 5 git commands to generate.