Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / layout / DefaultLayoutControl.java @ 33420

History | View | Annotate | Download (18.3 KB)

1
package org.gvsig.app.project.documents.layout;
2

    
3
import java.awt.Color;
4
import java.awt.Cursor;
5
import java.awt.Graphics;
6
import java.awt.Graphics2D;
7
import java.awt.Image;
8
import java.awt.Point;
9
import java.awt.Rectangle;
10
import java.awt.Toolkit;
11
import java.awt.geom.AffineTransform;
12
import java.awt.geom.Rectangle2D;
13
import java.awt.image.BufferedImage;
14
import java.awt.image.MemoryImageSource;
15
import java.util.HashMap;
16

    
17
import javax.swing.JComponent;
18

    
19
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
20
import org.gvsig.app.project.documents.layout.fframes.IFFrameEditableVertex;
21
import org.gvsig.app.project.documents.layout.fframes.IFFrameUseFMap;
22
import org.gvsig.app.project.documents.layout.geometryadapters.GeometryAdapter;
23
import org.gvsig.app.project.documents.layout.geometryadapters.PolyLineAdapter;
24
import org.gvsig.app.project.documents.layout.gui.FPopupMenu;
25
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
26
import org.gvsig.app.project.documents.layout.tools.LayoutAddBoxListenerImpl;
27
import org.gvsig.app.project.documents.layout.tools.LayoutAddCircleListenerImpl;
28
import org.gvsig.app.project.documents.layout.tools.LayoutAddLegendListenerImpl;
29
import org.gvsig.app.project.documents.layout.tools.LayoutAddLineListenerImpl;
30
import org.gvsig.app.project.documents.layout.tools.LayoutAddNorthListenerImpl;
31
import org.gvsig.app.project.documents.layout.tools.LayoutAddOverViewListenerImpl;
32
import org.gvsig.app.project.documents.layout.tools.LayoutAddPictureListenerImpl;
33
import org.gvsig.app.project.documents.layout.tools.LayoutAddPointListenerImpl;
34
import org.gvsig.app.project.documents.layout.tools.LayoutAddPolygonListenerImpl;
35
import org.gvsig.app.project.documents.layout.tools.LayoutAddPolylineListenerImpl;
36
import org.gvsig.app.project.documents.layout.tools.LayoutAddRectangleListenerImpl;
37
import org.gvsig.app.project.documents.layout.tools.LayoutAddScaleListenerImpl;
38
import org.gvsig.app.project.documents.layout.tools.LayoutAddTextListenerImpl;
39
import org.gvsig.app.project.documents.layout.tools.LayoutAddViewListenerImpl;
40
import org.gvsig.app.project.documents.layout.tools.LayoutEditGraphicsListenerImpl;
41
import org.gvsig.app.project.documents.layout.tools.LayoutPanListenerImpl;
42
import org.gvsig.app.project.documents.layout.tools.LayoutSelectListenerImpl;
43
import org.gvsig.app.project.documents.layout.tools.LayoutViewPanListenerImpl;
44
import org.gvsig.app.project.documents.layout.tools.LayoutViewZoomInListenerImpl;
45
import org.gvsig.app.project.documents.layout.tools.LayoutViewZoomOutListenerImpl;
46
import org.gvsig.app.project.documents.layout.tools.LayoutZoomInListenerImpl;
47
import org.gvsig.app.project.documents.layout.tools.LayoutZoomOutListenerImpl;
48
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutBehavior;
49
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutEditBehavior;
50
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutMoveBehavior;
51
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutPointBehavior;
52
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutRectangleBehavior;
53
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutSelectBehavior;
54
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutViewMoveBehavior;
55
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutViewZoomBehavior;
56
import org.gvsig.app.project.documents.layout.tools.behavior.LayoutZoomBehavior;
57
import org.gvsig.app.project.documents.layout.tools.listener.ILayoutGraphicListener;
58
import org.gvsig.fmap.dal.exception.ReadException;
59

    
60

    
61

    
62
/**
63
 * Control of Layout.
64
 *
65
 * @author Vicente Caballero Navarro
66
 */
