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
.trace
.TmfTrace
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
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 * <b><u>CopyExperimentDialog</u></b>
54 public class CopyTraceDialog
extends SelectionStatusDialog
{
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 private final TmfTraceElement fTrace
;
61 private Text fNewTraceName
;
62 private IFolder fTraceFolder
;
63 private TmfProjectElement fProject
;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
69 public CopyTraceDialog(Shell shell
, TmfTraceElement trace
) {
72 TmfTraceFolder folder
= (TmfTraceFolder
) trace
.getParent();
73 fTraceFolder
= folder
.getResource();
74 fProject
= trace
.getProject();
75 setTitle(Messages
.CopyTraceDialog_DialogTitle
);
76 setStatusLineAboveButtons(true);
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
84 protected Control
createDialogArea(Composite parent
) {
85 Composite composite
= (Composite
) super.createDialogArea(parent
);
86 composite
.setLayout(new GridLayout());
87 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
89 createNewTraceNameGroup(composite
);
93 private void createNewTraceNameGroup(Composite parent
) {
94 Font font
= parent
.getFont();
95 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
96 GridLayout layout
= new GridLayout();
97 layout
.numColumns
= 2;
98 folderGroup
.setLayout(layout
);
99 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
101 // Old trace name label
102 Label oldTraceLabel
= new Label(folderGroup
, SWT
.NONE
);
103 oldTraceLabel
.setFont(font
);
104 oldTraceLabel
.setText(Messages
.CopyTraceDialog_TraceName
);
106 // Old trace name field
107 Text oldTraceName
= new Text(folderGroup
, SWT
.BORDER
);
108 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
109 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
110 oldTraceName
.setLayoutData(data
);
111 oldTraceName
.setFont(font
);
112 oldTraceName
.setText(fTrace
.getName());
113 oldTraceName
.setEnabled(false);
115 // New trace name label
116 Label newTraceLabel
= new Label(folderGroup
, SWT
.NONE
);
117 newTraceLabel
.setFont(font
);
118 newTraceLabel
.setText(Messages
.CopyTraceDialog_TraceNewName
);
120 // New trace name entry field
121 fNewTraceName
= new Text(folderGroup
, SWT
.BORDER
);
122 data
= new GridData(GridData
.FILL_HORIZONTAL
);
123 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
124 fNewTraceName
.setLayoutData(data
);
125 fNewTraceName
.setFont(font
);
126 fNewTraceName
.addListener(SWT
.Modify
, new Listener() {
128 public void handleEvent(Event event
) {
129 validateNewTraceName();
134 private void validateNewTraceName() {
136 String name
= fNewTraceName
.getText();
137 IWorkspace workspace
= fTraceFolder
.getWorkspace();
138 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
140 if ("".equals(name
)) { //$NON-NLS-1$
141 updateStatus(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, IStatus
.ERROR
,
142 Messages
.Dialog_EmptyNameError
, null));
146 if (nameStatus
.isOK() == false) {
147 updateStatus(nameStatus
);
151 IPath path
= new Path(name
);
152 if (fTraceFolder
.getFolder(path
).exists() || fTraceFolder
.getFile(path
).exists()) {
153 updateStatus(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, IStatus
.ERROR
,
154 Messages
.Dialog_ExistingNameError
, null));
158 updateStatus(new Status(IStatus
.OK
, TmfUiPlugin
.PLUGIN_ID
, "")); //$NON-NLS-1$
161 // ------------------------------------------------------------------------
162 // SelectionStatusDialog
163 // ------------------------------------------------------------------------
166 protected void computeResult() {
170 public void create() {
172 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
176 protected void okPressed() {
177 IResource trace
= copyTrace(fNewTraceName
.getText());
181 setSelectionResult(new IResource
[] { trace
});
184 if (fProject
!= null) {
189 private IResource
copyTrace(final String newName
) {
191 IPath oldPath
= fTrace
.getResource().getFullPath();
192 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
194 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
196 public void execute(IProgressMonitor monitor
) throws CoreException
{
198 monitor
.beginTask("", 1000); //$NON-NLS-1$
199 if (monitor
.isCanceled()) {
200 throw new OperationCanceledException();
202 fTrace
.getResource().copy(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
203 // Delete any bookmarks file found in copied trace folder
204 IFolder folder
= fTraceFolder
.getFolder(newName
);
205 if (folder
.exists()) {
206 for (IResource member
: folder
.members()) {
207 if (TmfTrace
.class.getCanonicalName().equals(member
.getPersistentProperty(TmfTraceElement
.TRACETYPE
))) {
208 member
.delete(true, null);
212 if (monitor
.isCanceled()) {
213 throw new OperationCanceledException();
222 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
223 } catch (InterruptedException exception
) {
225 } catch (InvocationTargetException exception
) {
226 MessageDialog
.openError(getShell(), "", NLS
.bind("", exception
.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
228 } catch (RuntimeException exception
) {
232 return fTrace
.getResource();