svn commit: r14783 - trunk/src/argouml-app/src/org/argouml: gefext uml/ui

[email protected]
Newsgroups gmane.comp.lang.uml.argouml.cvs
Message-ID <[email protected]>
Author: tfmorris
Date: 2008-05-22 08:52:41-0700
New Revision: 14783

Modified:
   trunk/src/argouml-app/src/org/argouml/gefext/DeferredBufferedImage.java
   trunk/src/argouml-app/src/org/argouml/uml/ui/SaveGraphicsManager.java

Log:
Issue 3535 - move margin handling out of general image class.  Overrride GEF handling of empty diagrams.

Modified: trunk/src/argouml-app/src/org/argouml/gefext/DeferredBufferedImage.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/gefext/DeferredBufferedImage.java?view=diff&rev=14783&p1=trunk/src/argouml-app/src/org/argouml/gefext/DeferredBufferedImage.java&p2=trunk/src/argouml-app/src/org/argouml/gefext/DeferredBufferedImage.java&r1=14782&r2=14783
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/gefext/DeferredBufferedImage.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/gefext/DeferredBufferedImage.java	2008-05-22 08:52:41-0700
@@ -72,8 +72,7 @@
             new Color(TRANSPARENT_BG_COLOR, true);
     
     private static final int BUFFER_HEIGHT = 32;
-    private static final int MARGIN = 10;
-    
+ 
     private int x, y;
     private int width;
     private int height;
@@ -105,14 +104,6 @@
         y = drawingArea.y;
         width = drawingArea.width;
         height = drawingArea.height;
-        
-        // We're going to draw from the origin, so adjust width and height
-        // Also add a margin to the bottom and right, because GEF sets the
-        // bounding box very tight
-        width = width + x + MARGIN;
-        x = 0;
-        height = height + y + MARGIN;
-        y = 0;
 
         // Scale everything up
         x = x  * scale;
@@ -122,6 +113,10 @@
         scaledBufferHeight = BUFFER_HEIGHT * scale;
         
         // Create our bandbuffer which is just a small slice of the image
+        // TODO: We used a fixed height buffer now, but we could be smarter and
+        // compute a height which would fit in some memory budget, allowing us
+        // to use taller buffers with narrower images, minimizing the overhead
+        // of multiple rendering passes
         image = new BufferedImage(width, scaledBufferHeight, imageType);
 
         // Initialize band buffer bounds

Modified: trunk/src/argouml-app/src/org/argouml/uml/ui/SaveGraphicsManager.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/ui/SaveGraphicsManager.java?view=diff&rev=14783&p1=trunk/src/argouml-app/src/org/argouml/uml/ui/SaveGraphicsManager.java&p2=trunk/src/argouml-app/src/org/argouml/uml/ui/SaveGraphicsManager.java&r1=14782&r2=14783
==============================================================================
--- trunk/src/argouml-app/src/org/argouml/uml/ui/SaveGraphicsManager.java	(original)
+++ trunk/src/argouml-app/src/org/argouml/uml/ui/SaveGraphicsManager.java	2008-05-22 08:52:41-0700
@@ -25,6 +25,7 @@
 package org.argouml.uml.ui;
 
 import java.awt.Rectangle;
+import java.awt.event.ActionEvent;
 import java.awt.image.BufferedImage;
 import java.awt.image.RenderedImage;
 import java.beans.PropertyChangeEvent;
@@ -56,6 +57,7 @@
 import org.tigris.gef.base.CmdSavePS;
 import org.tigris.gef.base.CmdSaveSVG;
 import org.tigris.gef.base.Editor;
+import org.tigris.gef.base.Globals;
 import org.tigris.gef.base.SaveEPSAction;
 import org.tigris.gef.base.SaveGIFAction;
 import org.tigris.gef.base.SaveGraphicsAction;
@@ -76,6 +78,8 @@
  */
 public final class SaveGraphicsManager {
 
+    private static final int MIN_MARGIN = 15;
+    
     /**
      * The configuration key for the preferred graphics format.
      */
@@ -357,6 +361,34 @@
         c.addAll(otherFilters);
         return c;
     }
+    
+    /**
+     * Adjust the drawing area so that instead of a tight bounding box, it
+     * includes the canvas origin and some space around the lower and right
+     * sides so that the elements will be roughly centered. Elements which are
+     * off the top or left side of the canvas may still be clipped (ie if the
+     * original drawing area had a negative x or y coordinated).
+     * 
+     * @param area rectangle representing original drawing area
+     * @return an expanded rectangle
+     */
+    static Rectangle adjustDrawingArea(Rectangle area) {
+        int xMargin = area.x;
+        if (xMargin < 0) {
+            xMargin = 0;
+        }
+        int yMargin = area.y;
+        if (yMargin < 0) {
+            yMargin = 0;
+        }
+        int margin = Math.max(xMargin, yMargin);
+        if (margin < MIN_MARGIN) {
+            margin = MIN_MARGIN;
+        }
+        return new Rectangle(0, 0, 
+                area.width + (2 * margin), 
+                area.height + (2 * margin));
+    }
 }
 
 /**
@@ -442,7 +474,27 @@
     SavePNGAction2(String name) {
         super(name);
     }
-    
+
+    public void actionPerformed(ActionEvent ae) {
+        Editor ce = Globals.curEditor();
+        Rectangle drawingArea = 
+            ce.getLayerManager().getActiveLayer().calcDrawingArea();
+        // If the diagram is empty, GEF won't write anything, leaving us with
+        // an empty (and invalid) file.  Handle this case ourselves to prevent
+        // this from happening.
+        if (drawingArea.width <= 0 || drawingArea.height <= 0) {
+            Rectangle dummyArea = new Rectangle(0, 0, 50, 50);
+            try {
+                saveGraphics(outputStream, ce, dummyArea);
+            } catch (java.io.IOException e) {
+                LOG.error("Error while exporting Graphics:", e);
+            }
+            return;
+        }
+        
+        // Anything else is handled the normal way
+        super.actionPerformed(ae);
+    }
 
     /**
      * Write the diagram contained by the current editor into an OutputStream as
@@ -453,16 +505,21 @@
             Rectangle drawingArea)
         throws IOException {
 
+        Rectangle canvasArea = 
+            SaveGraphicsManager.adjustDrawingArea(drawingArea);
+        
         // Create an image which will do deferred rendering of the GEF
         // diagram on demand as data is pulled from it 
-        RenderedImage i = new DeferredBufferedImage(drawingArea,
+        RenderedImage i = new DeferredBufferedImage(canvasArea,
                 BufferedImage.TYPE_INT_ARGB, ce, scale);
 
         LOG.debug("Created DeferredBufferedImage - drawingArea = "
-                + drawingArea + " , scale = " + scale);
+                + canvasArea + " , scale = " + scale);
         
         ImageIO.write(i, "png", s);
 
     }
+    
+
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.