Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson |
013a5f1c | 3 | * |
12c155f5 FC |
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 | |
013a5f1c | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
339d539c | 11 | * Patrick Tasse - Add support for folder elements |
12c155f5 FC |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.tmf.ui.project.model; | |
15 | ||
16 | import java.util.ArrayList; | |
5a5c2fc7 | 17 | import java.util.Arrays; |
f537c959 | 18 | import java.util.HashMap; |
12c155f5 | 19 | import java.util.List; |
f537c959 | 20 | import java.util.Map; |
12c155f5 FC |
21 | |
22 | import org.eclipse.core.resources.IFolder; | |
f537c959 PT |
23 | import org.eclipse.core.resources.IResource; |
24 | import org.eclipse.core.runtime.CoreException; | |
339d539c | 25 | import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType; |
080600d9 | 26 | import org.eclipse.linuxtools.tmf.ui.properties.ReadOnlyTextPropertyDescriptor; |
12c155f5 FC |
27 | import org.eclipse.ui.views.properties.IPropertyDescriptor; |
28 | import org.eclipse.ui.views.properties.IPropertySource2; | |
12c155f5 FC |
29 | |
30 | /** | |
339d539c PT |
31 | * Implementation of trace folder model element representing a trace folder in |
32 | * the project. | |
12c155f5 | 33 | * <p> |
b544077e BH |
34 | * @version 1.0 |
35 | * @author Francois Chouinard | |
12c155f5 FC |
36 | */ |
37 | public class TmfTraceFolder extends TmfProjectModelElement implements IPropertySource2 { | |
38 | ||
39 | // ------------------------------------------------------------------------ | |
40 | // Constants | |
41 | // ------------------------------------------------------------------------ | |
12c155f5 | 42 | |
12c155f5 FC |
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$ | |
47 | ||
253d5be1 BH |
48 | private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName); |
49 | private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath); | |
339d539c | 50 | private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfLocation, sfLocation); |
12c155f5 FC |
51 | |
52 | private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, | |
53 | sfLocationDescriptor }; | |
54 | ||
55 | static { | |
56 | sfNameDescriptor.setCategory(sfInfoCategory); | |
57 | sfPathDescriptor.setCategory(sfInfoCategory); | |
58 | sfLocationDescriptor.setCategory(sfInfoCategory); | |
59 | } | |
60 | ||
61 | // ------------------------------------------------------------------------ | |
62 | // Constructor | |
63 | // ------------------------------------------------------------------------ | |
339d539c | 64 | |
b544077e | 65 | /** |
013a5f1c | 66 | * Constructor. |
339d539c | 67 | * Creates folder model element under the project. |
b544077e | 68 | * @param name The name of trace folder. |
013a5f1c | 69 | * @param resource The folder resource. |
b544077e BH |
70 | * @param parent The parent element (project). |
71 | */ | |
12c155f5 FC |
72 | public TmfTraceFolder(String name, IFolder resource, TmfProjectElement parent) { |
73 | super(name, resource, parent); | |
74 | parent.addChild(this); | |
75 | } | |
76 | ||
339d539c PT |
77 | /** |
78 | * Constructor. | |
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). | |
83 | * @since 3.0 | |
84 | */ | |
85 | public TmfTraceFolder(String name, IFolder resource, TmfTraceFolder parent) { | |
86 | super(name, resource, parent); | |
87 | parent.addChild(this); | |
88 | } | |
89 | ||
12c155f5 FC |
90 | // ------------------------------------------------------------------------ |
91 | // TmfProjectModelElement | |
92 | // ------------------------------------------------------------------------ | |
013a5f1c | 93 | |
12c155f5 FC |
94 | @Override |
95 | public IFolder getResource() { | |
96 | return (IFolder) fResource; | |
97 | } | |
98 | ||
12c155f5 | 99 | @Override |
f537c959 PT |
100 | void refreshChildren() { |
101 | IFolder folder = getResource(); | |
102 | ||
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); | |
107 | } | |
108 | ||
109 | try { | |
110 | IResource[] members = folder.members(); | |
111 | for (IResource resource : members) { | |
112 | String name = resource.getName(); | |
339d539c | 113 | boolean isFolder = resource instanceof IFolder && |
4b3b667b | 114 | (TmfTraceType.getTraceTypeId(resource) == null); |
f537c959 | 115 | ITmfProjectModelElement element = childrenMap.get(name); |
339d539c | 116 | if (isFolder && !(element instanceof TmfTraceFolder) && !(element instanceof TmfTraceElement)) { |
a4a116c3 | 117 | if (TmfTraceType.isDirectoryTrace(resource.getLocationURI().getPath())) { |
339d539c PT |
118 | element = new TmfTraceElement(name, resource, this); |
119 | } else { | |
120 | element = new TmfTraceFolder(name, (IFolder) resource, this); | |
121 | } | |
122 | } else if (!isFolder && !(element instanceof TmfTraceElement)) { | |
f537c959 | 123 | element = new TmfTraceElement(name, resource, this); |
339d539c PT |
124 | } else { |
125 | childrenMap.remove(name); | |
f537c959 | 126 | } |
339d539c | 127 | ((TmfProjectModelElement) element).refreshChildren(); |
f537c959 PT |
128 | } |
129 | } catch (CoreException e) { | |
130 | } | |
131 | ||
132 | // Cleanup dangling children from the model | |
133 | for (ITmfProjectModelElement danglingChild : childrenMap.values()) { | |
134 | removeChild(danglingChild); | |
135 | } | |
12c155f5 FC |
136 | } |
137 | ||
138 | // ------------------------------------------------------------------------ | |
139 | // Operations | |
140 | // ------------------------------------------------------------------------ | |
11252342 | 141 | |
b544077e | 142 | /** |
339d539c | 143 | * Returns a list of trace elements under the folder element, recursively. |
b544077e BH |
144 | * @return list of trace model elements |
145 | */ | |
12c155f5 FC |
146 | public List<TmfTraceElement> getTraces() { |
147 | List<ITmfProjectModelElement> children = getChildren(); | |
507b1336 | 148 | List<TmfTraceElement> traces = new ArrayList<>(); |
12c155f5 FC |
149 | for (ITmfProjectModelElement child : children) { |
150 | if (child instanceof TmfTraceElement) { | |
151 | traces.add((TmfTraceElement) child); | |
339d539c PT |
152 | } else if (child instanceof TmfTraceFolder) { |
153 | traces.addAll(((TmfTraceFolder) child).getTraces()); | |
12c155f5 FC |
154 | } |
155 | } | |
156 | return traces; | |
157 | } | |
158 | ||
159 | // ------------------------------------------------------------------------ | |
160 | // IPropertySource2 | |
161 | // ------------------------------------------------------------------------ | |
162 | ||
163 | @Override | |
164 | public Object getEditableValue() { | |
165 | return null; | |
166 | } | |
167 | ||
168 | @Override | |
169 | public IPropertyDescriptor[] getPropertyDescriptors() { | |
77fdc5df | 170 | return Arrays.copyOf(sfDescriptors, sfDescriptors.length); |
12c155f5 FC |
171 | } |
172 | ||
173 | @Override | |
174 | public Object getPropertyValue(Object id) { | |
175 | ||
013a5f1c | 176 | if (sfName.equals(id)) { |
12c155f5 | 177 | return getName(); |
013a5f1c | 178 | } |
12c155f5 | 179 | |
013a5f1c | 180 | if (sfPath.equals(id)) { |
12c155f5 | 181 | return getPath().toString(); |
013a5f1c | 182 | } |
12c155f5 | 183 | |
013a5f1c | 184 | if (sfLocation.equals(id)) { |
12c155f5 | 185 | return getLocation().toString(); |
013a5f1c | 186 | } |
12c155f5 FC |
187 | |
188 | return null; | |
189 | } | |
190 | ||
191 | @Override | |
192 | public void resetPropertyValue(Object id) { | |
193 | } | |
194 | ||
195 | @Override | |
196 | public void setPropertyValue(Object id, Object value) { | |
197 | } | |
198 | ||
199 | @Override | |
200 | public boolean isPropertyResettable(Object id) { | |
201 | return false; | |
202 | } | |
203 | ||
204 | @Override | |
205 | public boolean isPropertySet(Object id) { | |
206 | return false; | |
207 | } | |
208 | ||
209 | } |