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
.tracecompass
.tmf
.ui
.properties
.ReadOnlyTextPropertyDescriptor
;
28 import org
.eclipse
.ui
.views
.properties
.IPropertyDescriptor
;
29 import org
.eclipse
.ui
.views
.properties
.IPropertySource2
;
32 * Implementation of model element representing the unique "Experiments" folder
37 * @author Francois Chouinard
40 public class TmfExperimentFolder
extends TmfProjectModelElement
implements IPropertySource2
{
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
47 * The name of the experiment folder.
49 public static final String EXPER_FOLDER_NAME
= "Experiments"; //$NON-NLS-1$
51 // Property View stuff
52 private static final String INFO_CATEGORY
= "Info"; //$NON-NLS-1$
53 private static final String NAME
= "name"; //$NON-NLS-1$
54 private static final String PATH
= "path"; //$NON-NLS-1$
55 private static final String LOCATION
= "location"; //$NON-NLS-1$
57 private static final ReadOnlyTextPropertyDescriptor NAME_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(NAME
, NAME
);
58 private static final ReadOnlyTextPropertyDescriptor PATH_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(PATH
, PATH
);
59 private static final ReadOnlyTextPropertyDescriptor LOCATION_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(LOCATION
, LOCATION
);
61 private static final IPropertyDescriptor
[] DESCRIPTORS
= { NAME_DESCRIPTOR
, PATH_DESCRIPTOR
, LOCATION_DESCRIPTOR
};
64 NAME_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
65 PATH_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
66 LOCATION_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
75 * Creates a TmfExperimentFolder model element.
76 * @param name The name of the folder
77 * @param folder The folder reference
78 * @param parent The parent (project element)
80 public TmfExperimentFolder(String name
, IFolder folder
, TmfProjectElement parent
) {
81 super(name
, folder
, parent
);
84 // ------------------------------------------------------------------------
85 // TmfProjectModelElement
86 // ------------------------------------------------------------------------
89 public IFolder
getResource() {
90 return (IFolder
) super.getResource();
97 protected void refreshChildren() {
98 IFolder folder
= getResource();
100 // Get the children from the model
101 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<>();
102 for (ITmfProjectModelElement element
: getChildren()) {
103 childrenMap
.put(element
.getResource().getName(), element
);
107 IResource
[] members
= folder
.members();
108 for (IResource resource
: members
) {
109 if (resource
instanceof IFolder
) {
110 IFolder expFolder
= (IFolder
) resource
;
111 String name
= resource
.getName();
112 ITmfProjectModelElement element
= childrenMap
.get(name
);
113 if (element
instanceof TmfExperimentElement
) {
114 childrenMap
.remove(name
);
116 element
= new TmfExperimentElement(name
, expFolder
, this);
119 ((TmfExperimentElement
) element
).refreshChildren();
122 } catch (CoreException e
) {
125 // Cleanup dangling children from the model
126 for (ITmfProjectModelElement danglingChild
: childrenMap
.values()) {
127 removeChild(danglingChild
);
131 // ------------------------------------------------------------------------
133 // ------------------------------------------------------------------------
136 * Returns a list of experiment model elements under the experiments folder.
137 * @return list of experiment model elements
139 public List
<TmfExperimentElement
> getExperiments() {
140 List
<ITmfProjectModelElement
> children
= getChildren();
141 List
<TmfExperimentElement
> traces
= new ArrayList
<>();
142 for (ITmfProjectModelElement child
: children
) {
143 if (child
instanceof TmfExperimentElement
) {
144 traces
.add((TmfExperimentElement
) child
);
151 * Finds the experiment element for a given resource
154 * the resource to search for
155 * @return the experiment element if found else null
158 public @Nullable TmfExperimentElement
getExperiment(@NonNull IResource resource
) {
159 String name
= resource
.getName();
161 return getExperiment(name
);
167 * Finds the experiment element for a given name
170 * the name of experiment to search for
171 * @return the experiment element if found else null
174 public @Nullable TmfExperimentElement
getExperiment(@NonNull String name
) {
175 return getExperiments()
177 .filter(experiment
->
179 && (experiment
.getName().equals(name
)))
184 // ------------------------------------------------------------------------
186 // ------------------------------------------------------------------------
189 public Object
getEditableValue() {
194 public IPropertyDescriptor
[] getPropertyDescriptors() {
195 return Arrays
.copyOf(DESCRIPTORS
, DESCRIPTORS
.length
);
199 public Object
getPropertyValue(Object id
) {
201 if (NAME
.equals(id
)) {
205 if (PATH
.equals(id
)) {
206 return getPath().toString();
209 if (LOCATION
.equals(id
)) {
210 return getLocation().toString();
217 public void resetPropertyValue(Object id
) {
221 public void setPropertyValue(Object id
, Object value
) {
225 public boolean isPropertyResettable(Object id
) {
230 public boolean isPropertySet(Object id
) {