svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / fframes / FFrameGroup.java @ 324
History | View | Annotate | Download (11.2 KB)
1 | 5 | jldominguez | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program 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
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | */
|
||
22 | package org.gvsig.app.project.documents.layout.fframes; |
||
23 | |||
24 | import java.awt.Graphics2D; |
||
25 | import java.awt.geom.AffineTransform; |
||
26 | import java.awt.geom.Point2D; |
||
27 | import java.awt.geom.Rectangle2D; |
||
28 | import java.awt.image.BufferedImage; |
||
29 | import java.util.ArrayList; |
||
30 | import java.util.List; |
||
31 | |||
32 | import org.gvsig.andami.PluginServices; |
||
33 | import org.gvsig.app.project.Project; |
||
34 | import org.gvsig.app.project.documents.layout.FLayoutUtilities; |
||
35 | import org.gvsig.compat.print.PrintAttributes; |
||
36 | import org.gvsig.fmap.geom.Geometry; |
||
37 | import org.gvsig.tools.ToolsLocator; |
||
38 | import org.gvsig.tools.dynobject.DynStruct; |
||
39 | import org.gvsig.tools.persistence.PersistenceManager; |
||
40 | import org.gvsig.tools.persistence.PersistentState; |
||
41 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
42 | |||
43 | /**
|
||
44 | * FFrame que contiene a su vez un ArrayList de FFrames de cualquier tipo
|
||
45 | * incluso de si mismo.
|
||
46 | *
|
||
47 | * @author Vicente Caballero Navarro
|
||
48 | */
|
||
49 | public class FFrameGroup extends AbstractFFrameViewDependence implements |
||
50 | IFFrameUseProject, IFFrameViewDependence { |
||
51 | |||
52 | public static final String PERSISTENCE_DEFINITION_NAME = "FFrameGroup"; |
||
53 | |||
54 | private static final String FFRAMES_FIELD = "fframes"; |
||
55 | |||
56 | private List<IFFrame> m_fframes = new ArrayList<IFFrame>(); |
||
57 | private Rectangle2D.Double rg = null; |
||
58 | private AffineTransform m_at; |
||
59 | private Project project;
|
||
60 | |||
61 | /**
|
||
62 | * Crea un nuevo FFrameGroup.
|
||
63 | */
|
||
64 | public FFrameGroup() {
|
||
65 | |||
66 | } |
||
67 | |||
68 | /**
|
||
69 | * A?ade al Arraylist un nuevo FFrame para formar parte del grupo.
|
||
70 | *
|
||
71 | * @param fframe
|
||
72 | * FFrame a a?adir.
|
||
73 | */
|
||
74 | public void addFFrame(IFFrame fframe) { |
||
75 | m_fframes.add(fframe); |
||
76 | } |
||
77 | |||
78 | /**
|
||
79 | * Devuelve una ArrayList que contiene todos los FFrames que forman parte
|
||
80 | * del grupo.
|
||
81 | *
|
||
82 | * @return Arraylist con los fframes.
|
||
83 | */
|
||
84 | public IFFrame[] getFFrames() { |
||
85 | return m_fframes.toArray(new IFFrame[0]); |
||
86 | } |
||
87 | |||
88 | /**
|
||
89 | * Devuelve el rect?ngulo que contiene a todos los fframes seleccionados.
|
||
90 | *
|
||
91 | * @param at
|
||
92 | * Matriz de transformaci?n
|
||
93 | *
|
||
94 | * @return Rect?ngulo.
|
||
95 | */
|
||
96 | public Rectangle2D.Double getRectangle(AffineTransform at) { |
||
97 | boolean first = true; |
||
98 | Rectangle2D.Double rec = new Rectangle2D.Double(); |
||
99 | IFFrame[] fframes = getFFrames();
|
||
100 | for (int i = 0; i < fframes.length; i++) { |
||
101 | Rectangle2D.Double rs = fframes[i].getBoundingBox(at);
|
||
102 | |||
103 | if (first) {
|
||
104 | rec.setRect(rs); |
||
105 | first = false;
|
||
106 | } |
||
107 | |||
108 | rec.add(rs); |
||
109 | } |
||
110 | |||
111 | rg = new Rectangle2D.Double(); |
||
112 | rg.setRect(FLayoutUtilities.toSheetRect(rec, m_at)); |
||
113 | |||
114 | return rec;
|
||
115 | } |
||
116 | |||
117 | /**
|
||
118 | * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
|
||
119 | * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
|
||
120 | * de dibujar.
|
||
121 | *
|
||
122 | * @param g
|
||
123 | * Graphics
|
||
124 | * @param at
|
||
125 | * Transformada afin.
|
||
126 | * @param rv
|
||
127 | * rect?ngulo sobre el que hacer un clip.
|
||
128 | * @param imgBase
|
||
129 | * Imagen utilizada para acelerar el dibujado.
|
||
130 | */
|
||
131 | public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv, |
||
132 | BufferedImage imgBase) {
|
||
133 | Rectangle2D.Double r = getBoundingBox(at);
|
||
134 | g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y |
||
135 | + (r.height / 2));
|
||
136 | m_at = at; |
||
137 | |||
138 | for (int i = 0; i < m_fframes.size(); i++) { |
||
139 | m_fframes.get(i).draw(g, at, rv, imgBase); |
||
140 | } |
||
141 | |||
142 | g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y |
||
143 | + (r.height / 2));
|
||
144 | } |
||
145 | |||
146 | /**
|
||
147 | * Rellena la transformada que se esta utilizando en el Layout.
|
||
148 | *
|
||
149 | * @param at
|
||
150 | * Matriz de transformaci?n.
|
||
151 | */
|
||
152 | public void setAt(AffineTransform at) { |
||
153 | m_at = at; |
||
154 | } |
||
155 | |||
156 | /**
|
||
157 | * Reimplementaci?n del m?todo papa poder modificar los BoundBox de cada
|
||
158 | * uno de los FFrames que contiene dentro este FFrameGroup.
|
||
159 | *
|
||
160 | * @param r
|
||
161 | * Rect?ngulo.
|
||
162 | */
|
||
163 | public void setBoundBox(Rectangle2D r) { |
||
164 | getBoundBox().setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight()); |
||
165 | |||
166 | double dx = 1; |
||
167 | double dy = 1; |
||
168 | double dw = 1; |
||
169 | double dh = 1; |
||
170 | |||
171 | if (rg != null) { |
||
172 | Rectangle2D.Double raux1 =
|
||
173 | new Rectangle2D.Double(rg.x, rg.y, rg.width, rg.height); |
||
174 | dx = r.getX() - raux1.x; |
||
175 | dy = r.getY() - raux1.y; |
||
176 | dw = r.getWidth() / raux1.width; |
||
177 | dh = r.getHeight() / raux1.height; |
||
178 | IFFrame[] fframes = getFFrames();
|
||
179 | for (int i = 0; i < fframes.length; i++) { |
||
180 | IFFrame fframe = fframes[i]; |
||
181 | Rectangle2D.Double raux = new Rectangle2D.Double(); |
||
182 | raux.setRect(fframe.getBoundBox()); |
||
183 | |||
184 | AffineTransform escalado = new AffineTransform(); |
||
185 | |||
186 | escalado.setToScale(dw, dh); |
||
187 | escalado.translate(dx - r.getX(), dy - r.getY()); |
||
188 | |||
189 | Point2D.Double pd = new Point2D.Double(); |
||
190 | escalado.transform(new Point2D.Double(raux.x, raux.y), pd); |
||
191 | |||
192 | raux.x = pd.x + r.getX(); |
||
193 | raux.y = pd.y + r.getY(); |
||
194 | raux.width = raux.width * dw; |
||
195 | raux.height = raux.height * dh; |
||
196 | |||
197 | fframe.setBoundBox(raux); |
||
198 | } |
||
199 | } else {
|
||
200 | rg = new Rectangle2D.Double(); |
||
201 | rg.setRect(r); |
||
202 | } |
||
203 | |||
204 | rg.setRect(r); |
||
205 | } |
||
206 | |||
207 | /**
|
||
208 | * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getNameFFrame()
|
||
209 | */
|
||
210 | public String getNameFFrame() { |
||
211 | return PluginServices.getText(this, "grupo") + num; |
||
212 | } |
||
213 | |||
214 | public String getName() { |
||
215 | return PERSISTENCE_DEFINITION_NAME;
|
||
216 | } |
||
217 | |||
218 | /**
|
||
219 | * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#print(java.awt.Graphics2D,
|
||
220 | * java.awt.geom.AffineTransform)
|
||
221 | */
|
||
222 | public void print(Graphics2D g, AffineTransform at, Geometry geom, |
||
223 | PrintAttributes printingProperties) { |
||
224 | Rectangle2D.Double r = getBoundingBox(at);
|
||
225 | g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y |
||
226 | + (r.height / 2));
|
||
227 | IFFrame[] fframes = m_fframes.toArray(new IFFrame[0]); |
||
228 | for (int i = 0; i < fframes.length; i++) { |
||
229 | fframes[i].print(g, at, geom, printingProperties); |
||
230 | } |
||
231 | |||
232 | g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y |
||
233 | + (r.height / 2));
|
||
234 | } |
||
235 | |||
236 | /**
|
||
237 | * Inserta una referencia al proyecto nesecario.
|
||
238 | *
|
||
239 | * @param project
|
||
240 | * DOCUMENT ME!
|
||
241 | */
|
||
242 | public void setProject(Project project) { |
||
243 | this.project = project;
|
||
244 | } |
||
245 | |||
246 | public void initialize() { |
||
247 | |||
248 | } |
||
249 | |||
250 | public void clearFFrames() { |
||
251 | m_fframes.clear(); |
||
252 | } |
||
253 | |||
254 | public IFFrame removeFFrame(int i) { |
||
255 | return m_fframes.remove(i);
|
||
256 | } |
||
257 | |||
258 | public void removeFFrame(IFFrame fframe) { |
||
259 | m_fframes.remove(fframe); |
||
260 | } |
||
261 | |||
262 | public IFFrame clone() throws CloneNotSupportedException { |
||
263 | FFrameGroup frame = (FFrameGroup) super.clone();
|
||
264 | frame.setSelected(this.getSelected() != IFFrame.NOSELECT);
|
||
265 | frame.m_fframes = new ArrayList<IFFrame>(); |
||
266 | |||
267 | for (int i = 0; i < m_fframes.size(); i++) { |
||
268 | frame.addFFrame((IFFrame) m_fframes.get(i).clone()); |
||
269 | } |
||
270 | return frame;
|
||
271 | } |
||
272 | |||
273 | public void setFFrameDependence(IFFrame f) { |
||
274 | IFFrame[] frames = getFFrames();
|
||
275 | for (int i = 0; i < frames.length; i++) { |
||
276 | if (frames[i] instanceof IFFrameViewDependence) { |
||
277 | ((IFFrameViewDependence) frames[i]).setFFrameDependence(f); |
||
278 | } |
||
279 | } |
||
280 | } |
||
281 | |||
282 | public IFFrame[] getFFrameDependence() { |
||
283 | IFFrame[] frames = getFFrames();
|
||
284 | ArrayList<IFFrame> dependences = new ArrayList<IFFrame>(); |
||
285 | for (int i = 0; i < frames.length; i++) { |
||
286 | if (frames[i] instanceof IFFrameViewDependence) { |
||
287 | IFFrame[] framesAux =
|
||
288 | ((IFFrameViewDependence) frames[i]).getFFrameDependence(); |
||
289 | for (int j = 0; j < framesAux.length; j++) { |
||
290 | dependences.add(framesAux[i]); |
||
291 | } |
||
292 | } |
||
293 | } |
||
294 | return dependences.toArray(new IFFrame[0]); |
||
295 | } |
||
296 | |||
297 | public void refreshDependence(IFFrame fant, IFFrame fnew) { |
||
298 | IFFrame[] frames = getFFrames();
|
||
299 | for (int i = 0; i < frames.length; i++) { |
||
300 | |||
301 | if (fnew instanceof FFrameGroup) { |
||
302 | IFFrame[] framesGroupNew = ((FFrameGroup) fnew).getFFrames();
|
||
303 | for (int j = 0; j < framesGroupNew.length; j++) { |
||
304 | if (fant instanceof FFrameGroup) { |
||
305 | IFFrame[] framesGroupAnt =
|
||
306 | ((FFrameGroup) fant).getFFrames(); |
||
307 | for (int k = 0; k < framesGroupAnt.length; k++) { |
||
308 | if (framesGroupAnt[k] instanceof IFFrameViewDependence) { |
||
309 | refreshDependence(framesGroupAnt[k], |
||
310 | framesGroupNew[j]); |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 | } |
||
315 | } else
|
||
316 | if (frames[i] instanceof IFFrameViewDependence) { |
||
317 | ((IFFrameViewDependence) frames[i]).refreshDependence(fant, |
||
318 | fnew); |
||
319 | } |
||
320 | } |
||
321 | } |
||
322 | |||
323 | public static void registerPersistent() { |
||
324 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
325 | if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) { |
||
326 | DynStruct definition = |
||
327 | manager.addDefinition(FFrameGroup.class, |
||
328 | PERSISTENCE_DEFINITION_NAME, |
||
329 | "FFrameGroup persistence definition", null, null); |
||
330 | |||
331 | definition |
||
332 | .extend(manager |
||
333 | .getDefinition(AbstractFFrameViewDependence.PERSISTENCE_DEFINITION_NAME)); |
||
334 | |||
335 | definition.addDynFieldList(FFRAMES_FIELD) |
||
336 | .setClassOfItems(IFFrame.class).setMandatory(true);
|
||
337 | } |
||
338 | } |
||
339 | |||
340 | @Override
|
||
341 | public void loadFromState(PersistentState state) |
||
342 | throws PersistenceException {
|
||
343 | super.loadFromState(state);
|
||
344 | m_fframes = (List<IFFrame>) state.getList(FFRAMES_FIELD);
|
||
345 | } |
||
346 | |||
347 | @Override
|
||
348 | public void saveToState(PersistentState state) throws PersistenceException { |
||
349 | super.saveToState(state);
|
||
350 | state.set(FFRAMES_FIELD, m_fframes); |
||
351 | } |
||
352 | } |