Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f8d58b74bc0e167fb7410d541c44fa0dd8121e58 (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
/*******************************************************************************
 * Copyright (c) 2006, 2010 Wind River Systems 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:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.ui.viewmodel;

import org.eclipse.core.runtime.Platform;

/**
 * Implementation of basic view model context interface.
 * <p>  The main purpose of the VMC wrapper is to re-direct adapter 
 * queries.  The redirecting of adapter queries follows this order:
 * <ol>
 * <li>If context implements the adapter itself, it is returned.</li>
 * <li>If the VM Adapter implements the adapter, the VM Adapter is returned.</li>
 * <li>If the VM Provider implements the adapter, the VM Provider is returned.</li>
 * <li>If the VM Node implements the adapter, the VM Node is returned.</li>
 * </ol>
 * </p> 
 * <p> 
 * Note: Deriving classes must override the Object.equals/hashCode methods.
 * This is because the view model context objects are just wrappers that are 
 * created by the view model on demand, so the equals methods must use the 
 * object being wrapped to perform a meaningful comparison.    
 * 
 * @since 1.0
 */
abstract public class AbstractVMContext implements IVMContext {
    protected final IVMNode fNode;
    
    public AbstractVMContext(IVMNode node) {
        fNode = node;
    }
    
    public IVMNode getVMNode() { return fNode; }

    @SuppressWarnings("rawtypes")
    public Object getAdapter(Class adapter) {
        // If the context implements the given adapter directly, it always takes
        // precedence.
        if (adapter.isInstance(this)) {
            return this;
        }
        
        IVMProvider vmProvider = getVMNode().getVMProvider();
        IVMAdapter vmAdapter = vmProvider.getVMAdapter();
        if (adapter.isInstance(vmAdapter)) {
            return vmAdapter;
        } else if (adapter.isInstance(vmProvider)) {
            return vmProvider;
        } else if (adapter.isInstance(getVMNode())) {
            return getVMNode();
        }
        return Platform.getAdapterManager().getAdapter(this, adapter);
    }

    /** Deriving classes must override. */
    @Override
    abstract public boolean equals(Object obj);
    
    /** Deriving classes must override. */
    @Override
    abstract public int hashCode();
}

Back to the top