analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / project / wizards / CopyTraceDialog.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.tracecompass.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.osgi.util.NLS;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.graphics.Font;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Event;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Listener;
40 import org.eclipse.swt.widgets.Shell;
41 import org.eclipse.swt.widgets.Text;
42 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
43 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
44 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
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 trace dialog box.
51 * <p>
52 * @version 1.0
53 * @author Francois Chouinard
54 */
55 public class CopyTraceDialog extends SelectionStatusDialog {
56
57 // ------------------------------------------------------------------------
58 // Members
59 // ------------------------------------------------------------------------
60
61 private final TmfTraceElement fTrace;
62 private Text fNewTraceName;
63 private final IFolder fTraceFolder;
64
65 // ------------------------------------------------------------------------
66 // Constructor
67 // ------------------------------------------------------------------------
68 /**
69 * Constructor.
70 * @param shell The parent shell
71 * @param trace The trace model element.
72 */
73 public CopyTraceDialog(Shell shell, TmfTraceElement trace) {
74 super(shell);
75 fTrace = trace;
76 TmfTraceFolder folder = (TmfTraceFolder) trace.getParent();
77 fTraceFolder = folder.getResource();
78 setTitle(Messages.CopyTraceDialog_DialogTitle);
79 setStatusLineAboveButtons(true);
80 }
81
82 // ------------------------------------------------------------------------
83 // Dialog
84 // ------------------------------------------------------------------------
85
86 @Override
87 protected Control createDialogArea(Composite parent) {
88 Composite composite = (Composite) super.createDialogArea(parent);
89 composite.setLayout(new GridLayout());
90 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
91
92 createNewTraceNameGroup(composite);
93 return composite;
94 }
95
96 private void createNewTraceNameGroup(Composite parent) {
97 Font font = parent.getFont();
98 Composite folderGroup = new Composite(parent, SWT.NONE);
99 GridLayout layout = new GridLayout();
100 layout.numColumns = 2;
101 folderGroup.setLayout(layout);
102 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
103
104 // Old trace name label
105 Label oldTraceLabel = new Label(folderGroup, SWT.NONE);
106 oldTraceLabel.setFont(font);
107 oldTraceLabel.setText(Messages.CopyTraceDialog_TraceName);
108
109 // Old trace name field
110 Text oldTraceName = new Text(folderGroup, SWT.BORDER);
111 GridData data = new GridData(GridData.FILL_HORIZONTAL);
112 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
113 oldTraceName.setLayoutData(data);
114 oldTraceName.setFont(font);
115 oldTraceName.setText(fTrace.getName());
116 oldTraceName.setEnabled(false);
117
118 // New trace name label
119 Label newTraceLabel = new Label(folderGroup, SWT.NONE);
120 newTraceLabel.setFont(font);
121 newTraceLabel.setText(Messages.CopyTraceDialog_TraceNewName);
122
123 // New trace name entry field
124 fNewTraceName = new Text(folderGroup, SWT.BORDER);
125 data = new GridData(GridData.FILL_HORIZONTAL);
126 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
127 fNewTraceName.setLayoutData(data);
128 fNewTraceName.setFont(font);
129 fNewTraceName.addListener(SWT.Modify, new Listener() {
130 @Override
131 public void handleEvent(Event event) {
132 validateNewTraceName();
133 }
134 });
135 }
136
137 private void validateNewTraceName() {
138
139 String name = fNewTraceName.getText();
140 IWorkspace workspace = fTraceFolder.getWorkspace();
141 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
142
143 if ("".equals(name)) { //$NON-NLS-1$
144 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
145 Messages.Dialog_EmptyNameError, null));
146 return;
147 }
148
149 if (!nameStatus.isOK()) {
150 updateStatus(nameStatus);
151 return;
152 }
153
154 IPath path = new Path(name);
155 if (fTraceFolder.getFolder(path).exists() || fTraceFolder.getFile(path).exists()) {
156 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
157 Messages.Dialog_ExistingNameError, null));
158 return;
159 }
160
161 updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$
162 }
163
164 // ------------------------------------------------------------------------
165 // SelectionStatusDialog
166 // ------------------------------------------------------------------------
167
168 @Override
169 protected void computeResult() {
170 }
171
172 @Override
173 public void create() {
174 super.create();
175 getButton(IDialogConstants.OK_ID).setEnabled(false);
176 }
177
178 @Override
179 protected void okPressed() {
180 IResource trace = copyTrace(fNewTraceName.getText());
181 if (trace == null) {
182 return;
183 }
184 setSelectionResult(new IResource[] { trace });
185 super.okPressed();
186 }
187
188 private IResource copyTrace(final String newName) {
189
190 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
191 @Override
192 public void execute(IProgressMonitor monitor) throws CoreException {
193 try {
194 monitor.beginTask("", 1000); //$NON-NLS-1$
195 if (monitor.isCanceled()) {
196 throw new OperationCanceledException();
197 }
198 fTrace.copy(newName, true);
199 if (monitor.isCanceled()) {
200 throw new OperationCanceledException();
201 }
202 } finally {
203 monitor.done();
204 }
205 }
206 };
207
208 try {
209 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
210 } catch (InterruptedException exception) {
211 return null;
212 } catch (InvocationTargetException exception) {
213 MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
214 return null;
215 } catch (RuntimeException exception) {
216 return null;
217 }
218
219 return fTrace.getResource();
220
221 }
222
223 }
This page took 0.036217 seconds and 5 git commands to generate.