67
public class DefaultLayoutControl extends JComponent implements LayoutControl {
68
        public static final String PERSISTENCE_DEFINITION_NAME = "LayoutControl";
69
        
70
        private int status = DESACTUALIZADO;
71
    private Rectangle2D rectVisible;
72
    private BufferedImage img = null;
73
    private BufferedImage imgRuler = null;
74
    private boolean initial = true;
75
    private LayoutContext layoutContext;
76
    private Point origin = new Point(50, 50);
77
    private Point rectOrigin = new Point(origin);
78
    private Rectangle2D.Double rect = new Rectangle2D.Double(rectOrigin.x,
79
            rectOrigin.y, 400, 300);
80
    private FLayoutDraw layoutDraw = null;
81
    private FLayoutFunctions layoutFunctions = null;
82
    private LayoutBehavior currentLayoutTool = null;
83
    private Image imageCursor = null;
84
    private HashMap namesLayoutTools = new HashMap();
85
    private Point m_FirstPoint = new Point(0, 0);
86
    private Point m_PointAnt = new Point(0, 0);
87
    private Point m_LastPoint = new Point(0, 0);
88
    private LayoutEvents layoutEvents;
89
    private Point position;
90
    private GeometryAdapter geometryAdapter = new PolyLineAdapter();
91

    
92
    //private String prevTool;
93
    private String currentTool;
94
    private boolean m_bCancelDrawing = false;
95
    private Rectangle reSel = null;
96
    private boolean isReSel = true;
97
    private FLayoutZooms layoutZooms;
98

    
99
    private static Cursor transparentCursor = Toolkit.getDefaultToolkit()
100
            .createCustomCursor(Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(
101
            16, 16, new int[16 * 16], 0, 16)),
102
            new Point(0, 0), "invisiblecursor");
103

    
104

    
105
    /**
106
     * Create a new object of LayoutControl.
107
     * @param layout
108
     */
109
    public DefaultLayoutControl(LayoutPanel layout) {       
110
        layoutDraw = new FLayoutDraw(layout);
111
        layoutEvents = new LayoutEvents(layout);
112
        layoutZooms = new FLayoutZooms(layout);
113
        layoutFunctions = new FLayoutFunctions(layout);
114
        addComponentListener(layoutEvents);
115
        addMouseMotionListener(layoutEvents);
116
        addMouseListener(layoutEvents);
117

    
118
        LayoutPanListenerImpl lpl = new LayoutPanListenerImpl(layout);
119
        addLayoutTool("layoutpan", new LayoutMoveBehavior(lpl));
120

    
121
        LayoutZoomInListenerImpl lzil = new LayoutZoomInListenerImpl(layout);
122
        addLayoutTool("layoutzoomin", new LayoutZoomBehavior(lzil));
123

    
124
        LayoutZoomOutListenerImpl lzol = new LayoutZoomOutListenerImpl(layout);
125
        addLayoutTool("layoutzoomout", new LayoutZoomBehavior(lzol));
126

    
127
        LayoutAddViewListenerImpl lavl = new LayoutAddViewListenerImpl(layout);
128
        addLayoutTool("layoutaddview", new LayoutRectangleBehavior(lavl));
129

    
130
        LayoutAddOverViewListenerImpl laovl = new LayoutAddOverViewListenerImpl(layout);
131
        addLayoutTool("layoutaddoverview", new LayoutRectangleBehavior(laovl));
132

    
133
        LayoutAddPictureListenerImpl lapl = new LayoutAddPictureListenerImpl(layout);
134
        addLayoutTool("layoutaddpicture", new LayoutRectangleBehavior(lapl));
135

    
136
        LayoutAddNorthListenerImpl lanorthl = new LayoutAddNorthListenerImpl(layout);
137
        addLayoutTool("layoutaddnorth", new LayoutRectangleBehavior(lanorthl));
138

    
139
        LayoutAddScaleListenerImpl lasl = new LayoutAddScaleListenerImpl(layout);
140
        addLayoutTool("layoutaddscale", new LayoutRectangleBehavior(lasl));
141

    
142
        LayoutAddLegendListenerImpl lall = new LayoutAddLegendListenerImpl(layout);
143
        addLayoutTool("layoutaddlegend", new LayoutRectangleBehavior(lall));
144

    
145
        LayoutAddTextListenerImpl latl = new LayoutAddTextListenerImpl(layout);
146
        addLayoutTool("layoutaddtext", new LayoutRectangleBehavior(latl));
147

    
148
        LayoutAddBoxListenerImpl labl = new LayoutAddBoxListenerImpl(layout);
149
        addLayoutTool("layoutaddbox", new LayoutRectangleBehavior(labl));
150

    
151
        LayoutAddPointListenerImpl lapointl = new LayoutAddPointListenerImpl(layout);
152
        addLayoutTool("layoutaddpoint", new LayoutPointBehavior(lapointl));
153

    
154
        LayoutAddLineListenerImpl lalinel = new LayoutAddLineListenerImpl(layout);
155
        addLayoutTool("layoutaddline", new LayoutPointBehavior(lalinel));
156

    
157
        LayoutAddPolygonListenerImpl lapolygonl = new LayoutAddPolygonListenerImpl(layout);
158
        addLayoutTool("layoutaddpolygon", new LayoutPointBehavior(lapolygonl));
159

    
160
        LayoutAddPolylineListenerImpl lapolylinel = new LayoutAddPolylineListenerImpl(layout);
161
        addLayoutTool("layoutaddpolyline", new LayoutPointBehavior(lapolylinel));
162

    
163
        LayoutAddCircleListenerImpl lacirclel = new LayoutAddCircleListenerImpl(layout);
164
        addLayoutTool("layoutaddcircle", new LayoutPointBehavior(lacirclel));
165

    
166
        LayoutAddRectangleListenerImpl larectanglel = new LayoutAddRectangleListenerImpl(layout);
167
        addLayoutTool("layoutaddrectangle",
168
            new LayoutPointBehavior(larectanglel));
169

    
170
        LayoutViewPanListenerImpl lvpl = new LayoutViewPanListenerImpl(layout);
171
        addLayoutTool("layoutviewpan", new LayoutViewMoveBehavior(lvpl));
172

    
173
        LayoutViewZoomInListenerImpl lvzil = new LayoutViewZoomInListenerImpl(layout);
174
        addLayoutTool("layoutviewzoomin", new LayoutViewZoomBehavior(lvzil));
175

    
176
        LayoutViewZoomOutListenerImpl lvzol = new LayoutViewZoomOutListenerImpl(layout);
177
        addLayoutTool("layoutviewzoomout", new LayoutViewZoomBehavior(lvzol));
178

    
179
        LayoutSelectListenerImpl lselectl = new LayoutSelectListenerImpl(layout);
180
        addLayoutTool("layoutselect", new LayoutSelectBehavior(lselectl));
181

    
182
        LayoutEditGraphicsListenerImpl leditl = new LayoutEditGraphicsListenerImpl(layout);
183
        addLayoutTool("layoutedit", new LayoutEditBehavior(leditl));
184
        
185
        FPopupMenu.registerExtensionPoint();
186
    }
