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
.osgi
.util
.NLS
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.graphics
.Font
;
36 import org
.eclipse
.swt
.layout
.GridData
;
37 import org
.eclipse
.swt
.layout
.GridLayout
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Control
;
40 import org
.eclipse
.swt
.widgets
.Event
;
41 import org
.eclipse
.swt
.widgets
.Label
;
42 import org
.eclipse
.swt
.widgets
.Listener
;
43 import org
.eclipse
.swt
.widgets
.Shell
;
44 import org
.eclipse
.swt
.widgets
.Text
;
45 import org
.eclipse
.ui
.PlatformUI
;
46 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
47 import org
.eclipse
.ui
.dialogs
.SelectionStatusDialog
;
50 * 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
;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
76 * The TMF experiment model element
78 public CopyExperimentDialog(Shell shell
, TmfExperimentElement experiment
) {
80 fExperiment
= experiment
;
81 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
82 fExperimentFolder
= folder
.getResource();
83 setTitle(Messages
.CopyExperimentDialog_DialogTitle
);
84 setStatusLineAboveButtons(true);
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
92 protected Control
createDialogArea(Composite parent
) {
93 Composite composite
= (Composite
) super.createDialogArea(parent
);
94 composite
.setLayout(new GridLayout());
95 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
97 createNewExperimentNameGroup(composite
);
101 private void createNewExperimentNameGroup(Composite parent
) {
102 Font font
= parent
.getFont();
103 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
104 GridLayout layout
= new GridLayout();
105 layout
.numColumns
= 2;
106 folderGroup
.setLayout(layout
);
107 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
109 // Old experiment name label
110 Label oldExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
111 oldExperimentLabel
.setFont(font
);
112 oldExperimentLabel
.setText(Messages
.CopyExperimentDialog_ExperimentName
);
114 // Old experiment name field
115 Text oldExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
116 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
117 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
118 oldExperimentName
.setLayoutData(data
);
119 oldExperimentName
.setFont(font
);
120 oldExperimentName
.setText(fExperiment
.getName());
121 oldExperimentName
.setEnabled(false);
123 // New experiment name label
124 Label newExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
125 newExperimentLabel
.setFont(font
);
126 newExperimentLabel
.setText(Messages
.CopyExperimentDialog_ExperimentNewName
);
128 // New experiment name entry field
129 fNewExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
130 data
= new GridData(GridData
.FILL_HORIZONTAL
);
131 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
132 fNewExperimentName
.setLayoutData(data
);
133 fNewExperimentName
.setFont(font
);
134 fNewExperimentName
.addListener(SWT
.Modify
, new Listener() {
136 public void handleEvent(Event event
) {
137 validateNewExperimentName();
142 private void validateNewExperimentName() {
144 String name
= fNewExperimentName
.getText();
145 IWorkspace workspace
= fExperimentFolder
.getWorkspace();
146 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
148 if ("".equals(name
)) { //$NON-NLS-1$
149 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_EmptyNameError
, null));
153 if (!nameStatus
.isOK()) {
154 updateStatus(nameStatus
);
158 IPath path
= new Path(name
);
159 if (fExperimentFolder
.getFolder(path
).exists() || fExperimentFolder
.getFile(path
).exists()) {
160 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_ExistingNameError
, null));
164 updateStatus(new Status(IStatus
.OK
, Activator
.PLUGIN_ID
, "")); //$NON-NLS-1$
167 // ------------------------------------------------------------------------
168 // SelectionStatusDialog
169 // ------------------------------------------------------------------------
172 protected void computeResult() {
176 public void create() {
178 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
182 protected void okPressed() {
183 IFolder folder
= copyExperiment(fNewExperimentName
.getText());
184 if (folder
== null) {
187 setSelectionResult(new IFolder
[] { folder
});
191 private IFolder
copyExperiment(final String newName
) {
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();
202 fExperiment
.copy(newName
, true);
204 if (monitor
.isCanceled()) {
205 throw new OperationCanceledException();
213 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
214 } catch (InterruptedException exception
) {
216 } catch (InvocationTargetException exception
) {
217 MessageDialog
.openError(getShell(), "", NLS
.bind("", exception
.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
219 } catch (RuntimeException exception
) {
223 return fExperiment
.getResource();