Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5bfcacb3755dead5d2ebfd7af9b8bb246eee3976 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*******************************************************************************
 * Copyright (c) 2004, 2008 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.actions;

import java.io.File;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.util.ImportExportUtil;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

/**
 * @author Steffen Pingel
 */
public class ExportAction implements IViewActionDelegate {

	private ISelection selection;

	public void init(IViewPart view) {
		// ignore
	}

	public void run(IAction action) {
		if (selection.isEmpty() || !(selection instanceof StructuredSelection)) {
			MessageDialog.openError(TasksUiInternal.getShell(), Messages.ExportAction_Dialog_Title, Messages.ExportAction_Nothing_selected);
			return;
		}

		FileDialog dialog = new FileDialog(TasksUiInternal.getShell(), SWT.PRIMARY_MODAL | SWT.SAVE);
		dialog.setText(Messages.ExportAction_Dialog_Title);
		ImportExportUtil.configureFilter(dialog);
		dialog.setFileName(ITasksCoreConstants.EXPORT_FILE_NAME + ITasksCoreConstants.FILE_EXTENSION);
		String path = dialog.open();
		if (path != null) {
			File file = new File(path);
			// Prompt the user to confirm if save operation will cause an overwrite
			if (file.exists()) {
				if (!MessageDialog.openConfirm(TasksUiInternal.getShell(), Messages.ExportAction_Dialog_Title, NLS.bind(
						Messages.ExportAction_X_exists_Do_you_wish_to_overwrite, file.getPath()))) {
					return;
				}
			}
			try {
				ImportExportUtil.export(file, (IStructuredSelection) selection);
			} catch (CoreException e) {
				StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN,
						"Problems encountered during export", e)); //$NON-NLS-1$
				TasksUiInternal.displayStatus(Messages.ExportAction_Dialog_Title, new MultiStatus(ITasksCoreConstants.ID_PLUGIN, 0,
						new IStatus[] { e.getStatus() },
						Messages.ExportAction_Problems_encountered, e));
			}
		}
	}

	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = selection;
	}

}

Back to the top