Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9be142bd967f24c1d42b54772d26b01167e80551 (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) 2008 Ericsson 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:
 *     Ericsson - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.gdb.internal.ui.actions;

import org.eclipse.cdt.dsf.debug.ui.viewmodel.actions.AbstractVMProviderActionDelegate;
import org.eclipse.cdt.dsf.gdb.actions.IConnect;
import org.eclipse.cdt.dsf.gdb.launching.GdbLaunch;
import org.eclipse.cdt.dsf.ui.viewmodel.datamodel.IDMVMContext;
import org.eclipse.debug.ui.contexts.DebugContextEvent;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IViewPart;

/*
 * Action to trigger a prompt for a process to attach to
 */
public class ConnectActionDelegate extends AbstractVMProviderActionDelegate {
	
	/*
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	public void run(IAction action) {
		if (action.isEnabled()) {
			// disable the action so it cannot be run again until an event or 
			// selection change updates the enablement
			action.setEnabled(false);

			final IConnect connectCommand = getConnectCommand();
			if (connectCommand != null) {
				connectCommand.connect(null);
			}
		}
	}

    @Override
    public void init(IViewPart view) {
        super.init(view);
		updateEnablement();
    }
    
    @Override
    public void debugContextChanged(DebugContextEvent event) {
        super.debugContextChanged(event);
		updateEnablement();
    }

    @Override
	public void selectionChanged(IAction action, ISelection selection) {
		super.selectionChanged(action, selection);
		updateEnablement();
	}

	private void updateEnablement() {
		boolean enabled = false;
		final IConnect connectCommand = getConnectCommand();
		if (connectCommand != null) {
			enabled = connectCommand.canConnect();
		}
		getAction().setEnabled(enabled);
	}

	private IConnect getConnectCommand() {
		IConnect command = null;
		Object element = getViewerInput();
		if (element instanceof IDMVMContext) {
			IDMVMContext dmc = (IDMVMContext)element;
			command = (IConnect)dmc.getAdapter(IConnect.class);
		} else if (element instanceof GdbLaunch) {
			GdbLaunch launch = (GdbLaunch)element;
			command = (IConnect)launch.getSession().getModelAdapter(IConnect.class);
		}

		return command;
	}
}

Back to the top