Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / DefaultLayoutControl.java @ 36648

History | View | Annotate | Download (19.5 KB)

1
/* 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;
23

    
24
import java.awt.Color;
25
import java.awt.Cursor;
26
import java.awt.Graphics;
27
import java.awt.Graphics2D;
28
import java.awt.Image;
29
import java.awt.Point;
30
import java.awt.Rectangle;
31
import java.awt.Toolkit;
32
import java.awt.geom.AffineTransform;
33
import java.awt.geom.Rectangle2D;
34
import java.awt.image.BufferedImage;
35
import java.awt.image.MemoryImageSource;
36
import java.util.HashMap;
37

    
38
import javax.swing.JComponent;
39

    
40
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
41
import org.gvsig.app.project.documents.layout.fframes.IFFrameEditableVertex;
42
import org.gvsig.app.project.documents.layout.fframes.IFFrameUseFMap;
43
import org.gvsig.app.project.documents.layout.geometryadapters.GeometryAdapter;
44
import org.gvsig.app.project.documents.layout.geometryadapters.PolyLineAdapter;
45
import org.gvsig.app.project.documents.layout.gui.FPopupMenu;
46
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
47
import org.gvsig.app.project.documents.layout.tools.LayoutAddBoxListenerImpl;
48
import org.gvsig.app.project.documents.layout.tools.LayoutAddCircleListenerImpl;
49
import org.gvsig.app.project.documents.layout.tools.LayoutAddLegendListenerImpl;
50
import org.gvsig.app.project.documents.layout.tools.LayoutAddLineListenerImpl;
51
import org.gvsig.app.project.documents.layout.tools.LayoutAddNorthListenerImpl;
52
import org.gvsig.app.project.documents.layout.tools.LayoutAddOverViewListenerImpl;
53
import org.gvsig.app.project.documents.layout.tools.LayoutAddPictureListenerImpl;
54
import org.gvsig.app.project.documents.layout.tools.LayoutAddPointListenerImpl;
55
import org.gvsig.app.project.documents.layout.tools.LayoutAddPolygonListenerImpl;
56
import org.gvsig.app.project.documents.layout.tools.LayoutAddPolylineListenerImpl;
57
import org.gvsig.app.project.documents.layout.tools.LayoutAddRectangleListenerImpl;
58
import org.gvsig.app.project.documents.layout.tools.LayoutAddScaleListenerImpl;
59
import org.gvsig.app.project.documents.layout.tools.LayoutAddTextListenerImpl;
60
import org.gvsig.app.project.documents.layout.tools.LayoutAddViewListenerImpl;
61
import org.gvsig.app.project.documents.layout.tools.LayoutEditGraphicsListenerImpl;
62
import org.gvsig.app.project.documents.layout.tools.LayoutPanListenerImpl;
63
import org.gvsig.app.project.documents.layout.tools.LayoutSelectListenerImpl;
64
import org.gvsig.app.project.documents.layout.tools.LayoutViewPanListenerImpl;
65
import org.gvsig.app.project.documents.layout.tools.LayoutViewZoomInListenerImpl;
66
import org.gvsig.app.project.documents.layout.tools.LayoutViewZoomOutListenerImpl;
67
import org.gvsig.app.project.documents.layout.tools.LayoutZoomInListenerImpl;
68
import org.gvsig.app.project.documents.layout.tools.LayoutZoomOutListenerImpl;
69
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutBehavior;
70
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutEditBehavior;
71
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutMoveBehavior;
72
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutPointBehavior;
73
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutRectangleBehavior;
74
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutSelectBehavior;
75
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutViewMoveBehavior;
76
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutViewZoomBehavior;
77
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutZoomBehavior;
78
import org.gvsig.app.project.documents.layout.tools.listener.ILayoutGraphicListener;
79
import org.gvsig.fmap.dal.exception.ReadException;
80

    
81
/**
82
 * Control of Layout.
83
 * 
84
 * @author Vicente Caballero Navarro
85
 */
