• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.hierarchyviewerlib.ui;
18 
19 import com.android.ddmuilib.ImageLoader;
20 import com.android.hierarchyviewerlib.HierarchyViewerDirector;
21 import com.android.hierarchyviewerlib.device.ViewNode;
22 
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.PaintEvent;
25 import org.eclipse.swt.events.PaintListener;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.events.SelectionListener;
28 import org.eclipse.swt.events.ShellAdapter;
29 import org.eclipse.swt.events.ShellEvent;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.graphics.Rectangle;
32 import org.eclipse.swt.layout.FillLayout;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.layout.RowLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Canvas;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Shell;
41 
42 public class CaptureDisplay {
43     private static Shell sShell;
44 
45     private static Canvas sCanvas;
46 
47     private static Image sImage;
48 
49     private static ViewNode sViewNode;
50 
51     private static Composite sButtonBar;
52 
53     private static Button sOnWhite;
54 
55     private static Button sOnBlack;
56 
57     private static Button sShowExtras;
58 
show(Shell parentShell, ViewNode viewNode, Image image)59     public static void show(Shell parentShell, ViewNode viewNode, Image image) {
60         if (sShell == null) {
61             createShell();
62         }
63         if (sShell.isVisible() && CaptureDisplay.sViewNode != null) {
64             CaptureDisplay.sViewNode.dereferenceImage();
65         }
66         CaptureDisplay.sImage = image;
67         CaptureDisplay.sViewNode = viewNode;
68         viewNode.referenceImage();
69         sShell.setText(viewNode.name);
70 
71         boolean shellVisible = sShell.isVisible();
72         if (!shellVisible) {
73             sShell.setSize(0, 0);
74         }
75         Rectangle bounds =
76                 sShell.computeTrim(0, 0, Math.max(sButtonBar.getBounds().width,
77                         image.getBounds().width), sButtonBar.getBounds().height
78                         + image.getBounds().height + 5);
79         sShell.setSize(bounds.width, bounds.height);
80         if (!shellVisible) {
81             sShell.setLocation(parentShell.getBounds().x
82                     + (parentShell.getBounds().width - bounds.width) / 2, parentShell.getBounds().y
83                     + (parentShell.getBounds().height - bounds.height) / 2);
84         }
85         sShell.open();
86         if (shellVisible) {
87             sCanvas.redraw();
88         }
89     }
90 
createShell()91     private static void createShell() {
92         sShell = new Shell(Display.getDefault(), SWT.CLOSE | SWT.TITLE);
93         GridLayout gridLayout = new GridLayout();
94         gridLayout.marginWidth = 0;
95         gridLayout.marginHeight = 0;
96         sShell.setLayout(gridLayout);
97 
98         sButtonBar = new Composite(sShell, SWT.NONE);
99         RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
100         rowLayout.pack = true;
101         rowLayout.center = true;
102         sButtonBar.setLayout(rowLayout);
103         Composite buttons = new Composite(sButtonBar, SWT.NONE);
104         buttons.setLayout(new FillLayout());
105 
106         sOnWhite = new Button(buttons, SWT.TOGGLE);
107         sOnWhite.setText("On White");
108         sOnBlack = new Button(buttons, SWT.TOGGLE);
109         sOnBlack.setText("On Black");
110         sOnBlack.setSelection(true);
111         sOnWhite.addSelectionListener(sWhiteSelectionListener);
112         sOnBlack.addSelectionListener(sBlackSelectionListener);
113 
114         sShowExtras = new Button(sButtonBar, SWT.CHECK);
115         sShowExtras.setText("Show Extras");
116         sShowExtras.addSelectionListener(sExtrasSelectionListener);
117 
118         sCanvas = new Canvas(sShell, SWT.NONE);
119         sCanvas.setLayoutData(new GridData(GridData.FILL_BOTH));
120         sCanvas.addPaintListener(sPaintListener);
121 
122         sShell.addShellListener(sShellListener);
123 
124         ImageLoader imageLoader = ImageLoader.getLoader(HierarchyViewerDirector.class);
125         Image image = imageLoader.loadImage("display.png", Display.getDefault()); //$NON-NLS-1$
126         sShell.setImage(image);
127     }
128 
129     private static PaintListener sPaintListener = new PaintListener() {
130 
131         @Override
132         public void paintControl(PaintEvent e) {
133             if (sOnWhite.getSelection()) {
134                 e.gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
135             } else {
136                 e.gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
137             }
138             e.gc.fillRectangle(0, 0, sCanvas.getBounds().width, sCanvas.getBounds().height);
139             if (sImage != null) {
140                 int width = sImage.getBounds().width;
141                 int height = sImage.getBounds().height;
142                 int x = (sCanvas.getBounds().width - width) / 2;
143                 int y = (sCanvas.getBounds().height - height) / 2;
144                 e.gc.drawImage(sImage, x, y);
145                 if (sShowExtras.getSelection()) {
146                     if ((sViewNode.paddingLeft | sViewNode.paddingRight | sViewNode.paddingTop | sViewNode.paddingBottom) != 0) {
147                         e.gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
148                         e.gc.drawRectangle(x + sViewNode.paddingLeft, y + sViewNode.paddingTop, width
149                                 - sViewNode.paddingLeft - sViewNode.paddingRight - 1, height
150                                 - sViewNode.paddingTop - sViewNode.paddingBottom - 1);
151                     }
152                     if (sViewNode.hasMargins) {
153                         e.gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_GREEN));
154                         e.gc.drawRectangle(x - sViewNode.marginLeft, y - sViewNode.marginTop, width
155                                 + sViewNode.marginLeft + sViewNode.marginRight - 1, height
156                                 + sViewNode.marginTop + sViewNode.marginBottom - 1);
157                     }
158                     if (sViewNode.baseline != -1) {
159                         e.gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
160                         e.gc.drawLine(x, y + sViewNode.baseline, x + width - 1, sViewNode.baseline);
161                     }
162                 }
163             }
164         }
165     };
166 
167     private static ShellAdapter sShellListener = new ShellAdapter() {
168         @Override
169         public void shellClosed(ShellEvent e) {
170             e.doit = false;
171             sShell.setVisible(false);
172             if (sViewNode != null) {
173                 sViewNode.dereferenceImage();
174             }
175         }
176 
177     };
178 
179     private static SelectionListener sWhiteSelectionListener = new SelectionListener() {
180         @Override
181         public void widgetDefaultSelected(SelectionEvent e) {
182             // pass
183         }
184 
185         @Override
186         public void widgetSelected(SelectionEvent e) {
187             sOnWhite.setSelection(true);
188             sOnBlack.setSelection(false);
189             sCanvas.redraw();
190         }
191     };
192 
193     private static SelectionListener sBlackSelectionListener = new SelectionListener() {
194         @Override
195         public void widgetDefaultSelected(SelectionEvent e) {
196             // pass
197         }
198 
199         @Override
200         public void widgetSelected(SelectionEvent e) {
201             sOnBlack.setSelection(true);
202             sOnWhite.setSelection(false);
203             sCanvas.redraw();
204         }
205     };
206 
207     private static SelectionListener sExtrasSelectionListener = new SelectionListener() {
208         @Override
209         public void widgetDefaultSelected(SelectionEvent e) {
210             // pass
211         }
212 
213         @Override
214         public void widgetSelected(SelectionEvent e) {
215             sCanvas.redraw();
216         }
217     };
218 }
219