c0b5eef39396c38dda5f1f14bdc6e6a84b21a256
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson, École Polytechnique de Montréal
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
10 * Patrick Tasse - Initial API and implementation
11 * Geneviève Bastien - Move code to provide base classes for time graph view
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
;
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
;
23 import org
.eclipse
.jdt
.annotation
.NonNull
;
24 import org
.eclipse
.swt
.SWT
;
27 * An entry for use in the time graph views
29 public class TimeGraphEntry
implements ITimeGraphEntry
{
32 private ITimeGraphEntry fParent
= null;
34 /** List of child entries */
35 private final List
<ITimeGraphEntry
> fChildren
= new CopyOnWriteArrayList
<>();
37 /** Name of this entry (text to show) */
39 private long fStartTime
= SWT
.DEFAULT
;
40 private long fEndTime
= SWT
.DEFAULT
;
41 private @NonNull List
<ITimeEvent
> fEventList
= new ArrayList
<>();
42 private @NonNull List
<ITimeEvent
> fZoomedEventList
= new ArrayList
<>();
43 private Comparator
<ITimeGraphEntry
> fComparator
;
49 * The name of this entry
51 * The start time of this entry
53 * The end time of this entry
55 public TimeGraphEntry(String name
, long startTime
, long endTime
) {
57 fStartTime
= startTime
;
61 // ---------------------------------------------
62 // Getters and setters
63 // ---------------------------------------------
66 public ITimeGraphEntry
getParent() {
71 * Sets the entry's parent
73 * @param entry The new parent entry
76 * TODO: This method can be removed in the next major API version.
78 protected void setParent(TimeGraphEntry entry
) {
83 * Sets the entry's parent
85 * @param entry The new parent entry
88 * TODO: This method should be added to the interface in the next major API version.
90 protected void setParent(ITimeGraphEntry entry
) {
95 public synchronized boolean hasChildren() {
96 return fChildren
.size() > 0;
100 public synchronized List
<?
extends ITimeGraphEntry
> getChildren() {
105 public String
getName() {
110 * Update the entry name
113 * the updated entry name
115 public void setName(String name
) {
120 public long getStartTime() {
125 public long getEndTime() {
130 * Updates the end time
135 public void updateEndTime(long endTime
) {
136 fEndTime
= Math
.max(endTime
, fEndTime
);
140 public boolean hasTimeEvents() {
145 public Iterator
<ITimeEvent
> getTimeEventsIterator() {
146 if (hasTimeEvents()) {
147 return new EventIterator(fEventList
, fZoomedEventList
);
153 public Iterator
<ITimeEvent
> getTimeEventsIterator(long startTime
, long stopTime
, long visibleDuration
) {
154 if (!hasTimeEvents()) {
157 return new EventIterator(fEventList
, fZoomedEventList
, startTime
, stopTime
);
161 * Add an event to this entry's event list. If necessary, update the start
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.
166 * The time event to add
168 public void addEvent(ITimeEvent event
) {
169 long start
= event
.getTime();
170 long end
= start
+ event
.getDuration();
171 int lastIndex
= fEventList
.size() - 1;
172 if (lastIndex
>= 0 && fEventList
.get(lastIndex
).getTime() == event
.getTime()) {
173 fEventList
.set(lastIndex
, event
);
175 fEventList
.add(event
);
177 if (event
instanceof NullTimeEvent
) {
178 /* A NullTimeEvent should not affect the entry bounds */
181 if (fStartTime
== SWT
.DEFAULT
|| start
< fStartTime
) {
184 if (fEndTime
== SWT
.DEFAULT
|| end
> fEndTime
) {
190 * Set the general event list of this entry. The list should be modifiable
191 * but will only increase in size over time.
194 * The modifiable list of time events, or null to clear the list
196 public void setEventList(List
<ITimeEvent
> eventList
) {
197 if (eventList
!= null) {
198 fEventList
= eventList
;
200 fEventList
= new ArrayList
<>();
205 * Set the zoomed event list of this entry. The list should be modifiable
206 * but will only increase in size over time.
209 * The modifiable list of time events, or null to clear the list
211 public void setZoomedEventList(List
<ITimeEvent
> eventList
) {
212 if (eventList
!= null) {
213 fZoomedEventList
= eventList
;
215 fZoomedEventList
= new ArrayList
<>();
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.
227 * The time event to add
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
);
240 if (event
instanceof NullTimeEvent
) {
241 /* A NullTimeEvent should not affect the entry bounds */
244 if (fStartTime
== SWT
.DEFAULT
|| start
< fStartTime
) {
247 if (fEndTime
== SWT
.DEFAULT
|| end
> fEndTime
) {
253 * Add a child entry to this one
259 * TODO: This method can be removed in the next major API version.
261 public synchronized void addChild(TimeGraphEntry child
) {
262 addChild((ITimeGraphEntry
) child
);
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.
273 public synchronized void addChild(ITimeGraphEntry child
) {
275 * TODO: Use setParent() once it is added to the interface.
277 if (child
instanceof TimeGraphEntry
) {
278 ((TimeGraphEntry
) child
).fParent
= this;
280 if (fComparator
== null) {
281 fChildren
.add(child
);
284 for (i
= 0; i
< fChildren
.size(); i
++) {
285 ITimeGraphEntry entry
= fChildren
.get(i
);
286 if (fComparator
.compare(child
, entry
) < 0) {
290 fChildren
.add(i
, child
);
295 * Add a child entry to this one at the specified position
298 * Index at which the specified entry is to be inserted
302 public synchronized void addChild(int index
, ITimeGraphEntry child
) {
304 * TODO: Use setParent() once it is added to the interface.
306 if (child
instanceof TimeGraphEntry
) {
307 ((TimeGraphEntry
) child
).fParent
= this;
309 fChildren
.add(index
, child
);
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.
318 * The entry comparator
320 public synchronized void sortChildren(Comparator
<ITimeGraphEntry
> comparator
) {
321 fComparator
= comparator
;
322 if (comparator
== null) {
325 ITimeGraphEntry
[] array
= fChildren
.toArray(new ITimeGraphEntry
[0]);
326 Arrays
.sort(array
, comparator
);
328 fChildren
.addAll(Arrays
.asList(array
));
332 public String
toString() {
333 return getClass().getSimpleName() + '(' + fName
+ ')';
This page took 0.043508 seconds and 5 git commands to generate.