187

    
188
    public String getCurrentTool() {
189
        return currentTool;
190
    }
191

    
192
    public void addLayoutTool(String name, LayoutBehavior tool) {
193
        namesLayoutTools.put(name, tool);
194
        tool.setLayoutControl(this);
195
    }
196

    
197
    public void setLayoutContext(LayoutContext lc) {
198
        layoutContext = lc;
199
        layoutContext.addLayoutDrawListener(this);
200
    }
201

    
202
    /**
203
     * paintComponent of Layout.
204
     *
205
     * @param g Graphics of Layout.
206
     */
207
    protected void paintComponent(Graphics g) {
208
        clipVisibleRect((Graphics2D) g);
209

    
210
        Rectangle rClip = g.getClipBounds();
211

    
212
        if (rClip == null) {
213
            System.err.println("clip = null");
214
        }
215

    
216
        switch (status) {
217
        case DESACTUALIZADO:
218
            if (getWidth() == 0) {
219
                return;
220
            }
221

    
222
            img = new BufferedImage(getWidth(), getHeight(),
223
                    BufferedImage.TYPE_INT_ARGB);
224
            imgRuler = new BufferedImage(getWidth(), getHeight(),
225
                    BufferedImage.TYPE_INT_ARGB);
226

    
227
            Graphics gimag = img.getGraphics();
228
            Graphics gimgRuler = imgRuler.getGraphics();
229
            clipVisibleRect((Graphics2D) gimag);
230

    
231
            if (initial) {
232
                fullRect();
233
                initial = false;
234
            }
235

    
236
            try {
237
                layoutDraw.drawLayout((Graphics2D) gimag, img);
238
            } catch (ReadException e) {
239
                e.printStackTrace();
240
            }
241

    
242
            g.setClip(rClip);
243
            layoutDraw.drawRectangle((Graphics2D) g);
244

    
245
            g.drawImage(img, 0, 0, this);
246
            g.setClip(rClip);
247

    
248
            layoutDraw.drawGrid((Graphics2D) gimgRuler);
249
            layoutDraw.drawRuler((Graphics2D) gimgRuler, Color.black);
250
            setStatus(ACTUALIZADO);
251
            repaint();
252

    
253
            break;
254

    
255
        case ACTUALIZADO:
256
            layoutDraw.drawRectangle((Graphics2D) g);
257

    
258
            if (currentLayoutTool != null) {
259
                currentLayoutTool.paintComponent(g);
260
            } else {
261
                g.drawImage(img, 0, 0, this);
262

    
263
                layoutDraw.drawHandlers((Graphics2D) g, Color.black);
264
            }
265

    
266
            g.setClip(rClip);
267
            drawCursor(g);
268
        }
269
    }
