Internalize some more TMF APIs
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / CopyTraceDialog.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.IFolder;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.IWorkspace;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.OperationCanceledException;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.core.runtime.Status;
27 import org.eclipse.jface.dialogs.IDialogConstants;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
30 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
31 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
32 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
33 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
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 CopyTraceDialog extends SelectionStatusDialog {
55
56 // ------------------------------------------------------------------------
57 // Members
58 // ------------------------------------------------------------------------
59
60 private final TmfTraceElement fTrace;
61 private Text fNewTraceName;
62 private IFolder fTraceFolder;
63 private TmfProjectElement fProject;
64
65 // ------------------------------------------------------------------------
66 // Constructor
67 // ------------------------------------------------------------------------
68
69 public CopyTraceDialog(Shell shell, TmfTraceElement trace) {
70 super(shell);
71 fTrace = trace;
72 TmfTraceFolder folder = (TmfTraceFolder) trace.getParent();
73 fTraceFolder = folder.getResource();
74 fProject = trace.getProject();
75 setTitle(Messages.CopyTraceDialog_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 createNewTraceNameGroup(composite);
90 return composite;
91 }
92
93 private void createNewTraceNameGroup(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 trace name label
102 Label oldTraceLabel = new Label(folderGroup, SWT.NONE);
103 oldTraceLabel.setFont(font);
104 oldTraceLabel.setText(Messages.CopyTraceDialog_TraceName);
105
106 // Old trace name field
107 Text oldTraceName = new Text(folderGroup, SWT.BORDER);
108 GridData data = new GridData(GridData.FILL_HORIZONTAL);
109 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
110 oldTraceName.setLayoutData(data);
111 oldTraceName.setFont(font);
112 oldTraceName.setText(fTrace.getName());
113 oldTraceName.setEnabled(false);
114
115 // New trace name label
116 Label newTraceLabel = new Label(folderGroup, SWT.NONE);
117 newTraceLabel.setFont(font);
118 newTraceLabel.setText(Messages.CopyTraceDialog_TraceNewName);
119
120 // New trace name entry field
121 fNewTraceName = new Text(folderGroup, SWT.BORDER);
122 data = new GridData(GridData.FILL_HORIZONTAL);
123 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
124 fNewTraceName.setLayoutData(data);
125 fNewTraceName.setFont(font);
126 fNewTraceName.addListener(SWT.Modify, new Listener() {
127 @Override
128 public void handleEvent(Event event) {
129 validateNewTraceName();
130 }
131 });
132 }
133
134 private void validateNewTraceName() {
135
136 String name = fNewTraceName.getText();
137 IWorkspace workspace = fTraceFolder.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,
142 Messages.Dialog_EmptyNameError, null));
143 return;
144 }
145
146 if (nameStatus.isOK() == false) {
147 updateStatus(nameStatus);
148 return;
149 }
150
151 IPath path = new Path(name);
152 if (fTraceFolder.getFolder(path).exists() || fTraceFolder.getFile(path).exists()) {
153 updateStatus(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, IStatus.ERROR,
154 Messages.Dialog_ExistingNameError, null));
155 return;
156 }
157
158 updateStatus(new Status(IStatus.OK, TmfUiPlugin.PLUGIN_ID, "")); //$NON-NLS-1$
159 }
160
161 // ------------------------------------------------------------------------
162 // SelectionStatusDialog
163 // ------------------------------------------------------------------------
164
165 @Override
166 protected void computeResult() {
167 }
168
169 @Override
170 public void create() {
171 super.create();
172 getButton(IDialogConstants.OK_ID).setEnabled(false);
173 }
174
175 @Override
176 protected void okPressed() {
177 IResource trace = copyTrace(fNewTraceName.getText());
178 if (trace == null) {
179 return;
180 }
181 setSelectionResult(new IResource[] { trace });
182 super.okPressed();
183
184 if (fProject != null) {
185 fProject.refresh();
186 }
187 }
188
189 private IResource copyTrace(final String newName) {
190
191 IPath oldPath = fTrace.getResource().getFullPath();
192 final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$
193
194 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
195 @Override
196 public void execute(IProgressMonitor monitor) throws CoreException {
197 try {
198 monitor.beginTask("", 1000); //$NON-NLS-1$
199 if (monitor.isCanceled()) {
200 throw new OperationCanceledException();
201 }
202 fTrace.getResource().copy(newPath, IResource.FORCE | IResource.SHALLOW, null);
203 // Delete any bookmarks file found in copied trace folder
204 IFolder folder = fTraceFolder.getFolder(newName);
205 if (folder.exists()) {
206 for (IResource member : folder.members()) {
207 if (TmfTrace.class.getCanonicalName().equals(member.getPersistentProperty(TmfTraceElement.TRACETYPE))) {
208 member.delete(true, null);
209 }
210 }
211 }
212 if (monitor.isCanceled()) {
213 throw new OperationCanceledException();
214 }
215 } finally {
216 monitor.done();
217 }
218 }
219 };
220
221 try {
222 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
223 } catch (InterruptedException exception) {
224 return null;
225 } catch (InvocationTargetException exception) {
226 MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
227 return null;
228 } catch (RuntimeException exception) {
229 return null;
230 }
231
232 return fTrace.getResource();
233 }
234
235 }
This page took 0.036625 seconds and 5 git commands to generate.