Skip to main content
summaryrefslogtreecommitdiffstats
blob: 02283b32c00ba61aa92d10a08dfe65fd8e595bd9 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers 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
 *******************************************************************************/
package org.eclipse.mylyn.internal.tasks.ui.wizards;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.context.core.ContextCore;
import org.eclipse.mylyn.context.core.IInteractionContextManager;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.util.TaskDataExportOperation;
import org.eclipse.mylyn.tasks.core.AbstractTask;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.ui.IExportWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;

/**
 * Wizard for exporting tasklist data files to the file system. This wizard uses a single page: TaskDataExportWizardPage
 * 
 * @author Wesley Coelho
 * @author Mik Kersten
 */
public class TaskDataExportWizard extends Wizard implements IExportWizard {

	/**
	 * The name of the dialog store's section associated with the task data export wizard
	 */
	private final static String SETTINGS_SECTION = "org.eclipse.mylyn.tasklist.ui.exportWizard";

	public final static String ZIP_FILE_PREFIX = "mylyndata";

	private final static String ZIP_FILE_EXTENSION = ".zip";

	private final static String WINDOW_TITLE = "Export";

	private TaskDataExportWizardPage exportPage = null;

	public static String getZipFileName() {
		String fomratString = "yyyy-MM-dd";
		SimpleDateFormat format = new SimpleDateFormat(fomratString, Locale.ENGLISH);
		String date = format.format(new Date());
		return ZIP_FILE_PREFIX + "-" + date + ZIP_FILE_EXTENSION;
	}

	public TaskDataExportWizard() {
		IDialogSettings masterSettings = TasksUiPlugin.getDefault().getDialogSettings();
		setDialogSettings(getSettingsSection(masterSettings));
		setNeedsProgressMonitor(true);
		setWindowTitle(WINDOW_TITLE);
	}

	/**
	 * Finds or creates a dialog settings section that is used to make the dialog control settings persistent
	 */
	public IDialogSettings getSettingsSection(IDialogSettings master) {
		IDialogSettings settings = master.getSection(SETTINGS_SECTION);
		if (settings == null) {
			settings = master.addNewSection(SETTINGS_SECTION);
		}
		return settings;
	}

	@Override
	public void addPages() {
		exportPage = new TaskDataExportWizardPage();
		exportPage.setWizard(this);
		addPage(exportPage);
	}

	public void init(IWorkbench workbench, IStructuredSelection selection) {
		// no initialization needed
	}

	@Override
	public boolean canFinish() {
		return exportPage.isPageComplete();
	}

	/**
	 * Called when the user clicks finish. Saves the task data. Waits until all overwrite decisions have been made
	 * before starting to save files. If any overwrite is canceled, no files are saved and the user must adjust the
	 * dialog.
	 */
	@Override
	public boolean performFinish() {
		boolean overwrite = exportPage.overwrite();
		boolean zip = exportPage.zip();

		Collection<AbstractTask> taskContextsToExport = TasksUi.getTaskListManager().getTaskList().getAllTasks();

		// Get file paths to check for existence
		String destDir = exportPage.getDestinationDirectory();
		final File destDirFile = new File(destDir);
		if (!destDirFile.exists() || !destDirFile.isDirectory()) {
			// This should never happen
			StatusHandler.fail(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN,
					"Could not export data because specified location does not exist or is not a folder",
					new Exception()));
			return false;
		}

		final File destTaskListFile = new File(destDir + File.separator + ITasksCoreConstants.DEFAULT_TASK_LIST_FILE);
		final File destActivationHistoryFile = new File(destDir + File.separator
				+ IInteractionContextManager.CONTEXT_HISTORY_FILE_NAME
				+ IInteractionContextManager.CONTEXT_FILE_EXTENSION);
		final File destZipFile = new File(destDir + File.separator + getZipFileName());

		// Prompt the user to confirm if ANY of the save repositoryOperations will cause
		// an overwrite
		if (!overwrite) {

			if (zip) {
				if (destZipFile.exists()) {
					if (!MessageDialog.openConfirm(getShell(), "Confirm File Replace", "The zip file "
							+ destZipFile.getPath() + " already exists. Do you want to overwrite it?")) {
						return false;
					}
				}
			} else {
				if (exportPage.exportTaskList() && destTaskListFile.exists()) {
					if (!MessageDialog.openConfirm(getShell(), "Confirm File Replace", "The task list file "
							+ destTaskListFile.getPath() + " already exists. Do you want to overwrite it?")) {
						return false;
					}
				}

				if (exportPage.exportActivationHistory() && destActivationHistoryFile.exists()) {
					if (!MessageDialog.openConfirm(getShell(), "Confirm File Replace",
							"The task activation history file " + destActivationHistoryFile.getPath()
									+ " already exists. Do you want to overwrite it?")) {
						return false;
					}
				}

				if (exportPage.exportTaskContexts()) {
					for (AbstractTask task : taskContextsToExport) {
						File contextFile = ContextCore.getContextManager().getFileForContext(
								task.getHandleIdentifier());
						File destTaskFile = new File(destDir + File.separator + contextFile.getName());
						if (destTaskFile.exists()) {
							if (!MessageDialog.openConfirm(getShell(), "Confirm File Replace",
									"Task context files already exist in " + destDir
											+ ". Do you want to overwrite them?")) {
								return false;
							} else {
								break;
							}
						}
					}
				}
			}
		}

		// FileCopyJob job = new FileCopyJob(destZipFile, destTaskListFile,
		// destActivationHistoryFile);
		TaskDataExportOperation job = new TaskDataExportOperation(exportPage.getDestinationDirectory(),
				exportPage.exportTaskList(), exportPage.exportActivationHistory(), exportPage.exportTaskContexts(),
				exportPage.zip(), destZipFile.getName(), taskContextsToExport);
		IProgressService service = PlatformUI.getWorkbench().getProgressService();

		try {
			// TODO use the wizard's progress service or IProgressService.busyCursorWhile(): bug 210710 
			service.run(true, false, job);
		} catch (InvocationTargetException e) {
			StatusHandler.fail(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Could not export files", e));
		} catch (InterruptedException e) {
			StatusHandler.fail(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Could not export files", e));
		}

		exportPage.saveSettings();
		return true;
	}
}

Back to the top