tmf: Bug 460036: Fix multiple XML views when reopening TraceCompass
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.ui / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / ui / views / XmlViewInfo.java
CommitLineData
35423ee0
GB
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 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
2bdf0193 13package org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views;
35423ee0
GB
14
15import java.util.HashSet;
16import java.util.List;
17import java.util.Set;
e23402bb 18import java.util.concurrent.CountDownLatch;
35423ee0
GB
19
20import org.eclipse.jdt.annotation.NonNullByDefault;
21import org.eclipse.jdt.annotation.Nullable;
22import org.eclipse.jface.dialogs.IDialogSettings;
6eca054d
GB
23import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlUtils;
24import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
25import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module.TmfXmlAnalysisOutputSource;
35423ee0
GB
26import org.w3c.dom.Element;
27
28/**
29 * Class that manages information about a view: its title, the file, etc.
30 *
31 * @author Geneviève Bastien
32 */
33@NonNullByDefault
38fad53e 34public class XmlViewInfo extends AbstractXmlViewInfo {
35423ee0
GB
35
36 private static final String XML_VIEW_ID_PROPERTY = "XmlViewId"; //$NON-NLS-1$
37 private static final String XML_VIEW_FILE_PROPERTY = "XmlViewFile"; //$NON-NLS-1$
38
e23402bb
GB
39 /* Initialization is completed when name is set */
40 private final CountDownLatch fInitialized = new CountDownLatch(1);
38fad53e 41 /** This is the ID of the view described in the XML file */
35423ee0
GB
42 private @Nullable String fId = null;
43 private @Nullable String fFilePath = null;
e23402bb
GB
44 // If true, properties were set but not saved to persistent storage
45 private boolean fIsDirty = false;
35423ee0
GB
46
47 /**
48 * Constructor
49 *
50 * @param viewId
51 * The ID of the view
52 */
53 public XmlViewInfo(String viewId) {
38fad53e 54 super(viewId);
e23402bb
GB
55 /* Cannot get the properties yet, need to wait for the name */
56 }
35423ee0 57
e23402bb
GB
58 /**
59 * Waits for the view info's initialization to be completed. This happens
60 * once the name has been set
61 *
62 * @return Whether the initialization has completed successfully
63 */
64 public boolean waitForInitialization() {
65 try {
66 fInitialized.await();
67 } catch (InterruptedException e) {
68 return false;
69 }
70 return true;
35423ee0
GB
71 }
72
73 /**
74 * Set the data for this view and retrieves from it the view ID and the file
75 * path of the XML element this view uses.
76 *
77 * @param data
78 * A string of the form "XML view ID" +
79 * {@link TmfXmlAnalysisOutputSource#DATA_SEPARATOR} +
80 * "path of the file containing the XML element"
81 */
38fad53e 82 @Override
e23402bb 83 public synchronized void setViewData(String data) {
35423ee0
GB
84 String[] idFile = data.split(TmfXmlAnalysisOutputSource.DATA_SEPARATOR);
85 fId = (idFile.length > 0) ? idFile[0] : null;
86 fFilePath = (idFile.length > 1) ? idFile[1] : null;
e23402bb
GB
87 String viewName = getName();
88 if (viewName != null) {
89 savePersistentData();
90 } else {
91 fIsDirty = true;
92 }
93 }
94
95 @Override
96 public synchronized void setName(String name) {
97 super.setName(name);
98 if (fIsDirty) {
99 savePersistentData();
100 } else {
101 IDialogSettings settings = getPersistentPropertyStore();
102 fId = settings.get(XML_VIEW_ID_PROPERTY);
103 fFilePath = settings.get(XML_VIEW_FILE_PROPERTY);
104 }
105 fInitialized.countDown();
35423ee0
GB
106 }
107
38fad53e
JCK
108 @Override
109 protected void savePersistentData() {
35423ee0
GB
110 IDialogSettings settings = getPersistentPropertyStore();
111
112 settings.put(XML_VIEW_ID_PROPERTY, fId);
113 settings.put(XML_VIEW_FILE_PROPERTY, fFilePath);
114 }
115
116 /**
117 * Retrieve the XML element corresponding to the view
118 *
119 * @param xmlTag
120 * The XML tag corresponding to the view element (the type of
121 * view)
122 * @return The view {@link Element}
123 */
124 public @Nullable Element getViewElement(String xmlTag) {
125 String id = fId;
126 if (id == null) {
127 return null;
128 }
129 Element viewElement = XmlUtils.getElementInFile(fFilePath, xmlTag, id);
130 return viewElement;
131 }
132
133 /**
134 * Get the view title from the header information of the XML view element.
135 *
136 * @param viewElement
137 * The XML view element from which to get the title
138 * @return The view title
139 */
140 public @Nullable String getViewTitle(Element viewElement) {
141 List<Element> heads = XmlUtils.getChildElements(viewElement, TmfXmlStrings.HEAD);
142
143 String title = null;
144 if (!heads.isEmpty()) {
145 Element head = heads.get(0);
146 /* Set the title of this view from the label in the header */
147 List<Element> labels = XmlUtils.getChildElements(head, TmfXmlStrings.LABEL);
148 for (Element label : labels) {
149 if (!label.getAttribute(TmfXmlStrings.VALUE).isEmpty()) {
150 title = label.getAttribute(TmfXmlStrings.VALUE);
151 }
152 break;
153 }
154 }
155 return title;
156 }
157
158 /**
159 * Get the list of analysis IDs this view is for, as listed in the header of
160 * the XML element
161 *
162 * @param viewElement
163 * The XML view element from which to get the analysis IDs
164 * @return The list of all analysis IDs this view is for
165 */
166 public Set<String> getViewAnalysisIds(Element viewElement) {
167 List<Element> heads = XmlUtils.getChildElements(viewElement, TmfXmlStrings.HEAD);
168
169 Set<String> analysisIds = new HashSet<>();
170 if (!heads.isEmpty()) {
171 Element head = heads.get(0);
172
173 /* Get the application analysis from the view's XML header */
174 List<Element> applicableAnalysis = XmlUtils.getChildElements(head, TmfXmlStrings.ANALYSIS);
175 for (Element oneAnalysis : applicableAnalysis) {
176 analysisIds.add(oneAnalysis.getAttribute(TmfXmlStrings.ID));
177 }
178 }
179 return analysisIds;
180 }
35423ee0 181}
This page took 0.071878 seconds and 5 git commands to generate.