Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 363281

Summary: CopyToImageAction fails if the diagram contains any figure that has a negative line width
Product: [Modeling] GMF-Runtime Reporter: Andreas Mayer <an.mayer>
Component: GeneralAssignee: Project Inbox <gmf-runtime-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: an.mayer
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows Vista   
Whiteboard:

Description Andreas Mayer CLA 2011-11-09 05:51:06 EST
Build Identifier: M20110909-1335

CopyToImageAction (File > Save As Image File ...) fails with an IllegalArgumentException("negative width") from java.awt.BasicStroke, if any figure in the diagram has a negative line width (see also bug #243187). 

java.lang.IllegalArgumentException: negative width
    at java.awt.BasicStroke.<init>(BasicStroke.java:192)
    at org.eclipse.gmf.runtime.draw2d.ui.render.awt.internal.graphics.GraphicsToGraphics2DAdaptor.createStroke(GraphicsToGraphics2DAdaptor.java:1543)
    at org.eclipse.gmf.runtime.draw2d.ui.render.awt.internal.graphics.GraphicsToGraphics2DAdaptor.drawPolygon(GraphicsToGraphics2DAdaptor.java:633)
    at org.eclipse.draw2d.Graphics.drawPolygon(Graphics.java:293)
    at org.eclipse.gmf.runtime.draw2d.ui.internal.graphics.ScaledGraphics.drawPolygon(ScaledGraphics.java:356)
    at org.eclipse.draw2d.Polygon.outlineShape(Polygon.java:64)
    at org.eclipse.draw2d.Shape.paintOutline(Shape.java:175)
    at org.eclipse.draw2d.Shape.paintFigure(Shape.java:147)
    at org.eclipse.draw2d.Figure.paint(Figure.java:1115)
    at org.eclipse.draw2d.Figure.paintChildren(Figure.java:1167)
    at org.eclipse.draw2d.Figure.paintClientArea(Figure.java:1202)
    at org.eclipse.draw2d.Figure.paint(Figure.java:1117)
    at org.eclipse.gmf.runtime.diagram.ui.render.clipboard.DiagramGenerator.paintFigure(DiagramGenerator.java:373)
    at org.eclipse.gmf.runtime.diagram.ui.render.clipboard.DiagramGenerator.renderToGraphics(DiagramGenerator.java:237)
    at org.eclipse.gmf.runtime.diagram.ui.render.clipboard.DiagramGenerator.createSWTImageDescriptorForParts(DiagramGenerator.java:721)
    at org.eclipse.gmf.runtime.diagram.ui.render.util.CopyToImageUtil.copyToImage(CopyToImageUtil.java:376)
    at org.eclipse.gmf.runtime.diagram.ui.render.util.CopyToImageUtil.copyToImage(CopyToImageUtil.java:305)
    at org.eclipse.gmf.runtime.diagram.ui.render.actions.CopyToImageAction$1.run(CopyToImageAction.java:260)
    at org.eclipse.jface.operation.ModalContext.runInCurrentThread(ModalContext.java:464)
    at org.eclipse.jface.operation.ModalContext.run(ModalContext.java:372)
    at org.eclipse.jface.dialogs.ProgressMonitorDialog.run(ProgressMonitorDialog.java:507)
    at org.eclipse.gmf.runtime.diagram.ui.render.actions.CopyToImageAction.runCopyToImageUI(CopyToImageAction.java:170)
    [...]

Troubleshooting is difficult here, because the operation fails completely and the exception provides no hint at the offending figure. The Javadocs of Graphics don't specify a behaviour for negative line widths, but under Windows, lines with a negative width are drawn with a line width of 1. If GraphicsToGraphics2DAdaptor would do the same, then CopyToImageAction could succeed and you would be able to spot the error in the resulting image. Therefore, GraphicsToGraphics2DAdator.createStroke() should be changed as follows:

  /*
-  * SWT paints line width == 0 as if it is == 1, so AWT is synced up with that below.
+  * SWT paints line width <= 0 as if it is == 1, so AWT is synced up with that below.
   */
  stroke =
      new BasicStroke(
-         currentState.lineAttributes.width != 0 ? currentState.lineAttributes.width : 1,
+         currentState.lineAttributes.width > 0 ? currentState.lineAttributes.width : 1,
          awt_cap,
          awt_join,
          currentState.lineAttributes.miterLimit,
          awt_dash,
          currentState.lineAttributes.dashOffset);


Reproducible: Always