Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5aa9907a47e66a9ec71730cb24c2e87fa4abcd95 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*******************************************************************************
 * 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.tasklist;

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.tasklist.ITask;
import org.eclipse.mylar.tasklist.ITaskHandler;
import org.eclipse.mylar.tasklist.ITaskListCategory;
import org.eclipse.mylar.tasklist.ITaskListElement;
import org.eclipse.mylar.tasklist.MylarTasklistPlugin;
import org.eclipse.mylar.tasklist.internal.TaskCategory;
import org.eclipse.mylar.tasklist.ui.actions.CopyDescriptionAction;
import org.eclipse.mylar.tasklist.ui.actions.DeleteAction;
import org.eclipse.mylar.tasklist.ui.actions.GoIntoAction;
import org.eclipse.mylar.tasklist.ui.actions.OpenTaskEditorAction;
import org.eclipse.mylar.tasklist.ui.actions.RemoveFromCategoryAction;
import org.eclipse.mylar.tasklist.ui.actions.RenameAction;
import org.eclipse.mylar.tasklist.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 query = (BugzillaQueryCategory) element;
			MylarTasklistPlugin.getTaskListManager().deleteQuery(query);
		} 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();
			MylarTasklistPlugin.getTaskListManager().deleteTask(task);
			MylarPlugin.getContextManager().contextDeleted(task.getHandleIdentifier(), task.getPath());
			IWorkbenchPage page = MylarTasklistPlugin.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");
			}
		}
	}

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

	public void itemOpened(ITaskListElement element) {

		boolean offline = MylarTasklistPlugin.getPrefs().getBoolean(MylarPlugin.WORK_OFFLINE);
		
		if (element instanceof BugzillaTask) {
			BugzillaTask t = (BugzillaTask) element;
			MylarTasklistPlugin.ReportOpenMode mode = MylarTasklistPlugin.getDefault().getReportMode();
			if (mode == MylarTasklistPlugin.ReportOpenMode.EDITOR) {
				t.openTaskInEditor(offline);
			} else if (mode == MylarTasklistPlugin.ReportOpenMode.INTERNAL_BROWSER) {
				if(offline){
					MessageDialog.openInformation(null, "Unable to open bug", "Unable to open the selected bugzilla task since you are currently offline");
	    			return;
	    		}
				String title = "Bug #" + BugzillaTask.getBugId(t.getHandleIdentifier());
				BugzillaUITools.openUrl(title, title, t.getBugUrl());	    			
			} else {
				// not supported
			}
		} else if(element instanceof BugzillaCustomQuery){
			BugzillaCustomQuery queryCategory = (BugzillaCustomQuery)element;
	       	BugzillaCustomQueryDialog sqd = new BugzillaCustomQueryDialog(Display.getCurrent().getActiveShell(), queryCategory.getQueryString(), queryCategory.getDescription(false), queryCategory.getMaxHits()+"");
        	if(sqd.open() == Dialog.OK){
	        	queryCategory.setDescription(sqd.getName());
	        	queryCategory.setQueryString(sqd.getUrl());
	        	int maxHits = -1;
	        	try{
	        		maxHits = Integer.parseInt(sqd.getMaxHits());
	        	} catch(Exception e){}
	        	queryCategory.setMaxHits(maxHits);
	        	
	        	new RefreshBugzillaAction(queryCategory).run();
        	}
		}else if (element instanceof BugzillaQueryCategory){
			BugzillaQueryCategory queryCategory = (BugzillaQueryCategory)element;
	       	BugzillaQueryDialog sqd = new BugzillaQueryDialog(Display.getCurrent().getActiveShell(), queryCategory.getQueryString(), queryCategory.getDescription(false), queryCategory.getMaxHits()+"");
        	if(sqd.open() == Dialog.OK){
	        	queryCategory.setDescription(sqd.getName());
	        	queryCategory.setQueryString(sqd.getUrl());
	        	int maxHits = -1;
	        	try{
	        		maxHits = Integer.parseInt(sqd.getMaxHits());
	        	} catch(Exception e){}
	        	queryCategory.setMaxHits(maxHits);
	        	
	        	new RefreshBugzillaAction(queryCategory).run();
        	}
	    } else if(element instanceof BugzillaHit){
	    	BugzillaHit hit = (BugzillaHit)element;
	    	MylarTasklistPlugin.ReportOpenMode mode = MylarTasklistPlugin.getDefault().getReportMode();
	    	if (mode == MylarTasklistPlugin.ReportOpenMode.EDITOR) {
	    		if(hit.hasCorrespondingActivatableTask()){
		    		hit.getAssociatedTask().openTaskInEditor(offline);
		    	} else {
		    		if(offline){
		    			MessageDialog.openInformation(null, "Unable to open bug", "Unable to open the selected bugzilla report since you are currently offline");
		    			return;
		    		}
			    	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 == MylarTasklistPlugin.ReportOpenMode.INTERNAL_BROWSER) {
    			if(offline){
    				MessageDialog.openInformation(null, "Unable to open bug", "Unable to open the selected bugzilla report since you are currently offline");
	    			return;
	    		}
    			String title = "Bug #" + BugzillaTask.getBugId(hit.getHandleIdentifier());
    			BugzillaUITools.openUrl(title, title, 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, true);
        		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, true);
			}
			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.getHandleIdentifier());
			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 || action instanceof CopyDescriptionAction || action instanceof OpenTaskEditorAction || action instanceof RemoveFromCategoryAction){
				return true;
			} else {
				return false;
			}
		} else if(element instanceof BugzillaQueryCategory){
			if(action instanceof DeleteAction || action instanceof CopyDescriptionAction || action instanceof OpenTaskEditorAction || action instanceof RenameAction){
				return true;
			} else if(action instanceof GoIntoAction){
				BugzillaQueryCategory cat = (BugzillaQueryCategory) element;
				if(cat.getChildren().size() > 0){
					return true;
				}
			} else {
				return false;
			}
		}
		return false;
	}

	public void itemRemoved(ITaskListElement element, ITaskListCategory category) {
		if (element instanceof BugzillaTask){
			BugzillaTask task = (BugzillaTask) element;
			if (category != null) {
				category.removeTask(task);
			} else {
				String message = task.getDeleteConfirmationMessage();			
				boolean deleteConfirmed = MessageDialog.openQuestion(
			            Workbench.getInstance().getActiveWorkbenchWindow().getShell(),
			            "Confirm delete", message);
				if (!deleteConfirmed) 
					return;
				MylarTasklistPlugin.getTaskListManager().deleteTask(task);
			}
		}
	}

	public ITask dropItemToPlan(ITaskListElement element) {
		if(element instanceof BugzillaHit){
			BugzillaHit hit = (BugzillaHit) element;
			return hit.getOrCreateCorrespondingTask();
		} else if(element instanceof BugzillaTask){		
			return (ITask) element;
		} else {
			return null;
		}		
	} 
}

Back to the top