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 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 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 public void paintControl(PaintEvent e) { 132 if (sOnWhite.getSelection()) { 133 e.gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); 134 } else { 135 e.gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); 136 } 137 e.gc.fillRectangle(0, 0, sCanvas.getBounds().width, sCanvas.getBounds().height); 138 if (sImage != null) { 139 int width = sImage.getBounds().width; 140 int height = sImage.getBounds().height; 141 int x = (sCanvas.getBounds().width - width) / 2; 142 int y = (sCanvas.getBounds().height - height) / 2; 143 e.gc.drawImage(sImage, x, y); 144 if (sShowExtras.getSelection()) { 145 if ((sViewNode.paddingLeft | sViewNode.paddingRight | sViewNode.paddingTop | sViewNode.paddingBottom) != 0) { 146 e.gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE)); 147 e.gc.drawRectangle(x + sViewNode.paddingLeft, y + sViewNode.paddingTop, width 148 - sViewNode.paddingLeft - sViewNode.paddingRight - 1, height 149 - sViewNode.paddingTop - sViewNode.paddingBottom - 1); 150 } 151 if (sViewNode.hasMargins) { 152 e.gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_GREEN)); 153 e.gc.drawRectangle(x - sViewNode.marginLeft, y - sViewNode.marginTop, width 154 + sViewNode.marginLeft + sViewNode.marginRight - 1, height 155 + sViewNode.marginTop + sViewNode.marginBottom - 1); 156 } 157 if (sViewNode.baseline != -1) { 158 e.gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED)); 159 e.gc.drawLine(x, y + sViewNode.baseline, x + width - 1, sViewNode.baseline); 160 } 161 } 162 } 163 } 164 }; 165 166 private static ShellAdapter sShellListener = new ShellAdapter() { 167 @Override 168 public void shellClosed(ShellEvent e) { 169 e.doit = false; 170 sShell.setVisible(false); 171 if (sViewNode != null) { 172 sViewNode.dereferenceImage(); 173 } 174 } 175 176 }; 177 178 private static SelectionListener sWhiteSelectionListener = new SelectionListener() { 179 public void widgetDefaultSelected(SelectionEvent e) { 180 // pass 181 } 182 183 public void widgetSelected(SelectionEvent e) { 184 sOnWhite.setSelection(true); 185 sOnBlack.setSelection(false); 186 sCanvas.redraw(); 187 } 188 }; 189 190 private static SelectionListener sBlackSelectionListener = new SelectionListener() { 191 public void widgetDefaultSelected(SelectionEvent e) { 192 // pass 193 } 194 195 public void widgetSelected(SelectionEvent e) { 196 sOnBlack.setSelection(true); 197 sOnWhite.setSelection(false); 198 sCanvas.redraw(); 199 } 200 }; 201 202 private static SelectionListener sExtrasSelectionListener = new SelectionListener() { 203 public void widgetDefaultSelected(SelectionEvent e) { 204 // pass 205 } 206 207 public void widgetSelected(SelectionEvent e) { 208 sCanvas.redraw(); 209 } 210 }; 211 } 212