| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2010, 2013 Ericsson |
| 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 | * Francois Chouinard - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.ui.project.model; |
| 14 | |
| 15 | import java.io.ByteArrayInputStream; |
| 16 | import java.io.InputStream; |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.Arrays; |
| 19 | import java.util.List; |
| 20 | |
| 21 | import org.eclipse.core.resources.IFile; |
| 22 | import org.eclipse.core.resources.IFolder; |
| 23 | import org.eclipse.core.resources.IResource; |
| 24 | import org.eclipse.core.runtime.CoreException; |
| 25 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
| 26 | import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment; |
| 27 | import org.eclipse.ui.views.properties.IPropertyDescriptor; |
| 28 | import org.eclipse.ui.views.properties.IPropertySource2; |
| 29 | import org.eclipse.ui.views.properties.TextPropertyDescriptor; |
| 30 | |
| 31 | /** |
| 32 | * Implementation of TMF Experiment Model Element. |
| 33 | * <p> |
| 34 | * @version 1.0 |
| 35 | * @author Francois Chouinard |
| 36 | * |
| 37 | */ |
| 38 | public class TmfExperimentElement extends TmfWithFolderElement implements IPropertySource2 { |
| 39 | |
| 40 | // ------------------------------------------------------------------------ |
| 41 | // Constants |
| 42 | // ------------------------------------------------------------------------ |
| 43 | |
| 44 | // Property View stuff |
| 45 | private static final String sfInfoCategory = "Info"; //$NON-NLS-1$ |
| 46 | private static final String sfName = "name"; //$NON-NLS-1$ |
| 47 | private static final String sfPath = "path"; //$NON-NLS-1$ |
| 48 | private static final String sfLocation = "location"; //$NON-NLS-1$ |
| 49 | private static final String sfFolderSuffix = "_exp"; //$NON-NLS-1$ |
| 50 | |
| 51 | private static final TextPropertyDescriptor sfNameDescriptor = new TextPropertyDescriptor(sfName, sfName); |
| 52 | private static final TextPropertyDescriptor sfPathDescriptor = new TextPropertyDescriptor(sfPath, sfPath); |
| 53 | private static final TextPropertyDescriptor sfLocationDescriptor = new TextPropertyDescriptor(sfLocation, |
| 54 | sfLocation); |
| 55 | |
| 56 | private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, |
| 57 | sfLocationDescriptor }; |
| 58 | |
| 59 | static { |
| 60 | sfNameDescriptor.setCategory(sfInfoCategory); |
| 61 | sfPathDescriptor.setCategory(sfInfoCategory); |
| 62 | sfLocationDescriptor.setCategory(sfInfoCategory); |
| 63 | } |
| 64 | |
| 65 | private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$ |
| 66 | |
| 67 | // ------------------------------------------------------------------------ |
| 68 | // Constructors |
| 69 | // ------------------------------------------------------------------------ |
| 70 | /** |
| 71 | * Constructor |
| 72 | * @param name The name of the experiment |
| 73 | * @param folder The folder reference |
| 74 | * @param parent The experiment folder reference. |
| 75 | */ |
| 76 | public TmfExperimentElement(String name, IFolder folder, TmfExperimentFolder parent) { |
| 77 | super(name, folder, parent); |
| 78 | parent.addChild(this); |
| 79 | } |
| 80 | |
| 81 | // ------------------------------------------------------------------------ |
| 82 | // TmfProjectModelElement |
| 83 | // ------------------------------------------------------------------------ |
| 84 | |
| 85 | /* |
| 86 | * (non-Javadoc) |
| 87 | * @see org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectModelElement#getResource() |
| 88 | */ |
| 89 | @Override |
| 90 | public IFolder getResource() { |
| 91 | return (IFolder) fResource; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * (non-Javadoc) |
| 96 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getProject() |
| 97 | */ |
| 98 | @Override |
| 99 | public TmfProjectElement getProject() { |
| 100 | return (TmfProjectElement) getParent().getParent(); |
| 101 | } |
| 102 | |
| 103 | // ------------------------------------------------------------------------ |
| 104 | // Operations |
| 105 | // ------------------------------------------------------------------------ |
| 106 | /** |
| 107 | * Returns a list of TmfTraceElements contained in this experiment. |
| 108 | * @return a list of TmfTraceElements |
| 109 | */ |
| 110 | public List<TmfTraceElement> getTraces() { |
| 111 | List<ITmfProjectModelElement> children = getChildren(); |
| 112 | List<TmfTraceElement> traces = new ArrayList<TmfTraceElement>(); |
| 113 | for (ITmfProjectModelElement child : children) { |
| 114 | if (child instanceof TmfTraceElement) { |
| 115 | traces.add((TmfTraceElement) child); |
| 116 | } |
| 117 | } |
| 118 | return traces; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the file resource used to store bookmarks after creating it if necessary. |
| 123 | * The file will be created if it does not exist. |
| 124 | * @return the bookmarks file |
| 125 | * @throws CoreException if the bookmarks file cannot be created |
| 126 | * @since 2.0 |
| 127 | */ |
| 128 | public IFile createBookmarksFile() throws CoreException { |
| 129 | IFile file = getBookmarksFile(); |
| 130 | if (!file.exists()) { |
| 131 | final IFile bookmarksFile = getProject().getExperimentsFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE); |
| 132 | if (!bookmarksFile.exists()) { |
| 133 | final InputStream source = new ByteArrayInputStream(new byte[0]); |
| 134 | bookmarksFile.create(source, true, null); |
| 135 | } |
| 136 | bookmarksFile.setHidden(true); |
| 137 | file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null); |
| 138 | file.setHidden(true); |
| 139 | file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfExperiment.class.getCanonicalName()); |
| 140 | } |
| 141 | return file; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns the file resource used to store bookmarks. |
| 146 | * The file may not exist. |
| 147 | * @return the bookmarks file |
| 148 | * @since 2.0 |
| 149 | */ |
| 150 | public IFile getBookmarksFile() { |
| 151 | final IFolder folder = (IFolder) fResource; |
| 152 | IFile file = folder.getFile(getName() + '_'); |
| 153 | return file; |
| 154 | } |
| 155 | |
| 156 | // ------------------------------------------------------------------------ |
| 157 | // IPropertySource2 |
| 158 | // ------------------------------------------------------------------------ |
| 159 | |
| 160 | /* |
| 161 | * (non-Javadoc) |
| 162 | * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue() |
| 163 | */ |
| 164 | @Override |
| 165 | public Object getEditableValue() { |
| 166 | return null; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * (non-Javadoc) |
| 171 | * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors() |
| 172 | */ |
| 173 | @Override |
| 174 | public IPropertyDescriptor[] getPropertyDescriptors() { |
| 175 | return Arrays.copyOf(sfDescriptors, sfDescriptors.length); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * (non-Javadoc) |
| 180 | * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object) |
| 181 | */ |
| 182 | @Override |
| 183 | public Object getPropertyValue(Object id) { |
| 184 | |
| 185 | if (sfName.equals(id)) { |
| 186 | return getName(); |
| 187 | } |
| 188 | |
| 189 | if (sfPath.equals(id)) { |
| 190 | return getPath().toString(); |
| 191 | } |
| 192 | |
| 193 | if (sfLocation.equals(id)) { |
| 194 | return getLocation().toString(); |
| 195 | } |
| 196 | |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * (non-Javadoc) |
| 202 | * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object) |
| 203 | */ |
| 204 | @Override |
| 205 | public void resetPropertyValue(Object id) { |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * (non-Javadoc) |
| 210 | * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object) |
| 211 | */ |
| 212 | @Override |
| 213 | public void setPropertyValue(Object id, Object value) { |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * (non-Javadoc) |
| 218 | * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertyResettable(java.lang.Object) |
| 219 | */ |
| 220 | @Override |
| 221 | public boolean isPropertyResettable(Object id) { |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * (non-Javadoc) |
| 227 | * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertySet(java.lang.Object) |
| 228 | */ |
| 229 | @Override |
| 230 | public boolean isPropertySet(Object id) { |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Return the suffix for resource names |
| 236 | * @return The folder suffix |
| 237 | */ |
| 238 | @Override |
| 239 | public String getSuffix() { |
| 240 | return sfFolderSuffix; |
| 241 | } |
| 242 | |
| 243 | } |