KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > impl > MultiLayerDrawComponentImpl


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

26
27 package org.nightlabs.editor2d.impl;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import org.nightlabs.editor2d.DrawComponent;
37 import org.nightlabs.editor2d.EditorRuler;
38 import org.nightlabs.editor2d.Layer;
39 import org.nightlabs.editor2d.MultiLayerDrawComponent;
40 import org.nightlabs.editor2d.render.RenderModeListener;
41 import org.nightlabs.editor2d.render.RenderModeManager;
42
43 public class MultiLayerDrawComponentImpl
44 extends DrawComponentContainerImpl
45 implements MultiLayerDrawComponent
46 {
47   protected static final boolean GRID_ENABLED_EDEFAULT = false;
48   protected boolean gridEnabled = GRID_ENABLED_EDEFAULT;
49   
50   protected static final boolean RULERS_ENABLED_EDEFAULT = false;
51   protected boolean rulersEnabled = RULERS_ENABLED_EDEFAULT;
52   
53   protected static final boolean SNAP_TO_GEOMETRY_EDEFAULT = false;
54   protected boolean snapToGeometry = SNAP_TO_GEOMETRY_EDEFAULT;
55
56   protected static final double ZOOM_EDEFAULT = 1.0;
57   protected double zoom = ZOOM_EDEFAULT;
58
59   protected EditorRuler leftRuler = null;
60   protected EditorRuler topRuler = null;
61
62   protected static final long LAST_ID_EDEFAULT = 0L;
63   protected long lastID = LAST_ID_EDEFAULT;
64
65     protected static final Map JavaDoc CLASS2_DRAW_COMPONENTS_EDEFAULT = null;
66     protected transient Map JavaDoc class2DrawComponents = CLASS2_DRAW_COMPONENTS_EDEFAULT;
67
68   public MultiLayerDrawComponentImpl() {
69     super();
70     // TODO: check if this was unnecessary
71
setRenderModeManager(new RenderModeManager());
72   }
73
74   protected Layer currentLayer = null;
75   
76   /**
77    * @see org.nightlabs.editor2d.MultiLayerDrawComponent#getCurrentLayer()
78    */

79   public Layer getCurrentLayer() {
80         return currentLayer;
81     }
82
83   /**
84    * @see org.nightlabs.editor2d.MultiLayerDrawComponent#setCurrentLayer(Layer)
85    */

86   public void setCurrentLayer(Layer newCurrentLayer)
87   {
88         Layer oldCurrentLayer = currentLayer;
89         currentLayer = newCurrentLayer;
90         firePropertyChange(PROP_CURRENT_LAYER, oldCurrentLayer, currentLayer);
91     }
92
93   /**
94    * @see org.nightlabs.editor2d.MultiLayerDrawComponent#isGridEnabled()
95    */

96   public boolean isGridEnabled() {
97         return gridEnabled;
98     }
99
100   /**
101    * @see org.nightlabs.editor2d.MultiLayerDrawComponent#setGridEnabled(boolean)
102    */

103   public void setGridEnabled(boolean newGridEnabled) {
104         gridEnabled = newGridEnabled;
105     }
106
107   /**
108    * @see org.nightlabs.editor2d.MultiLayerDrawComponent#isRulersEnabled()
109    */

110   public boolean isRulersEnabled() {
111         return rulersEnabled;
112     }
113
114     /**
115      * <!-- begin-user-doc -->
116    * <!-- end-user-doc -->
117      * @generated
118      */

119   public void setRulersEnabled(boolean newRulersEnabled) {
120         rulersEnabled = newRulersEnabled;
121     }
122
123     /**
124      * <!-- begin-user-doc -->
125    * <!-- end-user-doc -->
126      * @generated
127      */

128   public boolean isSnapToGeometry() {
129         return snapToGeometry;
130     }
131
132     /**
133      * <!-- begin-user-doc -->
134    * <!-- end-user-doc -->
135      * @generated
136      */

137   public void setSnapToGeometry(boolean newSnapToGeometry) {
138         snapToGeometry = newSnapToGeometry;
139     }
140
141     /**
142      * <!-- begin-user-doc -->
143    * <!-- end-user-doc -->
144      * @generated
145      */

146   public double getZoom() {
147         return zoom;
148     }
149
150     /**
151      * <!-- begin-user-doc -->
152    * <!-- end-user-doc -->
153      * @generated
154      */

155   public void setZoom(double newZoom) {
156         zoom = newZoom;
157     }
158
159     /**
160      * <!-- begin-user-doc -->
161    * <!-- end-user-doc -->
162      * @generated
163      */

164   public EditorRuler getLeftRuler() {
165         return leftRuler;
166     }
167
168     /**
169      * <!-- begin-user-doc -->
170    * <!-- end-user-doc -->
171      * @generated
172      */

173   public void setLeftRuler(EditorRuler newLeftRuler) {
174     leftRuler = newLeftRuler;
175     }
176
177     /**
178      * <!-- begin-user-doc -->
179    * <!-- end-user-doc -->
180      * @generated
181      */

182   public EditorRuler getTopRuler() {
183         return topRuler;
184     }
185
186     /**
187      * <!-- begin-user-doc -->
188    * <!-- end-user-doc -->
189      * @generated
190      */

191   public void setTopRuler(EditorRuler newTopRuler) {
192     topRuler = newTopRuler;
193     }
194
195     /**
196      * <!-- begin-user-doc -->
197    * <!-- end-user-doc -->
198      * @generated
199      */

200   public long getLastID() {
201         return lastID;
202     }
203
204     /**
205      * <!-- begin-user-doc -->
206      * <!-- end-user-doc -->
207      * @generated
208      */

209     public void setLastID(long newLastID) {
210         lastID = newLastID;
211     }
212
213     /**
214      * <!-- begin-user-doc -->
215      * <!-- end-user-doc -->
216      *
217      */

218     public Map JavaDoc getClass2DrawComponents()
219     {
220         if (class2DrawComponents == null)
221             class2DrawComponents = new HashMap JavaDoc();
222                 
223         return class2DrawComponents;
224     }
225
226     /**
227      * <!-- begin-user-doc -->
228      * <!-- end-user-doc -->
229      * @generated
230      */

231     public void setClass2DrawComponents(Map JavaDoc newClass2DrawComponents) {
232         class2DrawComponents = newClass2DrawComponents;
233     }
234
235     /**
236      * <!-- begin-user-doc -->
237      * <!-- end-user-doc -->
238      *
239      */

240     public Map JavaDoc getId2DrawComponent()
241     {
242         if (id2DrawComponent == null)
243             id2DrawComponent = new HashMap JavaDoc();
244         
245         return id2DrawComponent;
246     }
247
248     /**
249      * <!-- begin-user-doc -->
250      * <!-- end-user-doc -->
251      * @generated
252      */

253     public void setId2DrawComponent(Map JavaDoc newId2DrawComponent) {
254         id2DrawComponent = newId2DrawComponent;
255     }
256
257   /**
258    * <!-- begin-user-doc -->
259    * <!-- end-user-doc -->
260    *
261    */

262   public long nextID() {
263     return ++lastID;
264   }
265
266   
267   /**
268    * <!-- begin-user-doc -->
269    * <!-- end-user-doc -->
270    *
271    */

272   public DrawComponent getDrawComponent(long id) {
273     return (DrawComponent) getId2DrawComponent().get(new Long JavaDoc(id));
274   }
275   
276   public void registerDrawComponent(DrawComponent drawComponent)
277   {
278     if (drawComponent != null)
279     {
280         // only register if the drawComponent haven�t been registered yet
281
if (drawComponent.getId() == DrawComponent.ID_EDEFAULT)
282         {
283         lastID = nextID();
284         drawComponent.setId(lastID);
285         getId2DrawComponent().put(new Long JavaDoc(lastID), drawComponent);
286         
287         Class JavaDoc dcClass = drawComponent.getClass();
288         List JavaDoc typeList = null;
289         if (!getClass2DrawComponents().containsKey(dcClass))
290         {
291           typeList = new ArrayList JavaDoc();
292           typeList.add(drawComponent);
293           getClass2DrawComponents().put(dcClass, typeList);
294           getPropertyChangeSupport().firePropertyChange(TYPE_ADDED, null, dcClass);
295         }
296         else if (getClass2DrawComponents().containsKey(dcClass))
297         {
298           typeList = (List JavaDoc) getClass2DrawComponents().get(dcClass);
299           typeList.add(drawComponent);
300         }
301   
302         if (drawComponent.getName() == "" && typeList != null) {
303           drawComponent.setName(drawComponent.getTypeName() + " " + typeList.size());
304         }
305         
306         drawComponent.setRenderMode(getRenderModeManager().getCurrentRenderMode());
307         }
308     }
309   }
310   
311   public void unregisterDrawComponent(DrawComponent drawComponent)
312   {
313     getId2DrawComponent().remove(new Long JavaDoc(drawComponent.getId()));
314     
315     Class JavaDoc dcClass = drawComponent.getClass();
316     if (getClass2DrawComponents().containsKey(dcClass)) {
317       Collection JavaDoc typeCollection = (Collection JavaDoc) getClass2DrawComponents().get(dcClass);
318       typeCollection.remove(drawComponent);
319     }
320   }
321   
322 // public void unregisterDrawComponent(DrawComponent drawComponent)
323
// {
324
// unregisterDrawComponent(drawComponent.getId());
325
// }
326

327   public void unregisterDrawComponent(long id)
328   {
329     DrawComponent dc = (DrawComponent) getId2DrawComponent().remove(new Long JavaDoc(id));
330     
331     Class JavaDoc dcClass = dc.getClass();
332     if (getClass2DrawComponents().containsKey(dcClass)) {
333       Collection JavaDoc typeCollection = (Collection JavaDoc) getClass2DrawComponents().get(dcClass);
334       typeCollection.remove(dc);
335     }
336   }
337
338     /**
339      * <!-- begin-user-doc -->
340    * <!-- end-user-doc -->
341      * @generated
342      */

343   public String JavaDoc toString()
344   {
345         StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
346         result.append(" (gridEnabled: ");
347         result.append(gridEnabled);
348         result.append(", rulersEnabled: ");
349         result.append(rulersEnabled);
350         result.append(", snapToGeometry: ");
351         result.append(snapToGeometry);
352         result.append(", zoom: ");
353         result.append(zoom);
354         result.append(", lastID: ");
355         result.append(lastID);
356         result.append(", class2DrawComponents: ");
357         result.append(class2DrawComponents);
358         result.append(", id2DrawComponent: ");
359         result.append(id2DrawComponent);
360         result.append(')');
361         return result.toString();
362     }
363
364     protected transient Map JavaDoc id2DrawComponent = null;
365   protected static final List JavaDoc EMPTY_LIST = new LinkedList JavaDoc();
366     
367   public List JavaDoc getDrawComponents(Class JavaDoc type)
368   {
369     if (getClass2DrawComponents().containsKey(type)) {
370         List JavaDoc types = (List JavaDoc) getClass2DrawComponents().get(type);
371         if (types != null)
372             return types;
373         else
374             return EMPTY_LIST;
375     }
376     return EMPTY_LIST;
377   }
378      
379   protected RenderModeListener renderModeListener = new RenderModeListener()
380   {
381         public void renderModeChanges(int renderMode)
382         {
383             setRenderMode(renderMode);
384         }
385     };
386
387     public void setRenderModeManager(RenderModeManager man)
388     {
389         if (renderModeManager != null)
390             renderModeManager.removeRenderModeListener(renderModeListener);
391         
392         this.renderModeManager = man;
393         renderModeManager.addRenderModeListener(renderModeListener);
394         super.setRenderModeManager(man);
395     }
396             
397 } //MultiLayerDrawComponentImpl
398
Popular Tags