1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.operations
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import org
.eclipse
.core
.resources
.IFolder
;
15 import org
.eclipse
.core
.resources
.IWorkspaceRoot
;
16 import org
.eclipse
.core
.runtime
.CoreException
;
17 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
18 import org
.eclipse
.core
.runtime
.IPath
;
19 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
20 import org
.eclipse
.core
.runtime
.IStatus
;
21 import org
.eclipse
.core
.runtime
.InvalidRegistryObjectException
;
22 import org
.eclipse
.core
.runtime
.Status
;
23 import org
.eclipse
.jdt
.annotation
.NonNull
;
24 import org
.eclipse
.jdt
.annotation
.Nullable
;
25 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
26 import org
.eclipse
.jface
.operation
.ModalContext
;
27 import org
.eclipse
.osgi
.util
.NLS
;
28 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
31 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
34 * Operation to create a new experiment.
36 * @author Bernd Hufmann
39 public class NewExperimentOperation
implements IRunnableWithProgress
{
41 private final @NonNull String fExperimentName
;
42 private final @NonNull TmfExperimentFolder fExperimentFolderRoot
;
43 private @Nullable IFolder fExperimentFolder
= null;
44 private @NonNull IStatus fStatus
= checkNotNull(Status
.OK_STATUS
);
49 * @param experimentFolder
50 * the experiment folder root {@link TmfExperimentFolder}
51 * @param experimentName
52 * the name of the experiment
54 public NewExperimentOperation (@NonNull TmfExperimentFolder experimentFolder
, @NonNull String experimentName
) {
55 fExperimentFolderRoot
= experimentFolder
;
56 fExperimentName
= experimentName
;
60 public void run(IProgressMonitor monitor
) {
61 final IFolder experimentFolder
= createExperiment(fExperimentName
);
63 monitor
.beginTask("", 1000); //$NON-NLS-1$
64 ModalContext
.checkCanceled(monitor
);
65 experimentFolder
.create(false, true, monitor
);
68 * Experiments can be set to the default experiment type. No
69 * need to force user to select an experiment type
71 IConfigurationElement ce
= TmfTraceType
.getTraceAttributes(TmfTraceType
.DEFAULT_EXPERIMENT_TYPE
);
73 experimentFolder
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, ce
.getAttribute(TmfTraceType
.ID_ATTR
));
75 fExperimentFolder
= experimentFolder
;
76 setStatus(checkNotNull(Status
.OK_STATUS
));
77 } catch (InterruptedException e
) {
78 setStatus(checkNotNull(Status
.CANCEL_STATUS
));
79 } catch (InvalidRegistryObjectException
| CoreException e
) {
80 String msg
= NLS
.bind(Messages
.NewExperimentOperation_CreationError
, fExperimentName
);
81 Activator
.getDefault().logError(msg
, e
);
82 setStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, msg
, e
));
87 * @return the experimentFolder
89 public @Nullable IFolder
getExperimentFolder() {
90 return fExperimentFolder
;
93 private IFolder
createExperiment(String experimentName
) {
94 IFolder expResource
= fExperimentFolderRoot
.getResource();
95 IWorkspaceRoot workspaceRoot
= expResource
.getWorkspace().getRoot();
96 IPath folderPath
= expResource
.getFullPath().append(experimentName
);
97 IFolder folder
= workspaceRoot
.getFolder(folderPath
);
102 * Set the status for this operation
107 protected void setStatus(@NonNull IStatus status
) {
112 * Returns the status of the operation execution.
116 public @NonNull IStatus
getStatus() {