Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 16de861281bbcb4d3949bb95552a23c0ff9da522 (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
/*******************************************************************************
 * Copyright (c) 2015 Red Hat.
 * 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:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.internal.vagrant.ui.commands;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.expressions.EvaluationContext;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.linuxtools.internal.vagrant.ui.Activator;
import org.eclipse.linuxtools.internal.vagrant.ui.SWTImagesFactory;
import org.eclipse.linuxtools.vagrant.core.EnumVMStatus;
import org.eclipse.linuxtools.vagrant.core.IVagrantConnection;
import org.eclipse.linuxtools.vagrant.core.IVagrantVM;
import org.eclipse.linuxtools.vagrant.core.IVagrantVMListener;
import org.eclipse.linuxtools.vagrant.core.VagrantService;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.ISources;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.IHandlerService;

public class VagrantToolBarContributionItem extends ContributionItem
		implements IVagrantVMListener {

	private List<IVagrantVM> vms = new ArrayList<>();

	public VagrantToolBarContributionItem() {
		IVagrantConnection conn = VagrantService.getInstance();
		conn.addVMListener(this);
		Thread t = new Thread(() -> {
			vms = conn.getVMs();
		});
		t.start();
	}

	public VagrantToolBarContributionItem(String id) {
		super(id);
	}

	@Override
	public void fill(Menu menu, int index) {
		// The menu passed in doesn't allow us to have sub-menus
		// Ignore it, get the parent IMenuManager and create the structure
		if (getParent() instanceof IMenuManager) {
			IMenuManager mm = (IMenuManager) getParent();
			IContributionItem v = mm.find(getId());
			// Menu manager contributions get aggregated so remove them first
			mm.removeAll();
			// dynamic menu contribution ensures fill() gets called
			mm.add(v);
			for (IVagrantVM vm : vms) {
				EnumVMStatus containerStatus = EnumVMStatus.fromStatusMessage(vm.state());
				ImageDescriptor img = (containerStatus == EnumVMStatus.RUNNING)
						? SWTImagesFactory.DESC_CONTAINER_STARTED
						: SWTImagesFactory.DESC_CONTAINER_STOPPED;
				IMenuManager vmM = new MenuManager(vm.name(), img, null);
				for (IAction act : getApplicableActions(vm)) {
					vmM.add(act);
				}
				mm.add(vmM);
			}
		}
	}

	private List<IAction> getApplicableActions(IVagrantVM vm) {
		final EnumVMStatus containerStatus = EnumVMStatus.fromStatusMessage(vm.state());
		List<IAction> list = new ArrayList<>();
		if (containerStatus == EnumVMStatus.RUNNING) {
			list.add(createAction(Messages.VagrantToolBarContributionItem_stop,
					"org.eclipse.linuxtools.vagrant.ui.commands.stopVM", //$NON-NLS-1$
					SWTImagesFactory.DESC_STOP, vm));
			list.add(createAction(Messages.VagrantToolBarContributionItem_destroy,
					"org.eclipse.linuxtools.vagrant.ui.commands.destroyVM", //$NON-NLS-1$
					SWTImagesFactory.DESC_REMOVE, vm));
			list.add(createAction(Messages.VagrantToolBarContributionItem_ssh,
					"org.eclipse.linuxtools.vagrant.ui.commands.sshVM", //$NON-NLS-1$
					SWTImagesFactory.DESC_CONSOLE, vm));
		} else {
			list.add(createAction(Messages.VagrantToolBarContributionItem_start,
					"org.eclipse.linuxtools.vagrant.ui.commands.startVM", //$NON-NLS-1$
					SWTImagesFactory.DESC_START, vm));
		}
		list.add(createAction(Messages.VagrantToolBarContributionItem_package,
				"org.eclipse.linuxtools.vagrant.ui.commands.packageVM", //$NON-NLS-1$
				SWTImagesFactory.DESC_BUILD, vm));
		list.add(createAction(Messages.VagrantToolBarContributionItem_open,
				"org.eclipse.linuxtools.vagrant.ui.commands.openVFile", //$NON-NLS-1$
				SWTImagesFactory.DESC_FILE, vm));
		return list;
	}

	private IAction createAction(String label, String id, ImageDescriptor img, IVagrantVM vm) {
		return new Action(label, img) {
			@Override
			public void run() {
				execute(id, new StructuredSelection(vm));
			}
		};
	}

	private void execute(String id, IStructuredSelection selection) {
		ICommandService service = PlatformUI.getWorkbench().getService(ICommandService.class);
		Command command = service != null ? service.getCommand(id) : null;
		if (command != null && command.isDefined()) {
			try {
				ParameterizedCommand pCmd = ParameterizedCommand.generateCommand(command, null);
				IHandlerService handlerSvc = PlatformUI.getWorkbench().getService(IHandlerService.class);
				IEvaluationContext ctx = handlerSvc.getCurrentState();
				ctx = new EvaluationContext(ctx, selection);
				ctx.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
				handlerSvc.executeCommandInContext(pCmd, null, ctx);
			} catch (Exception e) {
				Activator.log(e);
			}
		}
	}

	@Override
	public boolean isDynamic() {
		return true;
	}

	@Override
	public boolean isDirty() {
		return true;
	}

	@Override
	public void listChanged(IVagrantConnection connection,
			List<IVagrantVM> list) {
		this.vms = list;
		// Need to call update(false) so menu manager is up to date
		if (getParent() instanceof IMenuManager) {
			IMenuManager mm = (IMenuManager) getParent();
			Display.getDefault().asyncExec(() -> {
				mm.update(false);
			});
		}
	}
}

Back to the top