tmf: Refactor resource change listener in project model
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceFolder.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.project.model;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.linuxtools.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
25 import org.eclipse.ui.views.properties.IPropertyDescriptor;
26 import org.eclipse.ui.views.properties.IPropertySource2;
27
28 /**
29 * Implementation of trace folder model element representing the trace folder in the project.
30 * <p>
31 * @version 1.0
32 * @author Francois Chouinard
33 */
34 public class TmfTraceFolder extends TmfProjectModelElement implements IPropertySource2 {
35
36 // ------------------------------------------------------------------------
37 // Constants
38 // ------------------------------------------------------------------------
39 /**
40 * The name of the trace folder
41 */
42 public static final String TRACE_FOLDER_NAME = "Traces"; //$NON-NLS-1$
43
44 // Property View stuff
45 private static final String sfInfoCategory = "Info"; //$NON-NLS-1$
46 private static final String sfName = "name"; //$NON-NLS-1$
47 private static final String sfPath = "path"; //$NON-NLS-1$
48 private static final String sfLocation = "location"; //$NON-NLS-1$
49
50 private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName);
51 private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath);
52 private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfLocation,
53 sfLocation);
54
55 private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor,
56 sfLocationDescriptor };
57
58 static {
59 sfNameDescriptor.setCategory(sfInfoCategory);
60 sfPathDescriptor.setCategory(sfInfoCategory);
61 sfLocationDescriptor.setCategory(sfInfoCategory);
62 }
63
64 // ------------------------------------------------------------------------
65 // Constructor
66 // ------------------------------------------------------------------------
67 /**
68 * Constructor.
69 * Creates trace folder model element under the project.
70 * @param name The name of trace folder.
71 * @param resource The folder resource.
72 * @param parent The parent element (project).
73 */
74 public TmfTraceFolder(String name, IFolder resource, TmfProjectElement parent) {
75 super(name, resource, parent);
76 parent.addChild(this);
77 }
78
79 // ------------------------------------------------------------------------
80 // TmfProjectModelElement
81 // ------------------------------------------------------------------------
82
83 @Override
84 public IFolder getResource() {
85 return (IFolder) fResource;
86 }
87
88 @Override
89 public TmfProjectElement getProject() {
90 return (TmfProjectElement) getParent();
91 }
92
93 @Override
94 void refreshChildren() {
95 IFolder folder = getResource();
96
97 // Get the children from the model
98 Map<String, ITmfProjectModelElement> childrenMap = new HashMap<>();
99 for (ITmfProjectModelElement element : getChildren()) {
100 childrenMap.put(element.getResource().getName(), element);
101 }
102
103 try {
104 IResource[] members = folder.members();
105 for (IResource resource : members) {
106 String name = resource.getName();
107 ITmfProjectModelElement element = childrenMap.get(name);
108 if (element instanceof TmfTraceElement) {
109 childrenMap.remove(name);
110 } else {
111 element = new TmfTraceElement(name, resource, this);
112 }
113 ((TmfTraceElement) element).refreshChildren();
114 }
115 } catch (CoreException e) {
116 }
117
118 // Cleanup dangling children from the model
119 for (ITmfProjectModelElement danglingChild : childrenMap.values()) {
120 removeChild(danglingChild);
121 }
122 }
123
124 // ------------------------------------------------------------------------
125 // Operations
126 // ------------------------------------------------------------------------
127
128 /**
129 * Returns a list of trace model elements under the traces folder.
130 * @return list of trace model elements
131 */
132 public List<TmfTraceElement> getTraces() {
133 List<ITmfProjectModelElement> children = getChildren();
134 List<TmfTraceElement> traces = new ArrayList<>();
135 for (ITmfProjectModelElement child : children) {
136 if (child instanceof TmfTraceElement) {
137 traces.add((TmfTraceElement) child);
138 }
139 }
140 return traces;
141 }
142
143 // ------------------------------------------------------------------------
144 // IPropertySource2
145 // ------------------------------------------------------------------------
146
147 @Override
148 public Object getEditableValue() {
149 return null;
150 }
151
152 @Override
153 public IPropertyDescriptor[] getPropertyDescriptors() {
154 return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
155 }
156
157 @Override
158 public Object getPropertyValue(Object id) {
159
160 if (sfName.equals(id)) {
161 return getName();
162 }
163
164 if (sfPath.equals(id)) {
165 return getPath().toString();
166 }
167
168 if (sfLocation.equals(id)) {
169 return getLocation().toString();
170 }
171
172 return null;
173 }
174
175 @Override
176 public void resetPropertyValue(Object id) {
177 }
178
179 @Override
180 public void setPropertyValue(Object id, Object value) {
181 }
182
183 @Override
184 public boolean isPropertyResettable(Object id) {
185 return false;
186 }
187
188 @Override
189 public boolean isPropertySet(Object id) {
190 return false;
191 }
192
193 }
This page took 0.048465 seconds and 5 git commands to generate.