1 /*******************************************************************************
2 * Copyright (c) 2011, 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
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
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 trace folder model element representing a trace folder in
36 * @author Francois Chouinard
38 public class TmfTraceFolder
extends TmfProjectModelElement
implements IPropertySource2
{
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
44 private static final String INFO_CATEGORY
= "Info"; //$NON-NLS-1$
45 private static final String NAME
= "name"; //$NON-NLS-1$
46 private static final String PATH
= "path"; //$NON-NLS-1$
47 private static final String LOCATION
= "location"; //$NON-NLS-1$
49 private static final ReadOnlyTextPropertyDescriptor NAME_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(NAME
, NAME
);
50 private static final ReadOnlyTextPropertyDescriptor PATH_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(PATH
, PATH
);
51 private static final ReadOnlyTextPropertyDescriptor LOCATION_DESCRIPTOR
= new ReadOnlyTextPropertyDescriptor(LOCATION
, LOCATION
);
53 private static final IPropertyDescriptor
[] DESCRIPTORS
= { NAME_DESCRIPTOR
, PATH_DESCRIPTOR
,
54 LOCATION_DESCRIPTOR
};
57 NAME_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
58 PATH_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
59 LOCATION_DESCRIPTOR
.setCategory(INFO_CATEGORY
);
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
68 * Creates folder model element under the project.
69 * @param name The name of trace folder.
70 * @param resource The folder resource.
71 * @param parent The parent element (project).
73 public TmfTraceFolder(String name
, IFolder resource
, TmfProjectElement parent
) {
74 super(name
, resource
, parent
);
75 parent
.addChild(this);
80 * Creates folder model element under another folder.
81 * @param name The name of trace folder.
82 * @param resource The folder resource.
83 * @param parent The parent element (folder).
85 public TmfTraceFolder(String name
, IFolder resource
, TmfTraceFolder parent
) {
86 super(name
, resource
, parent
);
87 parent
.addChild(this);
90 // ------------------------------------------------------------------------
91 // TmfProjectModelElement
92 // ------------------------------------------------------------------------
95 public IFolder
getResource() {
96 return (IFolder
) fResource
;
100 void refreshChildren() {
101 IFolder folder
= getResource();
103 // Get the children from the model
104 Map
<String
, ITmfProjectModelElement
> childrenMap
= new HashMap
<>();
105 for (ITmfProjectModelElement element
: getChildren()) {
106 childrenMap
.put(element
.getResource().getName(), element
);
110 IResource
[] members
= folder
.members();
111 for (IResource resource
: members
) {
112 String name
= resource
.getName();
113 boolean isFolder
= resource
instanceof IFolder
&&
114 (TmfTraceType
.getTraceTypeId(resource
) == null);
115 ITmfProjectModelElement element
= childrenMap
.get(name
);
116 if (isFolder
&& !(element
instanceof TmfTraceFolder
) && !(element
instanceof TmfTraceElement
)) {
117 if (TmfTraceType
.isDirectoryTrace(resource
.getLocationURI().getPath())) {
118 element
= new TmfTraceElement(name
, resource
, this);
120 element
= new TmfTraceFolder(name
, (IFolder
) resource
, this);
122 } else if (!isFolder
&& !(element
instanceof TmfTraceElement
)) {
123 element
= new TmfTraceElement(name
, resource
, this);
125 childrenMap
.remove(name
);
127 if (element
!= null) {
128 ((TmfProjectModelElement
) element
).refreshChildren();
131 } catch (CoreException e
) {
134 // Cleanup dangling children from the model
135 for (ITmfProjectModelElement danglingChild
: childrenMap
.values()) {
136 removeChild(danglingChild
);
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
145 * Returns a list of trace elements under the folder element, recursively.
146 * @return list of trace model elements
148 public List
<TmfTraceElement
> getTraces() {
149 List
<ITmfProjectModelElement
> children
= getChildren();
150 List
<TmfTraceElement
> traces
= new ArrayList
<>();
151 for (ITmfProjectModelElement child
: children
) {
152 if (child
instanceof TmfTraceElement
) {
153 traces
.add((TmfTraceElement
) child
);
154 } else if (child
instanceof TmfTraceFolder
) {
155 traces
.addAll(((TmfTraceFolder
) child
).getTraces());
162 * Gets the traces elements under this folder containing the given resources
165 * resources to search for
166 * @return list of trace elements
169 public @NonNull List
<TmfTraceElement
> getTraceElements(@NonNull List
<IResource
> resources
) {
170 List
<TmfTraceElement
> traceElements
= new ArrayList
<>();
171 List
<TmfTraceElement
> children
= getTraces();
172 for (IResource resource
: resources
) {
173 TmfTraceElement el
= children
.stream()
174 .filter(traceElement
->
175 ((traceElement
!= null) &&
176 traceElement
.getResource().equals(resource
)))
181 traceElements
.add(el
);
184 return traceElements
;
187 // ------------------------------------------------------------------------
189 // ------------------------------------------------------------------------
192 public Object
getEditableValue() {
197 public IPropertyDescriptor
[] getPropertyDescriptors() {
198 return Arrays
.copyOf(DESCRIPTORS
, DESCRIPTORS
.length
);
202 public Object
getPropertyValue(Object id
) {
204 if (NAME
.equals(id
)) {
208 if (PATH
.equals(id
)) {
209 return getPath().toString();
212 if (LOCATION
.equals(id
)) {
213 return getLocation().toString();
220 public void resetPropertyValue(Object id
) {
224 public void setPropertyValue(Object id
, Object value
) {
228 public boolean isPropertyResettable(Object id
) {
233 public boolean isPropertySet(Object id
) {