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