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