Commit | Line | Data |
---|---|---|
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 | 13 | package org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views; |
35423ee0 GB |
14 | |
15 | import java.util.HashSet; | |
16 | import java.util.List; | |
17 | import java.util.Set; | |
18 | ||
19 | import org.eclipse.jdt.annotation.NonNullByDefault; | |
20 | import org.eclipse.jdt.annotation.Nullable; | |
21 | import org.eclipse.jface.dialogs.IDialogSettings; | |
2bdf0193 AM |
22 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.Activator; |
23 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; | |
24 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; | |
25 | import org.eclipse.tracecompass.tmf.analysis.xml.ui.module.TmfXmlAnalysisOutputSource; | |
35423ee0 GB |
26 | import 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 | |
34 | public class XmlViewInfo { | |
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 | ||
39 | private final String fViewId; | |
40 | private @Nullable String fId = null; | |
41 | private @Nullable String fFilePath = null; | |
42 | ||
43 | /** | |
44 | * Constructor | |
45 | * | |
46 | * @param viewId | |
47 | * The ID of the view | |
48 | */ | |
49 | public XmlViewInfo(String viewId) { | |
50 | fViewId = viewId; | |
51 | ||
52 | IDialogSettings settings = getPersistentPropertyStore(); | |
53 | ||
54 | fId = settings.get(XML_VIEW_ID_PROPERTY); | |
55 | fFilePath = settings.get(XML_VIEW_FILE_PROPERTY); | |
56 | } | |
57 | ||
58 | /** | |
59 | * Set the data for this view and retrieves from it the view ID and the file | |
60 | * path of the XML element this view uses. | |
61 | * | |
62 | * @param data | |
63 | * A string of the form "XML view ID" + | |
64 | * {@link TmfXmlAnalysisOutputSource#DATA_SEPARATOR} + | |
65 | * "path of the file containing the XML element" | |
66 | */ | |
67 | public void setViewData(String data) { | |
68 | String[] idFile = data.split(TmfXmlAnalysisOutputSource.DATA_SEPARATOR); | |
69 | fId = (idFile.length > 0) ? idFile[0] : null; | |
70 | fFilePath = (idFile.length > 1) ? idFile[1] : null; | |
71 | savePersistentData(); | |
72 | } | |
73 | ||
74 | private IDialogSettings getPersistentPropertyStore() { | |
75 | IDialogSettings settings = Activator.getDefault().getDialogSettings(); | |
76 | IDialogSettings section = settings.getSection(fViewId); | |
77 | if (section == null) { | |
78 | section = settings.addNewSection(fViewId); | |
79 | if (section == null) { | |
80 | throw new IllegalStateException(); | |
81 | } | |
82 | } | |
83 | return section; | |
84 | } | |
85 | ||
86 | private void savePersistentData() { | |
87 | IDialogSettings settings = getPersistentPropertyStore(); | |
88 | ||
89 | settings.put(XML_VIEW_ID_PROPERTY, fId); | |
90 | settings.put(XML_VIEW_FILE_PROPERTY, fFilePath); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Retrieve the XML element corresponding to the view | |
95 | * | |
96 | * @param xmlTag | |
97 | * The XML tag corresponding to the view element (the type of | |
98 | * view) | |
99 | * @return The view {@link Element} | |
100 | */ | |
101 | public @Nullable Element getViewElement(String xmlTag) { | |
102 | String id = fId; | |
103 | if (id == null) { | |
104 | return null; | |
105 | } | |
106 | Element viewElement = XmlUtils.getElementInFile(fFilePath, xmlTag, id); | |
107 | return viewElement; | |
108 | } | |
109 | ||
110 | /** | |
111 | * Get the view title from the header information of the XML view element. | |
112 | * | |
113 | * @param viewElement | |
114 | * The XML view element from which to get the title | |
115 | * @return The view title | |
116 | */ | |
117 | public @Nullable String getViewTitle(Element viewElement) { | |
118 | List<Element> heads = XmlUtils.getChildElements(viewElement, TmfXmlStrings.HEAD); | |
119 | ||
120 | String title = null; | |
121 | if (!heads.isEmpty()) { | |
122 | Element head = heads.get(0); | |
123 | /* Set the title of this view from the label in the header */ | |
124 | List<Element> labels = XmlUtils.getChildElements(head, TmfXmlStrings.LABEL); | |
125 | for (Element label : labels) { | |
126 | if (!label.getAttribute(TmfXmlStrings.VALUE).isEmpty()) { | |
127 | title = label.getAttribute(TmfXmlStrings.VALUE); | |
128 | } | |
129 | break; | |
130 | } | |
131 | } | |
132 | return title; | |
133 | } | |
134 | ||
135 | /** | |
136 | * Get the list of analysis IDs this view is for, as listed in the header of | |
137 | * the XML element | |
138 | * | |
139 | * @param viewElement | |
140 | * The XML view element from which to get the analysis IDs | |
141 | * @return The list of all analysis IDs this view is for | |
142 | */ | |
143 | public Set<String> getViewAnalysisIds(Element viewElement) { | |
144 | List<Element> heads = XmlUtils.getChildElements(viewElement, TmfXmlStrings.HEAD); | |
145 | ||
146 | Set<String> analysisIds = new HashSet<>(); | |
147 | if (!heads.isEmpty()) { | |
148 | Element head = heads.get(0); | |
149 | ||
150 | /* Get the application analysis from the view's XML header */ | |
151 | List<Element> applicableAnalysis = XmlUtils.getChildElements(head, TmfXmlStrings.ANALYSIS); | |
152 | for (Element oneAnalysis : applicableAnalysis) { | |
153 | analysisIds.add(oneAnalysis.getAttribute(TmfXmlStrings.ID)); | |
154 | } | |
155 | } | |
156 | return analysisIds; | |
157 | } | |
158 | ||
159 | } |