86
public class DefaultLayoutControl extends JComponent implements LayoutControl {
87

    
88
    private static final long serialVersionUID = -8290006366669291510L;
89

    
90
    public static final String PERSISTENCE_DEFINITION_NAME = "LayoutControl";
91

    
92
    private int status = DESACTUALIZADO;
93
    private Rectangle2D rectVisible;
94
    private BufferedImage img = null;
95
    private BufferedImage imgRuler = null;
96
    private boolean initial = true;
97
    private LayoutContext layoutContext;
98
    private Point origin = new Point(50, 50);
99
    private Point rectOrigin = new Point(origin);
100
    private Rectangle2D.Double rect = new Rectangle2D.Double(rectOrigin.x,
101
        rectOrigin.y, 400, 300);
102
    private FLayoutDraw layoutDraw = null;
103
    private FLayoutFunctions layoutFunctions = null;
104
    private LayoutBehavior currentLayoutTool = null;
105
    private Image imageCursor = null;
106
    private HashMap namesLayoutTools = new HashMap();
107
    private Point m_FirstPoint = new Point(0, 0);
108
    private Point m_PointAnt = new Point(0, 0);
109
    private Point m_LastPoint = new Point(0, 0);
110
    private LayoutEvents layoutEvents;
111
    private Point position;
112
    private GeometryAdapter geometryAdapter = new PolyLineAdapter();
113

    
114
    // private String prevTool;
115
    private String currentTool;
116
    private boolean m_bCancelDrawing = false;
117
    private Rectangle reSel = null;
118
    private boolean isReSel = true;
119
    private FLayoutZooms layoutZooms;
120

    
121
    private static Cursor transparentCursor = Toolkit.getDefaultToolkit()
122
        .createCustomCursor(
123
            Toolkit.getDefaultToolkit().createImage(
124
                new MemoryImageSource(16, 16, new int[16 * 16], 0, 16)),
125
            new Point(0, 0), "invisiblecursor");
126

    
127
    /**
128
     * Create a new object of LayoutControl.
129
     * 
130
     * @param layout
131
     */
132
    public DefaultLayoutControl(LayoutPanel layout) {
133
        layoutDraw = new FLayoutDraw(layout);
134
        layoutEvents = new LayoutEvents(layout);
135
        layoutZooms = new FLayoutZooms(layout);
136
        layoutFunctions = new FLayoutFunctions(layout);
137
        addComponentListener(layoutEvents);
138
        addMouseMotionListener(layoutEvents);
139
        addMouseListener(layoutEvents);
140

    
141
        LayoutPanListenerImpl lpl = new LayoutPanListenerImpl(layout);
142
        addLayoutTool("layoutpan", new LayoutMoveBehavior(lpl));
143

    
144
        LayoutZoomInListenerImpl lzil = new LayoutZoomInListenerImpl(layout);
145
        addLayoutTool("layoutzoomin", new LayoutZoomBehavior(lzil));
146

    
147
        LayoutZoomOutListenerImpl lzol = new LayoutZoomOutListenerImpl(layout);
148
        addLayoutTool("layoutzoomout", new LayoutZoomBehavior(lzol));
149

    
150
        LayoutAddViewListenerImpl lavl = new LayoutAddViewListenerImpl(layout);
151
        addLayoutTool("layoutaddview", new LayoutRectangleBehavior(lavl));
152

    
153
        LayoutAddOverViewListenerImpl laovl =
154
            new LayoutAddOverViewListenerImpl(layout);
155
        addLayoutTool("layoutaddoverview", new LayoutRectangleBehavior(laovl));
156

    
157
        LayoutAddPictureListenerImpl lapl =
158
            new LayoutAddPictureListenerImpl(layout);
159
        addLayoutTool("layoutaddpicture", new LayoutRectangleBehavior(lapl));
160

    
161
        LayoutAddNorthListenerImpl lanorthl =
162
            new LayoutAddNorthListenerImpl(layout);
163
        addLayoutTool("layoutaddnorth", new LayoutRectangleBehavior(lanorthl));
164

    
165
        LayoutAddScaleListenerImpl lasl =
166
            new LayoutAddScaleListenerImpl(layout);
167
        addLayoutTool("layoutaddscale", new LayoutRectangleBehavior(lasl));
168

    
169
        LayoutAddLegendListenerImpl lall =
170
            new LayoutAddLegendListenerImpl(layout);
171
        addLayoutTool("layoutaddlegend", new LayoutRectangleBehavior(lall));
172

    
173
        LayoutAddTextListenerImpl latl = new LayoutAddTextListenerImpl(layout);
174
        addLayoutTool("layoutaddtext", new LayoutRectangleBehavior(latl));
175

    
176
        LayoutAddBoxListenerImpl labl = new LayoutAddBoxListenerImpl(layout);
177
        addLayoutTool("layoutaddbox", new LayoutRectangleBehavior(labl));
178

    
179
        LayoutAddPointListenerImpl lapointl =
180
            new LayoutAddPointListenerImpl(layout);
181
        addLayoutTool("layoutaddpoint", new LayoutPointBehavior(lapointl));
182

    
183
        LayoutAddLineListenerImpl lalinel =
184
            new LayoutAddLineListenerImpl(layout);
185
        addLayoutTool("layoutaddline", new LayoutPointBehavior(lalinel));
186

    
187
        LayoutAddPolygonListenerImpl lapolygonl =
188
            new LayoutAddPolygonListenerImpl(layout);
189
        addLayoutTool("layoutaddpolygon", new LayoutPointBehavior(lapolygonl));
190

    
191
        LayoutAddPolylineListenerImpl lapolylinel =
192
            new LayoutAddPolylineListenerImpl(layout);
193
        addLayoutTool("layoutaddpolyline", new LayoutPointBehavior(lapolylinel));
194

    
195
        LayoutAddCircleListenerImpl lacirclel =
196
            new LayoutAddCircleListenerImpl(layout);
197
        addLayoutTool("layoutaddcircle", new LayoutPointBehavior(lacirclel));
198

    
199
        LayoutAddRectangleListenerImpl larectanglel =
200
            new LayoutAddRectangleListenerImpl(layout);
201
        addLayoutTool("layoutaddrectangle", new LayoutPointBehavior(
202
            larectanglel));
203

    
204
        LayoutViewPanListenerImpl lvpl = new LayoutViewPanListenerImpl(layout);
205
        addLayoutTool("layoutviewpan", new LayoutViewMoveBehavior(lvpl));
206

    
207
        LayoutViewZoomInListenerImpl lvzil =
208
            new LayoutViewZoomInListenerImpl(layout);
209
        addLayoutTool("layoutviewzoomin", new LayoutViewZoomBehavior(lvzil));
210

    
211
        LayoutViewZoomOutListenerImpl lvzol =
212
            new LayoutViewZoomOutListenerImpl(layout);
213
        addLayoutTool("layoutviewzoomout", new LayoutViewZoomBehavior(lvzol));
214

    
215
        LayoutSelectListenerImpl lselectl =
216
            new LayoutSelectListenerImpl(layout);
217
        addLayoutTool("layoutselect", new LayoutSelectBehavior(lselectl));
218

    
219
        LayoutEditGraphicsListenerImpl leditl =
220
            new LayoutEditGraphicsListenerImpl(layout);
221
        addLayoutTool("layoutedit", new LayoutEditBehavior(leditl));
222

    
223
        FPopupMenu.registerExtensionPoint();
224
    }
225

    
226
    public String getCurrentTool() {
227
        return currentTool;
228
    }
229

    
230
    public void addLayoutTool(String name, LayoutBehavior tool) {
231
        namesLayoutTools.put(name, tool);
232
        tool.setLayoutControl(this);
233
    }
234

    
235
    public void setLayoutContext(LayoutContext lc) {
236
        layoutContext = lc;
237
        layoutContext.addLayoutDrawListener(this);
238
    }
239

    
240
    /**
241
     * paintComponent of Layout.
242
     * 
243
     * @param g
244
     *            Graphics of Layout.
245
     */
246
    protected void paintComponent(Graphics g) {
247
        clipVisibleRect((Graphics2D) g);
248

    
249
        Rectangle rClip = g.getClipBounds();
250

    
251
        if (rClip == null) {
252
            System.err.println("clip = null");
253
        }
254

    
255
        switch (status) {
256
        case DESACTUALIZADO:
257
            if (getWidth() == 0) {
258
                return;
259
            }
260

    
261
            img =
262
                new BufferedImage(getWidth(), getHeight(),
263
                    BufferedImage.TYPE_INT_ARGB);
264
            imgRuler =
265
                new BufferedImage(getWidth(), getHeight(),
266
                    BufferedImage.TYPE_INT_ARGB);
267

    
268
            Graphics gimag = img.getGraphics();
269
            Graphics gimgRuler = imgRuler.getGraphics();
270
            clipVisibleRect((Graphics2D) gimag);
271

    
272
            if (initial) {
273
                fullRect();
274
                initial = false;
275
            }
276

    
277
            try {
278
                layoutDraw.drawLayout((Graphics2D) gimag, img);
279
            } catch (ReadException e) {
280
                e.printStackTrace();
281
            }
282

    
283
            g.setClip(rClip);
284
            layoutDraw.drawRectangle((Graphics2D) g);
285

    
286
            g.drawImage(img, 0, 0, this);
287
            g.setClip(rClip);
288

    
289
            layoutDraw.drawGrid((Graphics2D) gimgRuler);
290
            layoutDraw.drawRuler((Graphics2D) gimgRuler, Color.black);
291
            setStatus(ACTUALIZADO);
292
            repaint();
293

    
294
            break;
295

    
296
        case ACTUALIZADO:
297
            layoutDraw.drawRectangle((Graphics2D) g);
298

    
299
            if (currentLayoutTool != null) {
300
                currentLayoutTool.paintComponent(g);
301
            } else {
302
                g.drawImage(img, 0, 0, this);
303

    
304
                layoutDraw.drawHandlers((Graphics2D) g, Color.black);
305
            }
306

    
307
            g.setClip(rClip);
308
            drawCursor(g);
309
        }
310
    }
311

    
312
    public void setStatus(int s) {
313
        status = s;
314
    }
315

    
316
    /**
317
     * Clip on visible rectangle.
318
     * 
319
     * @param g2d
320
     *            Graphics.
321
     */
322
    private void clipVisibleRect(Graphics2D g2d) {
323
        rectVisible = this.getVisibleRect();
324
        g2d.clipRect((int) rectVisible.getMinX(), (int) rectVisible.getMinY(),
325
            (int) rectVisible.getWidth(), (int) rectVisible.getHeight());
326
    }
327

    
328
    public BufferedImage getImgRuler() {
329
        return imgRuler;
330
    }
331

    
332
    public void fullRect() {
333
        rect.setRect(origin.x, origin.y, getWidth() - (origin.x * 2),
334
            getHeight() - (origin.x * 2));
335

    
336
        if (layoutContext.getAttributes().isLandSpace()) {
337
            rect =
338
                layoutContext.getAttributes().getRectangleLandscape(rect,
339
                    getWidth(), getHeight());
340
        } else {
341
            rect =
342
                layoutContext.getAttributes().getRectanglePortrait(rect,
343
                    getWidth(), getHeight());
344
        }
345

    
346
        refresh();
347
    }
348

    
349
    public Rectangle2D.Double getRect() {
350
        return rect;
351
    }
352

    
353
    public void setRect(Rectangle2D r) {
354
        rect.setRect(r);
355
    }
356

    
357
    public BufferedImage getImage() {
358
        return img;
359
    }
360

    
361
    /**
362
     * Draw the cursor on the Graphics.
363
     * 
364
     * @param g
365
     *            Graphics.
366
     */
367
    private void drawCursor(Graphics g) {
368
        if ((imageCursor != null) && (position != null)) {
369
            Point pAdjusted = position;
370
            g.drawImage(imageCursor, (int) pAdjusted.getX() - 16,
371
                (int) pAdjusted.getY() - 16, this);
372
        }
373
    }
374

    
375
    public void setMapCursor(Image image) {
376
        imageCursor = image;
377
    }
378

    
379
    public void setTool(String toolName) {
380
        // prevTool=getCurrentTool();
381
        LayoutBehavior layoutTool =
382
            (LayoutBehavior) namesLayoutTools.get(toolName);
383
        currentLayoutTool = layoutTool;
384
        currentLayoutTool.setLayoutControl(this);
385
        currentTool = toolName;
386
        this.setMapCursor(layoutTool.getImageCursor());
387

    
388
        if (getCurrentLayoutTool().getListener() instanceof ILayoutGraphicListener) {
389
            geometryAdapter =
390
                ((ILayoutGraphicListener) getCurrentLayoutTool().getListener())
391
                    .createGeometryAdapter();
392
        }
393

    
394
        if (getCurrentTool().equals("layoutedit")) {
395
            startEdit();
396
        } else {
397
            stopEdit();
398
        }
399
    }
400

    
401
    public void startEdit() {
402
        IFFrame[] fframes = layoutContext.getFFrames();
403

    
404
        for (int i = 0; i < fframes.length; i++) {
405
            IFFrame frame = fframes[i];
406

    
407
            if (frame instanceof IFFrameEditableVertex) {
408
                if (frame.getSelected() != IFFrame.NOSELECT) {
409
                    ((IFFrameEditableVertex) frame).startEditing();
410
                } else {
411
                    ((IFFrameEditableVertex) frame).stopEditing();
412
                }
413
            }
414
        }
415

    
416
        refresh();
417
    }
418

    
419
    public void stopEdit() {
420
        boolean refresh = false;
421
        IFFrame[] fframes = layoutContext.getFFrames();
422

    
423
        for (int i = 0; i < fframes.length; i++) {
424
            IFFrame frame = fframes[i];
425

    
426
            if (frame instanceof IFFrameEditableVertex) {
427
                if (((IFFrameEditableVertex) frame).isEditing()) {
428
                    ((IFFrameEditableVertex) fframes[i]).stopEditing();
429
                    refresh = true;
430
                }
431
            }
432
        }
433

    
434
        if (refresh) {
435
            refresh();
436
        }
437
    }
438

    
439
    public Point getRectOrigin() {
440
        return rectOrigin;
441
    }
442

    
443
    public FLayoutDraw getLayoutDraw() {
444
        return layoutDraw;
445
    }
446

    
447
    public LayoutBehavior getCurrentLayoutTool() {
448
        return currentLayoutTool;
449
    }
450

    
451
    public Point getFirstPoint() {
452
        return m_FirstPoint;
453
    }
454

    
455
    public Point getPointAnt() {
456
        return m_PointAnt;
457
    }
458

    
459
    public Point getLastPoint() {
460
        return m_LastPoint;
461
    }
462

    
463
    public void setFirstPoint() {
464
        m_FirstPoint = position;
465
    }
466

    
467
    public void setPointAnt() {
468
        m_PointAnt = position;
469
    }
470

    
471
    public void setLastPoint() {
472
        m_LastPoint = position;
473
    }
474

    
475
    public void setPosition(Point point2) {
476
        if (layoutContext.isAdjustingToGrid()
477
            && getCurrentLayoutTool().isAdjustable()) {
478
            position =
479
                FLayoutUtilities.getPointGrid(point2, layoutContext
480
                    .getAttributes().getNumUnitsX(), layoutContext
481
                    .getAttributes().getNumUnitsY(), layoutContext.getAT());
482
        } else {
483
            position = point2;
484
        }
485
    }
486

    
487
    public Point getPosition() {
488
        return position;
489
    }
490

    
491
    public AffineTransform getAT() {
492
        return layoutContext.getAT();
493
    }
494

    
495
    public GeometryAdapter getGeometryAdapter() {
496
        return geometryAdapter;
497
    }
498

    
499
    public void delLastPoint() {
500
        getGeometryAdapter().delLastPoint();
501
    }
502

    
503
    public int addGeometryAdapterPoint() {
504
        return getGeometryAdapter().addPoint(
505
            FLayoutUtilities.toSheetPoint(getPosition(), getAT()));
506
    }
507

    
508
    public void setGeometryAdapterPoinPosition() {
509
        getGeometryAdapter().pointPosition(
510
            FLayoutUtilities.toSheetPoint(getPosition(), getAT()));
511
    }
512

    
513
    public void clearMouseImage() {
514
        setCursor(transparentCursor);
515
    }
516

    
517
    public void refresh() {
518
        setStatus(LayoutControl.DESACTUALIZADO);
519
        repaint();
520
    }
521

    
522
    public synchronized boolean isDrawingCancelled() {
523
        return m_bCancelDrawing;
524
    }
525

    
526
    public synchronized void setCancelDrawing(boolean b) {
527
        m_bCancelDrawing = b;
528

    
529
        for (int i = 0; i < layoutContext.getFFrames().length; i++) {
530
            IFFrame fframe = layoutContext.getFFrame(i);
531

    
532
            if (fframe instanceof IFFrameUseFMap
533
                && (((IFFrameUseFMap) fframe).getMapContext() != null)) {
534
                // //TODO((FFrameView)
535
                // getFFrames().get(i)).getFMap().setCancelDrawing(b);
536
            }
537
        }
538
    }
539

    
540
    public Rectangle getReSel() {
541
        return reSel;
542
    }
543

    
544
    public boolean isReSel() {
545
        return isReSel;
546
    }
547

    
548
    public void setIsReSel(boolean b) {
549
        isReSel = b;
550
    }
551

    
552
    public void viewFull() throws ReadException {
553
        IFFrame[] fframes = layoutContext.getFFrameSelected();
554

    
555
        for (int i = 0; i < fframes.length; i++) {
556
            if (fframes[i] instanceof IFFrameUseFMap) {
557
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
558

    
559
                if (fframe.getMapContext() != null) {
560
                    fframe.fullExtent();
561
                }
562

    
563
                fframe.refresh();
564
            }
565
        }
566

    
567
        refresh();
568
    }
569

    
570
    public FLayoutZooms getLayoutZooms() {
571
        return layoutZooms;
572
    }
573

    
574
    public FLayoutFunctions getLayoutFunctions() {
575
        return layoutFunctions;
576
    }
577

    
578
    public LayoutContext getLayoutContext() {
579
        return layoutContext;
580
    }
581

    
582
    public void setGeometryAdapter(GeometryAdapter adapter) {
583
        geometryAdapter = adapter;
584
    }
585

    
586
    public JComponent getComponent() {
587
        return this;
588
    }
589
}