KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.eclipse.draw2d.FigureListener;
34 import org.eclipse.draw2d.FreeformFigure;
35 import org.eclipse.draw2d.FreeformListener;
36 import org.eclipse.draw2d.IFigure;
37 import org.eclipse.draw2d.geometry.Insets;
38 import org.eclipse.draw2d.geometry.Rectangle;
39
40
41 public class LayerFreeformHelper
42 implements FreeformListener
43 {
44   public static final Logger LOGGER = Logger.getLogger(LayerFreeformHelper.class);
45   
46   class ChildTracker implements FigureListener {
47     public void figureMoved(IFigure source) {
48         invalidate();
49     }
50   }
51
52   private FreeformFigure host;
53   private Rectangle freeformExtent;
54   private FigureListener figureListener = new ChildTracker();
55
56   LayerFreeformHelper(FreeformFigure host) {
57     this.host = host;
58   }
59
60   public Rectangle getFreeformExtent()
61   {
62     if (freeformExtent != null)
63         return freeformExtent;
64     Rectangle r;
65     List JavaDoc children = host.getChildren();
66     for (int i = 0; i < children.size(); i++) {
67         IFigure child = (IFigure)children.get(i);
68 // if (child instanceof OversizedBufferFreeformLayer)
69
// r = child.getBounds();
70
// else if (child instanceof FreeformFigure)
71
// r = ((FreeformFigure) child).getFreeformExtent();
72
if (child instanceof FreeformFigure)
73             r = ((FreeformFigure) child).getFreeformExtent();
74         else
75             r = child.getBounds();
76         if (freeformExtent == null)
77             freeformExtent = r.getCopy();
78         else
79             freeformExtent.union(r);
80     }
81     Insets insets = host.getInsets();
82     if (freeformExtent == null)
83         freeformExtent = new Rectangle(0, 0, insets.getWidth(), insets.getHeight());
84     else {
85         host.translateToParent(freeformExtent);
86         freeformExtent.expand(insets);
87     }
88 // System.out.println("New extent calculated for " + host + " = " + freeformExtent);
89
return freeformExtent;
90   }
91
92   public void hookChild(IFigure child)
93   {
94 // if (child instanceof OversizedBufferFreeformLayer) {
95
// return;
96
// }
97
invalidate();
98     if (child instanceof FreeformFigure)
99         ((FreeformFigure)child).addFreeformListener(this);
100     else
101         child.addFigureListener(figureListener);
102   }
103
104   void invalidate()
105   {
106     freeformExtent = null;
107     host.fireExtentChanged();
108     if (host.getParent() != null)
109       if (host.getParent() instanceof MLDCFreeformLayer) {
110         // Do nothing
111
// LOGGER.debug("host.getParent() instanceof MLDCFreeformLayer!");
112
} else {
113             host.getParent().revalidate();
114       }
115     else
116       if (host instanceof OversizedBufferFreeformLayer) {
117         // Do nothing
118
// LOGGER.debug("host.getParent() instanceof OversizedBufferFreeformLayer!");
119
}
120       else {
121             host.revalidate();
122       }
123     
124 // freeformExtent = null;
125
// host.fireExtentChanged();
126
// if (host.getParent() != null)
127
// host.getParent().revalidate();
128
// else
129
// host.revalidate();
130
}
131
132   public void notifyFreeformExtentChanged() {
133     //A childs freeform extent has changed, therefore this extent must be recalculated
134
invalidate();
135   }
136
137   public void setFreeformBounds(Rectangle bounds) {
138     host.setBounds(bounds);
139     bounds = bounds.getCopy();
140     host.translateFromParent(bounds);
141     List JavaDoc children = host.getChildren();
142     for (int i = 0; i < children.size(); i++) {
143         IFigure child = (IFigure)children.get(i);
144         if (child instanceof FreeformFigure)
145             ((FreeformFigure) child).setFreeformBounds(bounds);
146     }
147   }
148
149   public void unhookChild(IFigure child) {
150     invalidate();
151     if (child instanceof FreeformFigure)
152         ((FreeformFigure)child).removeFreeformListener(this);
153     else
154         child.removeFigureListener(figureListener);
155   }
156 }
157
Popular Tags