KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > ViewerManager


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;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 import org.apache.log4j.Logger;
36 import org.eclipse.draw2d.FigureCanvas;
37 import org.eclipse.draw2d.IFigure;
38 import org.eclipse.draw2d.MouseMotionListener;
39 import org.eclipse.draw2d.Viewport;
40 import org.eclipse.draw2d.geometry.Point;
41 import org.eclipse.gef.EditPart;
42 import org.eclipse.gef.EditPartListener;
43 import org.eclipse.gef.EditPartViewer;
44 import org.eclipse.gef.RootEditPart;
45 import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
46 import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
47 import org.eclipse.jface.action.IStatusLineManager;
48 import org.nightlabs.config.Config;
49 import org.nightlabs.config.ConfigException;
50 import org.nightlabs.editor2d.config.PreferencesConfigModule;
51 import org.nightlabs.editor2d.edit.AbstractDrawComponentEditPart;
52 import org.nightlabs.editor2d.edit.LayerEditPart;
53 import org.nightlabs.editor2d.edit.MultiLayerDrawComponentEditPart;
54 import org.nightlabs.editor2d.figures.DrawComponentFigure;
55 import org.nightlabs.editor2d.render.RenderConstants;
56 import org.nightlabs.editor2d.render.Renderer;
57 import org.nightlabs.editor2d.util.EditorUtil;
58 import org.nightlabs.editor2d.viewer.descriptor.DescriptorManager;
59
60 public class ViewerManager
61 {
62   public static final Logger LOGGER = Logger.getLogger(ViewerManager.class);
63   
64   protected ScrollingGraphicalViewer viewer = null;
65   protected Viewport viewport = null;
66   protected Point mousePoint = new Point();
67   protected RootEditPart root = null;
68   protected IStatusLineManager statusLineMan = null;
69   public ViewerManager(ScrollingGraphicalViewer viewer, IStatusLineManager statusLineMan)
70   {
71     super();
72     this.viewer = viewer;
73     this.statusLineMan = statusLineMan;
74     root = viewer.getRootEditPart();
75     root.addEditPartListener(rootListener);
76     FigureCanvas canvas = (FigureCanvas) viewer.getControl();
77     viewport = canvas.getViewport();
78     initExcludeListRef();
79     conditionRef = new ConditionRef(createDefaultCondition());
80     viewport.addMouseMotionListener(mouseListener);
81     mousePoint = new Point();
82     initConfigModule();
83   }
84   
85   protected ExcludeListRef excludeListRef;
86   protected void initExcludeListRef()
87   {
88     List JavaDoc excludeList = new ArrayList JavaDoc();
89     LOGGER.debug("root = "+root);
90     excludeList.add(root);
91     excludeListRef = new ExcludeListRef(excludeList);
92   }
93   
94   protected EditPartListener rootListener = new EditPartListener.Stub()
95   {
96         public void removingChild(EditPart child, int index)
97         {
98             excludeListRef.getExcludeList().remove(child);
99             for (Iterator JavaDoc it = child.getChildren().iterator(); it.hasNext(); ) {
100                 excludeListRef.getExcludeList().remove(it.next());
101             }
102         }
103         public void childAdded(EditPart child, int index)
104         {
105             // exclude the ModelRoot (MultiLayerDrawComponent) and its Layers (Layer)
106
excludeListRef.getExcludeList().add(child);
107             for (Iterator JavaDoc it = child.getChildren().iterator(); it.hasNext(); ) {
108                 excludeListRef.getExcludeList().add(it.next());
109             }
110         }
111     };
112   
113   protected List JavaDoc ignoredClasses = new ArrayList JavaDoc();
114   public void addIgnoreTypeCollection(Collection JavaDoc editPartTypes)
115   {
116     if (editPartTypes == null)
117         throw new IllegalArgumentException JavaDoc("Param editPartTypes must not be null!");
118     
119     if (editPartTypes.isEmpty())
120         return;
121
122     for (Iterator JavaDoc it = editPartTypes.iterator(); it.hasNext(); ) {
123         Object JavaDoc o = it.next();
124         if (o instanceof Class JavaDoc) {
125             Class JavaDoc c = (Class JavaDoc) o;
126             addIgnoreType(c);
127         }
128     }
129     conditionRef.setCondition(createIgnoreCondition(ignoredClasses));
130   }
131     
132   public void addIgnoreType(Class JavaDoc c)
133   {
134         if (AbstractDrawComponentEditPart.class.isAssignableFrom(c) && !ignoredClasses.contains(c)) {
135             ignoredClasses.add(c);
136         }
137         conditionRef.setCondition(createIgnoreCondition(ignoredClasses));
138   }
139   
140   public void addExcludeCollection(Collection JavaDoc editParts)
141   {
142     if (editParts == null)
143             throw new IllegalArgumentException JavaDoc("Param editParts must not be null!");
144     
145     for (Iterator JavaDoc it = editParts.iterator(); it.hasNext(); ) {
146         Object JavaDoc o = it.next();
147         if (o instanceof AbstractDrawComponentEditPart && !excludeListRef.getExcludeList().contains(o)) {
148             EditPart ep = (EditPart) o;
149             excludeListRef.getExcludeList().add(ep);
150         }
151     }
152   }
153   
154   public void addExclude(AbstractDrawComponentEditPart ep)
155   {
156     if (ep == null)
157             throw new IllegalArgumentException JavaDoc("Param ep must not be null!");
158     
159     excludeListRef.getExcludeList().add(ep);
160   }
161   
162   public void setCondition(EditPartViewer.Conditional condition) {
163     conditionRef.setCondition(condition);
164   }
165   
166   protected Class JavaDoc exclusiveClass;
167   public void setExclusiveClass(Class JavaDoc c) {
168     exclusiveClass = c;
169   }
170     
171   protected DescriptorManager descriptorManager = new DescriptorManager();
172   public DescriptorManager getDescriptorManager() {
173     return descriptorManager;
174   }
175   public void setDescriptorManager(DescriptorManager descMan) {
176     this.descriptorManager = descMan;
177   }
178   
179   protected Point relativePoint = null;
180   protected AbstractDrawComponentEditPart oldPart = null;
181   
182 // protected MouseMotionListener mouseListener = new MouseMotionListener.Stub()
183
// {
184
// public void mouseMoved(org.eclipse.draw2d.MouseEvent me)
185
// {
186
// relativePoint = new Point(me.x, me.y);
187
// mousePoint = EditorUtil.toAbsoluteWithScrollOffset(root, me.x, me.y);
188
// EditPart part = viewer.findObjectAtExcluding(relativePoint, excludeListRef.getExcludeList(), conditionRef.getCondition());
189
// statusLineMan.setMessage(getMouseCoordinates());
190
// if (part != null)
191
// {
192
// if (!(part instanceof MultiLayerDrawComponentEditPart) &&
193
// !(part instanceof LayerEditPart))
194
// {
195
// if (!(part instanceof RootEditPart))
196
// {
197
// if (exclusiveClass == null) {
198
// if (part instanceof AbstractDrawComponentEditPart) {
199
// doRollOver((AbstractDrawComponentEditPart)part);
200
// }
201
// }
202
// else {
203
// if (exclusiveClass.equals(part.getClass())) {
204
// AbstractDrawComponentEditPart dcPart = (AbstractDrawComponentEditPart) exclusiveClass.cast(part);
205
// doRollOver(dcPart);
206
// }
207
// }
208
// }
209
// }
210
// }
211
// }
212
// };
213

214   protected MouseMotionListener mouseListener = new MouseMotionListener.Stub()
215   {
216     public void mouseMoved(org.eclipse.draw2d.MouseEvent me)
217     {
218         relativePoint = new Point(me.x, me.y);
219         mousePoint = EditorUtil.toAbsoluteWithScrollOffset(root, me.x, me.y);
220       EditPart part = viewer.findObjectAtExcluding(relativePoint, excludeListRef.getExcludeList(), conditionRef.getCondition());
221       statusLineMan.setMessage(getMouseCoordinates());
222       if (part != null)
223       {
224             if (exclusiveClass == null) {
225             if (part instanceof AbstractDrawComponentEditPart) {
226                 doRollOver((AbstractDrawComponentEditPart)part);
227             }
228             }
229             else {
230                 if (exclusiveClass.equals(part.getClass())) {
231                     AbstractDrawComponentEditPart dcPart = (AbstractDrawComponentEditPart) exclusiveClass.cast(part);
232                     doRollOver(dcPart);
233                 }
234             }
235       }
236     }
237   };
238   
239   protected String JavaDoc getMouseCoordinates()
240   {
241     return "MouseX = "+mousePoint.x+", MouseY = "+mousePoint.y;
242   }
243
244   protected IFigure getFeedbackLayer()
245   {
246     if (root instanceof ScalableFreeformRootEditPart) {
247         ScalableFreeformRootEditPart rootEditPart = (ScalableFreeformRootEditPart) root;
248         IFigure feedbackLayer = rootEditPart.getLayer(ScalableFreeformRootEditPart.FEEDBACK_LAYER);
249         return feedbackLayer;
250     }
251     return null;
252   }
253   
254   protected DrawComponentFigure rollOverFigure = null;
255   protected void addRollOver(AbstractDrawComponentEditPart dcPart)
256   {
257     if (getFeedbackLayer() != null && dcPart != null)
258     {
259         long addStart = System.currentTimeMillis();
260         DrawComponent dc = dcPart.getDrawComponent();
261 // Rectangle figureBounds = dcPart.getFigure().getBounds();
262
if (rollOverFigure == null)
263             rollOverFigure = new DrawComponentFigure();
264         Renderer r = dc.getRenderModeManager().getRenderer(RenderConstants.ROLLOVER_MODE, dc.getClass());
265         
266         if (r != null)
267         {
268             rollOverFigure.setRenderer(r);
269             rollOverFigure.setDrawComponent(dc);
270 // rollOverFigure.setBounds(figureBounds);
271
// add already calls repaint
272
getFeedbackLayer().add(rollOverFigure);
273         }
274         long addEnd = System.currentTimeMillis() - addStart;
275         LOGGER.debug("addRollOver took "+addEnd+" ms!");
276     }
277   }
278
279   protected void removeRollOver()
280   {
281     if (getFeedbackLayer() != null && rollOverFigure != null) {
282         // remove already calls repaint
283
getFeedbackLayer().remove(rollOverFigure);
284     }
285   }
286     
287   protected PreferencesConfigModule prefConfMod = null;
288   protected void initConfigModule()
289   {
290     try {
291         prefConfMod = (PreferencesConfigModule) Config.sharedInstance().createConfigModule(PreferencesConfigModule.class);
292     } catch (ConfigException ce) {
293         throw new RuntimeException JavaDoc(ce);
294     }
295   }
296   
297   protected void doRollOver(AbstractDrawComponentEditPart dcPart)
298   {
299     if (dcPart != null) {
300         DrawComponent dc = dcPart.getDrawComponent();
301         descriptorManager.setDrawComponent(dc);
302         if (prefConfMod.isShowStatusLine())
303             statusLineMan.setMessage(getMouseCoordinates() + ", " + descriptorManager.getEntriesAsString(false));
304                     
305 // removeRollOver();
306
// addRollOver(dcPart);
307
}
308     
309 // if (!dcPart.equals(oldPart)) {
310
// if (oldPart != null) {
311
// oldPart.getDrawComponent().setRenderMode(RenderModeManager.DEFAULT_MODE);
312
// oldPart.getFigure().repaint();
313
// }
314
// dcPart.getDrawComponent().setRenderMode(RenderModeManager.ROLLOVER_MODE);
315
// oldPart = dcPart;
316
// dcPart.getFigure().repaint();
317
// LOGGER.debug("dcPart = "+dcPart);
318
// }
319
}
320     
321   public static class ConditionRef
322   {
323     private EditPartViewer.Conditional condition;
324     public ConditionRef(EditPartViewer.Conditional condition)
325     {
326         this.condition = condition;
327     }
328     public EditPartViewer.Conditional getCondition() {
329             return condition;
330         }
331     public void setCondition(EditPartViewer.Conditional condition) {
332             this.condition = condition;
333         }
334   }
335
336   public static class ExcludeListRef
337   {
338     private Collection JavaDoc excludeList;
339     public ExcludeListRef(Collection JavaDoc excludeList) {
340         this.excludeList = excludeList;
341     }
342         public Collection JavaDoc getExcludeList() {
343             return excludeList;
344         }
345         public void setExcludeList(Collection JavaDoc excludeList) {
346             this.excludeList = excludeList;
347         }
348   }
349   
350   protected ConditionRef conditionRef = null;
351   protected EditPartViewer.Conditional createIgnoreCondition(final Collection JavaDoc classes)
352   {
353     EditPartViewer.Conditional condition = new EditPartViewer.Conditional()
354     {
355         public boolean evaluate(EditPart part)
356         {
357             if (!(part instanceof AbstractDrawComponentEditPart))
358                 return false;
359             
360             Class JavaDoc c = part.getClass();
361             if (classes.contains(c)) {
362                 return false;
363             }
364             
365             return true;
366         }
367         };
368         return condition;
369   }
370   
371   protected EditPartViewer.Conditional createExclusiveCondition(final Class JavaDoc c)
372   {
373     EditPartViewer.Conditional condition = new EditPartViewer.Conditional()
374     {
375         public boolean evaluate(EditPart part)
376         {
377             if (part.getClass().equals(c))
378                 return true;
379             
380             return false;
381         }
382     };
383     return condition;
384   }
385   
386   protected EditPartViewer.Conditional createDefaultCondition()
387   {
388     EditPartViewer.Conditional condition = new EditPartViewer.Conditional()
389     {
390         public boolean evaluate(EditPart part)
391         {
392             if (part instanceof MultiLayerDrawComponentEditPart ||
393                     part instanceof LayerEditPart)
394                 return false;
395             
396             if (part instanceof RootEditPart)
397                 return false;
398             
399             if (part instanceof AbstractDrawComponentEditPart)
400                 return true;
401             
402             return false;
403         }
404     };
405     return condition;
406   }
407    
408 }
409
Popular Tags