Fix some findbugs
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / CopyTraceDialog.java
CommitLineData
12c155f5
FC
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
13package org.eclipse.linuxtools.tmf.ui.project.wizards;
14
15import java.lang.reflect.InvocationTargetException;
16
3453cab5 17import org.eclipse.core.resources.IFolder;
12c155f5
FC
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;
d34665f9 29import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin;
e12ecd30 30import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
3453cab5 31import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
12c155f5
FC
32import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
33import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
3453cab5 34import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
12c155f5
FC
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/**
52 * <b><u>CopyExperimentDialog</u></b>
53 * <p>
54 */
55public class CopyTraceDialog extends SelectionStatusDialog {
56
57 // ------------------------------------------------------------------------
58 // Members
59 // ------------------------------------------------------------------------
60
61 private final TmfTraceElement fTrace;
62 private Text fNewTraceName;
3453cab5 63 private IFolder fTraceFolder;
12c155f5
FC
64 private TmfProjectElement fProject;
65
66 // ------------------------------------------------------------------------
67 // Constructor
68 // ------------------------------------------------------------------------
69
70 public CopyTraceDialog(Shell shell, TmfTraceElement trace) {
71 super(shell);
72 fTrace = trace;
3453cab5
PT
73 TmfTraceFolder folder = (TmfTraceFolder) trace.getParent();
74 fTraceFolder = folder.getResource();
12c155f5
FC
75 fProject = trace.getProject();
76 setTitle(Messages.CopyTraceDialog_DialogTitle);
77 setStatusLineAboveButtons(true);
78 }
79
80 // ------------------------------------------------------------------------
81 // Dialog
82 // ------------------------------------------------------------------------
83
84 @Override
85 protected Control createDialogArea(Composite parent) {
86 Composite composite = (Composite) super.createDialogArea(parent);
87 composite.setLayout(new GridLayout());
88 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
89
90 createNewTraceNameGroup(composite);
91 return composite;
92 }
93
94 private void createNewTraceNameGroup(Composite parent) {
95 Font font = parent.getFont();
96 Composite folderGroup = new Composite(parent, SWT.NONE);
97 GridLayout layout = new GridLayout();
98 layout.numColumns = 2;
99 folderGroup.setLayout(layout);
100 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
101
102 // Old trace name label
103 Label oldTraceLabel = new Label(folderGroup, SWT.NONE);
104 oldTraceLabel.setFont(font);
105 oldTraceLabel.setText(Messages.CopyTraceDialog_TraceName);
106
107 // Old trace name field
108 Text oldTraceName = new Text(folderGroup, SWT.BORDER);
109 GridData data = new GridData(GridData.FILL_HORIZONTAL);
110 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
111 oldTraceName.setLayoutData(data);
112 oldTraceName.setFont(font);
113 oldTraceName.setText(fTrace.getName());
114 oldTraceName.setEnabled(false);
115
116 // New trace name label
117 Label newTraceLabel = new Label(folderGroup, SWT.NONE);
118 newTraceLabel.setFont(font);
119 newTraceLabel.setText(Messages.CopyTraceDialog_TraceNewName);
120
121 // New trace name entry field
122 fNewTraceName = new Text(folderGroup, SWT.BORDER);
123 data = new GridData(GridData.FILL_HORIZONTAL);
124 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
125 fNewTraceName.setLayoutData(data);
126 fNewTraceName.setFont(font);
127 fNewTraceName.addListener(SWT.Modify, new Listener() {
128 @Override
129 public void handleEvent(Event event) {
130 validateNewTraceName();
131 }
132 });
133 }
134
135 private void validateNewTraceName() {
136
137 String name = fNewTraceName.getText();
138 IWorkspace workspace = fTraceFolder.getWorkspace();
139 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
140
141 if ("".equals(name)) { //$NON-NLS-1$
142 updateStatus(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, IStatus.ERROR,
143 Messages.Dialog_EmptyNameError, null));
144 return;
145 }
146
147 if (nameStatus.isOK() == false) {
148 updateStatus(nameStatus);
149 return;
150 }
151
152 IPath path = new Path(name);
153 if (fTraceFolder.getFolder(path).exists() || fTraceFolder.getFile(path).exists()) {
154 updateStatus(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, IStatus.ERROR,
155 Messages.Dialog_ExistingNameError, null));
156 return;
157 }
158
159 updateStatus(new Status(IStatus.OK, TmfUiPlugin.PLUGIN_ID, "")); //$NON-NLS-1$
160 }
161
162 // ------------------------------------------------------------------------
163 // SelectionStatusDialog
164 // ------------------------------------------------------------------------
165
166 @Override
167 protected void computeResult() {
168 }
169
170 @Override
171 public void create() {
172 super.create();
173 getButton(IDialogConstants.OK_ID).setEnabled(false);
174 }
175
176 @Override
177 protected void okPressed() {
178 IResource trace = copyTrace(fNewTraceName.getText());
179 if (trace == null) {
180 return;
181 }
182 setSelectionResult(new IResource[] { trace });
183 super.okPressed();
184
185 if (fProject != null) {
186 fProject.refresh();
187 }
188 }
189
3453cab5 190 private IResource copyTrace(final String newName) {
12c155f5
FC
191
192 IPath oldPath = fTrace.getResource().getFullPath();
193 final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$
194
195 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
196 @Override
197 public void execute(IProgressMonitor monitor) throws CoreException {
198 try {
199 monitor.beginTask("", 1000); //$NON-NLS-1$
200 if (monitor.isCanceled()) {
201 throw new OperationCanceledException();
202 }
5e4bf87d 203 // Copy supplementary files first
e12ecd30 204 fTrace.copySupplementaryFolder(newName);
5e4bf87d 205 // Copy the trace
12c155f5 206 fTrace.getResource().copy(newPath, IResource.FORCE | IResource.SHALLOW, null);
3453cab5
PT
207 // Delete any bookmarks file found in copied trace folder
208 IFolder folder = fTraceFolder.getFolder(newName);
209 if (folder.exists()) {
210 for (IResource member : folder.members()) {
e12ecd30 211 if (TmfTrace.class.getCanonicalName().equals(member.getPersistentProperty(TmfCommonConstants.TRACETYPE))) {
3453cab5
PT
212 member.delete(true, null);
213 }
214 }
215 }
12c155f5
FC
216 if (monitor.isCanceled()) {
217 throw new OperationCanceledException();
218 }
219 } finally {
220 monitor.done();
221 }
222 }
223 };
224
225 try {
226 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
227 } catch (InterruptedException exception) {
228 return null;
229 } catch (InvocationTargetException exception) {
230 MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
231 return null;
232 } catch (RuntimeException exception) {
233 return null;
234 }
235
236 return fTrace.getResource();
237 }
238
239}
This page took 0.039648 seconds and 5 git commands to generate.