Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFrame.java @ 725

History | View | Annotate | Download (15.1 KB)

1
/*
2
 * Created on 17-may-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
package com.iver.cit.gvsig.gui.layout.fframes;
8

    
9
import com.iver.andami.PluginServices;
10
import com.iver.andami.messages.NotificationManager;
11

    
12
import com.iver.cit.gvsig.Abrir;
13
import com.iver.cit.gvsig.fmap.DriverException;
14
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
15
import com.iver.cit.gvsig.gui.layout.FLayoutUtilities;
16
import com.iver.cit.gvsig.gui.layout.Layout;
17
import com.iver.cit.gvsig.gui.layout.Tag;
18
import com.iver.cit.gvsig.project.castor.Project;
19

    
20
import com.iver.utiles.XMLEntity;
21

    
22
import java.awt.Color;
23
import java.awt.Cursor;
24
import java.awt.Font;
25
import java.awt.Graphics2D;
26
import java.awt.Image;
27
import java.awt.Rectangle;
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

    
33
import java.net.URL;
34

    
35
import javax.swing.ImageIcon;
36

    
37

    
38
/**
39
 * Clase que implementa la interface IFFrame con los m?todos por defecto de
40
 * todos los FFrames  que extenderan de este, dejando uno como m?todo
41
 * abstracto para implementar por todos los  FFrames para ser dibujados.
42
 *
43
 * @author Vicente Caballero Navarro
44
 */
