Remove unneeded checkNotNull() calls
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / operations / NewExperimentOperation.java
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 org.eclipse.core.resources.IFolder;
13 import org.eclipse.core.resources.IWorkspaceRoot;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IConfigurationElement;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.InvalidRegistryObjectException;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.jface.operation.ModalContext;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
27 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
28 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
29 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder;
30
31 /**
32 * Operation to create a new experiment.
33 *
34 * @author Bernd Hufmann
35 *
36 */
37 public class NewExperimentOperation implements IRunnableWithProgress {
38
39 private final @NonNull String fExperimentName;
40 private final @NonNull TmfExperimentFolder fExperimentFolderRoot;
41 private @Nullable IFolder fExperimentFolder = null;
42 private @NonNull IStatus fStatus = Status.OK_STATUS;
43
44 /**
45 * Constructor
46 *
47 * @param experimentFolder
48 * the experiment folder root {@link TmfExperimentFolder}
49 * @param experimentName
50 * the name of the experiment
51 */
52 public NewExperimentOperation (@NonNull TmfExperimentFolder experimentFolder, @NonNull String experimentName) {
53 fExperimentFolderRoot = experimentFolder;
54 fExperimentName = experimentName;
55 }
56
57 @Override
58 public void run(IProgressMonitor monitor) {
59 final IFolder experimentFolder = createExperiment(fExperimentName);
60 try {
61 monitor.beginTask("", 1000); //$NON-NLS-1$
62 ModalContext.checkCanceled(monitor);
63 experimentFolder.create(false, true, monitor);
64
65 /*
66 * Experiments can be set to the default experiment type. No
67 * need to force user to select an experiment type
68 */
69 IConfigurationElement ce = TmfTraceType.getTraceAttributes(TmfTraceType.DEFAULT_EXPERIMENT_TYPE);
70 if (ce != null) {
71 experimentFolder.setPersistentProperty(TmfCommonConstants.TRACETYPE, ce.getAttribute(TmfTraceType.ID_ATTR));
72 }
73 fExperimentFolder = experimentFolder;
74 setStatus(Status.OK_STATUS);
75 } catch (InterruptedException e) {
76 setStatus(Status.CANCEL_STATUS);
77 } catch (InvalidRegistryObjectException | CoreException e) {
78 String msg = NLS.bind(Messages.NewExperimentOperation_CreationError, fExperimentName);
79 Activator.getDefault().logError(msg, e);
80 setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, msg, e));
81 }
82 }
83
84 /**
85 * @return the experimentFolder
86 */
87 public @Nullable IFolder getExperimentFolder() {
88 return fExperimentFolder;
89 }
90
91 private IFolder createExperiment(String experimentName) {
92 IFolder expResource = fExperimentFolderRoot.getResource();
93 IWorkspaceRoot workspaceRoot = expResource.getWorkspace().getRoot();
94 IPath folderPath = expResource.getFullPath().append(experimentName);
95 IFolder folder = workspaceRoot.getFolder(folderPath);
96 return folder;
97 }
98
99 /**
100 * Set the status for this operation
101 *
102 * @param status
103 * the status
104 */
105 protected void setStatus(@NonNull IStatus status) {
106 fStatus = status;
107 }
108
109 /**
110 * Returns the status of the operation execution.
111 *
112 * @return status
113 */
114 public @NonNull IStatus getStatus() {
115 return fStatus;
116 }
117
118 }
This page took 0.033341 seconds and 5 git commands to generate.