tmf: Update copyright headers in tmf.ui
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / CopyExperimentDialog.java
CommitLineData
12c155f5 1/*******************************************************************************
c8422608 2 * Copyright (c) 2011, 2013 Ericsson
99504bb8 3 *
12c155f5
FC
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
99504bb8 8 *
12c155f5
FC
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;
8fd82db5 29import org.eclipse.linuxtools.internal.tmf.ui.Activator;
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/**
b544077e 52 * Implementation of the copy experiement dialog box.
12c155f5 53 * <p>
b544077e
BH
54 * @version 1.0
55 * @author Francois Chouinard
12c155f5
FC
56 */
57public class CopyExperimentDialog extends SelectionStatusDialog {
58
59 // ------------------------------------------------------------------------
60 // Members
61 // ------------------------------------------------------------------------
62
63 private final TmfExperimentElement fExperiment;
64 private Text fNewExperimentName;
3453cab5 65 private IFolder fExperimentFolder;
12c155f5
FC
66 private TmfProjectElement fProject;
67
68 // ------------------------------------------------------------------------
69 // Constructor
70 // ------------------------------------------------------------------------
71
b544077e
BH
72 /**
73 * Constructor
74 * @param shell The parent shell
75 * @param experiment The TMF experiment model element
76 */
12c155f5
FC
77 public CopyExperimentDialog(Shell shell, TmfExperimentElement experiment) {
78 super(shell);
79 fExperiment = experiment;
80 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
81 fExperimentFolder = folder.getResource();
82 fProject = experiment.getProject();
83 setTitle(Messages.CopyExperimentDialog_DialogTitle);
84 setStatusLineAboveButtons(true);
85 }
86
87 // ------------------------------------------------------------------------
88 // Dialog
89 // ------------------------------------------------------------------------
99504bb8 90
b544077e
BH
91 /*
92 * (non-Javadoc)
93 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
94 */
12c155f5
FC
95 @Override
96 protected Control createDialogArea(Composite parent) {
97 Composite composite = (Composite) super.createDialogArea(parent);
98 composite.setLayout(new GridLayout());
99 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
100
101 createNewExperimentNameGroup(composite);
102 return composite;
103 }
104
105 private void createNewExperimentNameGroup(Composite parent) {
106 Font font = parent.getFont();
107 Composite folderGroup = new Composite(parent, SWT.NONE);
108 GridLayout layout = new GridLayout();
109 layout.numColumns = 2;
110 folderGroup.setLayout(layout);
111 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
112
113 // Old experiment name label
114 Label oldExperimentLabel = new Label(folderGroup, SWT.NONE);
115 oldExperimentLabel.setFont(font);
116 oldExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentName);
117
118 // Old experiment name field
119 Text oldExperimentName = new Text(folderGroup, SWT.BORDER);
120 GridData data = new GridData(GridData.FILL_HORIZONTAL);
121 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
122 oldExperimentName.setLayoutData(data);
123 oldExperimentName.setFont(font);
124 oldExperimentName.setText(fExperiment.getName());
125 oldExperimentName.setEnabled(false);
126
127 // New experiment name label
128 Label newExperimentLabel = new Label(folderGroup, SWT.NONE);
129 newExperimentLabel.setFont(font);
130 newExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentNewName);
131
132 // New experiment name entry field
133 fNewExperimentName = new Text(folderGroup, SWT.BORDER);
134 data = new GridData(GridData.FILL_HORIZONTAL);
135 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
136 fNewExperimentName.setLayoutData(data);
137 fNewExperimentName.setFont(font);
138 fNewExperimentName.addListener(SWT.Modify, new Listener() {
139 @Override
140 public void handleEvent(Event event) {
141 validateNewExperimentName();
142 }
143 });
144 }
145
146 private void validateNewExperimentName() {
99504bb8 147
12c155f5
FC
148 String name = fNewExperimentName.getText();
149 IWorkspace workspace = fExperimentFolder.getWorkspace();
150 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
151
152 if ("".equals(name)) { //$NON-NLS-1$
8fd82db5 153 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null));
12c155f5
FC
154 return;
155 }
99504bb8 156
9fa32496 157 if (!nameStatus.isOK()) {
12c155f5
FC
158 updateStatus(nameStatus);
159 return;
160 }
99504bb8 161
12c155f5
FC
162 IPath path = new Path(name);
163 if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) {
8fd82db5 164 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null));
12c155f5
FC
165 return;
166 }
167
8fd82db5 168 updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$
12c155f5
FC
169 }
170
171 // ------------------------------------------------------------------------
172 // SelectionStatusDialog
173 // ------------------------------------------------------------------------
b544077e
BH
174 /*
175 * (non-Javadoc)
176 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
177 */
12c155f5
FC
178 @Override
179 protected void computeResult() {
180 }
181
b544077e
BH
182 /*
183 * (non-Javadoc)
184 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#create()
185 */
12c155f5
FC
186 @Override
187 public void create() {
188 super.create();
189 getButton(IDialogConstants.OK_ID).setEnabled(false);
190 }
191
b544077e
BH
192 /*
193 * (non-Javadoc)
194 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
195 */
12c155f5
FC
196 @Override
197 protected void okPressed() {
198 IFolder folder = copyExperiment(fNewExperimentName.getText());
199 if (folder == null) {
200 return;
201 }
202 setSelectionResult(new IFolder[] { folder });
203 super.okPressed();
204
205 if (fProject != null) {
206 fProject.refresh();
207 }
208 }
209
3453cab5 210 private IFolder copyExperiment(final String newName) {
12c155f5
FC
211
212 IPath oldPath = fExperiment.getResource().getFullPath();
213 final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$
214
215 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
216 @Override
217 public void execute(IProgressMonitor monitor) throws CoreException {
218 try {
219 monitor.beginTask("", 1000); //$NON-NLS-1$
220 if (monitor.isCanceled()) {
221 throw new OperationCanceledException();
222 }
99504bb8
GB
223 // Copy supplementary files first
224 fExperiment.copySupplementaryFolder(newName);
3453cab5
PT
225 fExperiment.getResource().copy(newPath, IResource.FORCE | IResource.SHALLOW, null);
226 // Delete any bookmarks file found in copied experiment folder
227 IFolder folder = fExperimentFolder.getFolder(newName);
228 if (folder.exists()) {
229 for (IResource member : folder.members()) {
e12ecd30 230 if (TmfExperiment.class.getCanonicalName().equals(member.getPersistentProperty(TmfCommonConstants.TRACETYPE))) {
3453cab5
PT
231 member.delete(true, null);
232 }
233 }
234 }
12c155f5
FC
235 if (monitor.isCanceled()) {
236 throw new OperationCanceledException();
237 }
238 } finally {
239 monitor.done();
240 }
241 }
242 };
243 try {
244 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
245 } catch (InterruptedException exception) {
246 return null;
247 } catch (InvocationTargetException exception) {
248 MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
249 return null;
250 } catch (RuntimeException exception) {
251 return null;
252 }
253
254 return fExperiment.getResource();
255 }
256
257}
This page took 0.04576 seconds and 5 git commands to generate.