45
public abstract class FFrame implements IFFrame {
46
        private static final int N = 1;
47
        private static final int NE = 2;
48
        private static final int E = 3;
49
        private static final int SE = 4;
50
        private static final int S = 5;
51
        private static final int SO = 6;
52
        private static final int O = 7;
53
        private static final int NO = 8;
54
        private static final int RECT = 9;
55

    
56
        /** FFrame no selecccionado. */
57
        public static final int NOSELECT = 0;
58
        protected String m_name = "FFrame";
59
        private Rectangle2D.Double m_BoundBox = new Rectangle2D.Double();
60
        private Rectangle2D.Double m_BoundingBox = new Rectangle2D.Double();
61
        protected int m_Selected = 0;
62
        private Rectangle n = new Rectangle();
63
        private Rectangle ne = new Rectangle();
64
        private Rectangle e = new Rectangle();
65
        private Rectangle se = new Rectangle();
66
        private Rectangle s = new Rectangle();
67
        private Rectangle so = new Rectangle();
68
        private Rectangle o = new Rectangle();
69
        private Rectangle no = new Rectangle();
70
        private String tag = null;
71
        protected int num = 0;
72

    
73
        /**
74
         * Dibuja los handlers sobre el boundingBox en el graphics que se pasa como
75
         * par?metro.
76
         *
77
         * @param g Graphics sobre el que dibujar.
78
         */
79
        public void drawHandlers(Graphics2D g) {
80
                int size = 10;
81
                Rectangle2D.Double r = getBoundingBox(null);
82

    
83
                //g.drawRect((int) r.x, (int) r.y, (int) r.width, (int) r.height);
84
                g.fillRect((int) r.x - size, (int) r.y - size, size, size);
85
                no.setRect((int) r.x - size, (int) r.y - size, size, size);
86
                g.fillRect((int) r.getMaxX(), (int) r.y - size, size, size);
87
                ne.setRect((int) r.getMaxX(), (int) r.y - size, size, size);
88
                g.fillRect((int) r.x - size, (int) r.getMaxY(), size, size);
89
                so.setRect((int) r.x - size, (int) r.getMaxY(), size, size);
90
                g.fillRect((int) r.getMaxX(), (int) r.getMaxY(), size, size);
91
                se.setRect((int) r.getMaxX(), (int) r.getMaxY(), size, size);
92
                g.fillRect((int) r.getCenterX() - (size / 2), (int) r.y - size, size,
93
                        size);
94
                n.setRect((int) r.getCenterX() - (size / 2), (int) r.y - size, size,
95
                        size);
96
                g.fillRect((int) r.getCenterX() - (size / 2), (int) r.getMaxY(), size,
97
                        size);
98
                s.setRect((int) r.getCenterX() - (size / 2), (int) r.getMaxY(), size,
99
                        size);
100
                g.fillRect((int) r.x - size, (int) r.getCenterY() - (size / 2), size,
101
                        size);
102
                o.setRect((int) r.x - size, (int) r.getCenterY() - (size / 2), size,
103
                        size);
104
                g.fillRect((int) r.getMaxX(), (int) r.getCenterY() - (size / 2), size,
105
                        size);
106
                e.setRect((int) r.getMaxX(), (int) r.getCenterY() - (size / 2), size,
107
                        size);
108
        }
109

    
110
        /**
111
         * Establece que tipo de selecci?n se realiza sobre el fframe.
112
         *
113
         * @param p punto sobre el que se debe de establecer si se selecciona o no
114
         *                   el fframe.
115
         */
116
        public void setSelected(Point2D.Double p) {
117
                m_Selected = getContains(p);
118
        }
119

    
120
        /**
121
         * Actualiza el BoundBox del FFrame a partir de su rect?ngulo en pixels y
122
         * la matriz de transformaci?n.
123
         *
124
         * @param r Rect?ngulo.
125
         * @param at Matriz de transformaci?n.
126
         */
127
        public void updateRect(Rectangle2D r, AffineTransform at) {
128
                Rectangle2D.Double rec = FLayoutUtilities.toSheetRect(r, at);
129
                rec.setRect((int) rec.getMinX(), (int) rec.getMinY(),
130
                        (int) rec.getWidth(), (int) rec.getHeight());
131
                setBoundBox(rec);
132
        }
133

    
134
        /**
135
         * Devuelve el rect?ngulo a partir del desplazamiento en el eje x y el
136
         * desplazamiento en el eje y.
137
         *
138
         * @param difx desplazamiento sobre el eje x.
139
         * @param dify desplazamiento sobre el eje y.
140
         *
141
         * @return rect?ngulo modificado en funci?n del desplazamiento realizado.
142
         */
143
        public Rectangle2D getMovieRect(int difx, int dify) {
144
                double x = 0;
145
                double y = 0;
146
                double w = 0;
147
                double h = 0;
148

    
149
                Rectangle2D.Double rectaux = new Rectangle2D.Double(this.getBoundingBox(
150
                                        null).x, this.getBoundingBox(null).y,
151
                                this.getBoundingBox(null).width,
152
                                this.getBoundingBox(null).height);
153
                Rectangle2D.Double rec = this.getBoundingBox(null);
154
                int difn = 0;
155
                difn = difx;
156
                x = rectaux.x;
157
                y = rectaux.y;
158
                w = rectaux.width;
159
                h = rectaux.height;
160

    
161
                switch (this.getSelected()) {
162
                        case (RECT):
163
                                rectaux.setRect((x + difx), (y + dify), w, h);
164

    
165
                                break;
166

    
167
                        case (N):
168

    
169
                                if ((y + dify) > rec.getMaxY()) {
170
                                        y = rec.getMaxY();
171
                                } else {
172
                                        y = y + dify;
173
                                }
174

    
175
                                rectaux.setRect(x, y, w, Math.abs(h - dify));
176

    
177
                                break;
178

    
179
                        case (O):
180

    
181
                                if ((x + difx) > rec.getMaxX()) {
182
                                        x = rec.getMaxX();
183
                                } else {
184
                                        x = x + difx;
185
                                }
186

    
187
                                rectaux.setRect(x, y, Math.abs(w - difx), h);
188

    
189
                                break;
190

    
191
                        case (S):
192

    
193
                                if (y > (rec.getMaxY() + dify)) {
194
                                        y = rec.getMaxY() + dify;
195
                                }
196

    
197
                                rectaux.setRect(x, y, w, Math.abs(h + dify));
198

    
199
                                break;
200

    
201
                        case (E):
202

    
203
                                if (x > (rec.getMaxX() + difx)) {
204
                                        x = rec.getMaxX() + difx;
205
                                }
206

    
207
                                rectaux.setRect(x, y, Math.abs(w + difx), h);
208

    
209
                                break;
210

    
211
                        case (NE):
212

    
213
                                if ((y - difn) > rec.getMaxY()) {
214
                                        y = rec.getMaxY();
215
                                        x = rec.getMaxX() + difn;
216
                                } else {
217
                                        y = y - difn;
218
                                }
219

    
220
                                rectaux.setRect(x, y, Math.abs(w + difn), Math.abs(h + difn));
221

    
222
                                break;
223

    
224
                        case (NO):
225

    
226
                                if ((y + difn) > rec.getMaxY()) {
227
                                        y = rec.getMaxY();
228
                                        x = rec.getMaxX();
229
                                } else {
230
                                        x = x + difn;
231
                                        y = y + difn;
232
                                }
233

    
234
                                rectaux.setRect(x, y, Math.abs(w - difn), Math.abs(h - difn));
235

    
236
                                break;
237

    
238
                        case (SE):
239

    
240
                                if (y > (rec.getMaxY() + difn)) {
241
                                        y = rec.getMaxY() + difn;
242
                                        x = rec.getMaxX() + difn;
243
                                }
244

    
245
                                rectaux.setRect(x, y, Math.abs(w + difn), Math.abs(h + difn));
246

    
247
                                break;
248

    
249
                        case (SO):
250

    
251
                                if ((x + difn) > rec.getMaxX()) {
252
                                        x = rec.getMaxX();
253
                                        y = rec.getMaxY() - difn;
254
                                } else {
255
                                        x = x + difn;
256
                                }
257

    
258
                                rectaux.setRect(x, y, Math.abs(w - difn), Math.abs(h - difn));
259

    
260
                                break;
261

    
262
                        default:
263
                                rectaux.setRect((x), (y), w, h);
264
                }
265

    
266
                return rectaux;
267
        }
268

    
269
        /**
270
         * Devuelve un entero que representa el tipo de selecci?n que se ha
271
         * realizado sobre el fframe.
272
         *
273
         * @return tipo de selecci?n que se ha realizado.
274
         */
275
        public int getSelected() {
276
                return m_Selected;
277
        }
278

    
279
        /**
280
         * Devuelve true, si el punto que se pasa como par?metro esta contenido
281
         * dentro del boundingbox del fframe.
282
         *
283
         * @param p punto a comprobar.
284
         *
285
         * @return true si el punto esta dentro del boundingbox.
286
         */
287
        public boolean contains(Point2D.Double p) {
288
                return getBoundingBox(null).contains(p.x, p.y);
289
        }
290

    
291
        /**
292
         * Devuelve un entero que representa donde esta contenido el punto que se
293
         * pasa como par?metro.
294
         *
295
         * @param p punto a comparar.
296
         *
297
         * @return entero que representa como esta contenido el punto.
298
         */
299
        public int getContains(Point2D.Double p) {
300
                if (n.contains(p.x, p.y)) {
301
                        return N;
302
                } else if (ne.contains(p.x, p.y)) {
303
                        return NE;
304
                } else if (e.contains(p.x, p.y)) {
305
                        return E;
306
                } else if (e.contains(p.x, p.y)) {
307
                        return E;
308
                } else if (se.contains(p.x, p.y)) {
309
                        return SE;
310
                } else if (s.contains(p.x, p.y)) {
311
                        return S;
312
                } else if (so.contains(p.x, p.y)) {
313
                        return SO;
314
                } else if (o.contains(p.x, p.y)) {
315
                        return O;
316
                } else if (no.contains(p.x, p.y)) {
317
                        return NO;
318
                } else if (getBoundingBox(null).contains(p.x, p.y)) {
319
                        return RECT;
320
                }
321

    
322
                return 0;
323
        }
324

    
325
        /**
326
         * Devuelve el Cursor adecuado seg?n como est? contenido el punto, si es
327
         * para desplazamiento, o cambio de tama?o.
328
         *
329
         * @param p punto a comprobar.
330
         *
331
         * @return Cursor adecuado a la posici?n.
332
         */
333
        public Cursor getMapCursor(Point2D.Double p) {
334
                int select = getContains(p);
335

    
336
                switch (select) {
337
                        case (N):
338
                                return Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR);
339

    
340
                        case (NE):
341
                                return Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR);
342

    
343
                        case (E):
344
                                return Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
345

    
346
                        case (SE):
347
                                return Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);
