| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2013 Ericsson |
| 3 | * |
| 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 |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Francois Chouinard - Copied and adapted from NewFolderDialog |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.ui.project.wizards; |
| 14 | |
| 15 | import java.lang.reflect.InvocationTargetException; |
| 16 | |
| 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.Activator; |
| 30 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
| 31 | import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment; |
| 32 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
| 33 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; |
| 34 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement; |
| 35 | import org.eclipse.osgi.util.NLS; |
| 36 | import org.eclipse.swt.SWT; |
| 37 | import org.eclipse.swt.graphics.Font; |
| 38 | import org.eclipse.swt.layout.GridData; |
| 39 | import org.eclipse.swt.layout.GridLayout; |
| 40 | import org.eclipse.swt.widgets.Composite; |
| 41 | import org.eclipse.swt.widgets.Control; |
| 42 | import org.eclipse.swt.widgets.Event; |
| 43 | import org.eclipse.swt.widgets.Label; |
| 44 | import org.eclipse.swt.widgets.Listener; |
| 45 | import org.eclipse.swt.widgets.Shell; |
| 46 | import org.eclipse.swt.widgets.Text; |
| 47 | import org.eclipse.ui.PlatformUI; |
| 48 | import org.eclipse.ui.actions.WorkspaceModifyOperation; |
| 49 | import org.eclipse.ui.dialogs.SelectionStatusDialog; |
| 50 | |
| 51 | /** |
| 52 | * Implementation of the copy experiement dialog box. |
| 53 | * <p> |
| 54 | * @version 1.0 |
| 55 | * @author Francois Chouinard |
| 56 | */ |
| 57 | public class CopyExperimentDialog extends SelectionStatusDialog { |
| 58 | |
| 59 | // ------------------------------------------------------------------------ |
| 60 | // Members |
| 61 | // ------------------------------------------------------------------------ |
| 62 | |
| 63 | private final TmfExperimentElement fExperiment; |
| 64 | private Text fNewExperimentName; |
| 65 | private IFolder fExperimentFolder; |
| 66 | private TmfProjectElement fProject; |
| 67 | |
| 68 | // ------------------------------------------------------------------------ |
| 69 | // Constructor |
| 70 | // ------------------------------------------------------------------------ |
| 71 | |
| 72 | /** |
| 73 | * Constructor |
| 74 | * @param shell The parent shell |
| 75 | * @param experiment The TMF experiment model element |
| 76 | */ |
| 77 | public CopyExperimentDialog(Shell shell, TmfExperimentElement experiment) { |
| 78 | super(shell); |
| 79 | fExperiment = experiment; |
| 80 | TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent(); |
| 81 | fExperimentFolder = folder.getResource(); |
| 82 | fProject = experiment.getProject(); |
| 83 | setTitle(Messages.CopyExperimentDialog_DialogTitle); |
| 84 | setStatusLineAboveButtons(true); |
| 85 | } |
| 86 | |
| 87 | // ------------------------------------------------------------------------ |
| 88 | // Dialog |
| 89 | // ------------------------------------------------------------------------ |
| 90 | |
| 91 | /* |
| 92 | * (non-Javadoc) |
| 93 | * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) |
| 94 | */ |
| 95 | @Override |
| 96 | protected Control createDialogArea(Composite parent) { |
| 97 | Composite composite = (Composite) super.createDialogArea(parent); |
| 98 | composite.setLayout(new GridLayout()); |
| 99 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 100 | |
| 101 | createNewExperimentNameGroup(composite); |
| 102 | return composite; |
| 103 | } |
| 104 | |
| 105 | private void createNewExperimentNameGroup(Composite parent) { |
| 106 | Font font = parent.getFont(); |
| 107 | Composite folderGroup = new Composite(parent, SWT.NONE); |
| 108 | GridLayout layout = new GridLayout(); |
| 109 | layout.numColumns = 2; |
| 110 | folderGroup.setLayout(layout); |
| 111 | folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 112 | |
| 113 | // Old experiment name label |
| 114 | Label oldExperimentLabel = new Label(folderGroup, SWT.NONE); |
| 115 | oldExperimentLabel.setFont(font); |
| 116 | oldExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentName); |
| 117 | |
| 118 | // Old experiment name field |
| 119 | Text oldExperimentName = new Text(folderGroup, SWT.BORDER); |
| 120 | GridData data = new GridData(GridData.FILL_HORIZONTAL); |
| 121 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; |
| 122 | oldExperimentName.setLayoutData(data); |
| 123 | oldExperimentName.setFont(font); |
| 124 | oldExperimentName.setText(fExperiment.getName()); |
| 125 | oldExperimentName.setEnabled(false); |
| 126 | |
| 127 | // New experiment name label |
| 128 | Label newExperimentLabel = new Label(folderGroup, SWT.NONE); |
| 129 | newExperimentLabel.setFont(font); |
| 130 | newExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentNewName); |
| 131 | |
| 132 | // New experiment name entry field |
| 133 | fNewExperimentName = new Text(folderGroup, SWT.BORDER); |
| 134 | data = new GridData(GridData.FILL_HORIZONTAL); |
| 135 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; |
| 136 | fNewExperimentName.setLayoutData(data); |
| 137 | fNewExperimentName.setFont(font); |
| 138 | fNewExperimentName.addListener(SWT.Modify, new Listener() { |
| 139 | @Override |
| 140 | public void handleEvent(Event event) { |
| 141 | validateNewExperimentName(); |
| 142 | } |
| 143 | }); |
| 144 | } |
| 145 | |
| 146 | private void validateNewExperimentName() { |
| 147 | |
| 148 | String name = fNewExperimentName.getText(); |
| 149 | IWorkspace workspace = fExperimentFolder.getWorkspace(); |
| 150 | IStatus nameStatus = workspace.validateName(name, IResource.FOLDER); |
| 151 | |
| 152 | if ("".equals(name)) { //$NON-NLS-1$ |
| 153 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null)); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | if (!nameStatus.isOK()) { |
| 158 | updateStatus(nameStatus); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | IPath path = new Path(name); |
| 163 | if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) { |
| 164 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null)); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$ |
| 169 | } |
| 170 | |
| 171 | // ------------------------------------------------------------------------ |
| 172 | // SelectionStatusDialog |
| 173 | // ------------------------------------------------------------------------ |
| 174 | /* |
| 175 | * (non-Javadoc) |
| 176 | * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult() |
| 177 | */ |
| 178 | @Override |
| 179 | protected void computeResult() { |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * (non-Javadoc) |
| 184 | * @see org.eclipse.ui.dialogs.SelectionStatusDialog#create() |
| 185 | */ |
| 186 | @Override |
| 187 | public void create() { |
| 188 | super.create(); |
| 189 | getButton(IDialogConstants.OK_ID).setEnabled(false); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * (non-Javadoc) |
| 194 | * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed() |
| 195 | */ |
| 196 | @Override |
| 197 | protected void okPressed() { |
| 198 | IFolder folder = copyExperiment(fNewExperimentName.getText()); |
| 199 | if (folder == null) { |
| 200 | return; |
| 201 | } |
| 202 | setSelectionResult(new IFolder[] { folder }); |
| 203 | super.okPressed(); |
| 204 | |
| 205 | if (fProject != null) { |
| 206 | fProject.refresh(); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | private IFolder copyExperiment(final String newName) { |
| 211 | |
| 212 | IPath oldPath = fExperiment.getResource().getFullPath(); |
| 213 | final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$ |
| 214 | |
| 215 | WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { |
| 216 | @Override |
| 217 | public void execute(IProgressMonitor monitor) throws CoreException { |
| 218 | try { |
| 219 | monitor.beginTask("", 1000); //$NON-NLS-1$ |
| 220 | if (monitor.isCanceled()) { |
| 221 | throw new OperationCanceledException(); |
| 222 | } |
| 223 | // Copy supplementary files first |
| 224 | fExperiment.copySupplementaryFolder(newName); |
| 225 | fExperiment.getResource().copy(newPath, IResource.FORCE | IResource.SHALLOW, null); |
| 226 | // Delete any bookmarks file found in copied experiment folder |
| 227 | IFolder folder = fExperimentFolder.getFolder(newName); |
| 228 | if (folder.exists()) { |
| 229 | for (IResource member : folder.members()) { |
| 230 | if (TmfExperiment.class.getCanonicalName().equals(member.getPersistentProperty(TmfCommonConstants.TRACETYPE))) { |
| 231 | member.delete(true, null); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | if (monitor.isCanceled()) { |
| 236 | throw new OperationCanceledException(); |
| 237 | } |
| 238 | } finally { |
| 239 | monitor.done(); |
| 240 | } |
| 241 | } |
| 242 | }; |
| 243 | try { |
| 244 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); |
| 245 | } catch (InterruptedException exception) { |
| 246 | return null; |
| 247 | } catch (InvocationTargetException exception) { |
| 248 | MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$ |
| 249 | return null; |
| 250 | } catch (RuntimeException exception) { |
| 251 | return null; |
| 252 | } |
| 253 | |
| 254 | return fExperiment.getResource(); |
| 255 | } |
| 256 | |
| 257 | } |