/******************************************************************************* * Copyright (c) 2011, 2014 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: * Francois Chouinard - Initial API and implementation * Patrick Tasse - Add support for folder elements *******************************************************************************/ package fr.inria.linuxtools.tmf.ui.project.model; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.ui.views.properties.IPropertyDescriptor; import org.eclipse.ui.views.properties.IPropertySource2; import fr.inria.linuxtools.tmf.core.TmfCommonConstants; import fr.inria.linuxtools.tmf.core.project.model.TmfTraceType; import fr.inria.linuxtools.tmf.ui.properties.ReadOnlyTextPropertyDescriptor; /** * Implementation of trace folder model element representing a trace folder in * the project. * <p> * @version 1.0 * @author Francois Chouinard */ public class TmfTraceFolder extends TmfProjectModelElement implements IPropertySource2 { // ------------------------------------------------------------------------ // Constants // ------------------------------------------------------------------------ private static final String sfInfoCategory = "Info"; //$NON-NLS-1$ private static final String sfName = "name"; //$NON-NLS-1$ private static final String sfPath = "path"; //$NON-NLS-1$ private static final String sfLocation = "location"; //$NON-NLS-1$ private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName); private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath); private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfLocation, sfLocation); private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, sfLocationDescriptor }; static { sfNameDescriptor.setCategory(sfInfoCategory); sfPathDescriptor.setCategory(sfInfoCategory); sfLocationDescriptor.setCategory(sfInfoCategory); } // ------------------------------------------------------------------------ // Constructor // ------------------------------------------------------------------------ /** * Constructor. * Creates folder model element under the project. * @param name The name of trace folder. * @param resource The folder resource. * @param parent The parent element (project). */ public TmfTraceFolder(String name, IFolder resource, TmfProjectElement parent) { super(name, resource, parent); parent.addChild(this); } /** * Constructor. * Creates folder model element under another folder. * @param name The name of trace folder. * @param resource The folder resource. * @param parent The parent element (folder). * @since 3.0 */ public TmfTraceFolder(String name, IFolder resource, TmfTraceFolder parent) { super(name, resource, parent); parent.addChild(this); } // ------------------------------------------------------------------------ // TmfProjectModelElement // ------------------------------------------------------------------------ @Override public IFolder getResource() { return (IFolder) fResource; } @Override void refreshChildren() { IFolder folder = getResource(); // Get the children from the model Map<String, ITmfProjectModelElement> childrenMap = new HashMap<>(); for (ITmfProjectModelElement element : getChildren()) { childrenMap.put(element.getResource().getName(), element); } try { IResource[] members = folder.members(); for (IResource resource : members) { String name = resource.getName(); boolean isFolder = resource instanceof IFolder && (resource.getPersistentProperty(TmfCommonConstants.TRACETYPE) == null); ITmfProjectModelElement element = childrenMap.get(name); if (isFolder && !(element instanceof TmfTraceFolder) && !(element instanceof TmfTraceElement)) { if (TmfTraceType.isDirectoryTrace(resource.getLocationURI().getPath())) { element = new TmfTraceElement(name, resource, this); } else { element = new TmfTraceFolder(name, (IFolder) resource, this); } } else if (!isFolder && !(element instanceof TmfTraceElement)) { element = new TmfTraceElement(name, resource, this); } else { childrenMap.remove(name); } ((TmfProjectModelElement) element).refreshChildren(); } } catch (CoreException e) { } // Cleanup dangling children from the model for (ITmfProjectModelElement danglingChild : childrenMap.values()) { removeChild(danglingChild); } } // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ /** * Returns a list of trace elements under the folder element, recursively. * @return list of trace model elements */ public List<TmfTraceElement> getTraces() { List<ITmfProjectModelElement> children = getChildren(); List<TmfTraceElement> traces = new ArrayList<>(); for (ITmfProjectModelElement child : children) { if (child instanceof TmfTraceElement) { traces.add((TmfTraceElement) child); } else if (child instanceof TmfTraceFolder) { traces.addAll(((TmfTraceFolder) child).getTraces()); } } return traces; } // ------------------------------------------------------------------------ // IPropertySource2 // ------------------------------------------------------------------------ @Override public Object getEditableValue() { return null; } @Override public IPropertyDescriptor[] getPropertyDescriptors() { return Arrays.copyOf(sfDescriptors, sfDescriptors.length); } @Override public Object getPropertyValue(Object id) { if (sfName.equals(id)) { return getName(); } if (sfPath.equals(id)) { return getPath().toString(); } if (sfLocation.equals(id)) { return getLocation().toString(); } return null; } @Override public void resetPropertyValue(Object id) { } @Override public void setPropertyValue(Object id, Object value) { } @Override public boolean isPropertyResettable(Object id) { return false; } @Override public boolean isPropertySet(Object id) { return false; } }