tmf: Move some code to copy/add/remove traces in UI
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / CopyExperimentDialog.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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.linuxtools.tmf.ui.project.model.TmfProjectElement;
34 import org.eclipse.osgi.util.NLS;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.graphics.Font;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Event;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Listener;
44 import org.eclipse.swt.widgets.Shell;
45 import org.eclipse.swt.widgets.Text;
46 import org.eclipse.ui.PlatformUI;
47 import org.eclipse.ui.actions.WorkspaceModifyOperation;
48 import org.eclipse.ui.dialogs.SelectionStatusDialog;
49
50 /**
51 * Implementation of the copy experiement dialog box.
52 * <p>
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 private TmfProjectElement fProject;
66
67 // ------------------------------------------------------------------------
68 // Constructor
69 // ------------------------------------------------------------------------
70
71 /**
72 * Constructor
73 * @param shell The parent shell
74 * @param experiment The TMF experiment model element
75 */
76 public CopyExperimentDialog(Shell shell, TmfExperimentElement experiment) {
77 super(shell);
78 fExperiment = experiment;
79 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
80 fExperimentFolder = folder.getResource();
81 fProject = experiment.getProject();
82 setTitle(Messages.CopyExperimentDialog_DialogTitle);
83 setStatusLineAboveButtons(true);
84 }
85
86 // ------------------------------------------------------------------------
87 // Dialog
88 // ------------------------------------------------------------------------
89
90 /*
91 * (non-Javadoc)
92 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
93 */
94 @Override
95 protected Control createDialogArea(Composite parent) {
96 Composite composite = (Composite) super.createDialogArea(parent);
97 composite.setLayout(new GridLayout());
98 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
99
100 createNewExperimentNameGroup(composite);
101 return composite;
102 }
103
104 private void createNewExperimentNameGroup(Composite parent) {
105 Font font = parent.getFont();
106 Composite folderGroup = new Composite(parent, SWT.NONE);
107 GridLayout layout = new GridLayout();
108 layout.numColumns = 2;
109 folderGroup.setLayout(layout);
110 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
111
112 // Old experiment name label
113 Label oldExperimentLabel = new Label(folderGroup, SWT.NONE);
114 oldExperimentLabel.setFont(font);
115 oldExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentName);
116
117 // Old experiment name field
118 Text oldExperimentName = new Text(folderGroup, SWT.BORDER);
119 GridData data = new GridData(GridData.FILL_HORIZONTAL);
120 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
121 oldExperimentName.setLayoutData(data);
122 oldExperimentName.setFont(font);
123 oldExperimentName.setText(fExperiment.getName());
124 oldExperimentName.setEnabled(false);
125
126 // New experiment name label
127 Label newExperimentLabel = new Label(folderGroup, SWT.NONE);
128 newExperimentLabel.setFont(font);
129 newExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentNewName);
130
131 // New experiment name entry field
132 fNewExperimentName = new Text(folderGroup, SWT.BORDER);
133 data = new GridData(GridData.FILL_HORIZONTAL);
134 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
135 fNewExperimentName.setLayoutData(data);
136 fNewExperimentName.setFont(font);
137 fNewExperimentName.addListener(SWT.Modify, new Listener() {
138 @Override
139 public void handleEvent(Event event) {
140 validateNewExperimentName();
141 }
142 });
143 }
144
145 private void validateNewExperimentName() {
146
147 String name = fNewExperimentName.getText();
148 IWorkspace workspace = fExperimentFolder.getWorkspace();
149 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
150
151 if ("".equals(name)) { //$NON-NLS-1$
152 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null));
153 return;
154 }
155
156 if (!nameStatus.isOK()) {
157 updateStatus(nameStatus);
158 return;
159 }
160
161 IPath path = new Path(name);
162 if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) {
163 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null));
164 return;
165 }
166
167 updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$
168 }
169
170 // ------------------------------------------------------------------------
171 // SelectionStatusDialog
172 // ------------------------------------------------------------------------
173 /*
174 * (non-Javadoc)
175 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
176 */
177 @Override
178 protected void computeResult() {
179 }
180
181 /*
182 * (non-Javadoc)
183 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#create()
184 */
185 @Override
186 public void create() {
187 super.create();
188 getButton(IDialogConstants.OK_ID).setEnabled(false);
189 }
190
191 /*
192 * (non-Javadoc)
193 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
194 */
195 @Override
196 protected void okPressed() {
197 IFolder folder = copyExperiment(fNewExperimentName.getText());
198 if (folder == null) {
199 return;
200 }
201 setSelectionResult(new IFolder[] { folder });
202 super.okPressed();
203
204 if (fProject != null) {
205 fProject.refresh();
206 }
207 }
208
209 private IFolder copyExperiment(final String newName) {
210
211 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
212 @Override
213 public void execute(IProgressMonitor monitor) throws CoreException {
214 try {
215 monitor.beginTask("", 1000); //$NON-NLS-1$
216 if (monitor.isCanceled()) {
217 throw new OperationCanceledException();
218 }
219
220 fExperiment.copy(newName, true);
221
222 if (monitor.isCanceled()) {
223 throw new OperationCanceledException();
224 }
225 } finally {
226 monitor.done();
227 }
228 }
229 };
230 try {
231 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
232 } catch (InterruptedException exception) {
233 return null;
234 } catch (InvocationTargetException exception) {
235 MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
236 return null;
237 } catch (RuntimeException exception) {
238 return null;
239 }
240
241 return fExperiment.getResource();
242 }
243
244 }
This page took 0.041394 seconds and 6 git commands to generate.