KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > figures > MinimalBufferFreeformLayer


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

27
28 package org.nightlabs.editor2d.figures;
29
30 import java.awt.Color JavaDoc;
31 import java.awt.Graphics2D JavaDoc;
32 import java.awt.image.BufferedImage JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import org.eclipse.draw2d.Figure;
36 import org.eclipse.draw2d.FreeformLayer;
37 import org.eclipse.draw2d.Graphics;
38 import org.eclipse.draw2d.IFigure;
39 import org.eclipse.draw2d.J2DGraphics;
40 import org.eclipse.draw2d.UpdateManager;
41 import org.eclipse.draw2d.geometry.Point;
42 import org.eclipse.draw2d.geometry.Rectangle;
43 import org.eclipse.gef.EditPart;
44 import org.eclipse.gef.EditPartViewer;
45 import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
46 import org.eclipse.swt.widgets.Control;
47
48 import org.nightlabs.editor2d.util.EditorUtil;
49
50 /**
51  * @author Alexander Bieber <alex[AT]nightalbs[DOT]de>
52  *
53  */

54 public class MinimalBufferFreeformLayer
55     extends FreeformLayer
56     implements BufferedFreeformLayer
57 {
58     public static final int TYPE_SMART_UPDATE_ONLY = 1;
59     public static final int TYPE_USE_OFFSCREEN_BUFFER = 2;
60     
61     private Rectangle notifiedDamage;
62     
63     private EditPart editPart;
64     private UpdateManager updateManager;
65     /**
66      * The viewer control
67      */

68     private Control viewerControl;
69     /**
70      * The region currently viewed in
71      * screen coordinates. Width and
72      * height correspont with the buffers
73      * width and height.
74      */

75     private Rectangle viewerRegion = new Rectangle(0,0,0,0);
76     /**
77      * The current region that is drawn
78      * on the buffer in absolute coordinates.
79      */

80     private Rectangle bufferRegion;
81
82     /**
83      * The buffer
84      */

85     private BufferedImage JavaDoc bufferedImage;
86     /**
87      * The buffers graphics
88      */

89     private Graphics2D JavaDoc bufferedGraphics;
90     
91     /**
92      */

93     public MinimalBufferFreeformLayer() {
94         super();
95     }
96     
97     public void init(EditPart editPart) {
98         this.editPart = editPart;
99         EditPartViewer viewer = editPart.getRoot().getViewer();
100         if (viewer instanceof ScrollingGraphicalViewer) {
101             ScrollingGraphicalViewer graphicalViewer = (ScrollingGraphicalViewer) viewer;
102             Control control = graphicalViewer.getControl();
103             this.viewerControl = control;
104             // TODO: add controlListener
105
}
106     }
107     
108     /**
109      * Creates the buffer according to the size of
110      * the Control.
111      */

112     protected void createBuffer() {
113         if (bufferedImage != null){
114             bufferedGraphics.dispose();
115             bufferedImage.flush();
116             bufferedImage = null;
117             bufferedGraphics = null;
118         }
119         viewerRegion.setLocation(0,0);
120         viewerRegion.setSize(viewerControl.getSize().x, viewerControl.getSize().y);
121         bufferedImage = new BufferedImage JavaDoc(viewerRegion.width, viewerRegion.height, BufferedImage.TYPE_INT_ARGB);
122         bufferedGraphics = (Graphics2D JavaDoc)bufferedImage.getGraphics();
123     }
124     
125     protected void drawChildrenRegionOnBuffer(Rectangle region) {
126         // TODO: implement
127
}
128     
129     /**
130      * merges the buffer's content with newly painted
131      * figures to hold the correct view of the given
132      * region to paint afterwards
133      *
134      * @param regionToPaint The region to paint
135      */

136     protected void mergeBuffer(Rectangle regionToPaint) {
137         int absDX = bufferRegion.x - regionToPaint.x;
138         int absDY = bufferRegion.x - regionToPaint.x;
139         int relDX = EditorUtil.toRelative(editPart, absDX);
140         int relDY = EditorUtil.toRelative(editPart, absDY);
141         if (absDX == 0 && absDY == 0)
142             return;
143         
144         
145         if (Math.abs(relDX)>bufferRegion.width || Math.abs(relDY)>bufferRegion.height) {
146             // buffer needs complete redraw
147
drawChildrenRegionOnBuffer(regionToPaint);
148             // set the new buffer region to the region to paint
149
bufferRegion.setBounds(regionToPaint);
150             return;
151         }
152         // copy the region already drawn to the right position
153
bufferedGraphics.copyArea(
154                 0 , 0,
155                 viewerRegion.width, viewerRegion.height,
156                 relDX, relDY
157             );
158         // now draw the region to paint on the buffer
159
Rectangle region1;
160         Rectangle region2;
161         if (absDX >= 0) {
162             // dx pos copy to right
163
region1 = new Rectangle(0, 0, absDX, bufferRegion.height);
164             if (absDY >= 0)
165                 // to bottom
166
region2 = new Rectangle(absDX, 0, bufferRegion.width-absDX, absDY);
167             else
168                 // over the top
169
region2 = new Rectangle(absDX, bufferRegion.height+absDY, bufferRegion.width-absDX, -absDY);
170         }
171         else {
172             // dx pos copy to left
173
region1 = new Rectangle(bufferRegion.width + absDX, 0, -absDX, bufferRegion.height);
174             if (absDY >= 0)
175                 // to bottom
176
region2 = new Rectangle(0, 0, bufferRegion.width+absDX, absDY);
177             else
178                 // over the top
179
region2 = new Rectangle(0, bufferRegion.height+absDY, bufferRegion.width+absDX, -absDY);
180         }
181         drawChildrenRegionOnBuffer(region1);
182         drawChildrenRegionOnBuffer(region2);
183         // set the new buffer region to the region to paint
184
bufferRegion.setBounds(regionToPaint);
185     }
186     
187     /**
188      */

189     public void paint(Graphics graphics) {
190         if (!(graphics instanceof J2DGraphics)) {
191             super.paint(graphics);
192             return;
193         }
194         J2DGraphics j2dGraphics = (J2DGraphics)graphics;
195         // create the Graphics where the buffer is drawn on
196
Graphics2D JavaDoc g2d = j2dGraphics.createGraphics2D();
197         try {
198             // TODO: implement Minimal ... paint
199
// check if bufferRegion changed
200
Rectangle regionToPaint = EditorUtil.toAbsolute(editPart, viewerRegion);
201             if (!bufferRegion.equals(regionToPaint)) {
202                 // if so, merge actual buffer with the new bufferRegion copyArea ... Smart ...
203
}
204             
205             // do drawImage(bufferedImage);
206
double currentZoom = EditorUtil.getZoom(editPart);
207             // scale it invers of the current zoom ...
208
g2d.scale(1/currentZoom, 1/currentZoom);
209             // and translate it with the current scroll offset
210
// so 0,0 will be drawn on top left of the control
211
Point scrollOffset = EditorUtil.getScrollOffset(editPart);
212             g2d.translate(scrollOffset.x, scrollOffset.y);
213             // now copy the buffer region
214
g2d.setPaint(Color.WHITE);
215             g2d.fillRect(-2, -2, viewerRegion.width+2, viewerRegion.height+2);
216             g2d.drawImage(
217                     bufferedImage,
218                     0, 0, viewerRegion.width, viewerRegion.height,
219                     0, 0, viewerRegion.width, viewerRegion.height,
220                     null
221                 );
222         } finally {
223             g2d.dispose();
224         }
225         
226     }
227
228     /**
229      * @see org.nightlabs.editor2d.figures.BufferedFreeformLayer#refresh()
230      */

231     public void refresh() {
232         for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();) {
233             Figure child = (Figure) iter.next();
234             if (child instanceof ISmartUpdateFigure) {
235                 ((ISmartUpdateFigure)child).refresh();
236             }
237         }
238     }
239
240     /**
241      * @see org.nightlabs.editor2d.figures.BufferedFreeformLayer#refresh(org.eclipse.draw2d.Figure)
242      */

243     public void refresh(IFigure figure) {
244         for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();) {
245             Figure child = (Figure) iter.next();
246             if (child instanceof ISmartUpdateFigure) {
247                 ((ISmartUpdateFigure)child).refresh(figure);
248             }
249         }
250     }
251     
252 }
253
Popular Tags