Skip to main content
summaryrefslogtreecommitdiffstats
blob: f16b6324c66face0d9e06f2eefb35ec94d8668dc (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
/*******************************************************************************
 * 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.actions;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryQuery;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PlatformUI;

/**
 * Makes able to select an exported query file and import it back to the system.
 * 
 * @author Jevgeni Holodkov
 */
public class QueryImportAction extends Action implements IViewActionDelegate {

	protected ISelection selection;

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

	public void run(IAction action) {
		run();
	}

	public void selectionChanged(IAction action, ISelection selection) {
		// ignore
	}

	@Override
	public void run() {
		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		FileDialog dialog = new FileDialog(shell);
		dialog.setFilterExtensions(new String[] { "*" + ITasksCoreConstants.FILE_EXTENSION });

		String path = dialog.open();
		if (path != null) {
			File file = new File(path);
			if (file.isFile()) {
				try {
					List<AbstractRepositoryQuery> queries = TasksUiPlugin.getTaskListManager()
							.getTaskListWriter()
							.readQueries(file);
					Set<TaskRepository> repositories = TasksUiPlugin.getTaskListManager()
							.getTaskListWriter()
							.readRepositories(file);
					if (queries.size() > 0) {
						importQueries(queries, repositories, shell);
					} else {
						MessageDialog.openError(shell, "Query Import Error",
								"The specified file is not an exported query. Please, check that you have provided the correct file.");
					}
				} catch (IOException e) {
					MessageDialog.openError(shell, "Query Import Error",
							"The specified file is not an exported query. Please, check that you have provided the correct file.");
				}
			}
		}
	}

	/**
	 * @param queries
	 * @param repositories
	 * @param shell
	 */
	public void importQueries(List<AbstractRepositoryQuery> queries, Set<TaskRepository> repositories, Shell shell) {
		TasksUiPlugin.getRepositoryManager().insertRepositories(repositories,
				TasksUiPlugin.getDefault().getRepositoriesFilePath());
		List<AbstractRepositoryQuery> badQueries = TasksUiPlugin.getTaskListManager().insertQueries(queries);

		// notify user about importing
		String message = "The following queries were imported successfully: ";
		for (AbstractRepositoryQuery imported : queries) {
			if (!badQueries.contains(imported)) {
				message += "\n" + imported.getHandleIdentifier();
			}
		}

		if (badQueries.size() > 0) {
			message += "\n\n These queries were not imported, since their repository was not found: ";
			for (AbstractRepositoryQuery bad : badQueries) {
				message += "\n" + bad.getHandleIdentifier();
			}
		}

		MessageDialog.openInformation(shell, "Query Import Completed", message);
	}

}

Back to the top