Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5ea371e3b8a210d2f7124a55c1aaa131d114510e (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
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.actions.context;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IDisconnect;
import org.eclipse.debug.internal.ui.actions.provisional.IAsynchronousDisconnectAdapter;
import org.eclipse.debug.internal.ui.actions.provisional.IBooleanRequestMonitor;
import org.eclipse.debug.internal.ui.viewers.provisional.IAsynchronousRequestMonitor;
import org.eclipse.debug.ui.IDebugUIConstants;

/**
 * Default disconnect adapter for standard debug model.
 * 
 * @since 3.2
 */
public class DisconnectAdapter extends StandardActionAdapter implements IAsynchronousDisconnectAdapter {

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.ui.actions.provisional.IAsynchronousDisconnectAdapter#canDisconnect(java.lang.Object, org.eclipse.debug.internal.ui.actions.provisional.IBooleanRequestMonitor)
	 */
	public void canDisconnect(final Object element, final IBooleanRequestMonitor requestMonitor) {
		Job job = new Job("canDisconnect") { //$NON-NLS-1$
			protected IStatus run(IProgressMonitor monitor) {
				IDisconnect disconnect = getTarget(element);
				if (disconnect != null)
					requestMonitor.setResult(disconnect.canDisconnect());
				else
					requestMonitor.setResult(false);
				requestMonitor.done();
				return Status.OK_STATUS;
			}
		};
		job.setSystem(true);
		job.setRule(createUpdateSchedulingRule());
		job.schedule();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.ui.actions.provisional.IAsynchronousDisconnectAdapter#isDisconnected(java.lang.Object, org.eclipse.debug.internal.ui.actions.provisional.IBooleanRequestMonitor)
	 */
	public void isDisconnected(final Object element, final IBooleanRequestMonitor requestMonitor) {
		Job job = new Job("isDisconnected") { //$NON-NLS-1$
			protected IStatus run(IProgressMonitor monitor) {
				IDisconnect disconnect = getTarget(element);
				if (disconnect != null) {
					requestMonitor.setResult(disconnect.isDisconnected());
				} else {
					requestMonitor.setResult(false);
				}
				requestMonitor.done();
				return Status.OK_STATUS;
			}
		};
		job.setSystem(true);
		job.setRule(createUpdateSchedulingRule());
		job.schedule();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.ui.actions.provisional.IAsynchronousDisconnectAdapter#disconnect(java.lang.Object, org.eclipse.debug.internal.ui.viewers.provisional.IAsynchronousRequestMonitor)
	 */
	public void disconnect(final Object element, final IAsynchronousRequestMonitor requestMonitor) {
		Job job = new Job("isDisconnected") { //$NON-NLS-1$
			protected IStatus run(IProgressMonitor monitor) {
				IDisconnect disconnect = getTarget(element);

				if (disconnect == null) {
					requestMonitor.setStatus(new Status(IStatus.ERROR, IDebugUIConstants.PLUGIN_ID, IDebugUIConstants.INTERNAL_ERROR, "element must be an instance of or adapt to IDisconnect", //$NON-NLS-1$
							null));
				} else {
					try {
						disconnect.disconnect();
					} catch (DebugException e) {
						requestMonitor.setStatus(e.getStatus());
					}
				}
				requestMonitor.done();
				return Status.OK_STATUS;
			}
		};
		job.setSystem(true);
		job.schedule();
	}

	private IDisconnect getTarget(Object element) {
		if (element instanceof IDisconnect) {
			return (IDisconnect) element;
		} else if (element instanceof IAdaptable) {
			return (IDisconnect) ((IAdaptable) element).getAdapter(IDisconnect.class);
		}
		return null;
	}

}

Back to the top