348

    
349
                        case (S):
350
                                return Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
351

    
352
                        case (SO):
353
                                return Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR);
354

    
355
                        case (O):
356
                                return Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR);
357

    
358
                        case (NO):
359
                                return Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR);
360

    
361
                        case (RECT):
362
                                return Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
363
                }
364

    
365
                return null;
366
        }
367

    
368
        /**
369
         * Este m?todo se implementa en cada una de las fframe, ya que cada una se
370
         * dibuja de una forma diferente sobre el graphics. M?todo que dibuja
371
         * sobre el graphics que se le pasa como par?metro, seg?n la transformada
372
         * afin que se debe de aplicar y el rect?ngulo que se debe de dibujar.
373
         * M?todo que dibuja sobre el graphics que se le pasa como par?metro,
374
         * seg?n la transformada afin que se debe de aplicar y el rect?ngulo que
375
         * se debe de dibujar.
376
         *
377
         * @param g Graphics
378
         * @param at Transformada afin.
379
         * @param r rect?ngulo sobre el que hacer un clip.
380
         * @param imgBase DOCUMENT ME!
381
         *
382
         * @throws DriverIOException
383
         */
384
        public abstract void draw(Graphics2D g, AffineTransform at, Rectangle2D r,
385
                BufferedImage imgBase) throws DriverException;
