Created
March 27, 2015 14:58
-
-
Save nemecec/b08e43bc84cddc596212 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.zeroturnaround.eclipse.optimizer.plugin.debug; | |
| import org.eclipse.swt.SWT; | |
| import org.eclipse.swt.events.PaintEvent; | |
| import org.eclipse.swt.events.PaintListener; | |
| import org.eclipse.swt.graphics.Color; | |
| import org.eclipse.swt.graphics.GC; | |
| import org.eclipse.swt.graphics.Rectangle; | |
| import org.eclipse.swt.layout.FillLayout; | |
| import org.eclipse.swt.widgets.Canvas; | |
| import org.eclipse.swt.widgets.Composite; | |
| import org.eclipse.swt.widgets.Control; | |
| import org.eclipse.swt.widgets.Event; | |
| import org.eclipse.swt.widgets.Listener; | |
| import org.eclipse.swt.widgets.Shell; | |
| import org.eclipse.wb.swt.SWTResourceManager; | |
| public class ComponentVisualizer { | |
| private final SWTResourceManager resourceManager = new SWTResourceManager(); | |
| private final Shell shell; | |
| private final Canvas canvas; | |
| private final Shell shellToVisualize; | |
| public ComponentVisualizer(Shell shellToVisualize) { | |
| this(shellToVisualize, (Shell) shellToVisualize.getParent()); | |
| } | |
| public ComponentVisualizer(final Shell shellToVisualize, Shell parent) { | |
| this.shellToVisualize = shellToVisualize; | |
| this.shell = new Shell(parent, shellToVisualize.getStyle()); | |
| this.shell.setSize(this.shellToVisualize.getSize()); | |
| this.shell.setLayout(new FillLayout()); | |
| this.canvas = new Canvas(this.shell, SWT.NONE); | |
| this.canvas.addPaintListener(new PaintListener() { | |
| @Override | |
| public void paintControl(PaintEvent e) { | |
| ComponentVisualizer.this.paint(e); | |
| } | |
| }); | |
| Listener l = new Listener() { | |
| @Override | |
| public void handleEvent(Event event) { | |
| shell.setSize(shellToVisualize.getSize()); | |
| shell.redraw(); | |
| } | |
| }; | |
| this.shellToVisualize.addListener(SWT.Resize, l); | |
| this.shellToVisualize.addListener(SWT.Paint, l); | |
| } | |
| public void open() { | |
| this.shell.setLocation(0, 0); | |
| shellToVisualize.setLocation(this.shell.getSize().x, 0); | |
| shell.open(); | |
| } | |
| public void dispose() { | |
| shell.dispose(); | |
| resourceManager.dispose(); | |
| } | |
| protected void paint(PaintEvent e) { | |
| GC gc = e.gc; | |
| gc.setForeground(getDefaultForegroundColor()); | |
| paintComposite(gc, 0, 0, this.shellToVisualize); | |
| } | |
| private Color getDefaultForegroundColor() { | |
| return this.resourceManager.getColor(0, 0, 0); | |
| } | |
| private void paintComposite(GC gc, int x, int y, Composite composite) { | |
| if (!composite.isVisible()) return; | |
| Rectangle bounds = composite.getBounds(); | |
| if (composite instanceof Shell || composite.getParent() == null) { | |
| bounds = composite.getClientArea(); | |
| } | |
| paintControl(gc, x + bounds.x, y + bounds.y, this.shellToVisualize, bounds.width, bounds.height); | |
| for (Control c : composite.getChildren()) { | |
| if (c instanceof Composite) { | |
| paintComposite(gc, x+bounds.x, y+bounds.y, (Composite) c); | |
| } else { | |
| paintControl(gc, x+bounds.x, y+bounds.y, c); | |
| } | |
| } | |
| } | |
| private void paintControl(GC gc, int x, int y, Control control) { | |
| Rectangle bounds = control.getBounds(); | |
| int topX = x + bounds.x; | |
| int topY = y + bounds.y; | |
| int width = bounds.width; | |
| int height = bounds.height; | |
| paintControl(gc, topX, topY, control, width, height); | |
| } | |
| private void paintControl(GC gc, int topX, int topY, Control control, int width, int height) { | |
| if (control.isVisible()) { | |
| String text = control.toString() + " " + control.getClass().getName(); | |
| gc.drawText(text, topX, topY); | |
| gc.drawRectangle(topX, topY, width, height); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment