1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Patrick Tasse - Add support for folder elements
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Arrays
;
18 import java
.util
.HashMap
;
19 import java
.util
.List
;
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
.jdt
.annotation
.NonNull
;
26 import org
.eclipse
.jdt
.annotation
.Nullable
;
27 import org
.eclipse
.swt
.graphics
.Image
;
28 import org
.eclipse
.tracecompass
.tmf
.ui
.properties
.ReadOnlyTextPropertyDescriptor
;
29 import org
.eclipse
.ui
.views
.properties
.IPropertyDescriptor
;
30 import org
.eclipse
.ui
.views
.properties
.IPropertySource2
;
33 * Implementation of model element representing the unique "Experiments" folder
38 * @author Francois Chouinard
41 public class TmfExperimentFolder
extends TmfProjectModelElement
implements IPropertySource2
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
48 * The name of the experiment folder.
50 public static final String EXPER_FOLDER_NAME
= "Experiments"; //$NON-NLS-1$
52 // Property View stuff
53 private static final String INFO_CATEGORY
= "Info"; //$NON-NLS-1$
54 private static final String NAME
= "name"; //$NON-NLS-1$
55 private static final String PATH
= "path"; //$NON-NLS-1$
56 private static final String LOCATION
= "location"; //$NON-NLS-1$
58 private static final ReadOnlyTextPropertyDescriptor NAME_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(NAME
, NAME
);
59 private static final ReadOnlyTextPropertyDescriptor PATH_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(PATH
, PATH
);
60 private static final ReadOnlyTextPropertyDescriptor LOCATION_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(LOCATION
, LOCATION
);
62 private static final IPropertyDescriptor
[] DESCRIPTORS
= { NAME_DESCRIPTOR
, PATH_DESCRIPTOR
, LOCATION_DESCRIPTOR
};
65 NAME_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
66 PATH_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
67 LOCATION_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
76 * Creates a TmfExperimentFolder model element.
77 * @param name The name of the folder
78 * @param folder The folder reference
79 * @param parent The parent (project element)
81 public TmfExperimentFolder(String name
, IFolder folder
, TmfProjectElement parent
) {
82 super(name
, folder
, parent
);
85 // ------------------------------------------------------------------------
86 // TmfProjectModelElement
87 // ------------------------------------------------------------------------
90 public IFolder
getResource() {
91 return (IFolder
) super.getResource();
98 protected void refreshChildren() {
99 IFolder folder
= getResource();
101 // Get the children from the model
102 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<>();
103 for (ITmfProjectModelElement element
: getChildren()) {
104 childrenMap
.put(element
.getResource().getName(), element
);
108 IResource
[] members
= folder
.members();
109 for (IResource resource
: members
) {
110 if (resource
instanceof IFolder
) {
111 IFolder expFolder
= (IFolder
) resource
;
112 String name
= resource
.getName();
113 ITmfProjectModelElement element
= childrenMap
.get(name
);
114 if (element
instanceof TmfExperimentElement
) {
115 childrenMap
.remove(name
);
117 element
= new TmfExperimentElement(name
, expFolder
, this);
120 ((TmfExperimentElement
) element
).refreshChildren();
123 } catch (CoreException e
) {
126 // Cleanup dangling children from the model
127 for (ITmfProjectModelElement danglingChild
: childrenMap
.values()) {
128 removeChild(danglingChild
);
136 public Image
getIcon() {
137 return TmfProjectModelIcons
.FOLDER_ICON
;
144 public String
getLabelText() {
145 return getName() + " [" + getChildren().size() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
148 // ------------------------------------------------------------------------
150 // ------------------------------------------------------------------------
153 * Returns a list of experiment model elements under the experiments folder.
154 * @return list of experiment model elements
156 public List
<TmfExperimentElement
> getExperiments() {
157 List
<ITmfProjectModelElement
> children
= getChildren();
158 List
<TmfExperimentElement
> traces
= new ArrayList
<>();
159 for (ITmfProjectModelElement child
: children
) {
160 if (child
instanceof TmfExperimentElement
) {
161 traces
.add((TmfExperimentElement
) child
);
168 * Finds the experiment element for a given resource
171 * the resource to search for
172 * @return the experiment element if found else null
175 public @Nullable TmfExperimentElement
getExperiment(@NonNull IResource resource
) {
176 String name
= resource
.getName();
178 return getExperiment(name
);
184 * Finds the experiment element for a given name
187 * the name of experiment to search for
188 * @return the experiment element if found else null
191 public @Nullable TmfExperimentElement
getExperiment(@NonNull String name
) {
192 return getExperiments()
194 .filter(experiment
->
196 && (experiment
.getName().equals(name
)))
201 // ------------------------------------------------------------------------
203 // ------------------------------------------------------------------------
206 public Object
getEditableValue() {
211 public IPropertyDescriptor
[] getPropertyDescriptors() {
212 return Arrays
.copyOf(DESCRIPTORS
, DESCRIPTORS
.length
);
216 public Object
getPropertyValue(Object id
) {
218 if (NAME
.equals(id
)) {
222 if (PATH
.equals(id
)) {
223 return getPath().toString();
226 if (LOCATION
.equals(id
)) {
227 return getLocation().toString();
234 public void resetPropertyValue(Object id
) {
238 public void setPropertyValue(Object id
, Object value
) {
242 public boolean isPropertyResettable(Object id
) {
247 public boolean isPropertySet(Object id
) {