1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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
.tracecompass
.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
.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
;
50 * Implementation of the copy trace dialog box.
53 * @author Francois Chouinard
55 public class CopyTraceDialog
extends SelectionStatusDialog
{
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
61 private final TmfTraceElement fTrace
;
62 private Text fNewTraceName
;
63 private final IFolder fTraceFolder
;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 * @param shell The parent shell
71 * @param trace The trace model element.
73 public CopyTraceDialog(Shell shell
, TmfTraceElement trace
) {
76 TmfTraceFolder folder
= (TmfTraceFolder
) trace
.getParent();
77 fTraceFolder
= folder
.getResource();
78 setTitle(Messages
.CopyTraceDialog_DialogTitle
);
79 setStatusLineAboveButtons(true);
82 // ------------------------------------------------------------------------
84 // ------------------------------------------------------------------------
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
));
92 createNewTraceNameGroup(composite
);
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
));
104 // Old trace name label
105 Label oldTraceLabel
= new Label(folderGroup
, SWT
.NONE
);
106 oldTraceLabel
.setFont(font
);
107 oldTraceLabel
.setText(Messages
.CopyTraceDialog_TraceName
);
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);
118 // New trace name label
119 Label newTraceLabel
= new Label(folderGroup
, SWT
.NONE
);
120 newTraceLabel
.setFont(font
);
121 newTraceLabel
.setText(Messages
.CopyTraceDialog_TraceNewName
);
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() {
131 public void handleEvent(Event event
) {
132 validateNewTraceName();
137 private void validateNewTraceName() {
139 String name
= fNewTraceName
.getText();
140 IWorkspace workspace
= fTraceFolder
.getWorkspace();
141 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
143 if ("".equals(name
)) { //$NON-NLS-1$
144 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
,
145 Messages
.Dialog_EmptyNameError
, null));
149 if (!nameStatus
.isOK()) {
150 updateStatus(nameStatus
);
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));
161 updateStatus(new Status(IStatus
.OK
, Activator
.PLUGIN_ID
, "")); //$NON-NLS-1$
164 // ------------------------------------------------------------------------
165 // SelectionStatusDialog
166 // ------------------------------------------------------------------------
169 protected void computeResult() {
173 public void create() {
175 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
179 protected void okPressed() {
180 IResource trace
= copyTrace(fNewTraceName
.getText());
184 setSelectionResult(new IResource
[] { trace
});
188 private IResource
copyTrace(final String newName
) {
190 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
192 public void execute(IProgressMonitor monitor
) throws CoreException
{
194 monitor
.beginTask("", 1000); //$NON-NLS-1$
195 if (monitor
.isCanceled()) {
196 throw new OperationCanceledException();
198 fTrace
.copy(newName
, true);
199 if (monitor
.isCanceled()) {
200 throw new OperationCanceledException();
209 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
210 } catch (InterruptedException exception
) {
212 } catch (InvocationTargetException exception
) {
213 MessageDialog
.openError(getShell(), "", NLS
.bind("", exception
.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
215 } catch (RuntimeException exception
) {
219 return fTrace
.getResource();