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.views;
18 
19 import com.android.ddmuilib.ImageLoader;
20 import com.android.hierarchyviewerlib.HierarchyViewerDirector;
21 import com.android.hierarchyviewerlib.actions.PixelPerfectAutoRefreshAction;
22 import com.android.hierarchyviewerlib.models.PixelPerfectModel;
23 import com.android.hierarchyviewerlib.models.PixelPerfectModel.IImageChangeListener;
24 import com.android.hierarchyviewerlib.ui.PixelPerfectControls;
25 import com.android.hierarchyviewerlib.ui.PixelPerfectLoupe;
26 import com.android.hierarchyviewerlib.ui.PixelPerfectPixelPanel;
27 
28 import org.eclipse.jface.action.Action;
29 import org.eclipse.jface.action.IMenuManager;
30 import org.eclipse.jface.action.IToolBarManager;
31 import org.eclipse.jface.resource.ImageDescriptor;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.graphics.Image;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.ui.IActionBars;
39 import org.eclipse.ui.part.ViewPart;
40 
41 public class PixelPerfectLoupeView extends ViewPart implements IImageChangeListener {
42 
43     public static final String ID =
44             "com.android.ide.eclipse.hierarchyviewer.views.PixelPerfectLoupeView"; //$NON-NLS-1$
45 
46     private PixelPerfectLoupe mPixelPerfectLoupe;
47 
48     private Action mShowInLoupeAction = new Action("&Show Overlay", Action.AS_CHECK_BOX) {
49         @Override
50         public void run() {
51             mPixelPerfectLoupe.setShowOverlay(isChecked());
52         }
53     };
54     @Override
createPartControl(Composite parent)55     public void createPartControl(Composite parent) {
56         mShowInLoupeAction.setAccelerator(SWT.MOD1 + 'S');
57         ImageLoader imageLoader = ImageLoader.getLoader(HierarchyViewerDirector.class);
58         Image image = imageLoader.loadImage("show-overlay.png", Display.getDefault()); //$NON-NLS-1$
59         mShowInLoupeAction.setImageDescriptor(ImageDescriptor.createFromImage(image));
60         mShowInLoupeAction.setToolTipText("Show the overlay in the loupe view");
61         mShowInLoupeAction.setEnabled(PixelPerfectModel.getModel().getOverlayImage() != null);
62         PixelPerfectModel.getModel().addImageChangeListener(this);
63 
64         GridLayout loupeLayout = new GridLayout();
65         loupeLayout.marginWidth = loupeLayout.marginHeight = 0;
66         loupeLayout.horizontalSpacing = loupeLayout.verticalSpacing = 0;
67         parent.setLayout(loupeLayout);
68 
69         Composite pixelPerfectLoupeBorder = new Composite(parent, SWT.BORDER);
70         pixelPerfectLoupeBorder.setLayoutData(new GridData(GridData.FILL_BOTH));
71         GridLayout pixelPerfectLoupeBorderGridLayout = new GridLayout();
72         pixelPerfectLoupeBorderGridLayout.marginWidth =
73                 pixelPerfectLoupeBorderGridLayout.marginHeight = 0;
74         pixelPerfectLoupeBorderGridLayout.horizontalSpacing =
75                 pixelPerfectLoupeBorderGridLayout.verticalSpacing = 0;
76         pixelPerfectLoupeBorder.setLayout(pixelPerfectLoupeBorderGridLayout);
77 
78         mPixelPerfectLoupe = new PixelPerfectLoupe(pixelPerfectLoupeBorder);
79         mPixelPerfectLoupe.setLayoutData(new GridData(GridData.FILL_BOTH));
80 
81         PixelPerfectPixelPanel pixelPerfectPixelPanel =
82                 new PixelPerfectPixelPanel(pixelPerfectLoupeBorder);
83         pixelPerfectPixelPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
84 
85         // https://code.google.com/p/android/issues/detail?id=69199
86         // Disabled until accessibility issues are fixed
87         // PixelPerfectControls pixelPerfectControls =
88         //         new PixelPerfectControls(parent);
89         // pixelPerfectControls.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
90 
91         placeActions();
92     }
93 
placeActions()94     private void placeActions() {
95         IActionBars actionBars = getViewSite().getActionBars();
96 
97         IMenuManager mm = actionBars.getMenuManager();
98         mm.removeAll();
99         mm.add(PixelPerfectAutoRefreshAction.getAction());
100         mm.add(mShowInLoupeAction);
101 
102         IToolBarManager tm = actionBars.getToolBarManager();
103         tm.removeAll();
104         tm.add(PixelPerfectAutoRefreshAction.getAction());
105         tm.add(mShowInLoupeAction);
106     }
107 
108     @Override
dispose()109     public void dispose() {
110         super.dispose();
111         PixelPerfectModel.getModel().removeImageChangeListener(this);
112     }
113 
114     @Override
setFocus()115     public void setFocus() {
116         mPixelPerfectLoupe.setFocus();
117     }
118 
119     @Override
crosshairMoved()120     public void crosshairMoved() {
121         // pass
122     }
123 
124     @Override
treeChanged()125     public void treeChanged() {
126         // pass
127     }
128 
129     @Override
imageChanged()130     public void imageChanged() {
131         // pass
132     }
133 
134     @Override
imageLoaded()135     public void imageLoaded() {
136         Display.getDefault().syncExec(new Runnable() {
137             @Override
138             public void run() {
139                 Image overlayImage = PixelPerfectModel.getModel().getOverlayImage();
140                 mShowInLoupeAction.setEnabled(overlayImage != null);
141             }
142         });
143     }
144 
145     @Override
overlayChanged()146     public void overlayChanged() {
147         Display.getDefault().syncExec(new Runnable() {
148             @Override
149             public void run() {
150                 mShowInLoupeAction
151                         .setEnabled(PixelPerfectModel.getModel().getOverlayImage() != null);
152             }
153         });
154     }
155 
156     @Override
overlayTransparencyChanged()157     public void overlayTransparencyChanged() {
158         // pass
159     }
160 
161     @Override
selectionChanged()162     public void selectionChanged() {
163         // pass
164     }
165 
166     @Override
zoomChanged()167     public void zoomChanged() {
168         // pass
169     }
170 
171 }
172