1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, École Polytechnique de Montréal
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 * Geneviève Bastien - Moved the actual copy code to model element's class
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
;
16 import java
.lang
.reflect
.InvocationTargetException
;
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
.linuxtools
.internal
.tmf
.ui
.Activator
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
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
;
51 * Implementation of the copy experiement dialog box.
54 * @author Francois Chouinard
56 public class CopyExperimentDialog
extends SelectionStatusDialog
{
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 private final TmfExperimentElement fExperiment
;
63 private Text fNewExperimentName
;
64 private IFolder fExperimentFolder
;
65 private TmfProjectElement fProject
;
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
73 * @param shell The parent shell
74 * @param experiment The TMF experiment model element
76 public CopyExperimentDialog(Shell shell
, TmfExperimentElement experiment
) {
78 fExperiment
= experiment
;
79 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
80 fExperimentFolder
= folder
.getResource();
81 fProject
= experiment
.getProject();
82 setTitle(Messages
.CopyExperimentDialog_DialogTitle
);
83 setStatusLineAboveButtons(true);
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
92 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
95 protected Control
createDialogArea(Composite parent
) {
96 Composite composite
= (Composite
) super.createDialogArea(parent
);
97 composite
.setLayout(new GridLayout());
98 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
100 createNewExperimentNameGroup(composite
);
104 private void createNewExperimentNameGroup(Composite parent
) {
105 Font font
= parent
.getFont();
106 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
107 GridLayout layout
= new GridLayout();
108 layout
.numColumns
= 2;
109 folderGroup
.setLayout(layout
);
110 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
112 // Old experiment name label
113 Label oldExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
114 oldExperimentLabel
.setFont(font
);
115 oldExperimentLabel
.setText(Messages
.CopyExperimentDialog_ExperimentName
);
117 // Old experiment name field
118 Text oldExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
119 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
120 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
121 oldExperimentName
.setLayoutData(data
);
122 oldExperimentName
.setFont(font
);
123 oldExperimentName
.setText(fExperiment
.getName());
124 oldExperimentName
.setEnabled(false);
126 // New experiment name label
127 Label newExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
128 newExperimentLabel
.setFont(font
);
129 newExperimentLabel
.setText(Messages
.CopyExperimentDialog_ExperimentNewName
);
131 // New experiment name entry field
132 fNewExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
133 data
= new GridData(GridData
.FILL_HORIZONTAL
);
134 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
135 fNewExperimentName
.setLayoutData(data
);
136 fNewExperimentName
.setFont(font
);
137 fNewExperimentName
.addListener(SWT
.Modify
, new Listener() {
139 public void handleEvent(Event event
) {
140 validateNewExperimentName();
145 private void validateNewExperimentName() {
147 String name
= fNewExperimentName
.getText();
148 IWorkspace workspace
= fExperimentFolder
.getWorkspace();
149 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
151 if ("".equals(name
)) { //$NON-NLS-1$
152 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_EmptyNameError
, null));
156 if (!nameStatus
.isOK()) {
157 updateStatus(nameStatus
);
161 IPath path
= new Path(name
);
162 if (fExperimentFolder
.getFolder(path
).exists() || fExperimentFolder
.getFile(path
).exists()) {
163 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_ExistingNameError
, null));
167 updateStatus(new Status(IStatus
.OK
, Activator
.PLUGIN_ID
, "")); //$NON-NLS-1$
170 // ------------------------------------------------------------------------
171 // SelectionStatusDialog
172 // ------------------------------------------------------------------------
175 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
178 protected void computeResult() {
183 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#create()
186 public void create() {
188 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
193 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
196 protected void okPressed() {
197 IFolder folder
= copyExperiment(fNewExperimentName
.getText());
198 if (folder
== null) {
201 setSelectionResult(new IFolder
[] { folder
});
204 if (fProject
!= null) {
209 private IFolder
copyExperiment(final String newName
) {
211 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
213 public void execute(IProgressMonitor monitor
) throws CoreException
{
215 monitor
.beginTask("", 1000); //$NON-NLS-1$
216 if (monitor
.isCanceled()) {
217 throw new OperationCanceledException();
220 fExperiment
.copy(newName
, true);
222 if (monitor
.isCanceled()) {
223 throw new OperationCanceledException();
231 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
232 } catch (InterruptedException exception
) {
234 } catch (InvocationTargetException exception
) {
235 MessageDialog
.openError(getShell(), "", NLS
.bind("", exception
.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
237 } catch (RuntimeException exception
) {
241 return fExperiment
.getResource();