386

    
387
        /**
388
         * Devuelve el nombre que representa al fframe.
389
         *
390
         * @return String
391
         */
392
        public String getName() {
393
                return m_name;
394
        }
395

    
396
        /**
397
         * Rellena el String que representa al nombre del fframe.
398
         *
399
         * @param n nombre del fframe.
400
         */
401
        public void setName(String n) {
402
                m_name = n;
403
        }
404

    
405
        /**
406
         * Devuelve el boundingBox del fframe en funci?n de la transformada af?n
407
         * que se pasa como par?metro. Si se pasa como par?metro null, devuelve el
408
         * ?ltimo boundingbox que se calcul?.
409
         *
410
         * @param at Transformada af?n
411
         *
412
         * @return Rect?ngulo que representa el BoundingBox del fframe.
413
         */
414
        public Rectangle2D.Double getBoundingBox(AffineTransform at) {
415
                if (at == null) {
416
                        return m_BoundingBox;
417
                }
418

    
419
                m_BoundingBox = FLayoutUtilities.fromSheetRect(m_BoundBox, at);
420

    
421
                return m_BoundingBox;
422
        }
423

    
424
        /**
425
         * Rellena con el rect?ngulo que se pasa como par?metro el boundBox(en
426
         * cent?metros) del fframe del cual con una transformaci?n se podr?
427
         * calcular el BoundingBox (en pixels).
428
         *
429
         * @param r Rect?ngulo en cent?metros.
430
         */
431
        public void setBoundBox(Rectangle2D.Double r) {
432
                m_BoundBox.setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
433
        }
434

    
435
        /**
436
         * Devuelve el rect?ngulo que representa el fframe en cent?metros.
437
         *
438
         * @return Rect?ngulo en centimetros.
439
         */
440
        public Rectangle2D.Double getBoundBox() {
441
                return m_BoundBox;
442
        }
443

    
444
        /**
445
         * Pasando como par?metro true,  se toma como que esta seleccionado el
446
         * fframe  y si es false como que esta sin seleccionar,  de esta forma se
447
         * selecciona un fframe directamente  sin comprobar si un punto esta
448
         * contenido en ?l.
449
         *
450
         * @param b true si se quiere seleccionar y false si se quiere
451
         *                   deseleccionar.
452
         */
453
        public void setSelected(boolean b) {
454
                if (b) {
455
                        m_Selected = RECT;
456
                } else {
457
                        m_Selected = NOSELECT;
458
                }
459
        }
460

    
461
        /**
462
         * Crea un Objeto FFrame seg?n el tipo que sea, a partir de la informaci?n
463
         * del XMLEntity.
464
         *
465
         * @param xml XMLEntity
466
         * @param p DOCUMENT ME!
467
         *
468
         * @return Objeto de esta clase.
469
         */
470
        public static IFFrame createFFrame(XMLEntity xml, Layout l,Project p) {
471
                IFFrame fframe = null;
472

    
473
                try {
474
                        Class clase = Class.forName(xml.getStringProperty("nameClass"));
475
                        fframe = (IFFrame) clase.newInstance();
476
                } catch (Exception e) {
477
                        NotificationManager.addError("Clase de Frame sobre el Layout no reconocida",
478
                                e);
479
                }
480
                if (fframe instanceof FFrameView){
481
                        ((FFrameView)fframe).setProject(p);
482
                }
483
                fframe.setXMLEntity(xml, l);
484
                fframe.setName(xml.getStringProperty("m_name"));
485
                fframe.setBoundBox(new Rectangle2D.Double(xml.getDoubleProperty("x"),
486
                                xml.getDoubleProperty("y"), xml.getDoubleProperty("w"),
487
                                xml.getDoubleProperty("h")));
488
                fframe.setTag(xml.getStringProperty("tag"));
489

    
490
                return fframe;
491
        }
