|
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.util.ArrayList; |
| 12 |
import java.util.List; |
| 13 |
|
| 14 |
import org.eclipse.jface.action.Action; |
| 15 |
import org.eclipse.jface.action.IAction; |
| 16 |
import org.eclipse.jface.dialogs.MessageDialog; |
| 17 |
import org.eclipse.jface.viewers.ISelection; |
| 18 |
import org.eclipse.jface.viewers.StructuredSelection; |
| 19 |
import org.eclipse.mylyn.internal.tasks.ui.ITasksUiConstants; |
| 20 |
import org.eclipse.mylyn.monitor.core.StatusHandler; |
| 21 |
import org.eclipse.mylyn.tasks.core.AbstractRepositoryQuery; |
| 22 |
import org.eclipse.mylyn.tasks.ui.AbstractRepositoryConnectorUi; |
| 23 |
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin; |
| 24 |
import org.eclipse.ui.IViewActionDelegate; |
| 25 |
import org.eclipse.ui.IViewPart; |
| 26 |
import org.eclipse.ui.PlatformUI; |
| 27 |
import org.w3c.dom.Document; |
| 28 |
|
| 29 |
/** |
| 30 |
* Allow to clone a selected query. |
| 31 |
* |
| 32 |
* @author Jevgeni Holodkov |
| 33 |
*/ |
| 34 |
public class QueryCloneAction extends Action implements IViewActionDelegate { |
| 35 |
|
| 36 |
protected ISelection selection; |
| 37 |
|
| 38 |
public void init(IViewPart view) { |
| 39 |
// ignore |
| 40 |
} |
| 41 |
|
| 42 |
public void run(IAction action) { |
| 43 |
run(getSelectedQuery(selection)); |
| 44 |
} |
| 45 |
|
| 46 |
public void selectionChanged(IAction action, ISelection selection) { |
| 47 |
this.selection = selection; |
| 48 |
AbstractRepositoryQuery selectedQuery = getSelectedQuery(selection); |
| 49 |
action.setEnabled(true); |
| 50 |
if (selectedQuery != null) { |
| 51 |
action.setEnabled(true); |
| 52 |
} else { |
| 53 |
action.setEnabled(false); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
protected AbstractRepositoryQuery getSelectedQuery(ISelection newSelection) { |
| 58 |
if (selection instanceof StructuredSelection) { |
| 59 |
// allow to select only one element |
| 60 |
if (((StructuredSelection) selection).size() == 1) { |
| 61 |
Object selectedObject = ((StructuredSelection) selection).getFirstElement(); |
| 62 |
if (selectedObject instanceof AbstractRepositoryQuery) { |
| 63 |
return (AbstractRepositoryQuery) selectedObject; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
return null; |
| 68 |
} |
| 69 |
|
| 70 |
public void run(AbstractRepositoryQuery selectedQuery) { |
| 71 |
if (selectedQuery == null) { |
| 72 |
MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), |
| 73 |
ITasksUiConstants.TITLE_DIALOG, "No query selected."); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
List<AbstractRepositoryQuery> queries = new ArrayList<AbstractRepositoryQuery>(); |
| 78 |
queries.add(selectedQuery); |
| 79 |
|
| 80 |
Document queryDoc = TasksUiPlugin.getTaskListManager().getTaskListWriter().createQueryDocument(queries); |
| 81 |
List<AbstractRepositoryQuery> clonedQueries = TasksUiPlugin.getTaskListManager() |
| 82 |
.getTaskListWriter() |
| 83 |
.readQueryDocument(queryDoc); |
| 84 |
|
| 85 |
if (clonedQueries.size() > 0) { |
| 86 |
for (AbstractRepositoryQuery query : clonedQueries) { |
| 87 |
String handle = TasksUiPlugin.getTaskListManager().resolveIdentifiersConflict(query); |
| 88 |
query.setHandleIdentifier(handle); |
| 89 |
AbstractRepositoryConnectorUi connectorUi = TasksUiPlugin.getConnectorUi(query.getRepositoryKind()); |
| 90 |
connectorUi.openEditQueryDialog(query); |
| 91 |
} |
| 92 |
} else { |
| 93 |
// cannot happen |
| 94 |
StatusHandler.fail(new IllegalStateException(selectedQuery.toString()), "Query cloning did not succeeded.", true); |
| 95 |
return; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
} |