Skip to main content
summaryrefslogtreecommitdiffstats
blob: a3240e18566518cdc4a2ec9a55428ebd356085d8 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*******************************************************************************
 * Copyright (c) 2004 - 2005 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.bugzilla.ui.tasks;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylar.bugzilla.core.BugzillaPlugin;
import org.eclipse.mylar.bugzilla.ui.BugzillaOpenStructure;
import org.eclipse.mylar.bugzilla.ui.BugzillaUITools;
import org.eclipse.mylar.bugzilla.ui.BugzillaUiPlugin;
import org.eclipse.mylar.bugzilla.ui.ViewBugzillaAction;
import org.eclipse.mylar.bugzilla.ui.actions.RefreshBugzillaAction;
import org.eclipse.mylar.bugzilla.ui.actions.RefreshBugzillaReportsAction;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.tasks.ITask;
import org.eclipse.mylar.tasks.ITaskHandler;
import org.eclipse.mylar.tasks.ITaskListElement;
import org.eclipse.mylar.tasks.MylarTasksPlugin;
import org.eclipse.mylar.tasks.internal.TaskCategory;
import org.eclipse.mylar.tasks.ui.actions.DeleteAction;
import org.eclipse.mylar.tasks.ui.views.TaskListView;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.internal.Workbench;

/**
 * @author Mik Kersten and Ken Sueda
 */
public class BugzillaTaskHandler implements ITaskHandler {

	public void itemDeleted(ITaskListElement element) {
		if (element instanceof BugzillaQueryCategory) {
			boolean deleteConfirmed = MessageDialog.openQuestion(
		            Workbench.getInstance().getActiveWorkbenchWindow().getShell(),
		            "Confirm delete", 
		            "Delete the selected query and all contained tasks?");
			if (!deleteConfirmed) 
				return;
			BugzillaQueryCategory cat = (BugzillaQueryCategory) element;
			MylarTasksPlugin.getTaskListManager().deleteCategory(cat);
		} else if (element instanceof BugzillaTask) {
			BugzillaTask task = (BugzillaTask) element;
			if (task.isActive()) {
				MessageDialog.openError(Workbench.getInstance()
						.getActiveWorkbenchWindow().getShell(), "Delete failed",
						"Task must be deactivated in order to delete.");
				return;
			}
			
			String message = task.getDeleteConfirmationMessage();			
			boolean deleteConfirmed = MessageDialog.openQuestion(
		            Workbench.getInstance().getActiveWorkbenchWindow().getShell(),
		            "Confirm delete", message);
			if (!deleteConfirmed) 
				return;
									
			task.removeReport();
			MylarTasksPlugin.getTaskListManager().deleteTask(task);
			MylarPlugin.getContextManager().taskDeleted(task.getHandle(), task.getPath());
			IWorkbenchPage page = MylarTasksPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();

			// if we couldn't get the page, get out of here
			if (page == null)
				return;
			try {
				TaskListView.getDefault().closeTaskEditors(task, page);
			} catch (Exception e) {
				MylarPlugin.log(e, " deletion failed");
			}
		}
		// XXX inform can't delete hits???
	}

	public void taskCompleted(ITask task) {
		// TODO can't do this	
	}

