Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / FLayoutFunctions.java @ 5

History | View | Annotate | Download (8.61 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.Point;
25
import java.awt.Rectangle;
26
import java.awt.geom.AffineTransform;
27
import java.awt.geom.NoninvertibleTransformException;
28
import java.awt.geom.Point2D;
29
import java.awt.geom.Rectangle2D;
30

    
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33

    
34
import org.gvsig.app.project.documents.layout.commands.FrameCommandsRecord;
35
import org.gvsig.app.project.documents.layout.fframes.FFrameGroup;
36
import org.gvsig.app.project.documents.layout.fframes.IFFrame;
37
import org.gvsig.app.project.documents.layout.fframes.IFFrameUseFMap;
38
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
39
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
40
import org.gvsig.fmap.geom.GeometryLocator;
41
import org.gvsig.fmap.geom.GeometryManager;
42
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
43
import org.gvsig.tools.observer.Observable;
44
import org.gvsig.tools.observer.ObservableHelper;
45
import org.gvsig.tools.observer.Observer;
46

    
47
/**
48
 * Funciones utilizadas desde el Layout.
49
 * 
50
 * @author Vicente Caballero Navarro
51
 */
52
public class FLayoutFunctions implements Observable {
53

    
54
    protected static final Logger LOG = LoggerFactory
55
        .getLogger(FLayoutFunctions.class);
56
    private static final GeometryManager geomManager = GeometryLocator
57
        .getGeometryManager();
58
    private static final Logger logger = LoggerFactory
59
        .getLogger(FLayoutFunctions.class);
60
    
61
    private LayoutPanel layout = null;
62
    private ObservableHelper observers;
63
    /**
64
     * Crea un nuevo FLayoutFunctions.
65
     * 
66
     * @param l
67
     *            Referencia al Layout.
68
     */
69
    public FLayoutFunctions(LayoutPanel layoutPanel) {
70
        layout = layoutPanel;
71
        observers = new ObservableHelper();
72
        observers.addObserver(layoutPanel.getLayoutControl());
73
    }
74

    
75
    /**
76
     * Gestiona la herramienta de selecci?n sobre el Mapa.
77
     */
78
    public void setSelect() {
79
        IFFrame fframe = null;
80
        boolean isUpdate = false;
81
        layout.getLayoutContext().updateFFrames();
82
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
83
        FrameCommandsRecord efs =
84
            layout.getLayoutContext().getFrameCommandsRecord();
85
        efs.startComplex("move");
86
        for (int i = 0; i < fframes.length; i++) {
87
            fframe = fframes[i];
88

    
89
            int difx =
90
                (layout.getLayoutControl().getLastPoint().x - layout
91
                    .getLayoutControl().getFirstPoint().x);
92
            int dify =
93
                (layout.getLayoutControl().getLastPoint().y - layout
94
                    .getLayoutControl().getFirstPoint().y);
95

    
96
            if (((Math.abs(difx) > 3) || (Math.abs(dify) > 3))
97
                && (fframe.getSelected() != IFFrame.NOSELECT)) {
98
                Rectangle2D rectangle = fframe.getLastMoveRect();
99
                if (rectangle == null) {
100
                    efs.endComplex();
101
                    return;
102
                }
103
  
104
                if (fframe instanceof FFrameGroup) {
105
                    ((FFrameGroup) fframe).setAt(layout.getLayoutControl()
106
                        .getAT());
107
                }
108

    
109
                IFFrame fframeAux;
110
                try {
111
                    fframeAux = (IFFrame) fframe.clone();
112
                    fframeAux.setBoundBox(FLayoutUtilities.toSheetRect(
113
                        rectangle, layout.getLayoutControl().getAT()));
114
                    efs.update(fframe, fframeAux);
115
                    fframeAux.getBoundingBox(layout.getLayoutControl().getAT());
116

    
117
                    isUpdate = true;
118
                } catch (CloneNotSupportedException e) {
119
                    LOG.error("It is not possible clonate the object", e);
120
                }
121

    
122
            }
123
            Rectangle rect = null;
124
            if (layout.getLayoutControl().getReSel() == null) {
125
                rect = new Rectangle();
126
                rect.setFrameFromDiagonal(layout.getLayoutControl()
127
                    .getFirstPoint(), layout.getLayoutControl().getLastPoint());
128
            } else {
129
                rect = layout.getLayoutControl().getReSel();
130
            }
131
            if (layout.getLayoutControl().isReSel()
132
                && (rect.contains(fframe.getBoundingBox(layout
133
                    .getLayoutControl().getAT())))) {
134
                fframe.setSelected(true);
135
            }
136

    
137
            if (isUpdate) {
138
                observers.notifyObservers(this, 
139
                    new DefaultLayoutNotification(LayoutNotification.LAYOUT_INVALIDATED));
140
            } else {
141
                observers.notifyObservers(this, 
142
                    new DefaultLayoutNotification(LayoutNotification.LAYOUT_VALIDATED));
143
            }
144
        }
145
        efs.endComplex();
146
        layout.getLayoutContext().updateFFrames();
147
    }
148

    
149
    /**
150
     * Pan sobre la vista del FFrameView.
151
     * 
152
     * @param p1
153
     *            Punto inicial del desplazamiento.
154
     * @param p2
155
     *            Punto final del desplazamiento.
156
     */
157
    public void setViewPan(Point p1, Point p2) {
158
        IFFrame[] fframes = layout.getLayoutContext().getFFrames();
159
        for (int i = 0; i < fframes.length; i++) {
160
            if (fframes[i] instanceof IFFrameUseFMap) {
161
                IFFrameUseFMap fframe = (IFFrameUseFMap) fframes[i];
162

    
163
                if (((IFFrame) fframe).getSelected() != IFFrame.NOSELECT) {
164
                    if (fframe.getATMap() != null) {
165
                        Rectangle2D.Double r = new Rectangle2D.Double();
166
                        Rectangle2D extent =
167
                            fframe.getMapContext().getViewPort().getExtent();
168
                        if (extent != null) {
169
                            Point2D mp1 = toMapPoint(p1, fframe.getATMap());
170
                            Point2D mp2 = toMapPoint(p2, fframe.getATMap());
171
                            double x =
172
                                extent.getX() - (mp2.getX() - mp1.getX());
173
                            double y =
174
                                extent.getY() - (mp2.getY() - mp1.getY());
175
                            double width = extent.getWidth();
176
                            double height = extent.getHeight();
177
                            try {
178
                                fframe
179
                                    .getMapContext()
180
                                    .getViewPort()
181
                                    .setEnvelope(
182
                                        geomManager.createEnvelope(x, y, x
183
                                            + width, y + height,
184
                                            SUBTYPES.GEOM2D));
185
                            } catch (CreateEnvelopeException e) {
186
                                logger.error("Error creating the envelope", e);
187
                            }
188
                            fframe.refresh();
189
                            fframe.refreshOriginalExtent();
190
                        }
191
                    }
192
                }
193
            }
194
        }
195
    }
196

    
197
    /**
198
     * Devuelve un punto real a partir de un punto en pixels sobre la vista.
199
     * 
200
     * @param pScreen
201
     *            Punto en pixels.
202
     * @param at1
203
     *            Matriz de transformaci?n.
204
     * 
205
     * @return Punto real.
206
     */
207
    public static Point2D.Double toMapPoint(Point2D pScreen, AffineTransform at1) {
208
        Point2D.Double pWorld = new Point2D.Double();
209

    
210
        AffineTransform at;
211

    
212
        try {
213
            at = at1.createInverse();
214
            at.transform(pScreen, pWorld);
215
        } catch (NoninvertibleTransformException e) {
216
            // throw new RuntimeException(e);
217
        }
218

    
219
        return pWorld;
220
    }
221

    
222
    public void addObserver(Observer o) {
223
        observers.addObserver(o);        
224
    }
225

    
226
    public void deleteObserver(Observer o) {
227
      observers.deleteObserver(o);        
228
    }
229

    
230
    public void deleteObservers() {
231
       observers.deleteObservers();        
232
    }
233
}