| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2010, 2014 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 | * Patrick Tasse - Add support for folder elements |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.linuxtools.tmf.ui.project.model; |
| 15 | |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.Arrays; |
| 18 | import java.util.HashMap; |
| 19 | import java.util.List; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | import org.eclipse.core.resources.IFolder; |
| 23 | import org.eclipse.core.resources.IResource; |
| 24 | import org.eclipse.core.runtime.CoreException; |
| 25 | import org.eclipse.linuxtools.tmf.ui.properties.ReadOnlyTextPropertyDescriptor; |
| 26 | import org.eclipse.ui.views.properties.IPropertyDescriptor; |
| 27 | import org.eclipse.ui.views.properties.IPropertySource2; |
| 28 | |
| 29 | /** |
| 30 | * Implementation of model element representing the unique "Experiments" folder |
| 31 | * in the project. |
| 32 | * <p> |
| 33 | * |
| 34 | * @version 1.0 |
| 35 | * @author Francois Chouinard |
| 36 | * |
| 37 | */ |
| 38 | public class TmfExperimentFolder extends TmfProjectModelElement implements IPropertySource2 { |
| 39 | |
| 40 | // ------------------------------------------------------------------------ |
| 41 | // Constants |
| 42 | // ------------------------------------------------------------------------ |
| 43 | |
| 44 | /** |
| 45 | * The name of the experiment folder. |
| 46 | */ |
| 47 | public static final String EXPER_FOLDER_NAME = "Experiments"; //$NON-NLS-1$ |
| 48 | |
| 49 | // Property View stuff |
| 50 | private static final String sfInfoCategory = "Info"; //$NON-NLS-1$ |
| 51 | private static final String sfName = "name"; //$NON-NLS-1$ |
| 52 | private static final String sfPath = "path"; //$NON-NLS-1$ |
| 53 | private static final String sfLocation = "location"; //$NON-NLS-1$ |
| 54 | |
| 55 | private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName); |
| 56 | private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath); |
| 57 | private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfLocation, sfLocation); |
| 58 | |
| 59 | private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, sfLocationDescriptor }; |
| 60 | |
| 61 | static { |
| 62 | sfNameDescriptor.setCategory(sfInfoCategory); |
| 63 | sfPathDescriptor.setCategory(sfInfoCategory); |
| 64 | sfLocationDescriptor.setCategory(sfInfoCategory); |
| 65 | } |
| 66 | |
| 67 | // ------------------------------------------------------------------------ |
| 68 | // Constructors |
| 69 | // ------------------------------------------------------------------------ |
| 70 | |
| 71 | /** |
| 72 | * Constructor. |
| 73 | * Creates a TmfExperimentFolder model element. |
| 74 | * @param name The name of the folder |
| 75 | * @param folder The folder reference |
| 76 | * @param parent The parent (project element) |
| 77 | */ |
| 78 | public TmfExperimentFolder(String name, IFolder folder, TmfProjectElement parent) { |
| 79 | super(name, folder, parent); |
| 80 | parent.addChild(this); |
| 81 | } |
| 82 | |
| 83 | // ------------------------------------------------------------------------ |
| 84 | // TmfProjectModelElement |
| 85 | // ------------------------------------------------------------------------ |
| 86 | |
| 87 | @Override |
| 88 | public IFolder getResource() { |
| 89 | return (IFolder) fResource; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | void refreshChildren() { |
| 94 | IFolder folder = getResource(); |
| 95 | |
| 96 | // Get the children from the model |
| 97 | Map<String, ITmfProjectModelElement> childrenMap = new HashMap<>(); |
| 98 | for (ITmfProjectModelElement element : getChildren()) { |
| 99 | childrenMap.put(element.getResource().getName(), element); |
| 100 | } |
| 101 | |
| 102 | try { |
| 103 | IResource[] members = folder.members(); |
| 104 | for (IResource resource : members) { |
| 105 | if (resource instanceof IFolder) { |
| 106 | IFolder expFolder = (IFolder) resource; |
| 107 | String name = resource.getName(); |
| 108 | ITmfProjectModelElement element = childrenMap.get(name); |
| 109 | if (element instanceof TmfExperimentElement) { |
| 110 | childrenMap.remove(name); |
| 111 | } else { |
| 112 | element = new TmfExperimentElement(name, expFolder, this); |
| 113 | } |
| 114 | ((TmfExperimentElement) element).refreshChildren(); |
| 115 | } |
| 116 | } |
| 117 | } catch (CoreException e) { |
| 118 | } |
| 119 | |
| 120 | // Cleanup dangling children from the model |
| 121 | for (ITmfProjectModelElement danglingChild : childrenMap.values()) { |
| 122 | removeChild(danglingChild); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // ------------------------------------------------------------------------ |
| 127 | // Operations |
| 128 | // ------------------------------------------------------------------------ |
| 129 | |
| 130 | /** |
| 131 | * Returns a list of experiment model elements under the experiments folder. |
| 132 | * @return list of experiment model elements |
| 133 | * @since 3.0 |
| 134 | */ |
| 135 | public List<TmfExperimentElement> getExperiments() { |
| 136 | List<ITmfProjectModelElement> children = getChildren(); |
| 137 | List<TmfExperimentElement> traces = new ArrayList<>(); |
| 138 | for (ITmfProjectModelElement child : children) { |
| 139 | if (child instanceof TmfExperimentElement) { |
| 140 | traces.add((TmfExperimentElement) child); |
| 141 | } |
| 142 | } |
| 143 | return traces; |
| 144 | } |
| 145 | |
| 146 | // ------------------------------------------------------------------------ |
| 147 | // IPropertySource2 |
| 148 | // ------------------------------------------------------------------------ |
| 149 | |
| 150 | @Override |
| 151 | public Object getEditableValue() { |
| 152 | return null; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public IPropertyDescriptor[] getPropertyDescriptors() { |
| 157 | return Arrays.copyOf(sfDescriptors, sfDescriptors.length); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Object getPropertyValue(Object id) { |
| 162 | |
| 163 | if (sfName.equals(id)) { |
| 164 | return getName(); |
| 165 | } |
| 166 | |
| 167 | if (sfPath.equals(id)) { |
| 168 | return getPath().toString(); |
| 169 | } |
| 170 | |
| 171 | if (sfLocation.equals(id)) { |
| 172 | return getLocation().toString(); |
| 173 | } |
| 174 | |
| 175 | return null; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void resetPropertyValue(Object id) { |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void setPropertyValue(Object id, Object value) { |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public boolean isPropertyResettable(Object id) { |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public boolean isPropertySet(Object id) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | } |