KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > update > DefaultSelectionPolicy


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.viewers.update;
12
13 import org.eclipse.debug.core.model.IDebugElement;
14 import org.eclipse.debug.core.model.IStackFrame;
15 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelSelectionPolicy;
16 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
17 import org.eclipse.debug.ui.IDebugUIConstants;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20
21 /**
22  * Default selection policy for the debug view.
23  *
24  * @since 3.2
25  */

26 public class DefaultSelectionPolicy implements IModelSelectionPolicy {
27     
28     private IDebugElement fDebugElement;
29     
30     /**
31      * Constructs a new selection policy for the given debug
32      * element.
33      *
34      * @param element
35      */

36     public DefaultSelectionPolicy(IDebugElement element) {
37         fDebugElement = element;
38     }
39
40     /* (non-Javadoc)
41      * @see org.eclipse.debug.ui.viewers.IModelSelectionPolicy#contains(org.eclipse.jface.viewers.ISelection, org.eclipse.debug.ui.viewers.IPresentationContext)
42      */

43     public boolean contains(ISelection selection, IPresentationContext context) {
44         if (IDebugUIConstants.ID_DEBUG_VIEW.equals(context.getId())) {
45             if (selection instanceof IStructuredSelection) {
46                 IStructuredSelection ss = (IStructuredSelection) selection;
47                 Object JavaDoc element = ss.getFirstElement();
48                 if (element instanceof IDebugElement) {
49                     IDebugElement debugElement = (IDebugElement) element;
50                     return fDebugElement.getDebugTarget().equals(debugElement.getDebugTarget());
51                 }
52             }
53         }
54         return false;
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.debug.ui.viewers.IModelSelectionPolicy#overrides(org.eclipse.jface.viewers.ISelection, org.eclipse.jface.viewers.ISelection, org.eclipse.debug.ui.viewers.IPresentationContext)
59      */

60     public boolean overrides(ISelection existing, ISelection candidate, IPresentationContext context) {
61         if (IDebugUIConstants.ID_DEBUG_VIEW.equals(context.getId())) {
62             if (existing instanceof IStructuredSelection && candidate instanceof IStructuredSelection) {
63                 IStructuredSelection ssExisting = (IStructuredSelection) existing;
64                 IStructuredSelection ssCandidate = (IStructuredSelection) candidate;
65                 return overrides(ssExisting.getFirstElement(), ssCandidate.getFirstElement());
66             }
67         }
68         return true;
69     }
70     
71     protected boolean overrides(Object JavaDoc existing, Object JavaDoc candidate) {
72         if (existing == null) {
73             return true;
74         }
75         if (existing.equals(candidate)) {
76             return true;
77         }
78         if (existing instanceof IStackFrame && candidate instanceof IStackFrame) {
79             IStackFrame curr = (IStackFrame) existing;
80             IStackFrame next = (IStackFrame) candidate;
81             return curr.getThread().equals(next.getThread()) || !isSticky(existing);
82         }
83         return !isSticky(existing);
84     }
85
86     /* (non-Javadoc)
87      * @see org.eclipse.debug.ui.viewers.IModelSelectionPolicy#isSticky(org.eclipse.jface.viewers.ISelection, org.eclipse.debug.ui.viewers.IPresentationContext)
88      */

89     public boolean isSticky(ISelection selection, IPresentationContext context) {
90         if (IDebugUIConstants.ID_DEBUG_VIEW.equals(context.getId())) {
91             if (selection instanceof IStructuredSelection) {
92                 return isSticky(((IStructuredSelection)selection).getFirstElement());
93             }
94         }
95         return false;
96     }
97     
98     protected boolean isSticky(Object JavaDoc element) {
99         if (element instanceof IStackFrame) {
100             IStackFrame frame = (IStackFrame) element;
101             return frame.isSuspended();
102         }
103         return false;
104     }
105
106     /* (non-Javadoc)
107      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IModelSelectionPolicy#handleInvalidSelection(org.eclipse.jface.viewers.ISelection, org.eclipse.jface.viewers.ISelection)
108      */

109     public ISelection replaceInvalidSelection(ISelection selection, ISelection newSelection) {
110         return newSelection;
111     }
112 }
113
Popular Tags