Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 554992cd682632614053bbcba91713a23d6142c0 (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
/*******************************************************************************
 * Copyright (c) 2005, 2011 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
 *     Pawel Piech (Wind River) - Bug fixing
 *******************************************************************************/
package org.eclipse.debug.internal.ui.viewers.update;

import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IStackFrame;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelSelectionPolicy;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;

/**
 * Default selection policy for the debug view.
 *
 * @since 3.2
 */
public class DefaultSelectionPolicy implements IModelSelectionPolicy {

	private IDebugElement fDebugElement;

	/**
	 * Constructs a new selection policy for the given debug
	 * element.
	 *
	 * @param element the backing debug element
	 */
	public DefaultSelectionPolicy(IDebugElement element) {
		fDebugElement = element;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.viewers.IModelSelectionPolicy#contains(org.eclipse.jface.viewers.ISelection, org.eclipse.debug.ui.viewers.IPresentationContext)
	 */
	@Override
	public boolean contains(ISelection selection, IPresentationContext context) {
		if (IDebugUIConstants.ID_DEBUG_VIEW.equals(context.getId())) {
			if (selection instanceof IStructuredSelection) {
				IStructuredSelection ss = (IStructuredSelection) selection;
				Object element = ss.getFirstElement();
				if (element instanceof IDebugElement) {
					IDebugElement debugElement = (IDebugElement) element;
					return fDebugElement.getDebugTarget().equals(debugElement.getDebugTarget());
				}
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.viewers.IModelSelectionPolicy#overrides(org.eclipse.jface.viewers.ISelection, org.eclipse.jface.viewers.ISelection, org.eclipse.debug.ui.viewers.IPresentationContext)
	 */
	@Override
	public boolean overrides(ISelection existing, ISelection candidate, IPresentationContext context) {
		if (IDebugUIConstants.ID_DEBUG_VIEW.equals(context.getId())) {
			if (existing instanceof IStructuredSelection && candidate instanceof IStructuredSelection) {
				IStructuredSelection ssExisting = (IStructuredSelection) existing;
				IStructuredSelection ssCandidate = (IStructuredSelection) candidate;
				return overrides(ssExisting.getFirstElement(), ssCandidate.getFirstElement());
			}
		}
		return true;
	}

	protected boolean overrides(Object existing, Object candidate) {
		if (existing == null) {
			return true;
		}
		if (existing.equals(candidate)) {
			return true;
		}
		if (existing instanceof IStackFrame && candidate instanceof IStackFrame) {
			IStackFrame curr = (IStackFrame) existing;
			IStackFrame next = (IStackFrame) candidate;
			return curr.getThread().equals(next.getThread()) || !isSticky(existing);
		}
		return !isSticky(existing);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.ui.viewers.IModelSelectionPolicy#isSticky(org.eclipse.jface.viewers.ISelection, org.eclipse.debug.ui.viewers.IPresentationContext)
	 */
	@Override
	public boolean isSticky(ISelection selection, IPresentationContext context) {
		if (IDebugUIConstants.ID_DEBUG_VIEW.equals(context.getId())) {
			if (selection instanceof IStructuredSelection) {
				return isSticky(((IStructuredSelection)selection).getFirstElement());
			}
		}
		return false;
	}

	/**
	 * Returns if the selection should remain on the given selection
	 * @param element the element to check
	 * @return <code>true</code> if the selection should remain on the given element <code>false</code> otherwise
	 */
	protected boolean isSticky(Object element) {
		if (element instanceof IStackFrame) {
			IStackFrame frame = (IStackFrame) element;
			return frame.isSuspended() || frame.isStepping();
		}
		return false;
	}

    /**
     * If an attempt is made to select an invalid element, it usually indicates that the
     * currently selected element was removed from the model.  Instead of leaving the
     * selection empty, attempt to select the parent element instead.
     *
     * @param selection the selection to replace
     * @param newSelection the selection to use if the given selection is not an {@link ITreeSelection}
     * @return the replaced selection or <code>newSelection</code> if the given selection is not an {@link ITreeSelection}
     *
     * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IModelSelectionPolicy#replaceInvalidSelection(ISelection, ISelection)
     */
    @Override
	public ISelection replaceInvalidSelection(ISelection selection, ISelection newSelection) {
        if (selection instanceof ITreeSelection) {
            TreePath[] paths = ((ITreeSelection)selection).getPaths();
            if (paths.length > 0 && paths[0].getSegmentCount() > 1) {
                return new TreeSelection(paths[0].getParentPath());
            }
        }
        return newSelection;
    }
}

Back to the top