tmf/lttng: Update 2014 copyrights
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / CopyExperimentDialog.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal
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 * Geneviève Bastien - Moved the actual copy code to model element's class
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.project.wizards;
15
16 import java.lang.reflect.InvocationTargetException;
17
18 import org.eclipse.core.resources.IFolder;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.IWorkspace;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.OperationCanceledException;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.jface.dialogs.IDialogConstants;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
31 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
32 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder;
33 import org.eclipse.osgi.util.NLS;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.graphics.Font;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Event;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.swt.widgets.Listener;
43 import org.eclipse.swt.widgets.Shell;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.actions.WorkspaceModifyOperation;
47 import org.eclipse.ui.dialogs.SelectionStatusDialog;
48
49 /**
50 * Implementation of the copy experiement dialog box.
51 * <p>
52 *
53 * @version 1.0
54 * @author Francois Chouinard
55 */
56 public class CopyExperimentDialog extends SelectionStatusDialog {
57
58 // ------------------------------------------------------------------------
59 // Members
60 // ------------------------------------------------------------------------
61
62 private final TmfExperimentElement fExperiment;
63 private Text fNewExperimentName;
64 private IFolder fExperimentFolder;
65
66 // ------------------------------------------------------------------------
67 // Constructor
68 // ------------------------------------------------------------------------
69
70 /**
71 * Constructor
72 *
73 * @param shell
74 * The parent shell
75 * @param experiment
76 * The TMF experiment model element
77 */
78 public CopyExperimentDialog(Shell shell, TmfExperimentElement experiment) {
79 super(shell);
80 fExperiment = experiment;
81 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
82 fExperimentFolder = folder.getResource();
83 setTitle(Messages.CopyExperimentDialog_DialogTitle);
84 setStatusLineAboveButtons(true);
85 }
86
87 // ------------------------------------------------------------------------
88 // Dialog
89 // ------------------------------------------------------------------------
90
91 @Override
92 protected Control createDialogArea(Composite parent) {
93 Composite composite = (Composite) super.createDialogArea(parent);
94 composite.setLayout(new GridLayout());
95 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
96
97 createNewExperimentNameGroup(composite);
98 return composite;
99 }
100
101 private void createNewExperimentNameGroup(Composite parent) {
102 Font font = parent.getFont();
103 Composite folderGroup = new Composite(parent, SWT.NONE);
104 GridLayout layout = new GridLayout();
105 layout.numColumns = 2;
106 folderGroup.setLayout(layout);
107 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
108
109 // Old experiment name label
110 Label oldExperimentLabel = new Label(folderGroup, SWT.NONE);
111 oldExperimentLabel.setFont(font);
112 oldExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentName);
113
114 // Old experiment name field
115 Text oldExperimentName = new Text(folderGroup, SWT.BORDER);
116 GridData data = new GridData(GridData.FILL_HORIZONTAL);
117 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
118 oldExperimentName.setLayoutData(data);
119 oldExperimentName.setFont(font);
120 oldExperimentName.setText(fExperiment.getName());
121 oldExperimentName.setEnabled(false);
122
123 // New experiment name label
124 Label newExperimentLabel = new Label(folderGroup, SWT.NONE);
125 newExperimentLabel.setFont(font);
126 newExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentNewName);
127
128 // New experiment name entry field
129 fNewExperimentName = new Text(folderGroup, SWT.BORDER);
130 data = new GridData(GridData.FILL_HORIZONTAL);
131 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
132 fNewExperimentName.setLayoutData(data);
133 fNewExperimentName.setFont(font);
134 fNewExperimentName.addListener(SWT.Modify, new Listener() {
135 @Override
136 public void handleEvent(Event event) {
137 validateNewExperimentName();
138 }
139 });
140 }
141
142 private void validateNewExperimentName() {
143
144 String name = fNewExperimentName.getText();
145 IWorkspace workspace = fExperimentFolder.getWorkspace();
146 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
147
148 if ("".equals(name)) { //$NON-NLS-1$
149 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null));
150 return;
151 }
152
153 if (!nameStatus.isOK()) {
154 updateStatus(nameStatus);
155 return;
156 }
157
158 IPath path = new Path(name);
159 if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) {
160 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null));
161 return;
162 }
163
164 updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$
165 }
166
167 // ------------------------------------------------------------------------
168 // SelectionStatusDialog
169 // ------------------------------------------------------------------------
170
171 @Override
172 protected void computeResult() {
173 }
174
175 @Override
176 public void create() {
177 super.create();
178 getButton(IDialogConstants.OK_ID).setEnabled(false);
179 }
180
181 @Override
182 protected void okPressed() {
183 IFolder folder = copyExperiment(fNewExperimentName.getText());
184 if (folder == null) {
185 return;
186 }
187 setSelectionResult(new IFolder[] { folder });
188 super.okPressed();
189 }
190
191 private IFolder copyExperiment(final String newName) {
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 }
201
202 fExperiment.copy(newName, true);
203
204 if (monitor.isCanceled()) {
205 throw new OperationCanceledException();
206 }
207 } finally {
208 monitor.done();
209 }
210 }
211 };
212 try {
213 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
214 } catch (InterruptedException exception) {
215 return null;
216 } catch (InvocationTargetException exception) {
217 MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
218 return null;
219 } catch (RuntimeException exception) {
220 return null;
221 }
222
223 return fExperiment.getResource();
224 }
225
226 }
This page took 0.038307 seconds and 6 git commands to generate.