Commit | Line | Data |
---|---|---|
eff18017 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 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 | ||
10 | package org.eclipse.tracecompass.internal.tmf.ui.project.operations; | |
11 | ||
12 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
13 | ||
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; | |
32 | ||
33 | /** | |
34 | * Operation to create a new experiment. | |
35 | * | |
36 | * @author Bernd Hufmann | |
37 | * | |
38 | */ | |
39 | public class NewExperimentOperation implements IRunnableWithProgress { | |
40 | ||
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); | |
45 | ||
46 | /** | |
47 | * Constructor | |
48 | * | |
49 | * @param experimentFolder | |
50 | * the experiment folder root {@link TmfExperimentFolder} | |
51 | * @param experimentName | |
52 | * the name of the experiment | |
53 | */ | |
54 | public NewExperimentOperation (@NonNull TmfExperimentFolder experimentFolder, @NonNull String experimentName) { | |
55 | fExperimentFolderRoot = experimentFolder; | |
56 | fExperimentName = experimentName; | |
57 | } | |
58 | ||
59 | @Override | |
60 | public void run(IProgressMonitor monitor) { | |
61 | final IFolder experimentFolder = createExperiment(fExperimentName); | |
62 | try { | |
63 | monitor.beginTask("", 1000); //$NON-NLS-1$ | |
64 | ModalContext.checkCanceled(monitor); | |
65 | experimentFolder.create(false, true, monitor); | |
66 | ||
67 | /* | |
68 | * Experiments can be set to the default experiment type. No | |
69 | * need to force user to select an experiment type | |
70 | */ | |
71 | IConfigurationElement ce = TmfTraceType.getTraceAttributes(TmfTraceType.DEFAULT_EXPERIMENT_TYPE); | |
72 | if (ce != null) { | |
73 | experimentFolder.setPersistentProperty(TmfCommonConstants.TRACETYPE, ce.getAttribute(TmfTraceType.ID_ATTR)); | |
74 | } | |
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)); | |
83 | } | |
84 | } | |
85 | ||
86 | /** | |
87 | * @return the experimentFolder | |
88 | */ | |
89 | public @Nullable IFolder getExperimentFolder() { | |
90 | return fExperimentFolder; | |
91 | } | |
92 | ||
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); | |
98 | return folder; | |
99 | } | |
100 | ||
101 | /** | |
102 | * Set the status for this operation | |
103 | * | |
104 | * @param status | |
105 | * the status | |
106 | */ | |
107 | protected void setStatus(@NonNull IStatus status) { | |
108 | fStatus = status; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Returns the status of the operation execution. | |
113 | * | |
114 | * @return status | |
115 | */ | |
116 | public @NonNull IStatus getStatus() { | |
117 | return fStatus; | |
118 | } | |
119 | ||
120 | } |