/*******************************************************************************
 * 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.BugzillaQueryCategory;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.tasks.MylarTasksPlugin;
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;
	public RefreshBugzillaAction(TaskListView view) {
		this.view = view;
		setText("Bugzilla Rrefresh");
        setToolTipText("Bugzilla Refresh");
        setId(ID);
        setImageDescriptor(BugzillaImages.TASK_BUG_REFRESH);
	}
	@Override
	public void run() {
		if(MylarTasksPlugin.getTaskListManager().getTaskList().getActiveTasks().size() > 0){
			MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Cannot Perform Refresh", "Please deactivate all tasks before attempting to perform a refresh since the task may disapear.");
			return;
		}
		
		ISelection selection = this.view.getViewer().getSelection();
		Object 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();
							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());
			}
		} 
	}
}

@@ -1100,73 +1100,222 @@
 lar.
-core.MylarPlugin;%0Aimport org.eclipse.mylar.tasks.MylarTasksPlugin
+bugzilla.ui.tasks.BugzillaTask;%0Aimport org.eclipse.mylar.core.MylarPlugin;%0Aimport org.eclipse.mylar.tasks.ITask;%0Aimport org.eclipse.mylar.tasks.MylarTasksPlugin;%0Aimport org.eclipse.mylar.tasks.internal.TaskCategory
 ;%0Aim
@@ -3325,14 +3325,369 @@
 %09%09%7D%0A%09%09%7D 
+else if (obj instanceof TaskCategory) %7B%0A%09%09%09TaskCategory cat = (TaskCategory) obj;%0A%09%09%09for (ITask task : cat.getChildren()) %7B%0A%09%09%09%09if (task instanceof BugzillaTask) %7B%0A%09%09%09%09%09((BugzillaTask)task).refresh();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09view.getViewer().refresh();%0A%09%09%7D else if (obj instanceof BugzillaTask) %7B%0A%09%09%09((BugzillaTask)obj).refresh();%0A%09%09%09view.getViewer().refresh();%0A%09%09%7D
 %0A%09%7D%0A%7D%0A

Refresh for categories