Fix findbugs warnings + address concurrency issues in handlers
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / handlers / RefreshHandler.java
CommitLineData
d132bcc7
BH
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 **********************************************************************/
115b4a01 12package org.eclipse.linuxtools.internal.lttng2.ui.views.control.handlers;
d132bcc7
BH
13
14import java.util.Iterator;
15
16import org.eclipse.core.commands.ExecutionEvent;
17import org.eclipse.core.commands.ExecutionException;
18import org.eclipse.jface.viewers.ISelection;
19import org.eclipse.jface.viewers.StructuredSelection;
115b4a01
BH
20import org.eclipse.linuxtools.internal.lttng2.ui.views.control.ControlView;
21import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.TargetNodeState;
22import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TargetNodeComponent;
23import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlComponent;
d132bcc7
BH
24import org.eclipse.ui.IWorkbenchPage;
25
26/**
27 * <b><u>RefreshHandler</u></b>
28 * <p>
29 * Command handler implementation to refresh node configuration.
30 * </p>
31 */
32public class RefreshHandler extends BaseControlViewHandler {
33
34 // ------------------------------------------------------------------------
35 // Attributes
36 // ------------------------------------------------------------------------
37 /**
38 * The node component reference.
39 */
40 private TargetNodeComponent fNode;
41
42 // ------------------------------------------------------------------------
43 // Operations
44 // ------------------------------------------------------------------------
45
46 /*
47 * (non-Javadoc)
48 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
49 */
50 @Override
51 public Object execute(ExecutionEvent event) throws ExecutionException {
c56972bb
BH
52 fLock.lock();
53 try {
54 fNode.refresh();
55 } finally {
56 fLock.unlock();
57 }
d132bcc7
BH
58 return null;
59 }
60
61 /*
62 * (non-Javadoc)
63 * @see org.eclipse.core.commands.AbstractHandler#isEnabled()
64 */
65 @Override
66 public boolean isEnabled() {
d132bcc7
BH
67
68 // Get workbench page for the Control View
69 IWorkbenchPage page = getWorkbenchPage();
70 if (page == null) {
71 return false;
72 }
73
c56972bb 74 TargetNodeComponent node = null;
d132bcc7
BH
75 // Check if one or more session are selected
76 ISelection selection = page.getSelection(ControlView.ID);
77 if (selection instanceof StructuredSelection) {
78
79 StructuredSelection structered = ((StructuredSelection) selection);
80 for (Iterator<?> iterator = structered.iterator(); iterator.hasNext();) {
81 Object element = (Object) iterator.next();
82 if (element instanceof TraceControlComponent) {
83 TraceControlComponent component = (TraceControlComponent) element;
84 boolean isConnected = component.getTargetNodeState() == TargetNodeState.CONNECTED;
85 if (isConnected) {
86 while ((component != null) && component.getClass() != TargetNodeComponent.class) {
87 component = (TraceControlComponent) component.getParent();
88 }
89 if (component != null) {
c56972bb 90 node = (TargetNodeComponent) component;
d132bcc7
BH
91 }
92 }
93 }
94 }
95 }
c56972bb
BH
96
97 boolean isEnabled = node != null;
98
99 fLock.lock();
100 try {
101 fNode = null;
102 if (isEnabled) {
103 fNode = node;
104 }
105 } finally {
106 fLock.unlock();
107 }
108
109 return isEnabled;
d132bcc7
BH
110 }
111}
This page took 0.030383 seconds and 5 git commands to generate.