270

    
271
    public void setStatus(int s) {
272
        status = s;
273
    }
274

    
275
    /**
276
     * Clip on visible rectangle.
277
     *
278
     * @param g2d Graphics.
279
     */
280
    private void clipVisibleRect(Graphics2D g2d) {
281
        rectVisible = this.getVisibleRect();
282
        g2d.clipRect((int) rectVisible.getMinX(), (int) rectVisible.getMinY(),
283
            (int) rectVisible.getWidth(), (int) rectVisible.getHeight());
284
    }
285

    
286
    public BufferedImage getImgRuler() {
287
        return imgRuler;
288
    }
289

    
290
    public void fullRect() {
291
        rect.setRect(origin.x, origin.y, getWidth() - (origin.x * 2),
292
            getHeight() - (origin.x * 2));
293

    
294
        if (layoutContext.getAttributes().isLandSpace()) {
295
            rect = layoutContext.getAttributes().getRectangleLandscape(rect,
296
                    getWidth(), getHeight());
297
        } else {
298
            rect = layoutContext.getAttributes().getRectanglePortrait(rect,
299
                    getWidth(), getHeight());
300
        }
301

    
302
        refresh();
303
    }
304

    
305
    public Rectangle2D.Double getRect() {
306
        return rect;
307
    }
308

    
309
    public void setRect(Rectangle2D r) {
310
        rect.setRect(r);
311
    }
312

    
313
    public BufferedImage getImage() {
314
        return img;
315
    }
316

    
317
    /**
318
     * Draw the cursor on the Graphics.
319
     *
320
     * @param g Graphics.
321
     */
322
    private void drawCursor(Graphics g) {
323
        if ((imageCursor != null) && (position != null)) {
324
            Point pAdjusted = position;
325
            g.drawImage(imageCursor, (int) pAdjusted.getX() - 16,
326
                (int) pAdjusted.getY() - 16, this);
327
        }
328
    }
329

    
330
    public void setMapCursor(Image image) {
331
        imageCursor = image;
332
    }
333

    
334
    public void setTool(String toolName) {
335
        //prevTool=getCurrentTool();
336
        LayoutBehavior layoutTool = (LayoutBehavior) namesLayoutTools.get(toolName);
337
        currentLayoutTool = layoutTool;
338
        currentLayoutTool.setLayoutControl(this);
339
        currentTool = toolName;
340
        this.setMapCursor(layoutTool.getImageCursor());
341

    
342
        if (getCurrentLayoutTool().getListener() instanceof ILayoutGraphicListener) {
343
            geometryAdapter = ((ILayoutGraphicListener) getCurrentLayoutTool()
344
                                                            .getListener()).createGeometryAdapter();
345
        }
346

    
347
        if (getCurrentTool().equals("layoutedit")) {
348
            startEdit();
349
        } else {
350
            stopEdit();
351
        }
352
    }
353

    
354
    public void startEdit() {
355
        IFFrame[] fframes = layoutContext.getFFrames();
356

    
357
        for (int i = 0; i < fframes.length; i++) {
358
            IFFrame frame = fframes[i];
359

    
360
            if (frame instanceof IFFrameEditableVertex) {
361
                if (frame.getSelected() != IFFrame.NOSELECT) {
362
                    ((IFFrameEditableVertex) frame).startEditing();
363
                } else {
364
                    ((IFFrameEditableVertex) frame).stopEditing();
365
                }
366
            }
367
        }
368

    
369
        refresh();
370
    }
371

    
372
    public void stopEdit() {
373
        boolean refresh = false;
374
        IFFrame[] fframes = layoutContext.getFFrames();
375

    
376
        for (int i = 0; i < fframes.length; i++) {
377
            IFFrame frame = fframes[i];
378

    
379
            if (frame instanceof IFFrameEditableVertex) {
380
                if (((IFFrameEditableVertex) frame).isEditing()) {
381
                    ((IFFrameEditableVertex) fframes[i]).stopEditing();
382
                    refresh = true;
383
                }
384
            }
385
        }
386

    
387
        if (refresh) {
388
            refresh();
389
        }
390
    }
