Contribute CNF based TMF project handling
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / CopyExperimentDialog.java
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
13 package org.eclipse.linuxtools.tmf.ui.project.wizards;
14
15 import java.lang.reflect.InvocationTargetException;
16
17 import org.eclipse.core.resources.IContainer;
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.tmf.ui.TmfUiPlugin;
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 * <b><u>CopyExperimentDialog</u></b>
52 * <p>
53 */
54 public class CopyExperimentDialog extends SelectionStatusDialog {
55
56 // ------------------------------------------------------------------------
57 // Members
58 // ------------------------------------------------------------------------
59
60 private final TmfExperimentElement fExperiment;
61 private Text fNewExperimentName;
62 private IContainer fExperimentFolder;
63 private TmfProjectElement fProject;
64
65 // ------------------------------------------------------------------------
66 // Constructor
67 // ------------------------------------------------------------------------
68
69 public CopyExperimentDialog(Shell shell, TmfExperimentElement experiment) {
70 super(shell);
71 fExperiment = experiment;
72 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
73 fExperimentFolder = folder.getResource();
74 fProject = experiment.getProject();
75 setTitle(Messages.CopyExperimentDialog_DialogTitle);
76 setStatusLineAboveButtons(true);
77 }
78
79 // ------------------------------------------------------------------------
80 // Dialog
81 // ------------------------------------------------------------------------
82
83 @Override
84 protected Control createDialogArea(Composite parent) {
85 Composite composite = (Composite) super.createDialogArea(parent);
86 composite.setLayout(new GridLayout());
87 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
88
89 createNewExperimentNameGroup(composite);
90 return composite;
91 }
92
93 private void createNewExperimentNameGroup(Composite parent) {
94 Font font = parent.getFont();
95 Composite folderGroup = new Composite(parent, SWT.NONE);
96 GridLayout layout = new GridLayout();
97 layout.numColumns = 2;
98 folderGroup.setLayout(layout);
99 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
100
101 // Old experiment name label
102 Label oldExperimentLabel = new Label(folderGroup, SWT.NONE);
103 oldExperimentLabel.setFont(font);
104 oldExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentName);
105
106 // Old experiment name field
107 Text oldExperimentName = new Text(folderGroup, SWT.BORDER);
108 GridData data = new GridData(GridData.FILL_HORIZONTAL);
109 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
110 oldExperimentName.setLayoutData(data);
111 oldExperimentName.setFont(font);
112 oldExperimentName.setText(fExperiment.getName());
113 oldExperimentName.setEnabled(false);
114
115 // New experiment name label
116 Label newExperimentLabel = new Label(folderGroup, SWT.NONE);
117 newExperimentLabel.setFont(font);
118 newExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentNewName);
119
120 // New experiment name entry field
121 fNewExperimentName = new Text(folderGroup, SWT.BORDER);
122 data = new GridData(GridData.FILL_HORIZONTAL);
123 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
124 fNewExperimentName.setLayoutData(data);
125 fNewExperimentName.setFont(font);
126 fNewExperimentName.addListener(SWT.Modify, new Listener() {
127 @Override
128 public void handleEvent(Event event) {
129 validateNewExperimentName();
130 }
131 });
132 }
133
134 private void validateNewExperimentName() {
135
136 String name = fNewExperimentName.getText();
137 IWorkspace workspace = fExperimentFolder.getWorkspace();
138 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
139
140 if ("".equals(name)) { //$NON-NLS-1$
141 updateStatus(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null));
142 return;
143 }
144
145 if (nameStatus.isOK() == false) {
146 updateStatus(nameStatus);
147 return;
148 }
149
150 IPath path = new Path(name);
151 if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) {
152 updateStatus(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null));
153 return;
154 }
155
156 updateStatus(new Status(IStatus.OK, TmfUiPlugin.PLUGIN_ID, "")); //$NON-NLS-1$
157 }
158
159 // ------------------------------------------------------------------------
160 // SelectionStatusDialog
161 // ------------------------------------------------------------------------
162
163 @Override
164 protected void computeResult() {
165 }
166
167 @Override
168 public void create() {
169 super.create();
170 getButton(IDialogConstants.OK_ID).setEnabled(false);
171 }
172
173 @Override
174 protected void okPressed() {
175 IFolder folder = copyExperiment(fNewExperimentName.getText());
176 if (folder == null) {
177 return;
178 }
179 setSelectionResult(new IFolder[] { folder });
180 super.okPressed();
181
182 if (fProject != null) {
183 fProject.refresh();
184 }
185 }
186
187 private IFolder copyExperiment(String newName) {
188
189 IPath oldPath = fExperiment.getResource().getFullPath();
190 final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$
191
192 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
193 @Override
194 public void execute(IProgressMonitor monitor) throws CoreException {
195 try {
196 monitor.beginTask("", 1000); //$NON-NLS-1$
197 if (monitor.isCanceled()) {
198 throw new OperationCanceledException();
199 }
200 fExperiment.getResource().copy(newPath, IResource.FORCE | IResource.SHALLOW, null);
201 if (monitor.isCanceled()) {
202 throw new OperationCanceledException();
203 }
204 } finally {
205 monitor.done();
206 }
207 }
208 };
209 try {
210 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
211 } catch (InterruptedException exception) {
212 return null;
213 } catch (InvocationTargetException exception) {
214 MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
215 return null;
216 } catch (RuntimeException exception) {
217 return null;
218 }
219
220 return fExperiment.getResource();
221 }
222
223 }
This page took 0.034473 seconds and 5 git commands to generate.