1 /*******************************************************************************
2 * Copyright (c) 2010-2013 Ericsson, École Polytechnique de Montréal
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
10 * Bernd Hufmann - Added supplementary files handling (in class TmfTraceElement)
11 * Geneviève Bastien - Copied supplementary files handling from TmfTracElement
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
17 import org
.eclipse
.core
.resources
.IFolder
;
18 import org
.eclipse
.core
.resources
.IResource
;
19 import org
.eclipse
.core
.runtime
.CoreException
;
20 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
21 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
26 * Base class for project elements who will have folder elements
27 * under them to store supplementary files.
32 public abstract class TmfWithFolderElement
extends TmfProjectModelElement
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
41 * Creates model element.
42 * @param name The name of the element
43 * @param resource The resource.
44 * @param parent The parent element
46 public TmfWithFolderElement(String name
, IResource resource
, TmfProjectModelElement parent
) {
47 super(name
, resource
, parent
);
51 * Return the resource name for this element
53 * @return The name of the resource for this element
55 protected String
getResourceName() {
56 return fResource
.getName() + getSuffix();
60 * @return The suffix for resource names
62 protected String
getSuffix() {
63 return ""; //$NON-NLS-1$
67 * Deletes this element specific supplementary folder.
69 public void deleteSupplementaryFolder() {
70 IFolder supplFolder
= getTraceSupplementaryFolder(getResourceName());
71 if (supplFolder
.exists()) {
73 supplFolder
.delete(true, new NullProgressMonitor());
74 } catch (CoreException e
) {
75 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
81 * Renames the element specific supplementary folder according to the new element name.
83 * @param newName The new element name
85 public void renameSupplementaryFolder(String newName
) {
86 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getResourceName());
87 IFolder newSupplFolder
= getTraceSupplementaryFolder(newName
+ getSuffix());
89 // Rename supplementary folder
90 if (oldSupplFolder
.exists()) {
92 oldSupplFolder
.move(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
93 } catch (CoreException e
) {
94 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
100 * Copies the element specific supplementary folder to the new element name.
102 * @param newName The new element name
104 public void copySupplementaryFolder(String newName
) {
105 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getResourceName());
106 IFolder newSupplFolder
= getTraceSupplementaryFolder(newName
+ getSuffix());
108 // copy supplementary folder
109 if (oldSupplFolder
.exists()) {
111 oldSupplFolder
.copy(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
112 } catch (CoreException e
) {
113 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
119 * Copies the element specific supplementary folder a new folder.
121 * @param destination The destination folder to copy to.
123 public void copySupplementaryFolder(IFolder destination
) {
124 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getResourceName());
126 // copy supplementary folder
127 if (oldSupplFolder
.exists()) {
129 oldSupplFolder
.copy(destination
.getFullPath(), true, new NullProgressMonitor());
130 } catch (CoreException e
) {
131 Activator
.getDefault().logError("Error copying supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
138 * Refreshes the element specific supplementary folder information. It creates the folder if not exists.
139 * It sets the persistence property of the trace resource
141 public void refreshSupplementaryFolder() {
142 createSupplementaryDirectory();
146 * Checks if supplementary resource exist or not.
148 * @return <code>true</code> if one or more files are under the element supplementary folder
150 public boolean hasSupplementaryResources() {
151 IResource
[] resources
= getSupplementaryResources();
152 return (resources
.length
> 0);
156 * Returns the supplementary resources under the trace supplementary folder.
158 * @return array of resources under the trace supplementary folder.
160 public IResource
[] getSupplementaryResources() {
161 IFolder supplFolder
= getTraceSupplementaryFolder(getResourceName());
162 if (supplFolder
.exists()) {
164 return supplFolder
.members();
165 } catch (CoreException e
) {
166 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
169 return new IResource
[0];
173 * Deletes the given resources.
175 * @param resources array of resources to delete.
177 public void deleteSupplementaryResources(IResource
[] resources
) {
179 for (int i
= 0; i
< resources
.length
; i
++) {
181 resources
[i
].delete(true, new NullProgressMonitor());
182 } catch (CoreException e
) {
183 Activator
.getDefault().logError("Error deleting supplementary resource " + resources
[i
], e
); //$NON-NLS-1$
188 private void createSupplementaryDirectory() {
189 IFolder supplFolder
= getTraceSupplementaryFolder(getResourceName());
190 if (!supplFolder
.exists()) {
192 supplFolder
.create(true, true, new NullProgressMonitor());
193 } catch (CoreException e
) {
194 Activator
.getDefault().logError("Error creating resource supplementary file " + supplFolder
, e
); //$NON-NLS-1$
199 fResource
.setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, supplFolder
.getLocationURI().getPath());
200 } catch (CoreException e
) {
201 Activator
.getDefault().logError("Error setting persistant property " + TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, e
); //$NON-NLS-1$