Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6fcc16b0df06c1c7298d11951bab70de41e6c1ad (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
/*******************************************************************************
 * Copyright (c) 2012 Frank Becker 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:
 *     Frank Becker - initial API and implementation
 *     Tasktop Technologies - improvements
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.commands;

import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.ui.menus.UIElement;

/**
 * @author Frank Becker
 * @author Steffen Pingel
 */
public class DisconnectHandler extends AbstractHandler implements IElementUpdater {

	public Object execute(ExecutionEvent event) throws ExecutionException {
		Command command0 = event.getCommand();

		boolean oldValue = HandlerUtil.toggleCommandState(command0);
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection instanceof IStructuredSelection) {
			for (Iterator<?> iter = ((IStructuredSelection) selection).iterator(); iter.hasNext();) {
				Object item = iter.next();
				if (item instanceof TaskRepository) {
					((TaskRepository) item).setOffline(!oldValue);
					TasksUiPlugin.getRepositoryManager().notifyRepositorySettingsChanged((TaskRepository) item);
				}
			}
		}
		return null;
	}

	public void updateElement(UIElement element, @SuppressWarnings("rawtypes")
	Map parameters) {
		IWorkbenchWindow window = (IWorkbenchWindow) element.getServiceLocator().getService(IWorkbenchWindow.class);
		if (window != null) {
			IWorkbenchPage activePage = ((WorkbenchWindow) window).getActivePage();
			if (activePage != null) {
				IWorkbenchPart activePart = activePage.getActivePart();
				if (activePart != null) {
					ISelectionProvider selectionProvider = activePart.getSite().getSelectionProvider();
					if (selectionProvider != null) {
						ISelection selection = selectionProvider.getSelection();
						if (selection instanceof IStructuredSelection) {
							// only for enabled Handlers the updateElement is called
							// so we only need the first repository for set the state
							Object firstRepository = ((IStructuredSelection) selection).getFirstElement();
							if (firstRepository instanceof TaskRepository) {
								boolean checked = ((TaskRepository) firstRepository).isOffline();
								element.setChecked(checked);
							}
						}
					}
				}
			}
		}
	}
}

Back to the top