1 /*******************************************************************************
2 * Copyright (c) 2012, 2016 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
;
22 import java
.util
.regex
.Pattern
;
24 import org
.eclipse
.jdt
.annotation
.NonNull
;
25 import org
.eclipse
.swt
.SWT
;
28 * An entry for use in the time graph views
30 public class TimeGraphEntry
implements ITimeGraphEntry
{
33 private TimeGraphEntry fParent
= null;
35 /** List of child entries */
36 private final List
<@NonNull TimeGraphEntry
> fChildren
= new CopyOnWriteArrayList
<>();
38 /** Name of this entry (text to show) */
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
;
50 * The name of this entry
52 * The start time of this entry
54 * The end time of this entry
56 public TimeGraphEntry(String name
, long startTime
, long endTime
) {
58 fStartTime
= startTime
;
62 // ---------------------------------------------
63 // Getters and setters
64 // ---------------------------------------------
70 public TimeGraphEntry
getParent() {
75 * Sets the entry's parent
77 * @param entry The new parent entry
80 public void setParent(TimeGraphEntry entry
) {
85 public synchronized boolean hasChildren() {
86 return fChildren
.size() > 0;
90 public synchronized List
<@NonNull TimeGraphEntry
> getChildren() {
95 public String
getName() {
100 * Update the entry name
103 * the updated entry name
105 public void setName(String name
) {
110 public long getStartTime() {
115 public long getEndTime() {
120 * Updates the end time
125 public void updateEndTime(long endTime
) {
126 fEndTime
= Math
.max(endTime
, fEndTime
);
130 public boolean hasTimeEvents() {
135 public Iterator
<@NonNull ITimeEvent
> getTimeEventsIterator() {
136 if (hasTimeEvents()) {
137 return new EventIterator(fEventList
, fZoomedEventList
);
143 public Iterator
<@NonNull ITimeEvent
> getTimeEventsIterator(long startTime
, long stopTime
, long visibleDuration
) {
144 if (!hasTimeEvents()) {
147 return new EventIterator(fEventList
, fZoomedEventList
, startTime
, stopTime
);
151 * Add an event to this entry's event list. If necessary, update the start
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.
156 * The time event to add
158 public void addEvent(ITimeEvent event
) {
159 long start
= event
.getTime();
160 long end
= start
+ event
.getDuration();
161 int lastIndex
= fEventList
.size() - 1;
162 if (lastIndex
>= 0 && fEventList
.get(lastIndex
).getTime() == event
.getTime()) {
163 fEventList
.set(lastIndex
, event
);
165 fEventList
.add(event
);
167 if (event
instanceof NullTimeEvent
) {
168 /* A NullTimeEvent should not affect the entry bounds */
171 if (fStartTime
== SWT
.DEFAULT
|| start
< fStartTime
) {
174 if (fEndTime
== SWT
.DEFAULT
|| end
> fEndTime
) {
180 * Set the general event list of this entry. The list should be modifiable
181 * but will only increase in size over time.
184 * The modifiable list of time events, or null to clear the list
186 public void setEventList(List
<ITimeEvent
> eventList
) {
187 if (eventList
!= null) {
188 fEventList
= eventList
;
190 fEventList
= new ArrayList
<>();
195 * Set the zoomed event list of this entry. The list should be modifiable
196 * but will only increase in size over time.
199 * The modifiable list of time events, or null to clear the list
201 public void setZoomedEventList(List
<ITimeEvent
> eventList
) {
202 if (eventList
!= null) {
203 fZoomedEventList
= eventList
;
205 fZoomedEventList
= new ArrayList
<>();
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.
217 * The time event to add
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
);
230 if (event
instanceof NullTimeEvent
) {
231 /* A NullTimeEvent should not affect the entry bounds */
234 if (fStartTime
== SWT
.DEFAULT
|| start
< fStartTime
) {
237 if (fEndTime
== SWT
.DEFAULT
|| end
> fEndTime
) {
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.
250 public synchronized void addChild(@NonNull TimeGraphEntry child
) {
251 child
.setParent(this);
252 if (fComparator
== null) {
253 fChildren
.add(child
);
256 for (i
= 0; i
< fChildren
.size(); i
++) {
257 ITimeGraphEntry entry
= fChildren
.get(i
);
258 if (fComparator
.compare(child
, entry
) < 0) {
262 fChildren
.add(i
, child
);
267 * Add a child entry to this one at the specified position
270 * Index at which the specified entry is to be inserted
275 public synchronized void addChild(int index
, @NonNull TimeGraphEntry child
) {
276 child
.setParent(this);
277 fChildren
.add(index
, child
);
281 * Sort the children of this entry using the provided comparator. Subsequent
282 * calls to {@link #addChild(TimeGraphEntry)} will use this comparator to
283 * maintain the sort order.
286 * The entry comparator
288 public synchronized void sortChildren(Comparator
<ITimeGraphEntry
> comparator
) {
289 fComparator
= comparator
;
290 if (comparator
== null) {
293 @NonNull TimeGraphEntry
[] array
= fChildren
.toArray(new @NonNull TimeGraphEntry
[0]);
294 Arrays
.sort(array
, comparator
);
296 fChildren
.addAll(Arrays
.asList(array
));
300 public String
toString() {
301 return getClass().getSimpleName() + '(' + fName
+ ')';
308 public boolean matches(@NonNull Pattern pattern
) {
309 // Default implementation
310 return pattern
.matcher(fName
).find();
This page took 0.037476 seconds and 5 git commands to generate.