| 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 | |
| 13 | package org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.views; |
| 14 | |
| 15 | import java.util.HashSet; |
| 16 | import java.util.List; |
| 17 | import java.util.Set; |
| 18 | import java.util.concurrent.CountDownLatch; |
| 19 | |
| 20 | import org.eclipse.jdt.annotation.NonNullByDefault; |
| 21 | import org.eclipse.jdt.annotation.Nullable; |
| 22 | import org.eclipse.jface.dialogs.IDialogSettings; |
| 23 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlUtils; |
| 24 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; |
| 25 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.module.TmfXmlAnalysisOutputSource; |
| 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 extends AbstractXmlViewInfo { |
| 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 | /* Initialization is completed when name is set */ |
| 40 | private final CountDownLatch fInitialized = new CountDownLatch(1); |
| 41 | /** This is the ID of the view described in the XML file */ |
| 42 | private @Nullable String fId = null; |
| 43 | private @Nullable String fFilePath = null; |
| 44 | // If true, properties were set but not saved to persistent storage |
| 45 | private boolean fIsDirty = false; |
| 46 | |
| 47 | /** |
| 48 | * Constructor |
| 49 | * |
| 50 | * @param viewId |
| 51 | * The ID of the view |
| 52 | */ |
| 53 | public XmlViewInfo(String viewId) { |
| 54 | super(viewId); |
| 55 | /* Cannot get the properties yet, need to wait for the name */ |
| 56 | } |
| 57 | |
| 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; |
| 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 | */ |
| 82 | @Override |
| 83 | public synchronized void setViewData(String data) { |
| 84 | String[] idFile = data.split(TmfXmlAnalysisOutputSource.DATA_SEPARATOR); |
| 85 | fId = (idFile.length > 0) ? idFile[0] : null; |
| 86 | fFilePath = (idFile.length > 1) ? idFile[1] : null; |
| 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(); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected void savePersistentData() { |
| 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 | } |
| 181 | } |