summaryrefslogtreecommitdiff
path: root/src/plugins/emulator/src/com/motorola/studio/android/emulator/core/emulationui/AbstractEmuCtProvider.java
blob: 8f9b09419288f1d7c2d1c739d2d2ec85d8344a10 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.motorola.studio.android.emulator.core.emulationui;

import static com.motorola.studio.android.common.log.StudioLogger.warn;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.ui.IViewSite;

import com.motorola.studio.android.emulator.core.devfrm.DeviceFrameworkManager;

/**
 * DESCRIPTION:
 * This class is the abstract parent of all emulation views content providers
 *
 * RESPONSIBILITY:
 * Provide common method implementation for the several emulation views
 * content providers
 *
 * COLABORATORS:
 * None.
 *
 * USAGE:
 * The class is used when a emulation view content provider is instantiated
 */
public abstract class AbstractEmuCtProvider implements ITreeContentProvider
{
    /**
     * The id to be used when constructing a "sent to" node
     */
    public static final String SENT_TO_EMULATOR_ID = "sent_to";

    /**
     * The id to be used when constructing a "receive from" node
     */
    public static final String RECEIVE_FROM_EMULATOR_ID = "received_from";

    /**
     * The parent of the entire tree
     */
    private static IViewSite treeParent;

    /**
     * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(Object)
     */
    public Object getParent(Object element)
    {

        Object parent = null;

        if (element instanceof EmuViewerNode)
        {
            EmuViewerNode nodeElement = (EmuViewerNode) element;

            if (nodeElement instanceof EmuViewerRootNode)
            {
                // The IViewSite object is the parent of the whole tree
                parent = treeParent;
            }
            else
            {
                parent = nodeElement.getParent();
            }
        }
        else
        {
            warn("Tried to get parent of an object that is not an emulation tree node");
        }

        return parent;
    }

    /**
     * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(Object)
     */
    public boolean hasChildren(Object object)
    {

        boolean hasChildren = false;

        if (object instanceof EmuViewerNode)
        {
            EmuViewerNode nodeObject = (EmuViewerNode) object;

            // The node has children if its children collection is bigger than 0 in size
            hasChildren = (nodeObject.getChildren().size() > 0);

        }
        else
        {
            warn("Tried to test if an object that is not an emulation tree node has children");
        }

        return hasChildren;
    }

    /**
     * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(Object)
     */
    public Object[] getElements(Object parent)
    {

        Object[] elements;

        if (parent instanceof IViewSite)
        {

            if (treeParent == null)
            {
                // Sets the treeParent attribute to store which view site is the parent of the
                // whole tree. This is done only once for each content provider instance.
                // Each provider instance is supposed to be used with a single view
                treeParent = (IViewSite) parent;
            }

            Collection<EmuViewerRootNode> emuNodeCollection = new HashSet<EmuViewerRootNode>();

            Set<String> hostSet =
                    DeviceFrameworkManager.getInstance().getAllStartedInstancesHosts();

            // A root node will be added per active emulator at the tree viewer
            for (String host : hostSet)
            {
                EmuViewerRootNode node = new EmuViewerRootNode(host);
                emuNodeCollection.add(node);

                addChildrenToRootNode(node);
            }

            // Creating the array of elements (in this case, emulator root nodes) to be
            // returned, when the parent is the view site itself
            Object[] emuNodeArray = emuNodeCollection.toArray(new Object[emuNodeCollection.size()]);
            elements = emuNodeArray;

        }
        else
        {
            // When elements different from the view site are provided, the elements will be 
            // their children
            elements = getChildren(parent);
        }

        return elements;
    }

    /**
     * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(Object)
     */
    public Object[] getChildren(Object parent)
    {

        Set<EmuViewerNode> childrenCollection;
        Object[] returnArray;

        if (parent instanceof EmuViewerNode)
        {
            // Firstly, try to retrieve the parent's children by means of the appropriate method
            EmuViewerNode parentNode = (EmuViewerNode) parent;

            childrenCollection = parentNode.getChildren();

            // If the provided element is an emulator root node, it is needed to test if the
            // intermediate nodes were already created (they are not created in the first request).
            // If they were not created, assure that when the content framework requests, the 
            // intermediate nodes will be found. 
            //
            // This procedure guarantees that once an emulator is started, it has the intermediate
            // nodes constructed even if no emulation is being performed. 
            if (parentNode instanceof EmuViewerRootNode)
            {
                String host = ((EmuViewerRootNode) parentNode).getEmulatorIdentifier();
                for (EmuViewerNode child : childrenCollection)
                {
                    if (child.getChildren().size() == 0)
                    {

                        addChildrenToLeafParentNode(child, host);
                    }
                }
            }

            // Creating the array of elements to be returned
            returnArray = childrenCollection.toArray(new EmuViewerNode[childrenCollection.size()]);

        }
        else
        {
            warn("Tried to get children of an object that is not an emulation tree node");
            returnArray = new Object[0];
        }

        return returnArray;
    }

    /**
     * @see org.eclipse.jface.viewers.IContentProvider#dispose()
     */
    public void dispose()
    {
        // Do nothing
    }

    /**
     * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(Viewer, Object, Object)
     */
    public void inputChanged(Viewer v, Object oldInput, Object newInput)
    {
        // Do nothing
    }

    /**
     * Given a root node, adds children nodes to it
     * 
     * @param root The root node that will receive the children nodes
     */
    protected void addChildrenToRootNode(EmuViewerRootNode root)
    {

        root.addChild(new EmuViewerNode(root, RECEIVE_FROM_EMULATOR_ID));
        root.addChild(new EmuViewerNode(root, SENT_TO_EMULATOR_ID));
    }

    /**
     * Given a node, adds children leaf nodes to it
     * 
     * @param leafParentNode The node that will receive the children leaf nodes
     * @param host The identifier of the emulator that owns this sub-tree
     */
    protected abstract void addChildrenToLeafParentNode(EmuViewerNode leafParentNode, String host);
}