View Javadoc

1   /*
2   
3   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by the
7   Free Software Foundation; either version 2 of the License, or (at your
8   option) any later version.
9   
10  This program is distributed in the hope that it will
11  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  PURPOSE. See the GNU General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc., 59
17  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  
19  */
20  
21  package org.devaki.nextobjects.util;
22  
23  import java.io.File;
24  import java.awt.Point;
25  import java.awt.Rectangle;
26  import java.awt.image.BufferedImage;
27  import java.awt.Graphics2D;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.devaki.nextobjects.workspace.models.BaseModel;
31  import org.devaki.nextobjects.workspace.models.objects.BaseObject;
32  import org.devaki.nextobjects.workspace.models.graphics.ClassView;
33  import org.devaki.nextobjects.workspace.models.graphics.ObjectView;
34  
35  /***
36   *  This class is responsible for drawing models to files.
37   * 
38   * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
39   *
40   */
41  
42  public class NOImageTransform
43  {
44   /***
45    *  The Log4J logger
46    */
47   private static Log logger =
48    LogFactory.getLog(NOImageTransform.class.getName());
49  
50   /***
51    *  Dummy constructor
52    */
53   public NOImageTransform()
54   {
55   }
56  
57   /***
58   * Move the model to the upper left corner to get the samllest image.
59   * 
60   * @param pPDM the context model
61   * @param xOffset the X offset
62   * @param yOffset the Y offset 
63   */
64   private static void translateModel(
65    BaseModel pModel,
66    int xOffset,
67    int yOffset)
68   {
69    BaseObject[] tmp = pModel.getModelObjects();
70    // loop
71    for (int j = 0; j < tmp.length; j++)
72    {
73     if (tmp[j].getObjectView() instanceof ClassView)
74     {
75      ((ClassView) tmp[j].getObjectView()).setLocation(
76       new Point(
77        tmp[j].getObjectView().getLocation().x - xOffset,
78        tmp[j].getObjectView().getLocation().y - yOffset));
79        tmp[j].getObjectView().resetSelectionPoint();
80     }
81    }
82          for (int i = 0; i < pModel.getLabels().size(); i++)
83          {
84              ObjectView label =
85                  (ObjectView) pModel.getLabels().elementAt(i);
86              label.setLocation(
87                  new Point(
88                      label.getLocation().x - xOffset,
89                      label.getLocation().y - yOffset));
90                      label.resetSelectionPoint();
91          }
92   }
93  
94   /***
95    * Write the base model to a PNG file
96    * @param pModel the context model
97    * @param file the file to write
98    * @return success
99    */
100  public static boolean writeImage(BaseModel pModel, File file)
101  {
102   boolean success = false;
103 
104         // this is a hack, the model is moved to top left corner in 
105   // order to avoid drawing the whole 5000*5000 drawingArea.
106   // then the model is returned to old position.
107   // by that time it haven't been redrawed ...
108 
109 
110     // gets the size of the image to be created and its position on the panel
111     Rectangle modelRect=pModel.getModelView().calculateRectangle();
112  
113 
114     // image creation with the correct size
115     BufferedImage bi = new BufferedImage(modelRect.width ,
116      modelRect.height , 
117      BufferedImage.TYPE_4BYTE_ABGR_PRE);
118     Graphics2D g = (Graphics2D) bi.createGraphics();
119     
120     //avoid having a (always) not empty buffer - so it only help debug
121     
122     //having a model in background coming from some hypothetic malloc
123     g.clearRect(0,0,modelRect.width,modelRect.height);
124 
125   //move the model to the top left corner
126         translateModel(pModel, modelRect.x  , modelRect.y );
127 
128   //Avoid having selection points
129         ModelMan.resetCurrentObjects();
130         //PAint the buffered image        
131         pModel.getModelView().drawingArea.paint(g);
132         
133   //Reset the model to the original size.
134         translateModel(pModel, (modelRect.x ) * -1, (modelRect.y) * -1);
135     
136     // writes the PNG file
137     try
138   {
139    success = javax.imageio.ImageIO.write(bi, "PNG", file);
140   }
141     catch (Exception e)
142   {
143    logger.error(e.getMessage());
144    // most often lacks of jdk1.4
145    e.printStackTrace();
146   }
147 
148   return success;
149  }
150 }