1
1
/*******************************************************************************
2
* Copyright (c) 2012, 2013 Ericsson, École Polytechnique de Montréal
2
* Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
4
4
* All rights reserved. This program and the accompanying materials are
5
5
* made available under the terms of the Eclipse Public License v1.0 which
14
14
package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model;
16
16
import java.util.ArrayList;
17
import java.util.Arrays;
18
import java.util.Comparator;
17
19
import java.util.Iterator;
18
20
import java.util.List;
21
import java.util.concurrent.CopyOnWriteArrayList;
21
24
* An entry for use in the time graph views
25
28
public class TimeGraphEntry implements ITimeGraphEntry {
27
30
/** Entry's parent */
28
private TimeGraphEntry fParent = null;
31
private ITimeGraphEntry fParent = null;
30
33
/** List of child entries */
31
private final List<TimeGraphEntry> fChildren = new ArrayList<TimeGraphEntry>();
34
private final List<ITimeGraphEntry> fChildren = new CopyOnWriteArrayList<>();
33
36
/** Name of this entry (text to show) */
34
37
private String fName;
35
38
private long fStartTime = -1;
36
39
private long fEndTime = -1;
37
private List<ITimeEvent> fEventList = new ArrayList<ITimeEvent>();
38
private List<ITimeEvent> fZoomedEventList = new ArrayList<ITimeEvent>();
40
private List<ITimeEvent> fEventList = new ArrayList<>();
41
private List<ITimeEvent> fZoomedEventList = new ArrayList<>();
42
private Comparator<ITimeGraphEntry> fComparator;
68
72
* @param entry The new parent entry
75
* TODO: This method can be removed in the next major API version.
70
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) {
75
public boolean hasChildren() {
95
public synchronized boolean hasChildren() {
76
96
return fChildren.size() > 0;
80
public List<TimeGraphEntry> getChildren() {
100
public synchronized List<? extends ITimeGraphEntry> getChildren() {
131
163
* Add an event to this entry's event list. If necessary, update the start
132
* and end time of the entry.
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
137
170
public void addEvent(ITimeEvent event) {
138
171
long start = event.getTime();
139
172
long end = start + event.getDuration();
140
173
synchronized (fEventList) {
141
fEventList.add(event);
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);
142
180
if (fStartTime == -1 || start < fStartTime) {
143
181
fStartTime = start;
157
195
public void setEventList(List<ITimeEvent> eventList) {
158
196
if (eventList != null) {
159
fEventList = new ArrayList<ITimeEvent>(eventList);
197
fEventList = new ArrayList<>(eventList);
161
fEventList = new ArrayList<ITimeEvent>();
199
fEventList = new ArrayList<>();
171
209
public void setZoomedEventList(List<ITimeEvent> eventList) {
172
210
if (eventList != null) {
173
fZoomedEventList = new ArrayList<ITimeEvent>(eventList);
211
fZoomedEventList = new ArrayList<>(eventList);
175
fZoomedEventList = new ArrayList<ITimeEvent>();
213
fZoomedEventList = new ArrayList<>();
183
221
* The child entry
185
public void addChild(TimeGraphEntry child) {
186
child.fParent = this;
187
fChildren.add(child);
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));