Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bca4fe9f4b3ae27ca7699b5d5294f11c71b8f6d5 (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
/*******************************************************************************
 * Copyright (c) 2015 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.filesystem.ui.internal.handlers;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.runtime.IFSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.ui.nls.Messages;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * Handler for adding a file or folder to the favorites
 */
public class ToggleRevealOnConnectContributionItem extends ActionContributionItem {

    private static class ToggleAction extends Action {
        public ToggleAction() {
        	super(Messages.ToggleRevealOnConnectContributionItem_text, IAction.AS_CHECK_BOX);
        }

        @Override
        public void run() {
			boolean value = isChecked();
        	IStructuredSelection selection = getSelection();
        	if (selection != null) {
        		for (Object o : selection.toList()) {
        			if (o instanceof IFSTreeNode) {
						((IFSTreeNode) o).setRevealOnConnect(value);
        			}
				}
        	}
        }

    }

	public ToggleRevealOnConnectContributionItem() {
	    super(new ToggleAction());
    }

	@Override
	public void fill(Menu parent, int index) {
		updateAction();
	    super.fill(parent, index);
	}

	protected static IStructuredSelection getSelection() {
		IWorkbenchWindow ww = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (ww == null)
			return null;

		ISelection selection = ww.getSelectionService().getSelection();
		if (selection instanceof IStructuredSelection && !selection.isEmpty())
			return (IStructuredSelection) selection;

		return null;
    }

	private void updateAction() {
		boolean enabled = false;
		int on = 0;

		IStructuredSelection selection = getSelection();
		if (selection != null) {
			for (Object o : selection.toList()) {
				if (!(o instanceof IFSTreeNode)) {
					enabled = false;
					break;
				}
				IFSTreeNode node = (IFSTreeNode) o;
				if (node.isFileSystem()) {
					enabled = false;
					break;
				}
				on += node.isRevealOnConnect() ? 1 : -1;
				enabled = true;
			}
		}
		IAction action = getAction();
		action.setEnabled(enabled);
		action.setChecked(on > 0);
    }
}

Back to the top