Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal |
6256d8ad | 3 | * |
12c155f5 FC |
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 | |
6256d8ad | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Copied and adapted from NewFolderDialog | |
beb19106 | 11 | * Geneviève Bastien - Moved the actual copy code to model element's class |
12c155f5 FC |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.tmf.ui.project.wizards; | |
15 | ||
16 | import java.lang.reflect.InvocationTargetException; | |
17 | ||
3453cab5 | 18 | import org.eclipse.core.resources.IFolder; |
12c155f5 FC |
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; | |
8fd82db5 | 30 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
12c155f5 | 31 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
3453cab5 | 32 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
12c155f5 FC |
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; | |
48 | ||
49 | /** | |
b544077e | 50 | * Implementation of the copy trace dialog box. |
12c155f5 | 51 | * <p> |
b544077e BH |
52 | * @version 1.0 |
53 | * @author Francois Chouinard | |
12c155f5 FC |
54 | */ |
55 | public class CopyTraceDialog extends SelectionStatusDialog { | |
56 | ||
57 | // ------------------------------------------------------------------------ | |
58 | // Members | |
59 | // ------------------------------------------------------------------------ | |
60 | ||
61 | private final TmfTraceElement fTrace; | |
62 | private Text fNewTraceName; | |
6256d8ad | 63 | private final IFolder fTraceFolder; |
12c155f5 FC |
64 | |
65 | // ------------------------------------------------------------------------ | |
66 | // Constructor | |
67 | // ------------------------------------------------------------------------ | |
b544077e BH |
68 | /** |
69 | * Constructor. | |
70 | * @param shell The parent shell | |
71 | * @param trace The trace model element. | |
72 | */ | |
12c155f5 FC |
73 | public CopyTraceDialog(Shell shell, TmfTraceElement trace) { |
74 | super(shell); | |
75 | fTrace = trace; | |
3453cab5 PT |
76 | TmfTraceFolder folder = (TmfTraceFolder) trace.getParent(); |
77 | fTraceFolder = folder.getResource(); | |
12c155f5 FC |
78 | setTitle(Messages.CopyTraceDialog_DialogTitle); |
79 | setStatusLineAboveButtons(true); | |
80 | } | |
81 | ||
82 | // ------------------------------------------------------------------------ | |
83 | // Dialog | |
84 | // ------------------------------------------------------------------------ | |
11252342 | 85 | |
12c155f5 FC |
86 | @Override |
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)); | |
91 | ||
92 | createNewTraceNameGroup(composite); | |
93 | return composite; | |
94 | } | |
95 | ||
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)); | |
103 | ||
104 | // Old trace name label | |
105 | Label oldTraceLabel = new Label(folderGroup, SWT.NONE); | |
106 | oldTraceLabel.setFont(font); | |
107 | oldTraceLabel.setText(Messages.CopyTraceDialog_TraceName); | |
108 | ||
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); | |
117 | ||
118 | // New trace name label | |
119 | Label newTraceLabel = new Label(folderGroup, SWT.NONE); | |
120 | newTraceLabel.setFont(font); | |
121 | newTraceLabel.setText(Messages.CopyTraceDialog_TraceNewName); | |
122 | ||
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() { | |
130 | @Override | |
131 | public void handleEvent(Event event) { | |
132 | validateNewTraceName(); | |
133 | } | |
134 | }); | |
135 | } | |
136 | ||
137 | private void validateNewTraceName() { | |
138 | ||
139 | String name = fNewTraceName.getText(); | |
140 | IWorkspace workspace = fTraceFolder.getWorkspace(); | |
141 | IStatus nameStatus = workspace.validateName(name, IResource.FOLDER); | |
142 | ||
143 | if ("".equals(name)) { //$NON-NLS-1$ | |
8fd82db5 | 144 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, |
12c155f5 FC |
145 | Messages.Dialog_EmptyNameError, null)); |
146 | return; | |
147 | } | |
148 | ||
9fa32496 | 149 | if (!nameStatus.isOK()) { |
12c155f5 FC |
150 | updateStatus(nameStatus); |
151 | return; | |
152 | } | |
153 | ||
154 | IPath path = new Path(name); | |
155 | if (fTraceFolder.getFolder(path).exists() || fTraceFolder.getFile(path).exists()) { | |
8fd82db5 | 156 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, |
12c155f5 FC |
157 | Messages.Dialog_ExistingNameError, null)); |
158 | return; | |
159 | } | |
160 | ||
8fd82db5 | 161 | updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$ |
12c155f5 FC |
162 | } |
163 | ||
164 | // ------------------------------------------------------------------------ | |
165 | // SelectionStatusDialog | |
166 | // ------------------------------------------------------------------------ | |
167 | ||
168 | @Override | |
169 | protected void computeResult() { | |
170 | } | |
6256d8ad | 171 | |
12c155f5 FC |
172 | @Override |
173 | public void create() { | |
174 | super.create(); | |
175 | getButton(IDialogConstants.OK_ID).setEnabled(false); | |
176 | } | |
177 | ||
178 | @Override | |
179 | protected void okPressed() { | |
180 | IResource trace = copyTrace(fNewTraceName.getText()); | |
181 | if (trace == null) { | |
182 | return; | |
183 | } | |
184 | setSelectionResult(new IResource[] { trace }); | |
185 | super.okPressed(); | |
12c155f5 FC |
186 | } |
187 | ||
3453cab5 | 188 | private IResource copyTrace(final String newName) { |
12c155f5 | 189 | |
12c155f5 FC |
190 | WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { |
191 | @Override | |
192 | public void execute(IProgressMonitor monitor) throws CoreException { | |
193 | try { | |
194 | monitor.beginTask("", 1000); //$NON-NLS-1$ | |
195 | if (monitor.isCanceled()) { | |
196 | throw new OperationCanceledException(); | |
197 | } | |
beb19106 | 198 | fTrace.copy(newName, true); |
12c155f5 FC |
199 | if (monitor.isCanceled()) { |
200 | throw new OperationCanceledException(); | |
201 | } | |
202 | } finally { | |
203 | monitor.done(); | |
204 | } | |
205 | } | |
206 | }; | |
207 | ||
208 | try { | |
209 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
210 | } catch (InterruptedException exception) { | |
211 | return null; | |
212 | } catch (InvocationTargetException exception) { | |
213 | MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$ | |
214 | return null; | |
215 | } catch (RuntimeException exception) { | |
216 | return null; | |
217 | } | |
218 | ||
219 | return fTrace.getResource(); | |
beb19106 | 220 | |
12c155f5 FC |
221 | } |
222 | ||
223 | } |