Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / FLayoutDraw.java @ 2263

History | View | Annotate | Download (14.2 KB)

1
/*
2
 * Created on 27-sep-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.gui.layout;
46

    
47
import com.iver.cit.gvsig.fmap.DriverException;
48
import com.iver.cit.gvsig.gui.layout.fframes.FFrameView;
49
import com.iver.cit.gvsig.gui.layout.fframes.IFFrame;
50

    
51
import java.awt.Color;
52
import java.awt.Graphics2D;
53
import java.awt.geom.AffineTransform;
54
import java.awt.geom.Point2D;
55
import java.awt.geom.Rectangle2D;
56
import java.awt.image.BufferedImage;
57

    
58

    
59
/**
60
 * Clase que implementa los m?todos del Layout que dibujan sobre el Graphics.
61
 *
62
 * @author Vicente Caballero Navarro
63
 */
64
public class FLayoutDraw {
65
        private Layout layout;
66

    
67
        /**
68
         * Crea un nuevo FLayoutDraw.
69
         *
70
         * @param l Referencia al Layout.
71
         */
72
        public FLayoutDraw(Layout l) {
73
                layout = l;
74
        }
75

    
76
        /**
77
         * M?todo para dibujar el Layout y modificar la matriz de transformaci?n  a
78
         * partir del tama?o en pixels que tenga rect y el formato de papel
79
         * seleccionado.
80
         *
81
         * @param g2
82
         * @param imgBase Si es null, est?s imprimiendo. Si no, la usas para el
83
         *                   c?digo de  optimizaci?n.
84
         *
85
         * @throws DriverException
86
         */
87
        public void drawLayout(Graphics2D g2, BufferedImage imgBase)
88
                throws DriverException {
89
                /* PluginServices.getMainFrame().getStatusBar().setMessage("1",
90
                   layout.getAtributes().getNameUnit());
91
                 */
92
                layout.setCancelDrawing(false);
93

    
94
                double scale = 0;
95

    
96
                scale = layout.getRect().height / layout.getAtributes().m_sizePaper.getAlto() * 1;
97

    
98
                AffineTransform escalado = new AffineTransform();
99
                AffineTransform translacion = new AffineTransform();
100
                translacion.setToTranslation(layout.getRect().getMinX(),
101
                        layout.getRect().getMinY());
102
                escalado.setToScale(scale, scale);
103
                layout.getAT().setToIdentity();
104
                layout.getAT().concatenate(translacion);
105
                layout.getAT().concatenate(escalado);
106
                layout.getAtributes().setDistanceUnitX(layout.getRect());
107
                layout.getAtributes().setDistanceUnitY(layout.getRect());
108

    
109
                for (int i = 0; i < layout.getFFrames().size(); i++) {
110
                        if (layout.getFFrames().get(i) instanceof FFrameView) {
111
                                FFrameView fframe = (FFrameView) layout.getFFrames().get(i);
112
                                Rectangle2D rec = fframe.getBoundingBox(layout.getAT());
113
                                rec = rec.createIntersection(layout.getVisibleRect());
114

    
115
                                BufferedImage img = new BufferedImage((int) layout.getWidth(),
116
                                                (int) layout.getHeight(), BufferedImage.TYPE_INT_ARGB);
117
                                ((Graphics2D) img.getGraphics()).clipRect((int) rec.getMinX(),
118
                                        (int) rec.getMinY(), (int) rec.getWidth(),
119
                                        (int) rec.getHeight());
120
                                fframe.draw((Graphics2D) img.getGraphics(), layout.getAT(),
121
                                        layout.getVisibleRect(), imgBase);
122

    
123
                                if (fframe.getFMap() != null) {
124
                                        fframe.setAt((AffineTransform) fframe.getFMap().getViewPort()
125
                                                                                                                 .getAffineTransform()
126
                                                                                                                 .clone());
127
                                }
128

    
129
                                fframe.setBufferedImage(img);
130
                        }
131
                }
132

    
133
                for (int i = 0; i < layout.getFFrames().size(); i++) {
134
                        if (layout.getFFrames().get(i) instanceof FFrameView) {
135
                                g2.drawImage(((FFrameView) layout.getFFrames().get(i)).getBufferedImage(),
136
                                        0, 0, layout);
137
                        } else {
138
                                ((IFFrame) layout.getFFrames().get(i)).draw(g2, layout.getAT(),
139
                                        layout.getVisibleRect(), imgBase);
140
                        }
141

    
142
                        //          Dibuja el s?mbolo de que contiene un tag.
143
                        if (((IFFrame) layout.getFFrames().get(i)).getTag() != null) {
144
                                ((IFFrame) layout.getFFrames().get(i)).drawSymbolTag(g2);
145
                        }
146
                }
147

    
148
                if (!layout.getFFrames().isEmpty()) {
149
                        layout.setStatus(Layout.ACTUALIZADO);
150
                } else {
151
                        layout.setStatus(Layout.DESACTUALIZADO);
152
                }
153
        }
154

    
155
        /**
156
         * Dibuja sobre un Graphics2D el rect?ngulo que representa al folio.
157
         *
158
         * @param g Graphics2D
159
         */
160
        public void drawRectangle(Graphics2D g) {
161
                double unidadesX = layout.getAtributes().getUnitInPixelsX();
162
                double unidadesY = layout.getAtributes().getUnitInPixelsY();
163

    
164
                if ((unidadesX == 0) && (unidadesY == 0)) {
165
                        return;
166
                }
167

    
168
                g.setColor(Color.red);
169

    
170
                Rectangle2D.Double rectBig = new Rectangle2D.Double(layout.getRect().x,
171
                                layout.getRect().y, layout.getRect().width,
172
                                layout.getRect().height);
173

    
174
                g.fill3DRect((int) rectBig.x + 10, (int) rectBig.y + 10,
175
                        (int) rectBig.width, (int) rectBig.height, true);
176

    
177
                Rectangle2D.Double r = new Rectangle2D.Double();
178

    
179
                if (layout.getAtributes().isMargin()) {
180
                        r = new Rectangle2D.Double((layout.getRect().x +
181
                                        FLayoutUtilities.fromSheetDistance(
182
                                                layout.getAtributes().m_area[2], layout.getAT())),
183
                                        (layout.getRect().y +
184
                                        FLayoutUtilities.fromSheetDistance(
185
                                                layout.getAtributes().m_area[0], layout.getAT())),
186
                                        (layout.getRect().width -
187
                                        FLayoutUtilities.fromSheetDistance(layout.getAtributes().m_area[2] +
188
                                                layout.getAtributes().m_area[3], layout.getAT())),
189
                                        (layout.getRect().height -
190
                                        FLayoutUtilities.fromSheetDistance(layout.getAtributes().m_area[0] +
191
                                                layout.getAtributes().m_area[1], layout.getAT())));
192
                } else {
193
                        r.setRect(layout.getRect());
194
                }
195

    
196
                g.setColor(Color.white);
197
                g.fill(layout.getRect());
198

    
199
                g.setColor(Color.blue);
200
                g.drawRect((int) layout.getRect().getMinX(),
201
                        (int) layout.getRect().getMinY(),
202
                        (int) layout.getRect().getWidth(),
203
                        (int) layout.getRect().getHeight());
204

    
205
                if (layout.getAtributes().isMargin()) {
206
                        g.setColor(Color.black);
207

    
208
                        g.drawRect((int) r.x, (int) r.y, (int) r.width, (int) r.height);
209
                }
210
        }
211

    
212
        public void drawGrid(Graphics2D g)
213
        {
214
                int unidadesMin = 6;
215
                double unidadesX = layout.getAtributes().getUnitInPixelsX();
216
                double unidadesY = layout.getAtributes().getUnitInPixelsY();
217

    
218
                if ((unidadesX == 0) && (unidadesY == 0)) {
219
                        return;
220
                }
221
                Rectangle2D.Double rectBig = new Rectangle2D.Double(layout.getRect().x,
222
                                layout.getRect().y, layout.getRect().width,
223
                                layout.getRect().height);
224
                Rectangle2D.Double r = new Rectangle2D.Double();
225

    
226
                if (layout.getAtributes().isMargin()) {
227
                        r = new Rectangle2D.Double((layout.getRect().x +
228
                                        FLayoutUtilities.fromSheetDistance(
229
                                                layout.getAtributes().m_area[2], layout.getAT())),
230
                                        (layout.getRect().y +
231
                                        FLayoutUtilities.fromSheetDistance(
232
                                                layout.getAtributes().m_area[0], layout.getAT())),
233
                                        (layout.getRect().width -
234
                                        FLayoutUtilities.fromSheetDistance(layout.getAtributes().m_area[2] +
235
                                                layout.getAtributes().m_area[3], layout.getAT())),
236
                                        (layout.getRect().height -
237
                                        FLayoutUtilities.fromSheetDistance(layout.getAtributes().m_area[0] +
238
                                                layout.getAtributes().m_area[1], layout.getAT())));
239
                } else {
240
                        r.setRect(layout.getRect());
241
                }
242

    
243
                g.setColor(Color.BLUE);
244
                if (((unidadesX > unidadesMin) || (unidadesY > unidadesMin)) &&
245
                                layout.getIsDrawGrid()) {
246
                        double ax = r.getMinX();
247
                        double ay;
248

    
249
                        while (ax < (r.getMaxX())) {
250
                                ay = (r.getMinY());
251

    
252
                                while (ay < (r.getMaxY())) {
253
                                        g.drawLine((int) ax, (int) ay, (int) ax, (int) ay);
254
                                        ay = ay + unidadesY;
255
                                }
256

    
257
                                ax = ax + unidadesX;
258
                        }
259
                }
260

    
261
        }
262
        /**
263
         * Dibuja sobre el graphics2d las reglas.
264
         *
265
         * @param g graphics2d sobre el que se dibuja.
266
         * @param color Color de la regla.
267
         */
268
        public void drawRuler(Graphics2D g, Color color) {
269
                if (layout.getRuler()) {
270
                        int ini = 10;
271
                        int w = 30;
272
                        int wi = 16;
273

    
274
                        g.setColor(new Color(250, 255, 250, 180));
275
                        g.fillRect(ini, w, wi, layout.getHeight() - w);
276
                        g.fillRect(w, ini, layout.getWidth() - w, wi);
277

    
278
                        g.setColor(new Color(100, 155, 150, 180));
279
                        g.fillRect(w, ini, (int) layout.getRect().x - w, wi);
280
                        g.fillRect((int) layout.getRect().getMaxX(), ini,
281
                                (int) layout.getWidth() - (int) layout.getRect().getMaxX(), wi);
282

    
283
                        g.fillRect(ini, w, wi, (int) layout.getRect().y - w);
284
                        g.fillRect(ini, (int) layout.getRect().getMaxY(), wi,
285
                                (int) layout.getHeight() - (int) layout.getRect().getMaxY());
286

    
287
                        if (layout.getAtributes().isMargin()) {
288
                                g.setColor(new Color(50, 55, 50, 180));
289
                                g.fillRect((int) layout.getRect().x, ini,
290
                                        (int) FLayoutUtilities.fromSheetDistance(
291
                                                layout.getAtributes().m_area[2], layout.getAT()), wi);
292
                                g.fillRect((int) layout.getRect().getMaxX() -
293
                                        (int) FLayoutUtilities.fromSheetDistance(
294
                                                layout.getAtributes().m_area[3], layout.getAT()), ini,
295
                                        (int) FLayoutUtilities.fromSheetDistance(
296
                                                layout.getAtributes().m_area[3], layout.getAT()), wi);
297

    
298
                                g.fillRect(ini, (int) layout.getRect().y, wi,
299
                                        (int) FLayoutUtilities.fromSheetDistance(
300
                                                layout.getAtributes().m_area[0], layout.getAT()));
301
                                g.fillRect(ini,
302
                                        (int) layout.getRect().getMaxY() -
303
                                        (int) FLayoutUtilities.fromSheetDistance(
304
                                                layout.getAtributes().m_area[1], layout.getAT()), wi,
305
                                        (int) FLayoutUtilities.fromSheetDistance(
306
                                                layout.getAtributes().m_area[1], layout.getAT()));
307
                        }
308

    
309
                        g.setColor(color);
310
                        g.drawLine(w, wi + ini, layout.getWidth(), wi + ini);
311
                        g.drawLine(w, ini, layout.getWidth(), ini);
312
                        g.drawLine(ini, w, ini, layout.getHeight());
313
                        g.drawLine(wi + ini, w, wi + ini, layout.getHeight());
314

    
315
                        drawLineY(g, 5, 12, 22, layout.getRect().y, 0);
316
                        drawLineX(g, 5, 12, 22, layout.getRect().x, 0);
317

    
318
                        if (FLayoutUtilities.fromSheetDistance(1, layout.getAT()) > 15) {
319
                                drawLineY(g, 1, 12, 22, layout.getRect().y, 0);
320
                                drawLineX(g, 1, 12, 22, layout.getRect().x, 0);
321

    
322
                                if (FLayoutUtilities.fromSheetDistance(1, layout.getAT()) > 25) {
323
                                        drawLineY(g, 1, 18, 22, layout.getRect().y, 0.5);
324
                                        drawLineY(g, 0.1, 21, 22, layout.getRect().y, 0);
325
                                        drawLineX(g, 1, 18, 22, layout.getRect().x, 0.5);
326
                                        drawLineX(g, 0.1, 21, 22, layout.getRect().x, 0);
327
                                }
328
                        }
329
                }
330
        }
331

    
332
        /**
333
         * Dibuja sobre el graphics2d las l?neas verticales que representan a las
334
         * unidades de medida.
335
         *
336
         * @param g Graphics2d sobre el que se dibuja.
337
         * @param dist distancia en cent?metros de una l?nea a otra.
338
         * @param init inicio de la l?nea.
339
         * @param end fin de la l?nea.
340
         * @param y rect?ngulo, dentro del cual se dibujan las l?neas.
341
         * @param desp Desplazamiento.
342
         */
343
        private void drawLineY(Graphics2D g, double dist, int init, int end,
344
                double y, double desp) {
345
                double distY = FLayoutUtilities.fromSheetDistance(dist, layout.getAT());
346
                double rota = Math.toRadians(90);
347

    
348
                if (distY > 4) {
349
                        double despY = FLayoutUtilities.fromSheetDistance(desp,
350
                                        layout.getAT());
351

    
352
                        double posUnitY = y + despY;
353
                        double posUnitYNeg = posUnitY;
354
                        int num = 0;
355
                        double iniY = 40;
356
                        Point2D.Double pfin = FLayoutUtilities.fromSheetPoint(new Point2D.Double(
357
                                                layout.getWidth(), layout.getHeight()), layout.getAT());
358

    
359
                        while (posUnitY < (pfin.y - 5)) {
360
                                posUnitYNeg -= distY;
361

    
362
                                if (distY > 16) {
363
                                        if (init == 12) {
364
                                                if (posUnitY > iniY) {
365
                                                        g.rotate(-rota, 20, posUnitY - 12);
366
                                                        g.drawString(String.valueOf(num), (int) 10,
367
                                                                (int) posUnitY - 12);
368
                                                        g.rotate(rota, 20, posUnitY - 12);
369
                                                }
370

    
371
                                                if (dist == 5) {
372
                                                        num = num + 5;
373
                                                } else {
374
                                                        num++;
375
                                                }
376

    
377
                                                if (posUnitYNeg > iniY) {
378
                                                        g.rotate(-rota, 20, posUnitYNeg - 12);
379
                                                        g.drawString(String.valueOf(-num), (int) 10,
380
                                                                (int) posUnitYNeg - 12);
381
                                                        g.rotate(rota, 20, posUnitYNeg - 12);
382
                                                }
383
                                        }
384
                                }
385

    
386
                                if (posUnitY > iniY) {
387
                                        g.drawLine((int) 2 + init, (int) posUnitY, (int) 2 + end,
388
                                                (int) posUnitY);
389
                                }
390

    
391
                                if (posUnitYNeg > iniY) {
392
                                        g.drawLine((int) 2 + init, (int) posUnitYNeg,
393
                                                (int) 2 + end, (int) posUnitYNeg);
394
                                }
395

    
396
                                posUnitY += distY;
397
                        }
398
                }
399
        }
400

    
401
        /**
402
         * Dibuja sobre el graphics2d las l?neas horizontales que representan a las
403
         * unidades de medida.
404
         *
405
         * @param g Graphics2d sobre el que se dibuja.
406
         * @param dist distancia en cent?metros de una l?nea a otra.
407
         * @param init inicio de la l?nea.
408
         * @param end fin de la l?nea.
409
         * @param x rect?ngulo, dentro del cual se dibujan las l?neas.
410
         * @param desp Desplazamiento.
411
         */
412
        private void drawLineX(Graphics2D g, double dist, int init, int end,
413
                double x, double desp) {
414
                double distX = FLayoutUtilities.fromSheetDistance(dist, layout.getAT());
415

    
416
                if (distX > 4) {
417
                        double despX = FLayoutUtilities.fromSheetDistance(desp,
418
                                        layout.getAT());
419
                        double posUnitX = x + despX;
420
                        double posUnitXNeg = posUnitX;
421
                        int num = 0;
422
                        double iniX = 40;
423
                        Point2D.Double pfin = FLayoutUtilities.fromSheetPoint(new Point2D.Double(
424
                                                layout.getWidth(), layout.getHeight()), layout.getAT());
425

    
426
                        while (posUnitX < (pfin.x - 5)) {
427
                                posUnitXNeg -= distX;
428

    
429
                                if (init == 12) {
430
                                        if (distX > 16) {
431
                                                if (posUnitX > iniX) {
432
                                                        g.drawString(String.valueOf(num),
433
                                                                (int) posUnitX + 3, (int) 20);
434
                                                }
435

    
436
                                                if (dist == 5) {
437
                                                        num = num + 5;
438
                                                } else {
439
                                                        num++;
440
                                                }
441

    
442
                                                if (posUnitXNeg > iniX) {
443
                                                        g.drawString(String.valueOf(-num),
444
                                                                (int) posUnitXNeg + 3, (int) 20);
445
                                                }
446
                                        }
447
                                }
448

    
449
                                if (posUnitX > iniX) {
450
                                        g.drawLine((int) posUnitX, (int) 2 + init, (int) posUnitX,
451
                                                (int) 2 + end);
452
                                }
453

    
454
                                if (posUnitXNeg > iniX) {
455
                                        g.drawLine((int) posUnitXNeg, (int) 2 + init,
456
                                                (int) posUnitXNeg, (int) 2 + end);
457
                                }
458

    
459
                                posUnitX += distX;
460
                        }
461
                }
462
        }
463

    
464
        /**
465
         * Dibuja los handlers sobre los fframes que esten seleccionados.
466
         *
467
         * @param g Graphics sobre el que se dibuja.
468
         * @param color Color de los Handlers.
469
         */
470
        public void drawHandlers(Graphics2D g, Color color) {
471
                g.setColor(color);
472

    
473
                for (int i = 0; i < layout.getFFrames().size(); i++) {
474
                        IFFrame fframe = (IFFrame) layout.getFFrames().get(i);
475

    
476
                        if (fframe.getSelected() != IFFrame.NOSELECT) {
477
                                fframe.drawHandlers((Graphics2D) g);
478
                        }
479
                }
480
        }
481
}