391

    
392
    public Point getRectOrigin() {
393
        return rectOrigin;
394
    }
395

    
396
    public FLayoutDraw getLayoutDraw() {
397
        return layoutDraw;
398
    }
399

    
400
    public LayoutBehavior getCurrentLayoutTool() {
401
        return currentLayoutTool;
402
    }
403

    
404
    public Point getFirstPoint() {
405
        return m_FirstPoint;
406
    }
407

    
408
    public Point getPointAnt() {
409
        return m_PointAnt;
410
    }
411

    
412
    public Point getLastPoint() {
413
        return m_LastPoint;
414
    }
415

    
416
    public void setFirstPoint() {
417
        m_FirstPoint = position;
418
    }
419

    
420
    public void setPointAnt() {
421
        m_PointAnt = position;
422
    }
423

    
424
    public void setLastPoint() {
425
        m_LastPoint = position;
426
    }
427

    
428
    public void setPosition(Point point2) {
429
        if (layoutContext.isAdjustingToGrid() &&
430
                getCurrentLayoutTool().isAdjustable()) {
431
            position = FLayoutUtilities.getPointGrid(point2,
432
                    layoutContext.getAttributes().getNumUnitsX(),
433
                    layoutContext.getAttributes().getNumUnitsY(),
434
                    layoutContext.getAT());
435
        } else {
436
            position = point2;
437
        }
438
    }
439

    
440
    public Point getPosition() {
441
        return position;
442
    }
443

    
444
    public AffineTransform getAT() {
445
        return layoutContext.getAT();
446
    }
447

    
448
    public GeometryAdapter getGeometryAdapter() {
449
        return geometryAdapter;
450
    }
451

    
452
    public void delLastPoint() {
453
        getGeometryAdapter().delLastPoint();
454
    }
455

    
456
    public int addGeometryAdapterPoint() {
457
        return getGeometryAdapter().addPoint(FLayoutUtilities.toSheetPoint(
458
                getPosition(), getAT()));
459
    }
460

    
461
    public void setGeometryAdapterPoinPosition() {
462
        getGeometryAdapter().pointPosition(FLayoutUtilities.toSheetPoint(
463
                getPosition(), getAT()));
464
    }
465

    
466
    public void clearMouseImage() {
467
        setCursor(transparentCursor);
468
    }
469

    
470
    public void refresh() {
471
        setStatus(LayoutControl.DESACTUALIZADO);
472
        repaint();
473
    }
474

    
475
    public synchronized boolean isDrawingCancelled() {
476
        return m_bCancelDrawing;
477
    }
478

    
479
     public synchronized void setCancelDrawing(boolean b) {
480
        m_bCancelDrawing = b;
481

    
482
        for (int i = 0; i < layoutContext.getFFrames().length; i++) {
483
            IFFrame fframe = layoutContext.getFFrame(i);
484

    
485
            if (fframe instanceof IFFrameUseFMap &&
486
                    (((IFFrameUseFMap) fframe).getMapContext() != null)) {
487
                // //TODO((FFrameView)
488
                // getFFrames().get(i)).getFMap().setCancelDrawing(b);
489
            }
490
        }
491
    }
492

    
493
    public Rectangle getReSel() {
494
        return reSel;
495
    }
496

    
497
    public boolean isReSel() {
498
        return isReSel;
499
    }
500

    
501
    public void setIsReSel(boolean b) {
502
        isReSel = b;
503
    }
504

    
505
    public void viewFull() throws ReadException {
506
        IFFrame[] fframes = layoutContext.getFFrameSelected();
507

    
508
        for (int i = 0; i < fframes.length; i++) {
509
            if (fframes[i] instanceof IFFrameUseFMap) {
510
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
511

    
512
                if (fframe.getMapContext() != null) {
513
                    fframe.fullExtent();
514
                }
515

    
516
                fframe.refresh();
517
            }
518
        }
519

    
520
        refresh();
521
    }
522

    
523
    public FLayoutZooms getLayoutZooms() {
524
        return layoutZooms;
525
    }
526

    
527
    public FLayoutFunctions getLayoutFunctions() {
528
        return layoutFunctions;
529
    }
530

    
531
    public LayoutContext getLayoutContext() {
532
        return layoutContext;
533
    }
534

    
535
    public void setGeometryAdapter(GeometryAdapter adapter) {
536
        geometryAdapter = adapter;
537
    }
538

    
539
        public JComponent getComponent() {
540
                return this;
541
        }
542
}