492

    
493
        /**
494
         * Dibuja sobre el graphics el rect?ngulo del fframe en modo borrador.
495
         *
496
         * @param g Graphics so bre el que dibujar.
497
         */
498
        public void drawDraft(Graphics2D g) {
499
                Rectangle2D.Double r = getBoundingBox(null);
500
                g.setColor(Color.lightGray);
501
                g.fillRect((int) r.x, (int) r.y, (int) r.width, (int) r.height);
502
                g.setColor(Color.black);
503

    
504
                int scale = (int) (r.width / 12);
505
                Font f = new Font("SansSerif", Font.PLAIN, scale);
506
                g.setFont(f);
507
                g.drawString(getName(),
508
                        (int) (r.getCenterX() - ((getName().length() * scale) / 4)),
509
                        (int) (r.getCenterY()));
510
        }
511

    
512
        /**
513
         * Rellena con el n?mero de FFrame.
514
         *
515
         * @param i n?mero
516
         */
517
        public void setNum(int i) {
518
                num = i;
519
        }
520

    
521
        /**
522
         * Dibuja sobre el graphics el rect?ngulo del fframe pero vacio, mostrando
523
         * el nombre del fframe y vacio.
524
         *
525
         * @param g Graphics sobre el que dibujar.
526
         */
527
        public void drawEmpty(Graphics2D g) {
528
                Rectangle2D.Double r = getBoundingBox(null);
529
                g.setColor(Color.lightGray);
530
                g.fillRect((int) r.x, (int) r.y, (int) r.width, (int) r.height);
531
                g.setColor(Color.black);
532

    
533
                int scale = (int) (r.width / 12);
534
                Font f = new Font("SansSerif", Font.PLAIN, scale);
535
                g.setFont(f);
536

    
537
                String s = this.getNameFFrame() + " " +
538
                        PluginServices.getText(this, "vacia");
539

    
540
                g.drawString(s, (int) (r.getCenterX() - ((s.length() * scale) / 4)),
541
                        (int) (r.getCenterY()));
542
        }
543

    
544
        /**
545
         * Devuelve true si el rect?ngulo primero es null o si es distinto de null e intersecta.
546
         *
547
         * @param rv Rect?ngulo 
548
         * @param r Rect?ngulo
549
         *
550
         * @return True si intersecta o es null.
551
         */
552
        public boolean intersects(Rectangle2D rv, Rectangle2D r) {
553
                return (((rv != null) && rv.intersects(r)) || (rv == null));
554
        }
555

    
556
        /**
557
         * Abre el di?logo para cambiar o a?adir el tag.
558
         */
559
        public void openTag() {
560
                Tag tag = new Tag(this);
561
                PluginServices.getMDIManager().addView(tag);
562
        }
563

    
564
        /**
565
         * Rellena el tag del FFrame.
566
         *
567
         * @param s String que representa el valor a guardar en el tag.
568
         */
569
        public void setTag(String s) {
570
                tag = s;
571
        }
572

    
573
        /**
574
         * Devuelve el tag.
575
         *
576
         * @return tag.
577
         */
578
        public String getTag() {
579
                return tag;
580
        }
581

    
582
        /**
583
         * Dibuja sobre el graphics que se pasa como par?metro el icono que representa que contiene un tag.
584
         *
585
         * @param g Graphics sobre el que dibujar el icono.
586
         */
587
        public void drawSymbolTag(Graphics2D g) {
588
                Rectangle2D.Double rec = getBoundingBox(null);
589
                try{
590
                URL url = Abrir.class.getClassLoader().getResource("images/symbolTag.gif");
591
                Image image = new ImageIcon(url).getImage();
592
                g.drawImage(image, (int) rec.x, (int) rec.y, 25, 30, null);
593
                }catch (NullPointerException npe){
594
                        
595
                }
596
        }
597
}