Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 157324 Details for
Bug 300894
Control.print(gc) produces various side effects
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
An all in one test case
PrintComponentSWT6.java (text/java), 11.36 KB, created by
Christopher Deckers
on 2010-01-26 14:03:18 EST
(
hide
)
Description:
An all in one test case
Filename:
MIME Type:
Creator:
Christopher Deckers
Created:
2010-01-26 14:03:18 EST
Size:
11.36 KB
patch
obsolete
>package test; > >import java.awt.BorderLayout; >import java.awt.Canvas; >import java.awt.Container; >import java.awt.event.ItemEvent; >import java.awt.event.ItemListener; > >import javax.swing.JFrame; >import javax.swing.JToggleButton; >import javax.swing.SwingUtilities; > >import org.eclipse.swt.SWT; >import org.eclipse.swt.awt.SWT_AWT; >import org.eclipse.swt.browser.Browser; >import org.eclipse.swt.events.PaintEvent; >import org.eclipse.swt.events.PaintListener; >import org.eclipse.swt.graphics.GC; >import org.eclipse.swt.graphics.Image; >import org.eclipse.swt.graphics.ImageData; >import org.eclipse.swt.graphics.ImageLoader; >import org.eclipse.swt.graphics.Point; >import org.eclipse.swt.graphics.Rectangle; >import org.eclipse.swt.layout.FillLayout; >import org.eclipse.swt.widgets.Button; >import org.eclipse.swt.widgets.Composite; >import org.eclipse.swt.widgets.Control; >import org.eclipse.swt.widgets.Display; >import org.eclipse.swt.widgets.Label; >import org.eclipse.swt.widgets.Shell; > >public class PrintComponentSWT6 extends JFrame { > > private static enum PrintMode { > DEFAULT, > HACK, > DEFAULT_IMPROVED, > DEFAULT_IMPROVED_2, > } > > private static enum TestType { > STRESS_TEST, > SCREEN_OUTPUT, > IMAGE_OUTPUT, > } > > private static final PrintMode printMode = PrintMode.DEFAULT_IMPROVED_2; > private static final TestType testType = TestType.IMAGE_OUTPUT; > > private static boolean printControl(Control control, GC gc) { > switch(printMode) { > case DEFAULT_IMPROVED: { > boolean result = control.print(gc); > Point size = control.getSize(); > control.redraw(0, 0, size.x, size.y, true); > control.update(); > return result; > } > case DEFAULT_IMPROVED_2: { > Rectangle bounds = control.getBounds(); > boolean result = control.print(gc); > control.setLocation(bounds.x, bounds.y); > control.redraw(0, 0, bounds.width, bounds.height, true); > control.update(); > return result; > } > case HACK: { > return printRemoveClip(control, gc); > } > } > return control.print(gc); > } > > private static boolean printRemoveClip(Control control, GC gc) { > Rectangle bounds = control.getBounds(); > Display display = control.getDisplay(); > Composite oldParent = control.getParent(); > Shell tmpHiddenParentShell = new Shell(); > Shell tmpParentShell = new Shell(tmpHiddenParentShell, SWT.NO_TRIM | SWT.NO_FOCUS | SWT.NO_BACKGROUND); > Point location = display.map(control, null, control.getLocation()); > tmpParentShell.setLocation(location); > tmpParentShell.setSize(bounds.width, bounds.height); > org.eclipse.swt.widgets.Canvas screenshotCanvas = new org.eclipse.swt.widgets.Canvas(tmpParentShell, SWT.NO_BACKGROUND); > screenshotCanvas.setSize(bounds.width, bounds.height); > GC displayGC = new GC(display); > final Image screenshot = new Image(display, bounds.width, bounds.height); > displayGC.copyArea(screenshot, location.x, location.y); > displayGC.dispose(); > PaintListener paintListener = new PaintListener() { > public void paintControl(PaintEvent e) { > e.gc.drawImage(screenshot, 0, 0); > } > }; > tmpParentShell.addPaintListener(paintListener); > screenshotCanvas.addPaintListener(paintListener); > oldParent.addPaintListener(paintListener); > org.eclipse.swt.widgets.Canvas controlReplacementCanvas = new org.eclipse.swt.widgets.Canvas(oldParent, SWT.NO_BACKGROUND); > controlReplacementCanvas.setSize(bounds.width, bounds.height); > controlReplacementCanvas.addPaintListener(paintListener); > control.setRedraw(false); > oldParent.setRedraw(false); > control.setParent(tmpParentShell); > control.setLocation(0, 0); > control.moveBelow(screenshotCanvas); > tmpParentShell.setVisible(true); > boolean result = control.print(gc); > control.setParent(oldParent); > control.setLocation(bounds.x, bounds.y); > control.moveAbove(controlReplacementCanvas); > controlReplacementCanvas.dispose(); > oldParent.removePaintListener(paintListener); > tmpParentShell.dispose(); > tmpHiddenParentShell.dispose(); > oldParent.setRedraw(true); > control.setRedraw(true); > control.redraw(); > control.update(); > screenshot.dispose(); > return result; > } > > public PrintComponentSWT6() { > setDefaultCloseOperation(EXIT_ON_CLOSE); > Canvas canvas = new Canvas() { > @Override > public void addNotify() { > super.addNotify(); > final Canvas canvas_ = this; > display.asyncExec(new Runnable() { > public void run() { > final Shell shell = SWT_AWT.new_Shell(display, canvas_); > shell.setLayout(new FillLayout()); > Composite composite = new Composite(shell, SWT.NONE); > final Browser browser = new Browser(composite, SWT.NONE); > browser.setUrl("http://www.google.com"); > browser.setBounds(200, 0, 400, 400); > final Button button = new Button(composite, SWT.PUSH); > button.setBounds(-200, 0, 400, 400); > button.setText("Button"); > shell.setSize(400, 400); > shell.open(); > switch(testType) { > case IMAGE_OUTPUT: > case SCREEN_OUTPUT: { > new Thread() { > @Override > public void run() { > try { > sleep(2000); > } catch(Exception e) { > e.printStackTrace(); > } > display.asyncExec(new Runnable() { > private Image getImage(Control control) { > Point size = control.getSize(); > Image image = new Image(display, size.x, size.y); > GC gc = new GC(image); > printControl(control, gc); > gc.dispose(); > return image; > } > public void run() { > if(testType == TestType.SCREEN_OUTPUT) { > Shell newShell = new Shell(shell); > newShell.setText("Screen captures"); > Label buttonLabel = new Label(newShell, SWT.BORDER); > buttonLabel.setImage(getImage(button)); > buttonLabel.setBounds(0, 0, 400, 400); > Label browserLabel = new Label(newShell, SWT.BORDER); > browserLabel.setImage(getImage(browser)); > browserLabel.setBounds(400, 0, 400, 400); > Rectangle trim = newShell.computeTrim(0, 0, 800, 400); > newShell.setSize(trim.width, trim.height); > newShell.open(); > } else { > System.out.println("Printing to image..."); > Image image = new Image(display, 800, 400); > GC gc = new GC(image); > Image image1 = getImage(button); > gc.drawImage(image1, 0, 0); > image1.dispose(); > Image image2 = getImage(browser); > gc.drawImage(image2, 400, 0); > image2.dispose(); > gc.dispose(); > ImageLoader loader = new ImageLoader(); > loader.data = new ImageData[] {image.getImageData()}; > loader.save("PrintComponentSWT6.png", SWT.IMAGE_PNG); > image.dispose(); > } > } > }); > } > }.start(); > break; > } > case STRESS_TEST: { > new Thread() { > @Override > public void run() { > while (true) { > try { > sleep(1000); > } catch (Exception e) { > e.printStackTrace(); > } > if(display.isDisposed()) { > return; > } > display.asyncExec(new Runnable() { > public void run() { > if(browser.isDisposed()) { > return; > } > Point size = browser.getSize(); > Image image = new Image(display, size.x, size.y); > GC gc = new GC(image); > long start = System.currentTimeMillis(); > printControl(browser, gc); > printControl(browser, gc); > printControl(browser, gc); > System.err.println("total (x3): " + (System.currentTimeMillis() - start)); > gc.dispose(); > } > }); > } > } > }.start(); > break; > } > } > // The initial size is wrong on Windows so we have to force it > shell.setSize(canvas_.getWidth(), canvas_.getHeight()); > } > }); > } > }; > Container contentPane = getContentPane(); > contentPane.add(canvas, BorderLayout.CENTER); > final int px = 400; > final JToggleButton button = new JToggleButton("Auto moves " + px + "px right"); > button.addItemListener(new ItemListener() { > private volatile boolean isMoving; > private int count; > public void itemStateChanged(ItemEvent e) { > if(e.getStateChange() == ItemEvent.SELECTED) { > isMoving = true; > count = 0; > Runnable runnable = new Runnable() { > public void run() { > java.awt.Point location = PrintComponentSWT6.this.getLocation(); > count++; > if(count == px) { > button.doClick(); > return; > } > PrintComponentSWT6.this.setLocation(location.x + 1, location.y); > if(isMoving) { > final Runnable runnable = this; > new Thread() { > @Override > public void run() { > try { > Thread.sleep(10); > } catch (Exception e) { > } > SwingUtilities.invokeLater(runnable); > } > }.start(); > } > } > }; > SwingUtilities.invokeLater(runnable); > } else { > isMoving = false; > } > } > }); > contentPane.add(button, BorderLayout.SOUTH); > setSize(400, 300); > } > > private static Display display; > > public static void main(String[] args) { > System.setProperty("sun.awt.noerasebackground", "true"); > System.setProperty("sun.awt.xembedserver", "true"); > display = new Display(); > SwingUtilities.invokeLater(new Runnable() { > public void run() { > new PrintComponentSWT6().setVisible(true); > } > }); > while(true) { > if(!display.readAndDispatch()) { > display.sleep(); > } > } > } > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 300894
: 157324