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

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylar.bugzilla.ui.BugzillaImages;
import org.eclipse.mylar.bugzilla.ui.tasks.BugzillaHit;
import org.eclipse.mylar.bugzilla.ui.tasks.BugzillaQueryCategory;
import org.eclipse.mylar.bugzilla.ui.tasks.BugzillaTask;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.tasks.ITask;
import org.eclipse.mylar.tasks.MylarTasksPlugin;
import org.eclipse.mylar.tasks.internal.TaskCategory;
import org.eclipse.mylar.tasks.ui.views.TaskListView;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.progress.IProgressService;

/**
 * @author Ken Sueda
 */
public class RefreshBugzillaAction extends Action {
	
	public static final String ID = "org.eclipse.mylar.tasks.actions.refresh.bugzilla";
	
	private final TaskListView view;
	
	private BugzillaQueryCategory cat = null;
	
	public RefreshBugzillaAction(TaskListView view) {
		this.view = view;
		setText("Bugzilla Refresh");
        setToolTipText("Bugzilla Refresh");
        setId(ID);
        setImageDescriptor(BugzillaImages.TASK_BUG_REFRESH);
	}
	
	public RefreshBugzillaAction(TaskListView view, BugzillaQueryCategory cat) {
		assert(cat != null);
		this.view = view;
		this.cat =  cat;
		setText("Bugzilla Refresh");
        setToolTipText("Bugzilla Refresh");
        setId(ID);
        setImageDescriptor(BugzillaImages.TASK_BUG_REFRESH);
	}
	
	@Override
	public void run() {
		Object obj = cat;
		if(cat == null){
			ISelection selection = this.view.getViewer().getSelection();
			obj = ((IStructuredSelection) selection).getFirstElement();
		}
		if (obj instanceof BugzillaQueryCategory) {
			final BugzillaQueryCategory cat = (BugzillaQueryCategory) obj;
			WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
				protected void execute(IProgressMonitor monitor) throws CoreException {
					PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
						public void run() {
							cat.refreshBugs();
							for(BugzillaHit hit: cat.getChildren()){
								if(hit.hasCorrespondingActivatableTask()){
									((BugzillaTask)hit.getOrCreateCorrespondingTask()).refresh();
								}
							}
							RefreshBugzillaAction.this.view.getViewer().refresh();
						}
					});
				}
			};
			// Use the progess service to execute the runnable
			IProgressService service = PlatformUI.getWorkbench().getProgressService();
			try {
				service.run(true, false, op);
			} catch (InvocationTargetException e) {
				// Operation was canceled
				MylarPlugin.log(e, e.getMessage());
			} catch (InterruptedException e) {
				// Handle the wrapped exception
				MylarPlugin.log(e, e.getMessage());
			}
		} else if (obj instanceof TaskCategory) {
			TaskCategory cat = (TaskCategory) obj;
			for (ITask task : cat.getChildren()) {
				if (task instanceof BugzillaTask) {
					((BugzillaTask)task).refresh();
				}
			}
		} else if (obj instanceof BugzillaTask) {
			((BugzillaTask)obj).refresh();
		} else if(obj instanceof BugzillaHit){
			BugzillaHit hit = (BugzillaHit)obj;
			if(hit.hasCorrespondingActivatableTask()){
				hit.getAssociatedTask().refresh();
			}
		}
		for(ITask task: MylarTasksPlugin.getTaskListManager().getTaskList().getActiveTasks()){
			if(task instanceof BugzillaTask){
				ITask found = MylarTasksPlugin.getTaskListManager().getTaskList().getTaskForHandle(task.getHandle());
				if(found == null){
					MylarTasksPlugin.getTaskListManager().getTaskList().addRootTask(task);
					MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Bugzilla Task Moved To Root", "Bugzilla Task " + 
							BugzillaTask.getBugId(task.getHandle()) + 
							" has been moved to the root since it is activated and has disappeared from a query.");
				}
			}
		}
		view.getViewer().refresh();
	}
}

Back to the top