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
.linuxtools
.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
.linuxtools
.tmf
.core
.project
.model
.TmfTraceType
;
26 import org
.eclipse
.linuxtools
.tmf
.ui
.properties
.ReadOnlyTextPropertyDescriptor
;
27 import org
.eclipse
.ui
.views
.properties
.IPropertyDescriptor
;
28 import org
.eclipse
.ui
.views
.properties
.IPropertySource2
;
31 * Implementation of trace folder model element representing a trace folder in
35 * @author Francois Chouinard
37 public class TmfTraceFolder
extends TmfProjectModelElement
implements IPropertySource2
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
43 private static final String sfInfoCategory
= "Info"; //$NON-NLS-1$
44 private static final String sfName
= "name"; //$NON-NLS-1$
45 private static final String sfPath
= "path"; //$NON-NLS-1$
46 private static final String sfLocation
= "location"; //$NON-NLS-1$
48 private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor
= new ReadOnlyTextPropertyDescriptor(sfName
, sfName
);
49 private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor
= new ReadOnlyTextPropertyDescriptor(sfPath
, sfPath
);
50 private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor
= new ReadOnlyTextPropertyDescriptor(sfLocation
, sfLocation
);
52 private static final IPropertyDescriptor
[] sfDescriptors
= { sfNameDescriptor
, sfPathDescriptor
,
53 sfLocationDescriptor
};
56 sfNameDescriptor
.setCategory(sfInfoCategory
);
57 sfPathDescriptor
.setCategory(sfInfoCategory
);
58 sfLocationDescriptor
.setCategory(sfInfoCategory
);
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
67 * Creates folder model element under the project.
68 * @param name The name of trace folder.
69 * @param resource The folder resource.
70 * @param parent The parent element (project).
72 public TmfTraceFolder(String name
, IFolder resource
, TmfProjectElement parent
) {
73 super(name
, resource
, parent
);
74 parent
.addChild(this);
79 * Creates folder model element under another folder.
80 * @param name The name of trace folder.
81 * @param resource The folder resource.
82 * @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 ((TmfProjectModelElement
) element
).refreshChildren();
129 } catch (CoreException e
) {
132 // Cleanup dangling children from the model
133 for (ITmfProjectModelElement danglingChild
: childrenMap
.values()) {
134 removeChild(danglingChild
);
138 // ------------------------------------------------------------------------
140 // ------------------------------------------------------------------------
143 * Returns a list of trace elements under the folder element, recursively.
144 * @return list of trace model elements
146 public List
<TmfTraceElement
> getTraces() {
147 List
<ITmfProjectModelElement
> children
= getChildren();
148 List
<TmfTraceElement
> traces
= new ArrayList
<>();
149 for (ITmfProjectModelElement child
: children
) {
150 if (child
instanceof TmfTraceElement
) {
151 traces
.add((TmfTraceElement
) child
);
152 } else if (child
instanceof TmfTraceFolder
) {
153 traces
.addAll(((TmfTraceFolder
) child
).getTraces());
159 // ------------------------------------------------------------------------
161 // ------------------------------------------------------------------------
164 public Object
getEditableValue() {
169 public IPropertyDescriptor
[] getPropertyDescriptors() {
170 return Arrays
.copyOf(sfDescriptors
, sfDescriptors
.length
);
174 public Object
getPropertyValue(Object id
) {
176 if (sfName
.equals(id
)) {
180 if (sfPath
.equals(id
)) {
181 return getPath().toString();
184 if (sfLocation
.equals(id
)) {
185 return getLocation().toString();
192 public void resetPropertyValue(Object id
) {
196 public void setPropertyValue(Object id
, Object value
) {
200 public boolean isPropertyResettable(Object id
) {
205 public boolean isPropertySet(Object id
) {