1 /*******************************************************************************
2 * Copyright (c) 2011 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
.TmfUiPlugin
;
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 * <b><u>CopyExperimentDialog</u></b>
55 public class CopyExperimentDialog
extends SelectionStatusDialog
{
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
61 private final TmfExperimentElement fExperiment
;
62 private Text fNewExperimentName
;
63 private IFolder fExperimentFolder
;
64 private TmfProjectElement fProject
;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
70 public CopyExperimentDialog(Shell shell
, TmfExperimentElement experiment
) {
72 fExperiment
= experiment
;
73 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
74 fExperimentFolder
= folder
.getResource();
75 fProject
= experiment
.getProject();
76 setTitle(Messages
.CopyExperimentDialog_DialogTitle
);
77 setStatusLineAboveButtons(true);
80 // ------------------------------------------------------------------------
82 // ------------------------------------------------------------------------
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
));
90 createNewExperimentNameGroup(composite
);
94 private void createNewExperimentNameGroup(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
));
102 // Old experiment name label
103 Label oldExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
104 oldExperimentLabel
.setFont(font
);
105 oldExperimentLabel
.setText(Messages
.CopyExperimentDialog_ExperimentName
);
107 // Old experiment name field
108 Text oldExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
109 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
110 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
111 oldExperimentName
.setLayoutData(data
);
112 oldExperimentName
.setFont(font
);
113 oldExperimentName
.setText(fExperiment
.getName());
114 oldExperimentName
.setEnabled(false);
116 // New experiment name label
117 Label newExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
118 newExperimentLabel
.setFont(font
);
119 newExperimentLabel
.setText(Messages
.CopyExperimentDialog_ExperimentNewName
);
121 // New experiment name entry field
122 fNewExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
123 data
= new GridData(GridData
.FILL_HORIZONTAL
);
124 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
125 fNewExperimentName
.setLayoutData(data
);
126 fNewExperimentName
.setFont(font
);
127 fNewExperimentName
.addListener(SWT
.Modify
, new Listener() {
129 public void handleEvent(Event event
) {
130 validateNewExperimentName();
135 private void validateNewExperimentName() {
137 String name
= fNewExperimentName
.getText();
138 IWorkspace workspace
= fExperimentFolder
.getWorkspace();
139 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
141 if ("".equals(name
)) { //$NON-NLS-1$
142 updateStatus(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_EmptyNameError
, null));
146 if (!nameStatus
.isOK()) {
147 updateStatus(nameStatus
);
151 IPath path
= new Path(name
);
152 if (fExperimentFolder
.getFolder(path
).exists() || fExperimentFolder
.getFile(path
).exists()) {
153 updateStatus(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_ExistingNameError
, null));
157 updateStatus(new Status(IStatus
.OK
, TmfUiPlugin
.PLUGIN_ID
, "")); //$NON-NLS-1$
160 // ------------------------------------------------------------------------
161 // SelectionStatusDialog
162 // ------------------------------------------------------------------------
165 protected void computeResult() {
169 public void create() {
171 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
175 protected void okPressed() {
176 IFolder folder
= copyExperiment(fNewExperimentName
.getText());
177 if (folder
== null) {
180 setSelectionResult(new IFolder
[] { folder
});
183 if (fProject
!= null) {
188 private IFolder
copyExperiment(final String newName
) {
190 IPath oldPath
= fExperiment
.getResource().getFullPath();
191 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
193 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
195 public void execute(IProgressMonitor monitor
) throws CoreException
{
197 monitor
.beginTask("", 1000); //$NON-NLS-1$
198 if (monitor
.isCanceled()) {
199 throw new OperationCanceledException();
201 fExperiment
.getResource().copy(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
202 // Delete any bookmarks file found in copied experiment folder
203 IFolder folder
= fExperimentFolder
.getFolder(newName
);
204 if (folder
.exists()) {
205 for (IResource member
: folder
.members()) {
206 if (TmfExperiment
.class.getCanonicalName().equals(member
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
))) {
207 member
.delete(true, null);
211 if (monitor
.isCanceled()) {
212 throw new OperationCanceledException();
220 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
221 } catch (InterruptedException exception
) {
223 } catch (InvocationTargetException exception
) {
224 MessageDialog
.openError(getShell(), "", NLS
.bind("", exception
.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
226 } catch (RuntimeException exception
) {
230 return fExperiment
.getResource();