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