Commit | Line | Data |
---|---|---|
4999a196 | 1 | /******************************************************************************* |
1cf25311 | 2 | * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal |
4999a196 GB |
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.linuxtools.tmf.ui.widgets.timegraph.model; | |
15 | ||
16 | import java.util.ArrayList; | |
9ba941e9 PT |
17 | import java.util.Arrays; |
18 | import java.util.Comparator; | |
4999a196 GB |
19 | import java.util.Iterator; |
20 | import java.util.List; | |
1cf25311 | 21 | import java.util.concurrent.CopyOnWriteArrayList; |
4999a196 | 22 | |
4999a196 GB |
23 | /** |
24 | * An entry for use in the time graph views | |
25 | * | |
26 | * @since 2.1 | |
27 | */ | |
28 | public class TimeGraphEntry implements ITimeGraphEntry { | |
29 | ||
4999a196 | 30 | /** Entry's parent */ |
a3188982 | 31 | private ITimeGraphEntry fParent = null; |
4999a196 GB |
32 | |
33 | /** List of child entries */ | |
a3188982 | 34 | private final List<ITimeGraphEntry> fChildren = new CopyOnWriteArrayList<>(); |
4999a196 GB |
35 | |
36 | /** Name of this entry (text to show) */ | |
37 | private String fName; | |
38 | private long fStartTime = -1; | |
39 | private long fEndTime = -1; | |
507b1336 AM |
40 | private List<ITimeEvent> fEventList = new ArrayList<>(); |
41 | private List<ITimeEvent> fZoomedEventList = new ArrayList<>(); | |
9ba941e9 | 42 | private Comparator<ITimeGraphEntry> fComparator; |
4999a196 GB |
43 | |
44 | /** | |
45 | * Constructor | |
46 | * | |
4999a196 | 47 | * @param name |
1d46dc38 | 48 | * The name of this entry |
4999a196 | 49 | * @param startTime |
1d46dc38 | 50 | * The start time of this entry |
4999a196 | 51 | * @param endTime |
1d46dc38 | 52 | * The end time of this entry |
4999a196 | 53 | */ |
1d46dc38 | 54 | public TimeGraphEntry(String name, long startTime, long endTime) { |
4999a196 GB |
55 | fName = name; |
56 | fStartTime = startTime; | |
57 | fEndTime = endTime; | |
58 | } | |
59 | ||
60 | // --------------------------------------------- | |
61 | // Getters and setters | |
62 | // --------------------------------------------- | |
63 | ||
64 | @Override | |
65 | public ITimeGraphEntry getParent() { | |
66 | return fParent; | |
67 | } | |
68 | ||
69 | /** | |
70 | * Sets the entry's parent | |
71 | * | |
72 | * @param entry The new parent entry | |
73 | */ | |
a3188982 PT |
74 | /* |
75 | * TODO: This method can be removed in the next major API version. | |
76 | */ | |
4999a196 GB |
77 | protected void setParent(TimeGraphEntry entry) { |
78 | fParent = entry; | |
79 | } | |
80 | ||
a3188982 PT |
81 | /** |
82 | * Sets the entry's parent | |
83 | * | |
84 | * @param entry The new parent entry | |
85 | * @since 3.1 | |
86 | */ | |
87 | /* | |
88 | * TODO: This method should be added to the interface in the next major API version. | |
89 | */ | |
90 | protected void setParent(ITimeGraphEntry entry) { | |
91 | fParent = entry; | |
92 | } | |
93 | ||
4999a196 | 94 | @Override |
9ba941e9 | 95 | public synchronized boolean hasChildren() { |
4999a196 GB |
96 | return fChildren.size() > 0; |
97 | } | |
98 | ||
99 | @Override | |
9ba941e9 | 100 | public synchronized List<? extends ITimeGraphEntry> getChildren() { |
4999a196 GB |
101 | return fChildren; |
102 | } | |
103 | ||
104 | @Override | |
105 | public String getName() { | |
106 | return fName; | |
107 | } | |
108 | ||
109 | /** | |
110 | * Update the entry name | |
111 | * | |
112 | * @param name | |
113 | * the updated entry name | |
114 | */ | |
115 | public void setName(String name) { | |
116 | fName = name; | |
117 | } | |
118 | ||
119 | @Override | |
120 | public long getStartTime() { | |
121 | return fStartTime; | |
122 | } | |
123 | ||
124 | @Override | |
125 | public long getEndTime() { | |
126 | return fEndTime; | |
127 | } | |
128 | ||
1cf25311 PT |
129 | /** |
130 | * Updates the end time | |
131 | * | |
132 | * @param endTime | |
133 | * the end time | |
134 | * | |
135 | * @since 3.0 | |
136 | */ | |
137 | public void updateEndTime(long endTime) { | |
138 | fEndTime = Math.max(endTime, fEndTime); | |
139 | } | |
140 | ||
4999a196 GB |
141 | @Override |
142 | public boolean hasTimeEvents() { | |
143 | return true; | |
144 | } | |
145 | ||
146 | @Override | |
147 | public Iterator<ITimeEvent> getTimeEventsIterator() { | |
148 | if (hasTimeEvents()) { | |
149 | return new EventIterator(fEventList, fZoomedEventList); | |
150 | } | |
151 | return null; | |
152 | } | |
153 | ||
154 | @Override | |
155 | public Iterator<ITimeEvent> getTimeEventsIterator(long startTime, long stopTime, long visibleDuration) { | |
156 | if (!hasTimeEvents()) { | |
157 | return null; | |
158 | } | |
159 | return new EventIterator(fEventList, fZoomedEventList, startTime, stopTime); | |
160 | } | |
161 | ||
162 | /** | |
1d46dc38 | 163 | * Add an event to this entry's event list. If necessary, update the start |
1cf25311 PT |
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. | |
4999a196 GB |
166 | * |
167 | * @param event | |
1cf25311 | 168 | * The time event to add |
4999a196 GB |
169 | */ |
170 | public void addEvent(ITimeEvent event) { | |
171 | long start = event.getTime(); | |
172 | long end = start + event.getDuration(); | |
173 | synchronized (fEventList) { | |
1cf25311 PT |
174 | int lastIndex = fEventList.size() - 1; |
175 | if (lastIndex >= 0 && fEventList.get(lastIndex).getTime() == event.getTime()) { | |
176 | fEventList.set(lastIndex, event); | |
177 | } else { | |
178 | fEventList.add(event); | |
179 | } | |
4999a196 GB |
180 | if (fStartTime == -1 || start < fStartTime) { |
181 | fStartTime = start; | |
182 | } | |
183 | if (fEndTime == -1 || end > fEndTime) { | |
184 | fEndTime = end; | |
185 | } | |
186 | } | |
187 | } | |
188 | ||
189 | /** | |
190 | * Set the general event list of this entry. | |
191 | * | |
4999a196 GB |
192 | * @param eventList |
193 | * The list of time events | |
194 | */ | |
195 | public void setEventList(List<ITimeEvent> eventList) { | |
3ce8c834 | 196 | if (eventList != null) { |
507b1336 | 197 | fEventList = new ArrayList<>(eventList); |
3ce8c834 | 198 | } else { |
507b1336 | 199 | fEventList = new ArrayList<>(); |
3ce8c834 | 200 | } |
4999a196 GB |
201 | } |
202 | ||
203 | /** | |
204 | * Set the zoomed event list of this entry. | |
205 | * | |
4999a196 GB |
206 | * @param eventList |
207 | * The list of time events | |
208 | */ | |
209 | public void setZoomedEventList(List<ITimeEvent> eventList) { | |
3ce8c834 | 210 | if (eventList != null) { |
507b1336 | 211 | fZoomedEventList = new ArrayList<>(eventList); |
3ce8c834 | 212 | } else { |
507b1336 | 213 | fZoomedEventList = new ArrayList<>(); |
3ce8c834 | 214 | } |
4999a196 GB |
215 | } |
216 | ||
217 | /** | |
1d46dc38 | 218 | * Add a child entry to this one |
4999a196 GB |
219 | * |
220 | * @param child | |
221 | * The child entry | |
222 | */ | |
a3188982 PT |
223 | /* |
224 | * TODO: This method can be removed in the next major API version. | |
225 | */ | |
9ba941e9 PT |
226 | public synchronized void addChild(TimeGraphEntry child) { |
227 | addChild((ITimeGraphEntry) child); | |
4999a196 GB |
228 | } |
229 | ||
a3188982 | 230 | /** |
9ba941e9 PT |
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. | |
a3188982 PT |
234 | * |
235 | * @param child | |
236 | * The child entry | |
237 | * @since 3.1 | |
238 | */ | |
9ba941e9 PT |
239 | public synchronized void addChild(ITimeGraphEntry child) { |
240 | /* | |
241 | * TODO: Use setParent() once it is added to the interface. | |
242 | */ | |
a3188982 PT |
243 | if (child instanceof TimeGraphEntry) { |
244 | ((TimeGraphEntry) child).fParent = this; | |
245 | } | |
9ba941e9 PT |
246 | if (fComparator == null) { |
247 | fChildren.add(child); | |
248 | } else { | |
249 | int i; | |
250 | for (i = 0; i < fChildren.size(); i++) { | |
251 | ITimeGraphEntry entry = fChildren.get(i); | |
252 | if (fComparator.compare(child, entry) < 0) { | |
253 | break; | |
254 | } | |
255 | } | |
256 | fChildren.add(i, child); | |
257 | } | |
a3188982 PT |
258 | } |
259 | ||
260 | /** | |
261 | * Add a child entry to this one at the specified position | |
262 | * | |
263 | * @param index | |
264 | * Index at which the specified entry is to be inserted | |
265 | * @param child | |
266 | * The child entry | |
267 | * @since 3.1 | |
268 | */ | |
9ba941e9 PT |
269 | public synchronized void addChild(int index, ITimeGraphEntry child) { |
270 | /* | |
271 | * TODO: Use setParent() once it is added to the interface. | |
272 | */ | |
a3188982 PT |
273 | if (child instanceof TimeGraphEntry) { |
274 | ((TimeGraphEntry) child).fParent = this; | |
275 | } | |
276 | fChildren.add(index, child); | |
277 | } | |
278 | ||
9ba941e9 PT |
279 | /** |
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. | |
283 | * | |
284 | * @param comparator | |
285 | * The entry comparator | |
286 | * @since 3.1 | |
287 | */ | |
288 | public synchronized void sortChildren(Comparator<ITimeGraphEntry> comparator) { | |
289 | fComparator = comparator; | |
290 | if (comparator == null) { | |
291 | return; | |
292 | } | |
293 | ITimeGraphEntry[] array = fChildren.toArray(new ITimeGraphEntry[0]); | |
294 | Arrays.sort(array, comparator); | |
295 | fChildren.clear(); | |
296 | fChildren.addAll(Arrays.asList(array)); | |
297 | } | |
298 | ||
b1b156f3 PT |
299 | @Override |
300 | public String toString() { | |
301 | return getClass().getSimpleName() + '(' + fName + ')'; | |
302 | } | |
303 | ||
4999a196 | 304 | } |