1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.ide.eclipse.hierarchyviewer.views;
18
19import com.android.hierarchyviewerlib.actions.InspectScreenshotAction;
20import com.android.hierarchyviewerlib.actions.LoadViewHierarchyAction;
21import com.android.hierarchyviewerlib.actions.RefreshWindowsAction;
22import com.android.hierarchyviewerlib.ui.DeviceSelector;
23import com.android.ide.eclipse.hierarchyviewer.PixelPerfectPespective;
24import com.android.ide.eclipse.hierarchyviewer.TreeViewPerspective;
25
26import org.eclipse.jface.action.IMenuManager;
27import org.eclipse.jface.action.IToolBarManager;
28import org.eclipse.swt.layout.FillLayout;
29import org.eclipse.swt.widgets.Composite;
30import org.eclipse.ui.IActionBars;
31import org.eclipse.ui.IPerspectiveDescriptor;
32import org.eclipse.ui.IPerspectiveListener;
33import org.eclipse.ui.IWorkbenchPage;
34import org.eclipse.ui.part.ViewPart;
35
36public class DeviceSelectorView extends ViewPart implements IPerspectiveListener {
37
38    public static final String ID =
39            "com.android.ide.eclipse.hierarchyviewer.views.DeviceSelectorView"; //$NON-NLS-1$
40
41    private DeviceSelector mDeviceSelector;
42
43    @Override
44    public void createPartControl(Composite parent) {
45        parent.setLayout(new FillLayout());
46
47
48        IPerspectiveDescriptor perspective = getViewSite().getPage().getPerspective();
49        boolean doTreeViewStuff = true;
50        boolean doPixelPerfectStuff = true;
51        if (perspective.getId().equals(PixelPerfectPespective.ID)) {
52            doTreeViewStuff = false;
53        } else if (perspective.getId().equals(TreeViewPerspective.ID)) {
54            doPixelPerfectStuff = false;
55        }
56        mDeviceSelector = new DeviceSelector(parent, doTreeViewStuff, doPixelPerfectStuff);
57
58        placeActions(doTreeViewStuff, doPixelPerfectStuff);
59
60        getViewSite().getWorkbenchWindow().addPerspectiveListener(this);
61    }
62
63    @Override
64    public void dispose() {
65        super.dispose();
66        getViewSite().getWorkbenchWindow().removePerspectiveListener(this);
67    }
68
69    private void placeActions(boolean doTreeViewStuff, boolean doPixelPerfectStuff) {
70        IActionBars actionBars = getViewSite().getActionBars();
71
72        IMenuManager mm = actionBars.getMenuManager();
73        mm.removeAll();
74        mm.add(RefreshWindowsAction.getAction());
75
76        IToolBarManager tm = actionBars.getToolBarManager();
77        tm.removeAll();
78        tm.add(RefreshWindowsAction.getAction());
79
80        if (doTreeViewStuff) {
81            mm.add(LoadViewHierarchyAction.getAction());
82            tm.add(LoadViewHierarchyAction.getAction());
83        }
84        if (doPixelPerfectStuff) {
85            mm.add(InspectScreenshotAction.getAction());
86            tm.add(InspectScreenshotAction.getAction());
87        }
88
89        mm.updateAll(true);
90        tm.update(true);
91        actionBars.updateActionBars();
92    }
93
94    @Override
95    public void setFocus() {
96        mDeviceSelector.setFocus();
97    }
98
99    @Override
100    public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
101        if (perspective.getId().equals(PixelPerfectPespective.ID)) {
102            mDeviceSelector.setMode(false, true);
103            placeActions(false, true);
104        } else if (perspective.getId().equals(TreeViewPerspective.ID)) {
105            mDeviceSelector.setMode(true, false);
106            placeActions(true, false);
107        } else {
108            mDeviceSelector.setMode(true, true);
109            placeActions(true, true);
110        }
111    }
112
113    @Override
114    public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective,
115            String changeId) {
116        // pass
117    }
118
119}
120