Fix some null warnings
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.ui / src / org / eclipse / tracecompass / tmf / analysis / xml / ui / views / timegraph / XmlEntry.java
CommitLineData
1a23419e
FW
1/*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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 * Florian Wininger - Initial API and implementation
11 * Geneviève Bastien - Review of the initial implementation
12 *******************************************************************************/
13
2bdf0193 14package org.eclipse.tracecompass.tmf.analysis.xml.ui.views.timegraph;
1a23419e
FW
15
16import java.util.Collections;
df2597e0 17import java.util.Iterator;
1a23419e
FW
18import java.util.List;
19
20import org.eclipse.jdt.annotation.NonNull;
2bdf0193 21import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.TmfXmlUiStrings;
e894a508 22import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
1dd75589 23import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
e894a508 24import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
2bdf0193
AM
25import org.eclipse.tracecompass.tmf.analysis.xml.core.model.ITmfXmlModelFactory;
26import org.eclipse.tracecompass.tmf.analysis.xml.core.model.ITmfXmlStateAttribute;
27import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlLocation;
28import org.eclipse.tracecompass.tmf.analysis.xml.core.model.readonly.TmfXmlReadOnlyModelFactory;
29import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
30import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
31import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
df2597e0 32import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
2bdf0193 33import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
1a23419e
FW
34import org.w3c.dom.Element;
35
36/**
37 * An XML-defined entry, or row, to display in the XML state system view
38 *
39 * @author Florian Wininger
40 */
41public class XmlEntry extends TimeGraphEntry implements IXmlStateSystemContainer {
42
43 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
44
45 /** Type of resource */
46 public static enum EntryDisplayType {
47 /** Entries without events to display (filler rows, etc.) */
48 NULL,
49 /** Entries with time events */
50 DISPLAY
51 }
52
53 private final ITmfTrace fTrace;
54 private final EntryDisplayType fType;
55 private final int fBaseQuark;
56 private final int fDisplayQuark;
57 private final String fParentId;
58 private final String fId;
59 private final @NonNull ITmfStateSystem fSs;
60 private final Element fElement;
61
62 /**
63 * Constructor
64 *
65 * @param baseQuark
66 * The quark matching this entry, or <code>-1</code> if no quark
67 * @param displayQuark
68 * The quark containing the value to display. It was needed by
69 * the caller to get the start and end time of this entry, so we
70 * receive it as parameter from him.
71 * @param trace
72 * The trace on which we are working (FIXME: is this parameter
73 * useful?)
74 * @param name
75 * The name of this entry. It will be overridden if a "name" XML
76 * tag is specified in the entryElement. It will also be used as
77 * the ID of this entry if no "id" XML tag is specified. It
78 * typically is the attribute name corresponding the the base
79 * quark.
80 * @param startTime
81 * The start time of this entry lifetime
82 * @param endTime
83 * The end time of this entry
84 * @param type
85 * The display type of this entry
86 * @param ss
87 * The state system this entry belongs to
88 * @param entryElement
89 * The XML element describing this entry. This element will be
90 * used to determine, if available, the parent, ID, name and
91 * other display option of this entry
92 */
93 public XmlEntry(int baseQuark, int displayQuark, ITmfTrace trace, String name, long startTime, long endTime, EntryDisplayType type, @NonNull ITmfStateSystem ss, Element entryElement) {
94 super(name, startTime, endTime);
95 fTrace = trace;
96 fType = type;
97 fBaseQuark = baseQuark;
98 fDisplayQuark = displayQuark;
99 fSs = ss;
100 fElement = entryElement;
101
102 /* Get the parent if specified */
103 List<Element> elements = XmlUtils.getChildElements(fElement, TmfXmlUiStrings.PARENT_ELEMENT);
104 if (elements.size() > 0) {
105 fParentId = getFirstValue(elements.get(0));
106 } else {
107 fParentId = EMPTY_STRING;
108 }
109
110 /* Get the name of this entry */
111 elements = XmlUtils.getChildElements(fElement, TmfXmlUiStrings.NAME_ELEMENT);
112 if (elements.size() > 0) {
113 String nameFromSs = getFirstValue(elements.get(0));
114 if (!nameFromSs.isEmpty()) {
115 setName(nameFromSs);
116 }
117 }
118
119 /* Get the id of this entry */
120 elements = XmlUtils.getChildElements(fElement, TmfXmlUiStrings.ID_ELEMENT);
121 if (elements.size() > 0) {
122 fId = getFirstValue(elements.get(0));
123 } else {
124 fId = name;
125 }
126
127 }
128
129 /**
130 * Constructor
131 *
132 * @param baseQuark
133 * The quark matching this entry, or <code>-1</code> if no quark
134 * @param trace
135 * The trace on which we are working
136 * @param name
137 * The exec_name of this entry
138 * @param ss
139 * The state system this entry belongs to
140 */
141 public XmlEntry(int baseQuark, ITmfTrace trace, String name, @NonNull ITmfStateSystem ss) {
142 super(name, ss.getStartTime(), ss.getCurrentEndTime());
143 fTrace = trace;
144 fType = EntryDisplayType.NULL;
145 fBaseQuark = baseQuark;
146 fDisplayQuark = baseQuark;
147 fSs = ss;
148 fElement = null;
149 fParentId = EMPTY_STRING;
150 fId = name;
151 }
152
153 /** Return the state value of the first interval with a non-null value */
154 private String getFirstValue(Element stateAttribute) {
12685851
GB
155 if (stateAttribute == null) {
156 throw new IllegalArgumentException();
157 }
0a35a36f 158
1a23419e
FW
159 ITmfXmlModelFactory factory = TmfXmlReadOnlyModelFactory.getInstance();
160 ITmfXmlStateAttribute display = factory.createStateAttribute(stateAttribute, this);
161 int quark = display.getAttributeQuark(fBaseQuark);
162 if (quark != IXmlStateSystemContainer.ERROR_QUARK) {
0a35a36f
GB
163 ITmfStateInterval firstInterval = StateSystemUtils.queryUntilNonNullValue(fSs, quark, getStartTime(), getEndTime());
164 if (firstInterval != null) {
165 return firstInterval.getStateValue().toString();
1a23419e
FW
166 }
167 }
168 return EMPTY_STRING;
169 }
170
171 /**
172 * Get the trace this entry was taken from
173 *
174 * @return the entry's trace
175 */
176 public ITmfTrace getTrace() {
177 return fTrace;
178 }
179
180 /**
181 * Get the entry Type of this entry. Uses the inner EntryDisplayType enum.
182 *
183 * @return The entry type
184 */
185 public EntryDisplayType getType() {
186 return fType;
187 }
188
189 /**
190 * Get the quark from which to get the time event intervals for this entry.
191 *
192 * @return The attribute quark containing the intervals to display
193 */
194 public int getDisplayQuark() {
195 return fDisplayQuark;
196 }
197
198 /**
199 * Get this entry's ID
200 *
201 * @return The id of the entry.
202 */
203 public String getId() {
204 return fId;
205 }
206
207 /**
208 * Return the entry's parent ID. It corresponds to another entry's ID
209 * received from the {@link #getId()} method.
210 *
211 * @return The parent ID of this entry
212 */
213 public String getParentId() {
214 return fParentId;
215 }
216
217 @Override
218 public boolean hasTimeEvents() {
219 if (fType == EntryDisplayType.NULL) {
220 return false;
221 }
222 return true;
223 }
224
225 /**
226 * Add a child to this entry of type XmlEntry
227 *
228 * @param entry
229 * The entry to add
230 */
231 public void addChild(XmlEntry entry) {
232 int index;
233 for (index = 0; index < getChildren().size(); index++) {
234 XmlEntry other = (XmlEntry) getChildren().get(index);
235 if (entry.getType().compareTo(other.getType()) < 0) {
236 break;
237 } else if (entry.getType().equals(other.getType())) {
238 if (entry.getName().compareTo(other.getName()) < 0) {
239 break;
240 }
241 }
242 }
243
244 entry.setParent(this);
a3188982 245 addChild(index, entry);
1a23419e
FW
246 }
247
248 /**
249 * Return the state system this entry is associated to
250 *
251 * @return The state system, or <code>null</code> if the state system can't
252 * be found.
253 */
254 @Override
255 @NonNull
256 public ITmfStateSystem getStateSystem() {
257 return fSs;
258 }
259
260 @Override
261 public String getAttributeValue(String name) {
262 return name;
263 }
264
265 @Override
266 public Iterable<TmfXmlLocation> getLocations() {
267 return Collections.EMPTY_SET;
268 }
269
df2597e0
AM
270 @Override
271 public Iterator<@NonNull ITimeEvent> getTimeEventsIterator() {
272 return super.getTimeEventsIterator();
273 }
274
1a23419e 275}
This page took 0.081242 seconds and 5 git commands to generate.