1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson
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
10 * Francois Chouinard - Copied and adapted from NewFolderDialog
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
;
15 import java
.lang
.reflect
.InvocationTargetException
;
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
.Activator
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
35 import org
.eclipse
.osgi
.util
.NLS
;
36 import org
.eclipse
.swt
.SWT
;
37 import org
.eclipse
.swt
.graphics
.Font
;
38 import org
.eclipse
.swt
.layout
.GridData
;
39 import org
.eclipse
.swt
.layout
.GridLayout
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.swt
.widgets
.Control
;
42 import org
.eclipse
.swt
.widgets
.Event
;
43 import org
.eclipse
.swt
.widgets
.Label
;
44 import org
.eclipse
.swt
.widgets
.Listener
;
45 import org
.eclipse
.swt
.widgets
.Shell
;
46 import org
.eclipse
.swt
.widgets
.Text
;
47 import org
.eclipse
.ui
.PlatformUI
;
48 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
49 import org
.eclipse
.ui
.dialogs
.SelectionStatusDialog
;
52 * Implementation of the copy experiement dialog box.
55 * @author Francois Chouinard
57 public class CopyExperimentDialog
extends SelectionStatusDialog
{
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
63 private final TmfExperimentElement fExperiment
;
64 private Text fNewExperimentName
;
65 private IFolder fExperimentFolder
;
66 private TmfProjectElement fProject
;
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
74 * @param shell The parent shell
75 * @param experiment The TMF experiment model element
77 public CopyExperimentDialog(Shell shell
, TmfExperimentElement experiment
) {
79 fExperiment
= experiment
;
80 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
81 fExperimentFolder
= folder
.getResource();
82 fProject
= experiment
.getProject();
83 setTitle(Messages
.CopyExperimentDialog_DialogTitle
);
84 setStatusLineAboveButtons(true);
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
93 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
96 protected Control
createDialogArea(Composite parent
) {
97 Composite composite
= (Composite
) super.createDialogArea(parent
);
98 composite
.setLayout(new GridLayout());
99 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
101 createNewExperimentNameGroup(composite
);
105 private void createNewExperimentNameGroup(Composite parent
) {
106 Font font
= parent
.getFont();
107 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
108 GridLayout layout
= new GridLayout();
109 layout
.numColumns
= 2;
110 folderGroup
.setLayout(layout
);
111 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
113 // Old experiment name label
114 Label oldExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
115 oldExperimentLabel
.setFont(font
);
116 oldExperimentLabel
.setText(Messages
.CopyExperimentDialog_ExperimentName
);
118 // Old experiment name field
119 Text oldExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
120 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
121 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
122 oldExperimentName
.setLayoutData(data
);
123 oldExperimentName
.setFont(font
);
124 oldExperimentName
.setText(fExperiment
.getName());
125 oldExperimentName
.setEnabled(false);
127 // New experiment name label
128 Label newExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
129 newExperimentLabel
.setFont(font
);
130 newExperimentLabel
.setText(Messages
.CopyExperimentDialog_ExperimentNewName
);
132 // New experiment name entry field
133 fNewExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
134 data
= new GridData(GridData
.FILL_HORIZONTAL
);
135 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
136 fNewExperimentName
.setLayoutData(data
);
137 fNewExperimentName
.setFont(font
);
138 fNewExperimentName
.addListener(SWT
.Modify
, new Listener() {
140 public void handleEvent(Event event
) {
141 validateNewExperimentName();
146 private void validateNewExperimentName() {
148 String name
= fNewExperimentName
.getText();
149 IWorkspace workspace
= fExperimentFolder
.getWorkspace();
150 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
152 if ("".equals(name
)) { //$NON-NLS-1$
153 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_EmptyNameError
, null));
157 if (!nameStatus
.isOK()) {
158 updateStatus(nameStatus
);
162 IPath path
= new Path(name
);
163 if (fExperimentFolder
.getFolder(path
).exists() || fExperimentFolder
.getFile(path
).exists()) {
164 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_ExistingNameError
, null));
168 updateStatus(new Status(IStatus
.OK
, Activator
.PLUGIN_ID
, "")); //$NON-NLS-1$
171 // ------------------------------------------------------------------------
172 // SelectionStatusDialog
173 // ------------------------------------------------------------------------
176 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
179 protected void computeResult() {
184 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#create()
187 public void create() {
189 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
194 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
197 protected void okPressed() {
198 IFolder folder
= copyExperiment(fNewExperimentName
.getText());
199 if (folder
== null) {
202 setSelectionResult(new IFolder
[] { folder
});
205 if (fProject
!= null) {
210 private IFolder
copyExperiment(final String newName
) {
212 IPath oldPath
= fExperiment
.getResource().getFullPath();
213 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
215 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
217 public void execute(IProgressMonitor monitor
) throws CoreException
{
219 monitor
.beginTask("", 1000); //$NON-NLS-1$
220 if (monitor
.isCanceled()) {
221 throw new OperationCanceledException();
223 // Copy supplementary files first
224 fExperiment
.copySupplementaryFolder(newName
);
225 fExperiment
.getResource().copy(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
226 // Delete any bookmarks file found in copied experiment folder
227 IFolder folder
= fExperimentFolder
.getFolder(newName
);
228 if (folder
.exists()) {
229 for (IResource member
: folder
.members()) {
230 if (TmfExperiment
.class.getCanonicalName().equals(member
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
))) {
231 member
.delete(true, null);
235 if (monitor
.isCanceled()) {
236 throw new OperationCanceledException();
244 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
245 } catch (InterruptedException exception
) {
247 } catch (InvocationTargetException exception
) {
248 MessageDialog
.openError(getShell(), "", NLS
.bind("", exception
.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
250 } catch (RuntimeException exception
) {
254 return fExperiment
.getResource();