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 
17 package com.android.ide.eclipse.hierarchyviewer;
18 
19 import com.android.hierarchyviewerlib.HierarchyViewerDirector;
20 import com.android.hierarchyviewerlib.device.IHvDevice;
21 import com.android.hierarchyviewerlib.models.Window;
22 import com.android.ide.eclipse.hierarchyviewer.views.PixelPerfectTreeView;
23 import com.android.ide.eclipse.hierarchyviewer.views.PropertyView;
24 
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.core.runtime.jobs.ISchedulingRule;
29 import org.eclipse.core.runtime.jobs.Job;
30 import org.eclipse.ui.IWorkbenchPage;
31 import org.eclipse.ui.IWorkbenchPart;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.PartInitException;
34 
35 public class HierarchyViewerPluginDirector extends HierarchyViewerDirector {
36 
createDirector()37     public static HierarchyViewerDirector createDirector() {
38         return sDirector = new HierarchyViewerPluginDirector();
39     }
40 
41     @Override
executeInBackground(final String taskName, final Runnable task)42     public void executeInBackground(final String taskName, final Runnable task) {
43         Job job = new Job(taskName) {
44             @Override
45             protected IStatus run(IProgressMonitor monitor) {
46                 task.run();
47                 return Status.OK_STATUS;
48             }
49         };
50         job.setPriority(Job.SHORT);
51         job.setRule(mSchedulingRule);
52         job.schedule();
53     }
54 
55     private ISchedulingRule mSchedulingRule = new ISchedulingRule() {
56         @Override
57         public boolean contains(ISchedulingRule rule) {
58             return rule == this;
59         }
60 
61         @Override
62         public boolean isConflicting(ISchedulingRule rule) {
63             return rule == this;
64         }
65 
66     };
67 
68     @Override
getAdbLocation()69     public String getAdbLocation() {
70         return HierarchyViewerPlugin.getPlugin().getPreferenceStore().getString(
71                 HierarchyViewerPlugin.ADB_LOCATION);
72     }
73 
74     @Override
loadViewTreeData(Window window)75     public void loadViewTreeData(Window window) {
76         super.loadViewTreeData(window);
77 
78         // The windows tab hides the property tab, so let's bring the property
79         // tab
80         // forward.
81 
82         IWorkbenchWindow[] windows =
83                 HierarchyViewerPlugin.getPlugin().getWorkbench().getWorkbenchWindows();
84         for (IWorkbenchWindow currentWindow : windows) {
85             IWorkbenchPage page = currentWindow.getActivePage();
86             if (page.getPerspective().getId().equals(TreeViewPerspective.ID)) {
87                 try {
88                     IWorkbenchPart part = page.findView(PropertyView.ID);
89                     if (part != null) {
90                         page.showView(PropertyView.ID);
91                     }
92                 } catch (PartInitException e) {
93 
94                 }
95             }
96         }
97     }
98 
99     @Override
loadPixelPerfectData(IHvDevice device)100     public void loadPixelPerfectData(IHvDevice device) {
101         super.loadPixelPerfectData(device);
102 
103         // The windows tab hides the tree tab, so let's bring the tree tab
104         // forward.
105 
106         IWorkbenchWindow[] windows =
107                 HierarchyViewerPlugin.getPlugin().getWorkbench().getWorkbenchWindows();
108         for (IWorkbenchWindow window : windows) {
109             IWorkbenchPage page = window.getActivePage();
110             if (page.getPerspective().getId().equals(PixelPerfectPespective.ID)) {
111                 try {
112                     IWorkbenchPart part = page.findView(PixelPerfectTreeView.ID);
113                     if (part != null) {
114                         page.showView(PixelPerfectTreeView.ID);
115                     }
116                 } catch (PartInitException e) {
117 
118                 }
119             }
120         }
121     }
122 }
123