Add copyright header.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / CopyExperimentDialog.java
CommitLineData
12c155f5
FC
1/*******************************************************************************
2 * Copyright (c) 2011 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 - Copied and adapted from NewFolderDialog
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.ui.project.wizards;
14
15import java.lang.reflect.InvocationTargetException;
16
12c155f5
FC
17import org.eclipse.core.resources.IFolder;
18import org.eclipse.core.resources.IResource;
19import org.eclipse.core.resources.IWorkspace;
20import org.eclipse.core.runtime.CoreException;
21import org.eclipse.core.runtime.IPath;
22import org.eclipse.core.runtime.IProgressMonitor;
23import org.eclipse.core.runtime.IStatus;
24import org.eclipse.core.runtime.OperationCanceledException;
25import org.eclipse.core.runtime.Path;
26import org.eclipse.core.runtime.Status;
27import org.eclipse.jface.dialogs.IDialogConstants;
28import org.eclipse.jface.dialogs.MessageDialog;
d34665f9 29import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
e12ecd30 30import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
9e0640dc 31import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
12c155f5
FC
32import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
33import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder;
34import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
35import org.eclipse.osgi.util.NLS;
36import org.eclipse.swt.SWT;
37import org.eclipse.swt.graphics.Font;
38import org.eclipse.swt.layout.GridData;
39import org.eclipse.swt.layout.GridLayout;
40import org.eclipse.swt.widgets.Composite;
41import org.eclipse.swt.widgets.Control;
42import org.eclipse.swt.widgets.Event;
43import org.eclipse.swt.widgets.Label;
44import org.eclipse.swt.widgets.Listener;
45import org.eclipse.swt.widgets.Shell;
46import org.eclipse.swt.widgets.Text;
47import org.eclipse.ui.PlatformUI;
48import org.eclipse.ui.actions.WorkspaceModifyOperation;
49import org.eclipse.ui.dialogs.SelectionStatusDialog;
50
51/**
52 * <b><u>CopyExperimentDialog</u></b>
53 * <p>
54 */
55public class CopyExperimentDialog extends SelectionStatusDialog {
56
57 // ------------------------------------------------------------------------
58 // Members
59 // ------------------------------------------------------------------------
60
61 private final TmfExperimentElement fExperiment;
62 private Text fNewExperimentName;
3453cab5 63 private IFolder fExperimentFolder;
12c155f5
FC
64 private TmfProjectElement fProject;
65
66 // ------------------------------------------------------------------------
67 // Constructor
68 // ------------------------------------------------------------------------
69
70 public CopyExperimentDialog(Shell shell, TmfExperimentElement experiment) {
71 super(shell);
72 fExperiment = experiment;
73 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
74 fExperimentFolder = folder.getResource();
75 fProject = experiment.getProject();
76 setTitle(Messages.CopyExperimentDialog_DialogTitle);
77 setStatusLineAboveButtons(true);
78 }
79
80 // ------------------------------------------------------------------------
81 // Dialog
82 // ------------------------------------------------------------------------
83
84 @Override
85 protected Control createDialogArea(Composite parent) {
86 Composite composite = (Composite) super.createDialogArea(parent);
87 composite.setLayout(new GridLayout());
88 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
89
90 createNewExperimentNameGroup(composite);
91 return composite;
92 }
93
94 private void createNewExperimentNameGroup(Composite parent) {
95 Font font = parent.getFont();
96 Composite folderGroup = new Composite(parent, SWT.NONE);
97 GridLayout layout = new GridLayout();
98 layout.numColumns = 2;
99 folderGroup.setLayout(layout);
100 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
101
102 // Old experiment name label
103 Label oldExperimentLabel = new Label(folderGroup, SWT.NONE);
104 oldExperimentLabel.setFont(font);
105 oldExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentName);
106
107 // Old experiment name field
108 Text oldExperimentName = new Text(folderGroup, SWT.BORDER);
109 GridData data = new GridData(GridData.FILL_HORIZONTAL);
110 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
111 oldExperimentName.setLayoutData(data);
112 oldExperimentName.setFont(font);
113 oldExperimentName.setText(fExperiment.getName());
114 oldExperimentName.setEnabled(false);
115
116 // New experiment name label
117 Label newExperimentLabel = new Label(folderGroup, SWT.NONE);
118 newExperimentLabel.setFont(font);
119 newExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentNewName);
120
121 // New experiment name entry field
122 fNewExperimentName = new Text(folderGroup, SWT.BORDER);
123 data = new GridData(GridData.FILL_HORIZONTAL);
124 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
125 fNewExperimentName.setLayoutData(data);
126 fNewExperimentName.setFont(font);
127 fNewExperimentName.addListener(SWT.Modify, new Listener() {
128 @Override
129 public void handleEvent(Event event) {
130 validateNewExperimentName();
131 }
132 });
133 }
134
135 private void validateNewExperimentName() {
136
137 String name = fNewExperimentName.getText();
138 IWorkspace workspace = fExperimentFolder.getWorkspace();
139 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
140
141 if ("".equals(name)) { //$NON-NLS-1$
142 updateStatus(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null));
143 return;
144 }
145
9fa32496 146 if (!nameStatus.isOK()) {
12c155f5
FC
147 updateStatus(nameStatus);
148 return;
149 }
150
151 IPath path = new Path(name);
152 if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) {
153 updateStatus(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null));
154 return;
155 }
156
157 updateStatus(new Status(IStatus.OK, TmfUiPlugin.PLUGIN_ID, "")); //$NON-NLS-1$
158 }
159
160 // ------------------------------------------------------------------------
161 // SelectionStatusDialog
162 // ------------------------------------------------------------------------
163
164 @Override
165 protected void computeResult() {
166 }
167
168 @Override
169 public void create() {
170 super.create();
171 getButton(IDialogConstants.OK_ID).setEnabled(false);
172 }
173
174 @Override
175 protected void okPressed() {
176 IFolder folder = copyExperiment(fNewExperimentName.getText());
177 if (folder == null) {
178 return;
179 }
180 setSelectionResult(new IFolder[] { folder });
181 super.okPressed();
182
183 if (fProject != null) {
184 fProject.refresh();
185 }
186 }
187
3453cab5 188 private IFolder copyExperiment(final String newName) {
12c155f5
FC
189
190 IPath oldPath = fExperiment.getResource().getFullPath();
191 final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$
192
193 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
194 @Override
195 public void execute(IProgressMonitor monitor) throws CoreException {
196 try {
197 monitor.beginTask("", 1000); //$NON-NLS-1$
198 if (monitor.isCanceled()) {
199 throw new OperationCanceledException();
200 }
3453cab5
PT
201 fExperiment.getResource().copy(newPath, IResource.FORCE | IResource.SHALLOW, null);
202 // Delete any bookmarks file found in copied experiment folder
203 IFolder folder = fExperimentFolder.getFolder(newName);
204 if (folder.exists()) {
205 for (IResource member : folder.members()) {
e12ecd30 206 if (TmfExperiment.class.getCanonicalName().equals(member.getPersistentProperty(TmfCommonConstants.TRACETYPE))) {
3453cab5
PT
207 member.delete(true, null);
208 }
209 }
210 }
12c155f5
FC
211 if (monitor.isCanceled()) {
212 throw new OperationCanceledException();
213 }
214 } finally {
215 monitor.done();
216 }
217 }
218 };
219 try {
220 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
221 } catch (InterruptedException exception) {
222 return null;
223 } catch (InvocationTargetException exception) {
224 MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
225 return null;
226 } catch (RuntimeException exception) {
227 return null;
228 }
229
230 return fExperiment.getResource();
231 }
232
233}
This page took 0.039183 seconds and 5 git commands to generate.