KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on 07.06.2005
3  *
4  */

5 package com.nightlabs.editor2d.figures;
6
7 import java.awt.Color JavaDoc;
8 import java.awt.Graphics2D JavaDoc;
9 import java.awt.image.BufferedImage JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 import org.eclipse.draw2d.Figure;
13 import org.eclipse.draw2d.FreeformLayer;
14 import org.eclipse.draw2d.Graphics;
15 import org.eclipse.draw2d.J2DGraphics;
16 import org.eclipse.draw2d.UpdateManager;
17 import org.eclipse.draw2d.geometry.Point;
18 import org.eclipse.draw2d.geometry.Rectangle;
19 import org.eclipse.gef.EditPart;
20 import org.eclipse.gef.EditPartViewer;
21 import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
22 import org.eclipse.swt.widgets.Control;
23
24 import com.nightlabs.editor2d.util.EditorUtil;
25
26 /**
27  * @author Alexander Bieber <alex[AT]nightalbs[DOT]de>
28  *
29  */

30 public class MinimalBufferFreeformLayer
31     extends FreeformLayer
32     implements BufferedFreeformLayer
33 {
34     public static final int TYPE_SMART_UPDATE_ONLY = 1;
35     public static final int TYPE_USE_OFFSCREEN_BUFFER = 2;
36     
37     private Rectangle notifiedDamage;
38     
39     private EditPart editPart;
40     private UpdateManager updateManager;
41     /**
42      * The viewer control
43      */

44     private Control viewerControl;
45     /**
46      * The region currently viewed in
47      * screen coordinates. Width and
48      * height correspont with the buffers
49      * width and height.
50      */

51     private Rectangle viewerRegion = new Rectangle(0,0,0,0);
52     /**
53      * The current region that is drawn
54      * on the buffer in absolute coordinates.
55      */

56     private Rectangle bufferRegion;
57
58     /**
59      * The buffer
60      */

61     private BufferedImage JavaDoc bufferedImage;
62     /**
63      * The buffers graphics
64      */

65     private Graphics2D JavaDoc bufferedGraphics;
66     
67     /**
68      */

69     public MinimalBufferFreeformLayer() {
70         super();
71     }
72     
73     public void init(EditPart editPart) {
74         this.editPart = editPart;
75         EditPartViewer viewer = editPart.getRoot().getViewer();
76         if (viewer instanceof ScrollingGraphicalViewer) {
77             ScrollingGraphicalViewer graphicalViewer = (ScrollingGraphicalViewer) viewer;
78             Control control = graphicalViewer.getControl();
79             this.viewerControl = control;
80             // TODO: add controlListener
81
}
82     }
83     
84     /**
85      * Creates the buffer according to the size of
86      * the Control.
87      */

88     protected void createBuffer() {
89         if (bufferedImage != null){
90             bufferedGraphics.dispose();
91             bufferedImage.flush();
92             bufferedImage = null;
93             bufferedGraphics = null;
94         }
95         viewerRegion.setLocation(0,0);
96         viewerRegion.setSize(viewerControl.getSize().x, viewerControl.getSize().y);
97         bufferedImage = new BufferedImage JavaDoc(viewerRegion.width, viewerRegion.height, BufferedImage.TYPE_INT_ARGB);
98         bufferedGraphics = (Graphics2D JavaDoc)bufferedImage.getGraphics();
99     }
100     
101     protected void drawChildrenRegionOnBuffer(Rectangle region) {
102         // TODO: implement
103
}
104     
105     /**
106      * merges the buffer's content with newly painted
107      * figures to hold the correct view of the given
108      * region to paint afterwards
109      *
110      * @param regionToPaint The region to paint
111      */

112     protected void mergeBuffer(Rectangle regionToPaint) {
113         int absDX = bufferRegion.x - regionToPaint.x;
114         int absDY = bufferRegion.x - regionToPaint.x;
115         int relDX = EditorUtil.toRelative(editPart, absDX);
116         int relDY = EditorUtil.toRelative(editPart, absDY);
117         if (absDX == 0 && absDY == 0)
118             return;
119         
120         
121         if (Math.abs(relDX)>bufferRegion.width || Math.abs(relDY)>bufferRegion.height) {
122             // buffer needs complete redraw
123
drawChildrenRegionOnBuffer(regionToPaint);
124             // set the new buffer region to the region to paint
125
bufferRegion.setBounds(regionToPaint);
126             return;
127         }
128         // copy the region already drawn to the right position
129
bufferedGraphics.copyArea(
130                 0 , 0,
131                 viewerRegion.width, viewerRegion.height,
132                 relDX, relDY
133             );
134         // now draw the region to paint on the buffer
135
Rectangle region1;
136         Rectangle region2;
137         if (absDX >= 0) {
138             // dx pos copy to right
139
region1 = new Rectangle(0, 0, absDX, bufferRegion.height);
140             if (absDY >= 0)
141                 // to bottom
142
region2 = new Rectangle(absDX, 0, bufferRegion.width-absDX, absDY);
143             else
144                 // over the top
145
region2 = new Rectangle(absDX, bufferRegion.height+absDY, bufferRegion.width-absDX, -absDY);
146         }
147         else {
148             // dx pos copy to left
149
region1 = new Rectangle(bufferRegion.width + absDX, 0, -absDX, bufferRegion.height);
150             if (absDY >= 0)
151                 // to bottom
152
region2 = new Rectangle(0, 0, bufferRegion.width+absDX, absDY);
153             else
154                 // over the top
155
region2 = new Rectangle(0, bufferRegion.height+absDY, bufferRegion.width+absDX, -absDY);
156         }
157         drawChildrenRegionOnBuffer(region1);
158         drawChildrenRegionOnBuffer(region2);
159         // set the new buffer region to the region to paint
160
bufferRegion.setBounds(regionToPaint);
161     }
162     
163     /**
164      */

165     public void paint(Graphics graphics) {
166         if (!(graphics instanceof J2DGraphics)) {
167             super.paint(graphics);
168             return;
169         }
170         J2DGraphics j2dGraphics = (J2DGraphics)graphics;
171         // create the Graphics where the buffer is drawn on
172
Graphics2D JavaDoc g2d = j2dGraphics.createGraphics2D();
173         try {
174             // TODO: implement Minimal ... paint
175
// check if bufferRegion changed
176
Rectangle regionToPaint = EditorUtil.toAbsolute(editPart, viewerRegion);
177             if (!bufferRegion.equals(regionToPaint)) {
178                 // if so, merge actual buffer with the new bufferRegion copyArea ... Smart ...
179
}
180             
181             // do drawImage(bufferedImage);
182
double currentZoom = EditorUtil.getZoom(editPart);
183             // scale it invers of the current zoom ...
184
g2d.scale(1/currentZoom, 1/currentZoom);
185             // and translate it with the current scroll offset
186
// so 0,0 will be drawn on top left of the control
187
Point scrollOffset = EditorUtil.getScrollOffset(editPart);
188             g2d.translate(scrollOffset.x, scrollOffset.y);
189             // now copy the buffer region
190
g2d.setPaint(Color.WHITE);
191             g2d.fillRect(-2, -2, viewerRegion.width+2, viewerRegion.height+2);
192             g2d.drawImage(
193                     bufferedImage,
194                     0, 0, viewerRegion.width, viewerRegion.height,
195                     0, 0, viewerRegion.width, viewerRegion.height,
196                     null
197                 );
198         } finally {
199             g2d.dispose();
200         }
201         
202     }
203
204     /**
205      * @see com.nightlabs.editor2d.figures.BufferedFreeformLayer#refresh()
206      */

207     public void refresh() {
208         for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();) {
209             Figure child = (Figure) iter.next();
210             if (child instanceof ISmartUpdateFigure) {
211                 ((ISmartUpdateFigure)child).refresh();
212             }
213         }
214     }
215
216     /**
217      * @see com.nightlabs.editor2d.figures.BufferedFreeformLayer#refresh(org.eclipse.draw2d.Figure)
218      */

219     public void refresh(Figure figure) {
220         for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();) {
221             Figure child = (Figure) iter.next();
222             if (child instanceof ISmartUpdateFigure) {
223                 ((ISmartUpdateFigure)child).refresh(figure);
224             }
225         }
226     }
227     
228 }
229
Popular Tags