Commit | Line | Data |
---|---|---|
4999a196 | 1 | /******************************************************************************* |
50d36521 | 2 | * Copyright (c) 2012, 2015 Ericsson, École Polytechnique de Montréal |
4999a196 GB |
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 | * Patrick Tasse - Initial API and implementation | |
11 | * Geneviève Bastien - Move code to provide base classes for time graph view | |
12 | *******************************************************************************/ | |
13 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model; |
4999a196 GB |
15 | |
16 | import java.util.ArrayList; | |
9ba941e9 PT |
17 | import java.util.Arrays; |
18 | import java.util.Comparator; | |
4999a196 GB |
19 | import java.util.Iterator; |
20 | import java.util.List; | |
1cf25311 | 21 | import java.util.concurrent.CopyOnWriteArrayList; |
4999a196 | 22 | |
0b02f22a | 23 | import org.eclipse.jdt.annotation.NonNull; |
50d36521 PT |
24 | import org.eclipse.swt.SWT; |
25 | ||
4999a196 GB |
26 | /** |
27 | * An entry for use in the time graph views | |
4999a196 GB |
28 | */ |
29 | public class TimeGraphEntry implements ITimeGraphEntry { | |
30 | ||
4999a196 | 31 | /** Entry's parent */ |
a3188982 | 32 | private ITimeGraphEntry fParent = null; |
4999a196 GB |
33 | |
34 | /** List of child entries */ | |
a3188982 | 35 | private final List<ITimeGraphEntry> fChildren = new CopyOnWriteArrayList<>(); |
4999a196 GB |
36 | |
37 | /** Name of this entry (text to show) */ | |
38 | private String fName; | |
50d36521 PT |
39 | private long fStartTime = SWT.DEFAULT; |
40 | private long fEndTime = SWT.DEFAULT; | |
0b02f22a PT |
41 | private @NonNull List<ITimeEvent> fEventList = new ArrayList<>(); |
42 | private @NonNull List<ITimeEvent> fZoomedEventList = new ArrayList<>(); | |
9ba941e9 | 43 | private Comparator<ITimeGraphEntry> fComparator; |
4999a196 GB |
44 | |
45 | /** | |
46 | * Constructor | |
47 | * | |
4999a196 | 48 | * @param name |
1d46dc38 | 49 | * The name of this entry |
4999a196 | 50 | * @param startTime |
1d46dc38 | 51 | * The start time of this entry |
4999a196 | 52 | * @param endTime |
1d46dc38 | 53 | * The end time of this entry |
4999a196 | 54 | */ |
1d46dc38 | 55 | public TimeGraphEntry(String name, long startTime, long endTime) { |
4999a196 GB |
56 | fName = name; |
57 | fStartTime = startTime; | |
58 | fEndTime = endTime; | |
59 | } | |
60 | ||
61 | // --------------------------------------------- | |
62 | // Getters and setters | |
63 | // --------------------------------------------- | |
64 | ||
65 | @Override | |
66 | public ITimeGraphEntry getParent() { | |
67 | return fParent; | |
68 | } | |
69 | ||
70 | /** | |
71 | * Sets the entry's parent | |
72 | * | |
73 | * @param entry The new parent entry | |
74 | */ | |
a3188982 PT |
75 | /* |
76 | * TODO: This method can be removed in the next major API version. | |
77 | */ | |
4999a196 GB |
78 | protected void setParent(TimeGraphEntry entry) { |
79 | fParent = entry; | |
80 | } | |
81 | ||
a3188982 PT |
82 | /** |
83 | * Sets the entry's parent | |
84 | * | |
85 | * @param entry The new parent entry | |
a3188982 PT |
86 | */ |
87 | /* | |
88 | * TODO: This method should be added to the interface in the next major API version. | |
89 | */ | |
90 | protected void setParent(ITimeGraphEntry entry) { | |
91 | fParent = entry; | |
92 | } | |
93 | ||
4999a196 | 94 | @Override |
9ba941e9 | 95 | public synchronized boolean hasChildren() { |
4999a196 GB |
96 | return fChildren.size() > 0; |
97 | } | |
98 | ||
99 | @Override | |
9ba941e9 | 100 | public synchronized List<? extends ITimeGraphEntry> getChildren() { |
4999a196 GB |
101 | return fChildren; |
102 | } | |
103 | ||
104 | @Override | |
105 | public String getName() { | |
106 | return fName; | |
107 | } | |
108 | ||
109 | /** | |
110 | * Update the entry name | |
111 | * | |
112 | * @param name | |
113 | * the updated entry name | |
114 | */ | |
115 | public void setName(String name) { | |
116 | fName = name; | |
117 | } | |
118 | ||
119 | @Override | |
120 | public long getStartTime() { | |
121 | return fStartTime; | |
122 | } | |
123 | ||
124 | @Override | |
125 | public long getEndTime() { | |
126 | return fEndTime; | |
127 | } | |
128 | ||
1cf25311 PT |
129 | /** |
130 | * Updates the end time | |
131 | * | |
132 | * @param endTime | |
133 | * the end time | |
1cf25311 PT |
134 | */ |
135 | public void updateEndTime(long endTime) { | |
136 | fEndTime = Math.max(endTime, fEndTime); | |
137 | } | |
138 | ||
4999a196 GB |
139 | @Override |
140 | public boolean hasTimeEvents() { | |
141 | return true; | |
142 | } | |
143 | ||
144 | @Override | |
145 | public Iterator<ITimeEvent> getTimeEventsIterator() { | |
146 | if (hasTimeEvents()) { | |
147 | return new EventIterator(fEventList, fZoomedEventList); | |
148 | } | |
149 | return null; | |
150 | } | |
151 | ||
152 | @Override | |
153 | public Iterator<ITimeEvent> getTimeEventsIterator(long startTime, long stopTime, long visibleDuration) { | |
154 | if (!hasTimeEvents()) { | |
155 | return null; | |
156 | } | |
157 | return new EventIterator(fEventList, fZoomedEventList, startTime, stopTime); | |
158 | } | |
159 | ||
160 | /** | |
1d46dc38 | 161 | * Add an event to this entry's event list. If necessary, update the start |
1cf25311 PT |
162 | * and end time of the entry. If the event list's last event starts at the |
163 | * same time as the event to add, it is replaced by the new event. | |
4999a196 GB |
164 | * |
165 | * @param event | |
1cf25311 | 166 | * The time event to add |
4999a196 GB |
167 | */ |
168 | public void addEvent(ITimeEvent event) { | |
169 | long start = event.getTime(); | |
170 | long end = start + event.getDuration(); | |
0b02f22a PT |
171 | int lastIndex = fEventList.size() - 1; |
172 | if (lastIndex >= 0 && fEventList.get(lastIndex).getTime() == event.getTime()) { | |
173 | fEventList.set(lastIndex, event); | |
174 | } else { | |
175 | fEventList.add(event); | |
176 | } | |
cb982e03 PT |
177 | if (event instanceof NullTimeEvent) { |
178 | /* A NullTimeEvent should not affect the entry bounds */ | |
179 | return; | |
180 | } | |
0b02f22a PT |
181 | if (fStartTime == SWT.DEFAULT || start < fStartTime) { |
182 | fStartTime = start; | |
183 | } | |
184 | if (fEndTime == SWT.DEFAULT || end > fEndTime) { | |
185 | fEndTime = end; | |
4999a196 GB |
186 | } |
187 | } | |
188 | ||
189 | /** | |
0b02f22a PT |
190 | * Set the general event list of this entry. The list should be modifiable |
191 | * but will only increase in size over time. | |
4999a196 | 192 | * |
4999a196 | 193 | * @param eventList |
0b02f22a | 194 | * The modifiable list of time events, or null to clear the list |
4999a196 GB |
195 | */ |
196 | public void setEventList(List<ITimeEvent> eventList) { | |
3ce8c834 | 197 | if (eventList != null) { |
0b02f22a | 198 | fEventList = eventList; |
3ce8c834 | 199 | } else { |
507b1336 | 200 | fEventList = new ArrayList<>(); |
3ce8c834 | 201 | } |
4999a196 GB |
202 | } |
203 | ||
204 | /** | |
0b02f22a PT |
205 | * Set the zoomed event list of this entry. The list should be modifiable |
206 | * but will only increase in size over time. | |
4999a196 | 207 | * |
4999a196 | 208 | * @param eventList |
0b02f22a | 209 | * The modifiable list of time events, or null to clear the list |
4999a196 GB |
210 | */ |
211 | public void setZoomedEventList(List<ITimeEvent> eventList) { | |
3ce8c834 | 212 | if (eventList != null) { |
0b02f22a | 213 | fZoomedEventList = eventList; |
3ce8c834 | 214 | } else { |
507b1336 | 215 | fZoomedEventList = new ArrayList<>(); |
3ce8c834 | 216 | } |
4999a196 GB |
217 | } |
218 | ||
0b02f22a PT |
219 | /** |
220 | * Add an event to this entry's zoomed event list. If necessary, update the | |
221 | * start and end time of the entry. If the zoomed event list's last event | |
222 | * starts at the same time as the event to add, it is replaced by the new | |
223 | * event. If the new event starts before the zoomed event list's last event, | |
224 | * the new event is ignored and is assumed to be already part of the list. | |
225 | * | |
226 | * @param event | |
227 | * The time event to add | |
0336f981 | 228 | * @since 1.1 |
0b02f22a PT |
229 | */ |
230 | public void addZoomedEvent(ITimeEvent event) { | |
231 | long start = event.getTime(); | |
232 | long end = start + event.getDuration(); | |
233 | int lastIndex = fZoomedEventList.size() - 1; | |
234 | long lastStart = lastIndex >= 0 ? fZoomedEventList.get(lastIndex).getTime() : Long.MIN_VALUE; | |
235 | if (start > lastStart) { | |
236 | fZoomedEventList.add(event); | |
237 | } else if (start == lastStart) { | |
238 | fZoomedEventList.set(lastIndex, event); | |
239 | } | |
cb982e03 PT |
240 | if (event instanceof NullTimeEvent) { |
241 | /* A NullTimeEvent should not affect the entry bounds */ | |
242 | return; | |
243 | } | |
0b02f22a PT |
244 | if (fStartTime == SWT.DEFAULT || start < fStartTime) { |
245 | fStartTime = start; | |
246 | } | |
247 | if (fEndTime == SWT.DEFAULT || end > fEndTime) { | |
248 | fEndTime = end; | |
249 | } | |
250 | } | |
251 | ||
4999a196 | 252 | /** |
1d46dc38 | 253 | * Add a child entry to this one |
4999a196 GB |
254 | * |
255 | * @param child | |
256 | * The child entry | |
257 | */ | |
a3188982 PT |
258 | /* |
259 | * TODO: This method can be removed in the next major API version. | |
260 | */ | |
9ba941e9 PT |
261 | public synchronized void addChild(TimeGraphEntry child) { |
262 | addChild((ITimeGraphEntry) child); | |
4999a196 GB |
263 | } |
264 | ||
a3188982 | 265 | /** |
9ba941e9 PT |
266 | * Add a child entry to this one. If a comparator was previously set with |
267 | * {@link #sortChildren(Comparator)}, the entry will be inserted in its | |
268 | * sort-order position. Otherwise it will be added to the end of the list. | |
a3188982 PT |
269 | * |
270 | * @param child | |
271 | * The child entry | |
a3188982 | 272 | */ |
9ba941e9 PT |
273 | public synchronized void addChild(ITimeGraphEntry child) { |
274 | /* | |
275 | * TODO: Use setParent() once it is added to the interface. | |
276 | */ | |
a3188982 PT |
277 | if (child instanceof TimeGraphEntry) { |
278 | ((TimeGraphEntry) child).fParent = this; | |
279 | } | |
9ba941e9 PT |
280 | if (fComparator == null) { |
281 | fChildren.add(child); | |
282 | } else { | |
283 | int i; | |
284 | for (i = 0; i < fChildren.size(); i++) { | |
285 | ITimeGraphEntry entry = fChildren.get(i); | |
286 | if (fComparator.compare(child, entry) < 0) { | |
287 | break; | |
288 | } | |
289 | } | |
290 | fChildren.add(i, child); | |
291 | } | |
a3188982 PT |
292 | } |
293 | ||
294 | /** | |
295 | * Add a child entry to this one at the specified position | |
296 | * | |
297 | * @param index | |
298 | * Index at which the specified entry is to be inserted | |
299 | * @param child | |
300 | * The child entry | |
a3188982 | 301 | */ |
9ba941e9 PT |
302 | public synchronized void addChild(int index, ITimeGraphEntry child) { |
303 | /* | |
304 | * TODO: Use setParent() once it is added to the interface. | |
305 | */ | |
a3188982 PT |
306 | if (child instanceof TimeGraphEntry) { |
307 | ((TimeGraphEntry) child).fParent = this; | |
308 | } | |
309 | fChildren.add(index, child); | |
310 | } | |
311 | ||
9ba941e9 PT |
312 | /** |
313 | * Sort the children of this entry using the provided comparator. Subsequent | |
314 | * calls to {@link #addChild(ITimeGraphEntry)} will use this comparator to | |
315 | * maintain the sort order. | |
316 | * | |
317 | * @param comparator | |
318 | * The entry comparator | |
9ba941e9 PT |
319 | */ |
320 | public synchronized void sortChildren(Comparator<ITimeGraphEntry> comparator) { | |
321 | fComparator = comparator; | |
322 | if (comparator == null) { | |
323 | return; | |
324 | } | |
325 | ITimeGraphEntry[] array = fChildren.toArray(new ITimeGraphEntry[0]); | |
326 | Arrays.sort(array, comparator); | |
327 | fChildren.clear(); | |
328 | fChildren.addAll(Arrays.asList(array)); | |
329 | } | |
330 | ||
b1b156f3 PT |
331 | @Override |
332 | public String toString() { | |
333 | return getClass().getSimpleName() + '(' + fName + ')'; | |
334 | } | |
335 | ||
4999a196 | 336 | } |