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; | |
17 | import java.util.Iterator; | |
18 | import java.util.List; | |
1cf25311 | 19 | import java.util.concurrent.CopyOnWriteArrayList; |
4999a196 | 20 | |
4999a196 GB |
21 | /** |
22 | * An entry for use in the time graph views | |
23 | * | |
24 | * @since 2.1 | |
25 | */ | |
26 | public class TimeGraphEntry implements ITimeGraphEntry { | |
27 | ||
4999a196 | 28 | /** Entry's parent */ |
a3188982 | 29 | private ITimeGraphEntry fParent = null; |
4999a196 GB |
30 | |
31 | /** List of child entries */ | |
a3188982 | 32 | private final List<ITimeGraphEntry> fChildren = new CopyOnWriteArrayList<>(); |
4999a196 GB |
33 | |
34 | /** Name of this entry (text to show) */ | |
35 | private String fName; | |
36 | private long fStartTime = -1; | |
37 | private long fEndTime = -1; | |
507b1336 AM |
38 | private List<ITimeEvent> fEventList = new ArrayList<>(); |
39 | private List<ITimeEvent> fZoomedEventList = new ArrayList<>(); | |
4999a196 GB |
40 | |
41 | /** | |
42 | * Constructor | |
43 | * | |
4999a196 | 44 | * @param name |
1d46dc38 | 45 | * The name of this entry |
4999a196 | 46 | * @param startTime |
1d46dc38 | 47 | * The start time of this entry |
4999a196 | 48 | * @param endTime |
1d46dc38 | 49 | * The end time of this entry |
4999a196 | 50 | */ |
1d46dc38 | 51 | public TimeGraphEntry(String name, long startTime, long endTime) { |
4999a196 GB |
52 | fName = name; |
53 | fStartTime = startTime; | |
54 | fEndTime = endTime; | |
55 | } | |
56 | ||
57 | // --------------------------------------------- | |
58 | // Getters and setters | |
59 | // --------------------------------------------- | |
60 | ||
61 | @Override | |
62 | public ITimeGraphEntry getParent() { | |
63 | return fParent; | |
64 | } | |
65 | ||
66 | /** | |
67 | * Sets the entry's parent | |
68 | * | |
69 | * @param entry The new parent entry | |
70 | */ | |
a3188982 PT |
71 | /* |
72 | * TODO: This method can be removed in the next major API version. | |
73 | */ | |
4999a196 GB |
74 | protected void setParent(TimeGraphEntry entry) { |
75 | fParent = entry; | |
76 | } | |
77 | ||
a3188982 PT |
78 | /** |
79 | * Sets the entry's parent | |
80 | * | |
81 | * @param entry The new parent entry | |
82 | * @since 3.1 | |
83 | */ | |
84 | /* | |
85 | * TODO: This method should be added to the interface in the next major API version. | |
86 | */ | |
87 | protected void setParent(ITimeGraphEntry entry) { | |
88 | fParent = entry; | |
89 | } | |
90 | ||
4999a196 GB |
91 | @Override |
92 | public boolean hasChildren() { | |
93 | return fChildren.size() > 0; | |
94 | } | |
95 | ||
96 | @Override | |
a3188982 | 97 | public List<? extends ITimeGraphEntry> getChildren() { |
4999a196 GB |
98 | return fChildren; |
99 | } | |
100 | ||
101 | @Override | |
102 | public String getName() { | |
103 | return fName; | |
104 | } | |
105 | ||
106 | /** | |
107 | * Update the entry name | |
108 | * | |
109 | * @param name | |
110 | * the updated entry name | |
111 | */ | |
112 | public void setName(String name) { | |
113 | fName = name; | |
114 | } | |
115 | ||
116 | @Override | |
117 | public long getStartTime() { | |
118 | return fStartTime; | |
119 | } | |
120 | ||
121 | @Override | |
122 | public long getEndTime() { | |
123 | return fEndTime; | |
124 | } | |
125 | ||
1cf25311 PT |
126 | /** |
127 | * Updates the end time | |
128 | * | |
129 | * @param endTime | |
130 | * the end time | |
131 | * | |
132 | * @since 3.0 | |
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 | } | |
4999a196 GB |
177 | if (fStartTime == -1 || start < fStartTime) { |
178 | fStartTime = start; | |
179 | } | |
180 | if (fEndTime == -1 || end > fEndTime) { | |
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 | */ | |
4999a196 GB |
223 | public void addChild(TimeGraphEntry child) { |
224 | child.fParent = this; | |
225 | fChildren.add(child); | |
226 | } | |
227 | ||
a3188982 PT |
228 | /** |
229 | * Add a child entry to this one | |
230 | * | |
231 | * @param child | |
232 | * The child entry | |
233 | * @since 3.1 | |
234 | */ | |
235 | public void addChild(ITimeGraphEntry child) { | |
236 | if (child instanceof TimeGraphEntry) { | |
237 | ((TimeGraphEntry) child).fParent = this; | |
238 | } | |
239 | fChildren.add(child); | |
240 | } | |
241 | ||
242 | /** | |
243 | * Add a child entry to this one at the specified position | |
244 | * | |
245 | * @param index | |
246 | * Index at which the specified entry is to be inserted | |
247 | * @param child | |
248 | * The child entry | |
249 | * @since 3.1 | |
250 | */ | |
251 | public void addChild(int index, ITimeGraphEntry child) { | |
252 | if (child instanceof TimeGraphEntry) { | |
253 | ((TimeGraphEntry) child).fParent = this; | |
254 | } | |
255 | fChildren.add(index, child); | |
256 | } | |
257 | ||
b1b156f3 PT |
258 | @Override |
259 | public String toString() { | |
260 | return getClass().getSimpleName() + '(' + fName + ')'; | |
261 | } | |
262 | ||
4999a196 | 263 | } |