|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004, 2007 Mylyn project committers and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
*******************************************************************************/ |
| 8 |
|
| 9 |
package org.eclipse.mylyn.internal.tasks.ui.actions; |
| 10 |
|
| 11 |
import java.io.File; |
| 12 |
import java.text.SimpleDateFormat; |
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.Date; |
| 15 |
import java.util.List; |
| 16 |
import java.util.Locale; |
| 17 |
|
| 18 |
import org.eclipse.jface.action.Action; |
| 19 |
import org.eclipse.jface.action.IAction; |
| 20 |
import org.eclipse.jface.dialogs.MessageDialog; |
| 21 |
import org.eclipse.jface.viewers.ISelection; |
| 22 |
import org.eclipse.jface.viewers.StructuredSelection; |
| 23 |
import org.eclipse.mylyn.internal.tasks.ui.ITasksUiConstants; |
| 24 |
import org.eclipse.mylyn.tasks.core.AbstractRepositoryQuery; |
| 25 |
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin; |
| 26 |
import org.eclipse.swt.SWT; |
| 27 |
import org.eclipse.swt.widgets.FileDialog; |
| 28 |
import org.eclipse.swt.widgets.Shell; |
| 29 |
import org.eclipse.ui.IViewActionDelegate; |
| 30 |
import org.eclipse.ui.IViewPart; |
| 31 |
import org.eclipse.ui.PlatformUI; |
| 32 |
|
| 33 |
/** |
| 34 |
* Makes able to export selected query to the file system. |
| 35 |
* @author Jevgeni Holodkov |
| 36 |
*/ |
| 37 |
public class QueryExportAction extends Action implements IViewActionDelegate { |
| 38 |
|
| 39 |
protected ISelection selection; |
| 40 |
|
| 41 |
public void init(IViewPart view) { |
| 42 |
// ignore |
| 43 |
} |
| 44 |
|
| 45 |
public void run(IAction action) { |
| 46 |
run(getSelectedQueries(selection)); |
| 47 |
} |
| 48 |
|
| 49 |
public void selectionChanged(IAction action, ISelection selection) { |
| 50 |
this.selection = selection; |
| 51 |
List<AbstractRepositoryQuery> selectedQueries = getSelectedQueries(selection); |
| 52 |
action.setEnabled(true); |
| 53 |
if (selectedQueries.size() > 0) { |
| 54 |
action.setEnabled(true); |
| 55 |
} else { |
| 56 |
action.setEnabled(false); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
@SuppressWarnings("unchecked") |
| 61 |
protected List<AbstractRepositoryQuery> getSelectedQueries(ISelection newSelection) { |
| 62 |
List<AbstractRepositoryQuery> selectedQueries = new ArrayList<AbstractRepositoryQuery>(); |
| 63 |
if (selection instanceof StructuredSelection) { |
| 64 |
List selectedObjects = ((StructuredSelection) selection).toList(); |
| 65 |
for (Object selectedObject : selectedObjects) { |
| 66 |
if (selectedObject instanceof AbstractRepositoryQuery) { |
| 67 |
selectedQueries.add((AbstractRepositoryQuery)selectedObject); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
return selectedQueries; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public void run(List<AbstractRepositoryQuery> queries) { |
| 77 |
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); |
| 78 |
FileDialog dialog = new FileDialog(shell, SWT.PRIMARY_MODAL | SWT.SAVE); |
| 79 |
dialog.setFilterExtensions(new String[] { "*" + ITasksUiConstants.FILE_EXTENSION }); |
| 80 |
if (queries.size() == 1) { |
| 81 |
dialog.setFileName(queries.get(0).getHandleIdentifier()); |
| 82 |
} else { |
| 83 |
String fomratString = "yyyy-MM-dd"; |
| 84 |
SimpleDateFormat format = new SimpleDateFormat(fomratString, Locale.ENGLISH); |
| 85 |
String date = format.format(new Date()); |
| 86 |
dialog.setFileName(date + "-exported-queries"); |
| 87 |
} |
| 88 |
|
| 89 |
String path = dialog.open(); |
| 90 |
if (path != null) { |
| 91 |
File file = new File(path); |
| 92 |
if (file.isDirectory()) { |
| 93 |
MessageDialog.openError(shell, "Query Export Error", "Could not export query because specified location is a folder"); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
// Prompt the user to confirm if save operation will cause an overwrite |
| 98 |
if (file.exists()) { |
| 99 |
if (!MessageDialog.openConfirm(shell, "Confirm File Replace", "The file " + file.getPath() |
| 100 |
+ " already exists. Do you want to overwrite it?")) { |
| 101 |
return; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
TasksUiPlugin.getTaskListManager().getTaskListWriter().writeQueries(queries, file); |
| 106 |
} |
| 107 |
return; |
| 108 |
} |
| 109 |
} |