| 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 | * Francois Chouinard - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
| 14 | |
| 15 | import java.util.Iterator; |
| 16 | |
| 17 | import org.eclipse.core.expressions.PropertyTester; |
| 18 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 19 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
| 20 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
| 21 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
| 22 | |
| 23 | /** |
| 24 | * <b><u>TracePropertyTester</u></b> |
| 25 | * <p> |
| 26 | */ |
| 27 | public class TracePropertyTester extends PropertyTester { |
| 28 | |
| 29 | // ------------------------------------------------------------------------ |
| 30 | // Constants |
| 31 | // ------------------------------------------------------------------------ |
| 32 | |
| 33 | private final static String IS_IN_TRACE_FOLDER = "isInTraceFolder"; //$NON-NLS-1$ |
| 34 | private final static String IS_EXPERIMENT_TRACE = "isExperimentTrace"; //$NON-NLS-1$ |
| 35 | private final static String HAS_SUPPLEMENTARY_FILES = "hasSupplementaryFiles"; //$NON-NLS-1$ |
| 36 | private final static String TRACE_TYPE = "traceType"; //$NON-NLS-1$ |
| 37 | |
| 38 | // ------------------------------------------------------------------------ |
| 39 | // Constructor |
| 40 | // ------------------------------------------------------------------------ |
| 41 | |
| 42 | /** |
| 43 | * Default constructor |
| 44 | */ |
| 45 | public TracePropertyTester() { |
| 46 | } |
| 47 | |
| 48 | // ------------------------------------------------------------------------ |
| 49 | // IPropertyTester |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
| 52 | /* (non-Javadoc) |
| 53 | * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object) |
| 54 | */ |
| 55 | @Override |
| 56 | public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { |
| 57 | |
| 58 | // Check if the selected elements are in the trace folder |
| 59 | if (IS_IN_TRACE_FOLDER.equals(property)) { |
| 60 | if (receiver != null && receiver instanceof IStructuredSelection) { |
| 61 | Iterator<?> iter = ((IStructuredSelection) receiver).iterator(); |
| 62 | while (iter.hasNext()) { |
| 63 | Object item = iter.next(); |
| 64 | if (item instanceof TmfTraceElement) { |
| 65 | TmfTraceElement trace = (TmfTraceElement) item; |
| 66 | if (!(trace.getParent() instanceof TmfTraceFolder)) { |
| 67 | return false; |
| 68 | } |
| 69 | } else { |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | return true; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Check if the parent of a trace element is an experiment |
| 78 | if (IS_EXPERIMENT_TRACE.equals(property)) { |
| 79 | if (receiver != null && receiver instanceof TmfTraceElement) { |
| 80 | TmfTraceElement trace = (TmfTraceElement) receiver; |
| 81 | return trace.getParent() instanceof TmfExperimentElement; |
| 82 | } |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | // Check if traces has supplementary files |
| 87 | if (HAS_SUPPLEMENTARY_FILES.equals(property)) { |
| 88 | if (receiver == null) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | if (receiver instanceof TmfTraceElement) { |
| 93 | TmfTraceElement trace = (TmfTraceElement) receiver; |
| 94 | return trace.hasSupplementaryResources(); |
| 95 | } else if (receiver instanceof TmfExperimentElement) { |
| 96 | TmfExperimentElement trace = (TmfExperimentElement) receiver; |
| 97 | boolean hasHistory = false; |
| 98 | for (TmfTraceElement aTrace : trace.getTraces()) { |
| 99 | hasHistory |= aTrace.hasSupplementaryResources(); |
| 100 | } |
| 101 | return hasHistory; |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // Check if the trace element is of a specific trace type |
| 107 | if (TRACE_TYPE.equals(property)) { |
| 108 | if (receiver != null && receiver instanceof TmfTraceElement) { |
| 109 | TmfTraceElement trace = (TmfTraceElement) receiver; |
| 110 | if (expectedValue instanceof String && expectedValue.equals(trace.getTraceType())) { |
| 111 | return true; |
| 112 | } |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | } |