Commit | Line | Data |
---|---|---|
4999a196 | 1 | /******************************************************************************* |
50d36521 | 2 | * Copyright (c) 2012, 2015 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 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model; |
4999a196 GB |
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 | |
50d36521 PT |
23 | import org.eclipse.swt.SWT; |
24 | ||
4999a196 GB |
25 | /** |
26 | * An entry for use in the time graph views | |
4999a196 GB |
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; | |
50d36521 PT |
38 | private long fStartTime = SWT.DEFAULT; |
39 | private long fEndTime = SWT.DEFAULT; | |
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 | |
a3188982 PT |
85 | */ |
86 | /* | |
87 | * TODO: This method should be added to the interface in the next major API version. | |
88 | */ | |
89 | protected void setParent(ITimeGraphEntry entry) { | |
90 | fParent = entry; | |
91 | } | |
92 | ||
4999a196 | 93 | @Override |
9ba941e9 | 94 | public synchronized boolean hasChildren() { |
4999a196 GB |
95 | return fChildren.size() > 0; |
96 | } | |
97 | ||
98 | @Override | |
9ba941e9 | 99 | public synchronized List<? extends ITimeGraphEntry> getChildren() { |
4999a196 GB |
100 | return fChildren; |
101 | } | |
102 | ||
103 | @Override | |
104 | public String getName() { | |
105 | return fName; | |
106 | } | |
107 | ||
108 | /** | |
109 | * Update the entry name | |
110 | * | |
111 | * @param name | |
112 | * the updated entry name | |
113 | */ | |
114 | public void setName(String name) { | |
115 | fName = name; | |
116 | } | |
117 | ||
118 | @Override | |
119 | public long getStartTime() { | |
120 | return fStartTime; | |
121 | } | |
122 | ||
123 | @Override | |
124 | public long getEndTime() { | |
125 | return fEndTime; | |
126 | } | |
127 | ||
1cf25311 PT |
128 | /** |
129 | * Updates the end time | |
130 | * | |
131 | * @param endTime | |
132 | * the end time | |
1cf25311 PT |
133 | */ |
134 | public void updateEndTime(long endTime) { | |
135 | fEndTime = Math.max(endTime, fEndTime); | |
136 | } | |
137 | ||
4999a196 GB |
138 | @Override |
139 | public boolean hasTimeEvents() { | |
140 | return true; | |
141 | } | |
142 | ||
143 | @Override | |
144 | public Iterator<ITimeEvent> getTimeEventsIterator() { | |
145 | if (hasTimeEvents()) { | |
146 | return new EventIterator(fEventList, fZoomedEventList); | |
147 | } | |
148 | return null; | |
149 | } | |
150 | ||
151 | @Override | |
152 | public Iterator<ITimeEvent> getTimeEventsIterator(long startTime, long stopTime, long visibleDuration) { | |
153 | if (!hasTimeEvents()) { | |
154 | return null; | |
155 | } | |
156 | return new EventIterator(fEventList, fZoomedEventList, startTime, stopTime); | |
157 | } | |
158 | ||
159 | /** | |
1d46dc38 | 160 | * Add an event to this entry's event list. If necessary, update the start |
1cf25311 PT |
161 | * and end time of the entry. If the event list's last event starts at the |
162 | * same time as the event to add, it is replaced by the new event. | |
4999a196 GB |
163 | * |
164 | * @param event | |
1cf25311 | 165 | * The time event to add |
4999a196 GB |
166 | */ |
167 | public void addEvent(ITimeEvent event) { | |
168 | long start = event.getTime(); | |
169 | long end = start + event.getDuration(); | |
170 | synchronized (fEventList) { | |
1cf25311 PT |
171 | int lastIndex = fEventList.size() - 1; |
172 | if (lastIndex >= 0 && fEventList.get(lastIndex).getTime() == event.getTime()) { | |
173 | fEventList.set(lastIndex, event); | |
174 | } else { | |
175 | fEventList.add(event); | |
176 | } | |
50d36521 | 177 | if (fStartTime == SWT.DEFAULT || start < fStartTime) { |
4999a196 GB |
178 | fStartTime = start; |
179 | } | |
50d36521 | 180 | if (fEndTime == SWT.DEFAULT || end > fEndTime) { |
4999a196 GB |
181 | fEndTime = end; |
182 | } | |
183 | } | |
184 | } | |
185 | ||
186 | /** | |
187 | * Set the general event list of this entry. | |
188 | * | |
4999a196 GB |
189 | * @param eventList |
190 | * The list of time events | |
191 | */ | |
192 | public void setEventList(List<ITimeEvent> eventList) { | |
3ce8c834 | 193 | if (eventList != null) { |
507b1336 | 194 | fEventList = new ArrayList<>(eventList); |
3ce8c834 | 195 | } else { |
507b1336 | 196 | fEventList = new ArrayList<>(); |
3ce8c834 | 197 | } |
4999a196 GB |
198 | } |
199 | ||
200 | /** | |
201 | * Set the zoomed event list of this entry. | |
202 | * | |
4999a196 GB |
203 | * @param eventList |
204 | * The list of time events | |
205 | */ | |
206 | public void setZoomedEventList(List<ITimeEvent> eventList) { | |
3ce8c834 | 207 | if (eventList != null) { |
507b1336 | 208 | fZoomedEventList = new ArrayList<>(eventList); |
3ce8c834 | 209 | } else { |
507b1336 | 210 | fZoomedEventList = new ArrayList<>(); |
3ce8c834 | 211 | } |
4999a196 GB |
212 | } |
213 | ||
214 | /** | |
1d46dc38 | 215 | * Add a child entry to this one |
4999a196 GB |
216 | * |
217 | * @param child | |
218 | * The child entry | |
219 | */ | |
a3188982 PT |
220 | /* |
221 | * TODO: This method can be removed in the next major API version. | |
222 | */ | |
9ba941e9 PT |
223 | public synchronized void addChild(TimeGraphEntry child) { |
224 | addChild((ITimeGraphEntry) child); | |
4999a196 GB |
225 | } |
226 | ||
a3188982 | 227 | /** |
9ba941e9 PT |
228 | * Add a child entry to this one. If a comparator was previously set with |
229 | * {@link #sortChildren(Comparator)}, the entry will be inserted in its | |
230 | * sort-order position. Otherwise it will be added to the end of the list. | |
a3188982 PT |
231 | * |
232 | * @param child | |
233 | * The child entry | |
a3188982 | 234 | */ |
9ba941e9 PT |
235 | public synchronized void addChild(ITimeGraphEntry child) { |
236 | /* | |
237 | * TODO: Use setParent() once it is added to the interface. | |
238 | */ | |
a3188982 PT |
239 | if (child instanceof TimeGraphEntry) { |
240 | ((TimeGraphEntry) child).fParent = this; | |
241 | } | |
9ba941e9 PT |
242 | if (fComparator == null) { |
243 | fChildren.add(child); | |
244 | } else { | |
245 | int i; | |
246 | for (i = 0; i < fChildren.size(); i++) { | |
247 | ITimeGraphEntry entry = fChildren.get(i); | |
248 | if (fComparator.compare(child, entry) < 0) { | |
249 | break; | |
250 | } | |
251 | } | |
252 | fChildren.add(i, child); | |
253 | } | |
a3188982 PT |
254 | } |
255 | ||
256 | /** | |
257 | * Add a child entry to this one at the specified position | |
258 | * | |
259 | * @param index | |
260 | * Index at which the specified entry is to be inserted | |
261 | * @param child | |
262 | * The child entry | |
a3188982 | 263 | */ |
9ba941e9 PT |
264 | public synchronized void addChild(int index, ITimeGraphEntry child) { |
265 | /* | |
266 | * TODO: Use setParent() once it is added to the interface. | |
267 | */ | |
a3188982 PT |
268 | if (child instanceof TimeGraphEntry) { |
269 | ((TimeGraphEntry) child).fParent = this; | |
270 | } | |
271 | fChildren.add(index, child); | |
272 | } | |
273 | ||
9ba941e9 PT |
274 | /** |
275 | * Sort the children of this entry using the provided comparator. Subsequent | |
276 | * calls to {@link #addChild(ITimeGraphEntry)} will use this comparator to | |
277 | * maintain the sort order. | |
278 | * | |
279 | * @param comparator | |
280 | * The entry comparator | |
9ba941e9 PT |
281 | */ |
282 | public synchronized void sortChildren(Comparator<ITimeGraphEntry> comparator) { | |
283 | fComparator = comparator; | |
284 | if (comparator == null) { | |
285 | return; | |
286 | } | |
287 | ITimeGraphEntry[] array = fChildren.toArray(new ITimeGraphEntry[0]); | |
288 | Arrays.sort(array, comparator); | |
289 | fChildren.clear(); | |
290 | fChildren.addAll(Arrays.asList(array)); | |
291 | } | |
292 | ||
b1b156f3 PT |
293 | @Override |
294 | public String toString() { | |
295 | return getClass().getSimpleName() + '(' + fName + ')'; | |
296 | } | |
297 | ||
4999a196 | 298 | } |