control: Fix dead store sonar warning in destroy session handler
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / handlers / DestroySessionHandler.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2015 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.tracecompass.internal.lttng2.control.ui.views.handlers;
13
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.Job;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.tracecompass.internal.lttng2.control.core.model.TraceSessionState;
28 import org.eclipse.tracecompass.internal.lttng2.control.ui.Activator;
29 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.ControlView;
30 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.IConfirmDialog;
31 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.dialogs.TraceControlDialogFactory;
32 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
33 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionComponent;
34 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceSessionGroup;
35 import org.eclipse.ui.IWorkbenchPage;
36 import org.eclipse.ui.IWorkbenchWindow;
37 import org.eclipse.ui.PlatformUI;
38
39 /**
40 * <p>
41 * Command handler implementation to destroy one or more trace sessions.
42 * </p>
43 *
44 * @author Bernd Hufmann
45 */
46 public class DestroySessionHandler extends BaseControlViewHandler {
47
48 // ------------------------------------------------------------------------
49 // Attributes
50 // ------------------------------------------------------------------------
51 /**
52 * The list of session components the command is to be executed on.
53 */
54 @NonNull private final List<TraceSessionComponent> fSessions = new ArrayList<>();
55
56 // ------------------------------------------------------------------------
57 // Operations
58 // ------------------------------------------------------------------------
59
60 @Override
61 public Object execute(ExecutionEvent event) throws ExecutionException {
62
63 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
64
65 if (window == null) {
66 return false;
67 }
68
69 List<TraceSessionComponent> tmpSessions = new ArrayList<>();
70
71 // Make a copy of the session list to avoid concurrent modification
72 // of the list of sessions
73 fLock.lock();
74 try {
75 tmpSessions.addAll(fSessions);
76 } finally {
77 fLock.unlock();
78 }
79 final List<TraceSessionComponent> sessions = tmpSessions;
80
81 // Get user confirmation
82 IConfirmDialog dialog = TraceControlDialogFactory.getInstance().getConfirmDialog();
83 if (!dialog.openConfirm(window.getShell(),
84 Messages.TraceControl_DestroyConfirmationTitle,
85 Messages.TraceControl_DestroyConfirmationMessage)) {
86
87 return null;
88 }
89
90 Job job = new Job(Messages.TraceControl_DestroySessionJob) {
91 @Override
92 protected IStatus run(IProgressMonitor monitor) {
93 try {
94 for (TraceSessionComponent session : sessions) {
95 // Destroy all selected sessions
96 TraceSessionGroup sessionGroup = (TraceSessionGroup)session.getParent();
97 sessionGroup.destroySession(session, monitor);
98 }
99 } catch (ExecutionException e) {
100 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.TraceControl_DestroySessionFailure, e);
101 }
102 return Status.OK_STATUS;
103 }
104 };
105 job.setUser(true);
106 job.schedule();
107
108 return null;
109 }
110
111 @Override
112 public boolean isEnabled() {
113 // Get workbench page for the Control View
114 IWorkbenchPage page = getWorkbenchPage();
115 if (page == null) {
116 return false;
117 }
118
119 List<TraceSessionComponent> sessions = new ArrayList<>(0);
120
121 // Check if one or more session are selected
122 ISelection selection = page.getSelection(ControlView.ID);
123 if (selection instanceof StructuredSelection) {
124 StructuredSelection structered = ((StructuredSelection) selection);
125 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
126 Object element = iterator.next();
127 if (element instanceof TraceSessionComponent) {
128 // Add only TraceSessionComponents that are inactive and not destroyed
129 TraceSessionComponent session = (TraceSessionComponent) element;
130 if ((session.getSessionState() == TraceSessionState.INACTIVE) && (!session.isDestroyed())) {
131 sessions.add((TraceSessionComponent)element);
132 }
133 }
134 }
135 }
136 boolean isEnabled = !sessions.isEmpty();
137 fLock.lock();
138 try {
139 fSessions.clear();
140 if (isEnabled) {
141 fSessions.addAll(sessions);
142 }
143 } finally {
144 fLock.unlock();
145 }
146 return isEnabled;
147 }
148 }
This page took 0.042399 seconds and 5 git commands to generate.