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.tmf.analysis.xml.core.module.XmlUtils; |
23 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; | |
24 | import org.eclipse.tracecompass.tmf.analysis.xml.ui.module.TmfXmlAnalysisOutputSource; | |
35423ee0 GB |
25 | import org.w3c.dom.Element; |
26 | ||
27 | /** | |
28 | * Class that manages information about a view: its title, the file, etc. | |
29 | * | |
30 | * @author Geneviève Bastien | |
31 | */ | |
32 | @NonNullByDefault | |
38fad53e | 33 | public class XmlViewInfo extends AbstractXmlViewInfo { |
35423ee0 GB |
34 | |
35 | private static final String XML_VIEW_ID_PROPERTY = "XmlViewId"; //$NON-NLS-1$ | |
36 | private static final String XML_VIEW_FILE_PROPERTY = "XmlViewFile"; //$NON-NLS-1$ | |
37 | ||
38fad53e | 38 | /** This is the ID of the view described in the XML file */ |
35423ee0 GB |
39 | private @Nullable String fId = null; |
40 | private @Nullable String fFilePath = null; | |
41 | ||
42 | /** | |
43 | * Constructor | |
44 | * | |
45 | * @param viewId | |
46 | * The ID of the view | |
47 | */ | |
48 | public XmlViewInfo(String viewId) { | |
38fad53e | 49 | super(viewId); |
35423ee0 GB |
50 | |
51 | IDialogSettings settings = getPersistentPropertyStore(); | |
35423ee0 GB |
52 | fId = settings.get(XML_VIEW_ID_PROPERTY); |
53 | fFilePath = settings.get(XML_VIEW_FILE_PROPERTY); | |
54 | } | |
55 | ||
56 | /** | |
57 | * Set the data for this view and retrieves from it the view ID and the file | |
58 | * path of the XML element this view uses. | |
59 | * | |
60 | * @param data | |
61 | * A string of the form "XML view ID" + | |
62 | * {@link TmfXmlAnalysisOutputSource#DATA_SEPARATOR} + | |
63 | * "path of the file containing the XML element" | |
64 | */ | |
38fad53e | 65 | @Override |
35423ee0 GB |
66 | public void setViewData(String data) { |
67 | String[] idFile = data.split(TmfXmlAnalysisOutputSource.DATA_SEPARATOR); | |
68 | fId = (idFile.length > 0) ? idFile[0] : null; | |
69 | fFilePath = (idFile.length > 1) ? idFile[1] : null; | |
70 | savePersistentData(); | |
71 | } | |
72 | ||
38fad53e JCK |
73 | @Override |
74 | protected void savePersistentData() { | |
35423ee0 GB |
75 | IDialogSettings settings = getPersistentPropertyStore(); |
76 | ||
77 | settings.put(XML_VIEW_ID_PROPERTY, fId); | |
78 | settings.put(XML_VIEW_FILE_PROPERTY, fFilePath); | |
79 | } | |
80 | ||
81 | /** | |
82 | * Retrieve the XML element corresponding to the view | |
83 | * | |
84 | * @param xmlTag | |
85 | * The XML tag corresponding to the view element (the type of | |
86 | * view) | |
87 | * @return The view {@link Element} | |
88 | */ | |
89 | public @Nullable Element getViewElement(String xmlTag) { | |
90 | String id = fId; | |
91 | if (id == null) { | |
92 | return null; | |
93 | } | |
94 | Element viewElement = XmlUtils.getElementInFile(fFilePath, xmlTag, id); | |
95 | return viewElement; | |
96 | } | |
97 | ||
98 | /** | |
99 | * Get the view title from the header information of the XML view element. | |
100 | * | |
101 | * @param viewElement | |
102 | * The XML view element from which to get the title | |
103 | * @return The view title | |
104 | */ | |
105 | public @Nullable String getViewTitle(Element viewElement) { | |
106 | List<Element> heads = XmlUtils.getChildElements(viewElement, TmfXmlStrings.HEAD); | |
107 | ||
108 | String title = null; | |
109 | if (!heads.isEmpty()) { | |
110 | Element head = heads.get(0); | |
111 | /* Set the title of this view from the label in the header */ | |
112 | List<Element> labels = XmlUtils.getChildElements(head, TmfXmlStrings.LABEL); | |
113 | for (Element label : labels) { | |
114 | if (!label.getAttribute(TmfXmlStrings.VALUE).isEmpty()) { | |
115 | title = label.getAttribute(TmfXmlStrings.VALUE); | |
116 | } | |
117 | break; | |
118 | } | |
119 | } | |
120 | return title; | |
121 | } | |
122 | ||
123 | /** | |
124 | * Get the list of analysis IDs this view is for, as listed in the header of | |
125 | * the XML element | |
126 | * | |
127 | * @param viewElement | |
128 | * The XML view element from which to get the analysis IDs | |
129 | * @return The list of all analysis IDs this view is for | |
130 | */ | |
131 | public Set<String> getViewAnalysisIds(Element viewElement) { | |
132 | List<Element> heads = XmlUtils.getChildElements(viewElement, TmfXmlStrings.HEAD); | |
133 | ||
134 | Set<String> analysisIds = new HashSet<>(); | |
135 | if (!heads.isEmpty()) { | |
136 | Element head = heads.get(0); | |
137 | ||
138 | /* Get the application analysis from the view's XML header */ | |
139 | List<Element> applicableAnalysis = XmlUtils.getChildElements(head, TmfXmlStrings.ANALYSIS); | |
140 | for (Element oneAnalysis : applicableAnalysis) { | |
141 | analysisIds.add(oneAnalysis.getAttribute(TmfXmlStrings.ID)); | |
142 | } | |
143 | } | |
144 | return analysisIds; | |
145 | } | |
35423ee0 | 146 | } |