1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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
.linuxtools
.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
;
24 * An entry for use in the time graph views
28 public class TimeGraphEntry
implements ITimeGraphEntry
{
31 private ITimeGraphEntry fParent
= null;
33 /** List of child entries */
34 private final List
<ITimeGraphEntry
> fChildren
= new CopyOnWriteArrayList
<>();
36 /** Name of this entry (text to show) */
38 private long fStartTime
= -1;
39 private long fEndTime
= -1;
40 private List
<ITimeEvent
> fEventList
= new ArrayList
<>();
41 private List
<ITimeEvent
> fZoomedEventList
= new ArrayList
<>();
42 private Comparator
<ITimeGraphEntry
> fComparator
;
48 * The name of this entry
50 * The start time of this entry
52 * The end time of this entry
54 public TimeGraphEntry(String name
, long startTime
, long endTime
) {
56 fStartTime
= startTime
;
60 // ---------------------------------------------
61 // Getters and setters
62 // ---------------------------------------------
65 public ITimeGraphEntry
getParent() {
70 * Sets the entry's parent
72 * @param entry The new parent entry
75 * TODO: This method can be removed in the next major API version.
77 protected void setParent(TimeGraphEntry entry
) {
82 * Sets the entry's parent
84 * @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
137 public void updateEndTime(long endTime
) {
138 fEndTime
= Math
.max(endTime
, fEndTime
);
142 public boolean hasTimeEvents() {
147 public Iterator
<ITimeEvent
> getTimeEventsIterator() {
148 if (hasTimeEvents()) {
149 return new EventIterator(fEventList
, fZoomedEventList
);
155 public Iterator
<ITimeEvent
> getTimeEventsIterator(long startTime
, long stopTime
, long visibleDuration
) {
156 if (!hasTimeEvents()) {
159 return new EventIterator(fEventList
, fZoomedEventList
, startTime
, stopTime
);
163 * Add an event to this entry's event list. If necessary, update the start
164 * and end time of the entry. If the event list's last event starts at the
165 * same time as the event to add, it is replaced by the new event.
168 * The time event to add
170 public void addEvent(ITimeEvent event
) {
171 long start
= event
.getTime();
172 long end
= start
+ event
.getDuration();
173 synchronized (fEventList
) {
174 int lastIndex
= fEventList
.size() - 1;
175 if (lastIndex
>= 0 && fEventList
.get(lastIndex
).getTime() == event
.getTime()) {
176 fEventList
.set(lastIndex
, event
);
178 fEventList
.add(event
);
180 if (fStartTime
== -1 || start
< fStartTime
) {
183 if (fEndTime
== -1 || end
> fEndTime
) {
190 * Set the general event list of this entry.
193 * The list of time events
195 public void setEventList(List
<ITimeEvent
> eventList
) {
196 if (eventList
!= null) {
197 fEventList
= new ArrayList
<>(eventList
);
199 fEventList
= new ArrayList
<>();
204 * Set the zoomed event list of this entry.
207 * The list of time events
209 public void setZoomedEventList(List
<ITimeEvent
> eventList
) {
210 if (eventList
!= null) {
211 fZoomedEventList
= new ArrayList
<>(eventList
);
213 fZoomedEventList
= new ArrayList
<>();
218 * Add a child entry to this one
224 * TODO: This method can be removed in the next major API version.
226 public synchronized void addChild(TimeGraphEntry child
) {
227 addChild((ITimeGraphEntry
) child
);
231 * Add a child entry to this one. If a comparator was previously set with
232 * {@link #sortChildren(Comparator)}, the entry will be inserted in its
233 * sort-order position. Otherwise it will be added to the end of the list.
239 public synchronized void addChild(ITimeGraphEntry child
) {
241 * TODO: Use setParent() once it is added to the interface.
243 if (child
instanceof TimeGraphEntry
) {
244 ((TimeGraphEntry
) child
).fParent
= this;
246 if (fComparator
== null) {
247 fChildren
.add(child
);
250 for (i
= 0; i
< fChildren
.size(); i
++) {
251 ITimeGraphEntry entry
= fChildren
.get(i
);
252 if (fComparator
.compare(child
, entry
) < 0) {
256 fChildren
.add(i
, child
);
261 * Add a child entry to this one at the specified position
264 * Index at which the specified entry is to be inserted
269 public synchronized void addChild(int index
, ITimeGraphEntry child
) {
271 * TODO: Use setParent() once it is added to the interface.
273 if (child
instanceof TimeGraphEntry
) {
274 ((TimeGraphEntry
) child
).fParent
= this;
276 fChildren
.add(index
, child
);
280 * Sort the children of this entry using the provided comparator. Subsequent
281 * calls to {@link #addChild(ITimeGraphEntry)} will use this comparator to
282 * maintain the sort order.
285 * The entry comparator
288 public synchronized void sortChildren(Comparator
<ITimeGraphEntry
> comparator
) {
289 fComparator
= comparator
;
290 if (comparator
== null) {
293 ITimeGraphEntry
[] array
= fChildren
.toArray(new ITimeGraphEntry
[0]);
294 Arrays
.sort(array
, comparator
);
296 fChildren
.addAll(Arrays
.asList(array
));
300 public String
toString() {
301 return getClass().getSimpleName() + '(' + fName
+ ')';
This page took 0.053393 seconds and 5 git commands to generate.