KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.eclipse.draw2d.Figure;
34 import org.eclipse.draw2d.FigureCanvas;
35 import org.eclipse.draw2d.FreeformLayer;
36 import org.eclipse.draw2d.Graphics;
37 import org.eclipse.draw2d.IFigure;
38 import org.eclipse.draw2d.UpdateListener;
39 import org.eclipse.draw2d.UpdateManager;
40 import org.eclipse.draw2d.geometry.Rectangle;
41 import org.eclipse.gef.EditPart;
42 import org.eclipse.gef.EditPartViewer;
43 import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
44 import org.eclipse.swt.widgets.Control;
45
46 import org.nightlabs.editor2d.util.EditorUtil;
47
48 /**
49  * @author Alexander Bieber <alex[AT]nightalbs[DOT]de>
50  *
51  */

52 public class SmartUpdateFreeformLayer
53     extends FreeformLayer
54     implements BufferedFreeformLayer
55 {
56     public static final int TYPE_SMART_UPDATE_ONLY = 1;
57     public static final int TYPE_USE_OFFSCREEN_BUFFER = 2;
58     
59     private Rectangle notifiedDamage;
60     private EditPart editPart;
61     private UpdateManager updateManager;
62     
63     private UpdateListener updateListener = new UpdateListener() {
64         public void notifyPainting(Rectangle damage, Map JavaDoc dirtyRegions) {
65             if (editPart == null)
66                 return;
67             notifiedDamage = damage;
68             notifiedDamage = EditorUtil.toAbsolute(editPart, damage);
69         }
70         public void notifyValidating() {
71         }
72     };
73     
74     
75     /**
76      */

77     public SmartUpdateFreeformLayer() {
78         super();
79     }
80     
81     public void init(EditPart editPart) {
82         this.editPart = editPart;
83         EditPartViewer viewer = editPart.getRoot().getViewer();
84         if (viewer instanceof ScrollingGraphicalViewer) {
85             ScrollingGraphicalViewer graphicalViewer = (ScrollingGraphicalViewer) viewer;
86             Control control = graphicalViewer.getControl();
87             if (control instanceof FigureCanvas) {
88                 FigureCanvas figureCanvas = (FigureCanvas)graphicalViewer.getControl();
89                 updateManager = figureCanvas.getLightweightSystem().getUpdateManager();
90                 if (updateManager != null)
91                     registerOnDeferredUpdateManager(updateManager);
92             }
93         }
94     }
95     
96     /**
97      * Registers this Figures updateListener to the given
98      * updateManager.
99      *
100      * @param updateManager The updateManager to register to.
101      */

102     protected void registerOnDeferredUpdateManager(UpdateManager updateManager) {
103         if (updateManager == null)
104             return;
105         updateManager.removeUpdateListener(updateListener);
106         updateManager.addUpdateListener(updateListener);
107     }
108     
109     /**
110      * Overrides Figure's paint as this will always
111      * paint all its children even if not neccessary.
112      * This implementation takes the last region notified
113      * by the UpdateManager (wich will be notified synchronously
114      * right before this paint method will be invoked
115      * whithin UpdateManager.repairDamage) and first asks all
116      * its children wether they do intersect this region.
117      * If true and the child is an instance of
118      * {@link ISmartUpdateFigure} its {@link ISmartUpdateFigure#paintRegion(Rectangle)}
119      * method will be invoked instead of the default
120      * {@link Figure#paint(Graphics)} method.
121      */

122     public void paint(Graphics graphics) {
123         
124         if (notifiedDamage == null) {
125             // no update notification was done
126
// before this call
127
// do default painting (all children)
128
super.paint(graphics);
129             return;
130         }
131         
132         // synchronous notification was done right before paint
133
try
134         // make sure the temporary Damage is reset in finally
135
{
136             for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();) {
137                 IFigure child = (IFigure) iter.next();
138                 if (child.intersects(notifiedDamage)) {
139                     graphics.pushState();
140                     try {
141                         graphics.setClip(child.getBounds());
142 // System.out.println("MLDC paint child: "+child+" child.getBounds() "+child.getBounds());
143
if (child instanceof ISmartUpdateFigure)
144                             ((ISmartUpdateFigure)child).paintRegion(graphics, notifiedDamage);
145                         else
146                             child.paint(graphics);
147                         graphics.restoreState();
148                     } finally
149                     {
150                         graphics.popState();
151                     }
152                 }
153             }
154         }
155         finally
156         {
157             notifiedDamage = null;
158         }
159     }
160
161     /**
162      * @see org.nightlabs.editor2d.figures.BufferedFreeformLayer#refresh()
163      */

164     public void refresh() {
165         for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();) {
166             Figure child = (Figure) iter.next();
167             if (child instanceof ISmartUpdateFigure) {
168                 ((ISmartUpdateFigure)child).refresh();
169             }
170         }
171     }
172
173     /**
174      * @see org.nightlabs.editor2d.figures.BufferedFreeformLayer#refresh(org.eclipse.draw2d.Figure)
175      */

176     public void refresh(IFigure figure) {
177         for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();) {
178             Figure child = (Figure) iter.next();
179             if (child instanceof ISmartUpdateFigure) {
180                 ((ISmartUpdateFigure)child).refresh(figure);
181             }
182         }
183     }
184     
185 }
186
Popular Tags