	public void itemOpened(ITaskListElement element) {

		if (element instanceof BugzillaTask) {
			BugzillaTask t = (BugzillaTask) element;
			MylarTasksPlugin.Report_Open_Mode mode = MylarTasksPlugin.getDefault().getReportMode();
			if (mode == MylarTasksPlugin.Report_Open_Mode.EDITOR) {
				t.openTaskInEditor();
			} else if (mode == MylarTasksPlugin.Report_Open_Mode.INTERNAL_BROWSER) {
				BugzillaUITools.openUrl(t.getBugUrl());	    			
			} else {
				// not supported
			}
		}
		else if (element instanceof BugzillaQueryCategory){
			BugzillaQueryCategory queryCategory = (BugzillaQueryCategory)element;
	       	BugzillaQueryDialog sqd = new BugzillaQueryDialog(Display.getCurrent().getActiveShell(), queryCategory.getUrl(), queryCategory.getDescription(false));
        	if(sqd.open() == Dialog.OK){
	        	queryCategory.setDescription(sqd.getName());
	        	queryCategory.setUrl(sqd.getUrl());
	        	
	        	new RefreshBugzillaAction(queryCategory).run();
        	}
	    } else if(element instanceof BugzillaHit){
	    	BugzillaHit hit = (BugzillaHit)element;
	    	MylarTasksPlugin.Report_Open_Mode mode = MylarTasksPlugin.getDefault().getReportMode();
	    	if (mode == MylarTasksPlugin.Report_Open_Mode.EDITOR) {
	    		if(hit.hasCorrespondingActivatableTask()){
		    		hit.getAssociatedTask().openTaskInEditor();
		    	} else {
			    	BugzillaOpenStructure open = new BugzillaOpenStructure(((BugzillaHit)element).getServerName(), ((BugzillaHit)element).getID(),-1);
			    	List<BugzillaOpenStructure> selectedBugs = new ArrayList<BugzillaOpenStructure>();
			    	selectedBugs.add(open);
			    	ViewBugzillaAction viewBugs = new ViewBugzillaAction("Display bugs in editor", selectedBugs);
					viewBugs.schedule();
		    	}
    		} else if (mode == MylarTasksPlugin.Report_Open_Mode.INTERNAL_BROWSER) {
    			BugzillaUITools.openUrl(hit.getBugUrl());  			
    		} else {
    			// not supported
    		}
	    }
		
	}

	public boolean acceptsItem(ITaskListElement element) {
		return element instanceof BugzillaTask || element instanceof BugzillaHit || element instanceof BugzillaQueryCategory;
	}

	public void dropItem(ITaskListElement element, TaskCategory cat) {
		if (element instanceof BugzillaHit) {
        	BugzillaHit bh = (BugzillaHit) element;
    		if (bh.getAssociatedTask() != null) {
        		bh.getAssociatedTask().setCategory(cat);
        		cat.addTask(bh.getAssociatedTask());
        	} else {
        		BugzillaTask bt = new BugzillaTask(bh);
        		bh.setAssociatedTask(bt);
        		bt.setCategory(cat);
        		cat.addTask(bt);
        		BugzillaUiPlugin.getDefault().getBugzillaTaskListManager().addToBugzillaTaskRegistry(bt);
        	}
		}		
	}

	public void taskClosed(ITask element, IWorkbenchPage page) {
		try{
			IEditorInput input = null;		
			if (element instanceof BugzillaTask) {
				input = new BugzillaTaskEditorInput((BugzillaTask)element);
			}
			IEditorPart editor = page.findEditor(input);
	
			if (editor != null) {
				page.closeEditor(editor, false);
			}
		} catch (Exception e){
			MylarPlugin.log(e, "Error while trying to close a bugzilla task");
		}
	}

	public ITask taskAdded(ITask newTask) {
		if(newTask instanceof BugzillaTask){
			BugzillaTask bugTask = BugzillaUiPlugin.getDefault().getBugzillaTaskListManager().getFromBugzillaTaskRegistry(newTask.getHandle());
			if(bugTask == null){
				BugzillaUiPlugin.getDefault().getBugzillaTaskListManager().addToBugzillaTaskRegistry((BugzillaTask)newTask);
				bugTask = (BugzillaTask)newTask;	
			}
			return bugTask;
		}
		return null;
	}

	public void restoreState(TaskListView taskListView) {
		if (BugzillaPlugin.getDefault().refreshOnStartUpEnabled()) {
			RefreshBugzillaReportsAction refresh = new RefreshBugzillaReportsAction();
			refresh.setShowProgress(false);
			refresh.run();
			refresh.setShowProgress(true);
		}		
	}

	public boolean enableAction(Action action, ITaskListElement element) {

		if(element instanceof BugzillaHit){
			return false;
		} else if(element instanceof BugzillaTask){
			if(action instanceof DeleteAction){
				return true;
			} else {
				return false;
			}
		} else if(element instanceof BugzillaQueryCategory){
			return false;
		}
		return false;
	} 
}

Back to the top