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 / FFrameTable.java @ 318
History | View | Annotate | Download (15.5 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.Color; |
||
25 | import java.awt.Graphics2D; |
||
26 | import java.awt.Image; |
||
27 | import java.awt.event.MouseEvent; |
||
28 | import java.awt.geom.AffineTransform; |
||
29 | import java.awt.geom.Point2D; |
||
30 | import java.awt.geom.Rectangle2D; |
||
31 | import java.awt.image.BufferedImage; |
||
32 | import java.util.ArrayList; |
||
33 | import java.util.List; |
||
34 | |||
35 | import org.gvsig.andami.PluginServices; |
||
36 | import org.gvsig.fmap.dal.exception.ReadException; |
||
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 | * DOCUMENT ME!
|
||
45 | *
|
||
46 | * @author Vicente Caballero Navarro
|
||
47 | */
|
||
48 | public class FFrameTable extends FFrameGroup implements IFFrameGroupSelectable { |
||
49 | |||
50 | public static final String PERSISTENCE_DEFINITION_NAME = "FFrameTable"; |
||
51 | |||
52 | private static final String NUMCOLUMNS_FIELD = "numColumns"; |
||
53 | private static final String NUMROWS_FIELD = "numRows"; |
||
54 | |||
55 | private int numColumns = 2; |
||
56 | private int numRows = 2; |
||
57 | private static AffineTransform identity = new AffineTransform(); |
||
58 | private boolean selectFFrameBasic = false; |
||
59 | private static Image iMove = null; |
||
60 | |||
61 | /**
|
||
62 | * DOCUMENT ME!
|
||
63 | *
|
||
64 | * @param g
|
||
65 | * DOCUMENT ME!
|
||
66 | * @param at
|
||
67 | * DOCUMENT ME!
|
||
68 | * @param rv
|
||
69 | * DOCUMENT ME!
|
||
70 | * @param imgBase
|
||
71 | * DOCUMENT ME!
|
||
72 | */
|
||
73 | public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv, |
||
74 | BufferedImage imgBase) {
|
||
75 | Rectangle2D.Double r = getBoundingBox(at);
|
||
76 | g.setColor(Color.black);
|
||
77 | g.rotate(Math.toRadians(getRotation()), r.x + (r.width / 2), r.y |
||
78 | + (r.height / 2));
|
||
79 | drawRectangles(g, at, rv, imgBase); |
||
80 | g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y |
||
81 | + (r.height / 2));
|
||
82 | super.draw(g, at, rv, imgBase);
|
||
83 | } |
||
84 | |||
85 | private void drawRectangles(Graphics2D g, AffineTransform at, |
||
86 | Rectangle2D rv, BufferedImage imgBase) { |
||
87 | IFFrame[] fframes = getFFrames();
|
||
88 | for (int i = 0; i < fframes.length; i++) { |
||
89 | FFrameBasic basic = (FFrameBasic) fframes[i]; |
||
90 | basic.draw(g, at, rv, imgBase); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /**
|
||
95 | * DOCUMENT ME!
|
||
96 | *
|
||
97 | * @param r
|
||
98 | * DOCUMENT ME!
|
||
99 | * @param g
|
||
100 | * DOCUMENT ME!
|
||
101 | */
|
||
102 | public void drawBox(Rectangle2D r, Graphics2D g) { |
||
103 | calculateTable(r); |
||
104 | IFFrame[] fframes = getFFrames();
|
||
105 | for (int i = 0; i < fframes.length; i++) { |
||
106 | FFrameBasic basic = (FFrameBasic) fframes[i]; |
||
107 | basic.draw(g, identity, null, null); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /**
|
||
112 | * DOCUMENT ME!
|
||
113 | *
|
||
114 | * @param g
|
||
115 | * DOCUMENT ME!
|
||
116 | * @param at
|
||
117 | * DOCUMENT ME!
|
||
118 | *
|
||
119 | * @throws ReadDriverException
|
||
120 | */
|
||
121 | public void print(Graphics2D g, AffineTransform at) throws ReadException { |
||
122 | draw(g, at, null, null); |
||
123 | } |
||
124 | |||
125 | /**
|
||
126 | * DOCUMENT ME!
|
||
127 | *
|
||
128 | * @return DOCUMENT ME!
|
||
129 | */
|
||
130 | public String getNameFFrame() { |
||
131 | return PluginServices.getText(this, "box") + num; |
||
132 | } |
||
133 | |||
134 | public String getName() { |
||
135 | return PERSISTENCE_DEFINITION_NAME;
|
||
136 | } |
||
137 | |||
138 | /**
|
||
139 | * DOCUMENT ME!
|
||
140 | *
|
||
141 | * @return DOCUMENT ME!
|
||
142 | */
|
||
143 | public int getNumColumns() { |
||
144 | return numColumns;
|
||
145 | } |
||
146 | |||
147 | /**
|
||
148 | * DOCUMENT ME!
|
||
149 | *
|
||
150 | * @param numColumns
|
||
151 | * DOCUMENT ME!
|
||
152 | */
|
||
153 | public void setNumColumns(int numColumns) { |
||
154 | this.numColumns = numColumns;
|
||
155 | } |
||
156 | |||
157 | /**
|
||
158 | * DOCUMENT ME!
|
||
159 | *
|
||
160 | * @return DOCUMENT ME!
|
||
161 | */
|
||
162 | public int getNumRows() { |
||
163 | return numRows;
|
||
164 | } |
||
165 | |||
166 | /**
|
||
167 | * DOCUMENT ME!
|
||
168 | *
|
||
169 | * @param numRows
|
||
170 | * DOCUMENT ME!
|
||
171 | */
|
||
172 | public void setNumRows(int numRows) { |
||
173 | this.numRows = numRows;
|
||
174 | } |
||
175 | |||
176 | public void calculateTable(Rectangle2D r) { |
||
177 | double wC = r.getWidth() / numColumns;
|
||
178 | double hR = r.getHeight() / numRows;
|
||
179 | int rows = numRows;
|
||
180 | clearFFrames(); |
||
181 | for (int i = 0; i < numColumns; i++) { |
||
182 | for (int j = 0; j < rows; j++) { |
||
183 | double x = r.getX() + (wC * i);
|
||
184 | double y = r.getY() + (hR * j);
|
||
185 | double w = wC;
|
||
186 | double h = hR;
|
||
187 | Rectangle2D rBasic = new Rectangle2D.Double(x, y, w, h); |
||
188 | FFrameBasic basic = |
||
189 | (FFrameBasic) layoutManager |
||
190 | .createFrame(FFrameBasic.PERSISTENCE_DEFINITION_NAME); |
||
191 | basic.setRectangle(rBasic); |
||
192 | addFFrame(basic); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
197 | public void selectFFrame(boolean b) { |
||
198 | selectFFrameBasic = b; |
||
199 | } |
||
200 | |||
201 | public IFFrame joinFFrame() {
|
||
202 | IFFrame[] fframes = this.getFFrames(); |
||
203 | Rectangle2D r = null; |
||
204 | |||
205 | for (int i = fframes.length - 1; i >= 0; i--) { |
||
206 | if (fframes[i].getSelected() != IFFrame.NOSELECT) {
|
||
207 | if (r == null) { |
||
208 | r = (Rectangle2D) fframes[i].getBoundBox().clone();
|
||
209 | } else {
|
||
210 | r.add(fframes[i].getBoundBox()); |
||
211 | } |
||
212 | this.removeFFrame(i);
|
||
213 | } |
||
214 | } |
||
215 | if (r != null) { |
||
216 | // Layout
|
||
217 | FFrameBasic basic = |
||
218 | (FFrameBasic) layoutManager |
||
219 | .createFrame(FFrameBasic.PERSISTENCE_DEFINITION_NAME); |
||
220 | basic.setRectangle(r); |
||
221 | this.addFFrame(basic);
|
||
222 | return this; |
||
223 | } |
||
224 | |||
225 | return null; |
||
226 | } |
||
227 | |||
228 | public void setSelected(Point2D p, MouseEvent e) { |
||
229 | if (!selectFFrameBasic) {
|
||
230 | super.setSelected(p, e);
|
||
231 | } else {
|
||
232 | setSelectedGroup(p, e); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | public boolean contains(Point2D p) { |
||
237 | if (!selectFFrameBasic) {
|
||
238 | return super.contains(p); |
||
239 | } |
||
240 | return contains(p);
|
||
241 | } |
||
242 | |||
243 | public void drawHandlers(Graphics2D g) { |
||
244 | if (!selectFFrameBasic) {
|
||
245 | super.drawHandlers(g);
|
||
246 | } else {
|
||
247 | drawHandlersGroup(g); |
||
248 | |||
249 | } |
||
250 | } |
||
251 | |||
252 | public int getContains(Point2D p) { |
||
253 | if (!selectFFrameBasic) {
|
||
254 | return super.getContains(p); |
||
255 | } |
||
256 | return getContainsGroup(p);
|
||
257 | } |
||
258 | |||
259 | public Rectangle2D getMovieRect(int difx, int dify) { |
||
260 | // TODO Esto de momento lo dejo que no se pueda mover
|
||
261 | // cuando se tiene seleccionada la herramienta de selecci�n de un
|
||
262 | // FFrameBasic.
|
||
263 | Rectangle2D r = super.getMovieRect(difx, dify); |
||
264 | if (!selectFFrameBasic) {
|
||
265 | return r;
|
||
266 | } |
||
267 | r = super.getMovieRect(0, 0); |
||
268 | return r;
|
||
269 | } |
||
270 | |||
271 | public Rectangle2D getMovieRectGroup(int difX, int difY) { |
||
272 | Rectangle2D r = null; |
||
273 | IFFrame[] fframes = getFFrames();
|
||
274 | List<IFFrame> selected = new ArrayList<IFFrame>(); |
||
275 | for (int i = 0; i < fframes.length; i++) { |
||
276 | IFFrame fframe = fframes[i]; |
||
277 | if (fframe.getSelected() != IFFrame.NOSELECT) {
|
||
278 | selected.add(fframe); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | for (int i = 0; i < selected.size(); i++) { |
||
283 | |||
284 | if (i == 0) { |
||
285 | r = selected.get(i).getMovieRect(difX, difY); |
||
286 | } else {
|
||
287 | r.add(selected.get(i).getMovieRect(difX, difY)); |
||
288 | } |
||
289 | } |
||
290 | return r;
|
||
291 | } |
||
292 | |||
293 | public int getContainsGroup(Point2D p) { |
||
294 | List<IFFrame> selected = new ArrayList<IFFrame>(); |
||
295 | IFFrame[] fframes = getFFrames();
|
||
296 | for (int i = 0; i < fframes.length; i++) { |
||
297 | IFFrame fframe = fframes[i]; |
||
298 | int contains = fframe.getContains(p);
|
||
299 | if (contains != IFFrame.NOSELECT) {
|
||
300 | if (contains == IFFrame.RECT) {
|
||
301 | selected.add(fframe); |
||
302 | } else {
|
||
303 | return contains;
|
||
304 | } |
||
305 | } |
||
306 | } |
||
307 | if (selected.size() > 0) { |
||
308 | return selected.get(0).getContains(p); |
||
309 | } |
||
310 | return 0; |
||
311 | } |
||
312 | |||
313 | /**
|
||
314 | * Dibuja los handlers sobre el boundingBox en el graphics que se pasa como
|
||
315 | * par�metro.
|
||
316 | *
|
||
317 | * @param g
|
||
318 | * Graphics sobre el que dibujar.
|
||
319 | */
|
||
320 | public void drawHandlersGroup(Graphics2D g) { |
||
321 | int size = 8; |
||
322 | g.setColor(Color.gray);
|
||
323 | IFFrame[] fframes = getFFrames();
|
||
324 | for (int i = 0; i < fframes.length; i++) { |
||
325 | IFFrame fframe = fframes[i]; |
||
326 | if (fframe.getSelected() != IFFrame.NOSELECT) {
|
||
327 | Rectangle2D r = fframe.getBoundingBox(null); |
||
328 | Point2D p = new Point2D.Double(); |
||
329 | g.rotate(Math.toRadians(getRotation()),
|
||
330 | r.getX() + (r.getWidth() / 2), r.getY()
|
||
331 | + (r.getHeight() / 2));
|
||
332 | |||
333 | AffineTransform atRotate = new AffineTransform(); |
||
334 | atRotate.rotate(Math.toRadians(getRotation()),
|
||
335 | r.getX() + (r.getWidth() / 2), r.getY()
|
||
336 | + (r.getHeight() / 2));
|
||
337 | |||
338 | g.fillRect((int) r.getX() - size / 2, |
||
339 | (int) r.getY() - size / 2, size, size); |
||
340 | atRotate |
||
341 | .transform(new Point2D.Double(r.getX() - size / 2, r.getY() |
||
342 | - size / 2), p);
|
||
343 | no.setRect((int) p.getX(), (int) p.getY(), size, size); |
||
344 | |||
345 | g.fillRect((int) r.getMaxX() - size / 2, (int) r.getY() - size |
||
346 | / 2, size, size);
|
||
347 | atRotate.transform( |
||
348 | new Point2D.Double(r.getMaxX() - size / 2, r.getY() - size |
||
349 | / 2), p);
|
||
350 | ne.setRect((int) p.getX(), (int) p.getY(), size, size); |
||
351 | |||
352 | g.fillRect((int) r.getX() - size / 2, (int) r.getMaxY() - size |
||
353 | / 2, size, size);
|
||
354 | atRotate.transform( |
||
355 | new Point2D.Double(r.getX() - size / 2, r.getMaxY() - size |
||
356 | / 2), p);
|
||
357 | so.setRect((int) p.getX(), (int) p.getY(), size, size); |
||
358 | |||
359 | g.fillRect((int) r.getMaxX() - size / 2, (int) r.getMaxY() |
||
360 | - size / 2, size, size);
|
||
361 | atRotate.transform( |
||
362 | new Point2D.Double(r.getMaxX() - size / 2, r.getMaxY() |
||
363 | - size / 2), p);
|
||
364 | se.setRect((int) p.getX(), (int) p.getY(), size, size); |
||
365 | |||
366 | g.fillRect((int) r.getCenterX() - (size / 2), (int) r.getY() |
||
367 | - size / 2, size, size);
|
||
368 | atRotate.transform(new Point2D.Double(r.getCenterX() |
||
369 | - (size / 2), r.getY() - size / 2), p); |
||
370 | n.setRect((int) p.getX(), (int) p.getY(), size, size); |
||
371 | |||
372 | g.fillRect((int) r.getCenterX() - (size / 2), (int) r.getMaxY() |
||
373 | - size / 2, size, size);
|
||
374 | atRotate.transform(new Point2D.Double(r.getCenterX() |
||
375 | - (size / 2), r.getMaxY() - size / 2), p); |
||
376 | s.setRect((int) p.getX(), (int) p.getY(), size, size); |
||
377 | |||
378 | g.fillRect((int) r.getX() - size / 2, (int) r.getCenterY() |
||
379 | - (size / 2), size, size);
|
||
380 | atRotate.transform( |
||
381 | new Point2D.Double(r.getX() - size / 2, r.getCenterY() |
||
382 | - (size / 2)), p);
|
||
383 | o.setRect((int) p.getX(), (int) p.getY(), size, size); |
||
384 | |||
385 | g.fillRect((int) r.getMaxX() - size / 2, (int) r.getCenterY() |
||
386 | - (size / 2), size, size);
|
||
387 | atRotate.transform( |
||
388 | new Point2D.Double(r.getMaxX() - size / 2, r.getCenterY() |
||
389 | - (size / 2)), p);
|
||
390 | e.setRect((int) p.getX() - size / 2, (int) p.getY() - size / 2, |
||
391 | size, size); |
||
392 | g.rotate(Math.toRadians(-getRotation()),
|
||
393 | r.getX() + (r.getWidth() / 2), r.getY()
|
||
394 | + (r.getHeight() / 2));
|
||
395 | } |
||
396 | } |
||
397 | |||
398 | } |
||
399 | |||
400 | public boolean containsGroup(Point2D p) { |
||
401 | IFFrame[] fframes = getFFrames();
|
||
402 | for (int i = 0; i < fframes.length; i++) { |
||
403 | IFFrame fframe = fframes[i]; |
||
404 | if (fframe.contains(p)) {
|
||
405 | return true; |
||
406 | } |
||
407 | } |
||
408 | return false; |
||
409 | } |
||
410 | |||
411 | public void setSelectedGroup(Point2D p, MouseEvent e) { |
||
412 | IFFrame[] fframes = getFFrames();
|
||
413 | if (!e.isShiftDown()) {
|
||
414 | for (int j = 0; j < fframes.length; j++) { |
||
415 | fframes[j].setSelected(false);
|
||
416 | } |
||
417 | for (int i = 0; i < fframes.length; i++) { |
||
418 | IFFrame fframe = fframes[i]; |
||
419 | fframe.setSelected(p, e); |
||
420 | } |
||
421 | } else {
|
||
422 | for (int i = 0; i < fframes.length; i++) { |
||
423 | IFFrame fframe = fframes[i]; |
||
424 | if (fframe.contains(p)) {
|
||
425 | if ((fframe.getSelected() == IFFrame.NOSELECT)) {
|
||
426 | fframe.setSelected(p, e); |
||
427 | } else {
|
||
428 | fframe.setSelected(false);
|
||
429 | } |
||
430 | } |
||
431 | } |
||
432 | } |
||
433 | } |
||
434 | |||
435 | public int getSelectedGroup() { |
||
436 | return 0; |
||
437 | } |
||
438 | |||
439 | public Image getMapCursor(Point2D p) { |
||
440 | if (!selectFFrameBasic) {
|
||
441 | return super.getMapCursor(p); |
||
442 | } |
||
443 | return getMapCursorGroup(p);
|
||
444 | } |
||
445 | |||
446 | public Image getMapCursorGroup(Point2D p) { |
||
447 | int select = getContains(p);
|
||
448 | switch (select) {
|
||
449 | case (RECT):
|
||
450 | return iMove;
|
||
451 | } |
||
452 | return null; |
||
453 | } |
||
454 | |||
455 | /**
|
||
456 | * Initilizes the static icons
|
||
457 | */
|
||
458 | public static void initializeIcons() { |
||
459 | 125 | jldominguez | iMove = PluginServices.getIconTheme().get("graphic-move-icon").getImage();
|
460 | 5 | jldominguez | } |
461 | |||
462 | public static void registerPersistent() { |
||
463 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
464 | if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) { |
||
465 | DynStruct definition = |
||
466 | manager.addDefinition(FFrameTable.class, |
||
467 | PERSISTENCE_DEFINITION_NAME, |
||
468 | "FFrameTable persistence definition", null, null); |
||
469 | |||
470 | definition.extend(manager |
||
471 | .getDefinition(FFrameGroup.PERSISTENCE_DEFINITION_NAME)); |
||
472 | |||
473 | definition.addDynFieldInt(NUMCOLUMNS_FIELD).setMandatory(true);
|
||
474 | definition.addDynFieldInt(NUMROWS_FIELD).setMandatory(true);
|
||
475 | } |
||
476 | } |
||
477 | |||
478 | @Override
|
||
479 | public void loadFromState(PersistentState state) |
||
480 | throws PersistenceException {
|
||
481 | super.loadFromState(state);
|
||
482 | numColumns = state.getInt(NUMCOLUMNS_FIELD); |
||
483 | numRows = state.getInt(NUMROWS_FIELD); |
||
484 | } |
||
485 | |||
486 | @Override
|
||
487 | public void saveToState(PersistentState state) throws PersistenceException { |
||
488 | super.saveToState(state);
|
||
489 | state.set(NUMCOLUMNS_FIELD, numColumns); |
||
490 | state.set(NUMROWS_FIELD, numRows); |
||
491 | } |
||
492 | } |