Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / fframes / FFrameOverView.java @ 760

History | View | Annotate | Download (9.52 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.fframes;
23

    
24
import java.awt.Color;
25
import java.awt.Dimension;
26
import java.awt.Graphics;
27
import java.awt.Graphics2D;
28
import java.awt.Image;
29
import java.awt.Point;
30
import java.awt.geom.AffineTransform;
31
import java.awt.geom.Line2D;
32
import java.awt.geom.Rectangle2D;
33
import java.awt.image.BufferedImage;
34

    
35
import javax.swing.JOptionPane;
36

    
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39
import org.gvsig.andami.PluginServices;
40
import org.gvsig.andami.messages.NotificationManager;
41
import org.gvsig.app.ApplicationLocator;
42
import org.gvsig.app.project.documents.view.ViewDocument;
43
import org.gvsig.compat.print.PrintAttributes;
44
import org.gvsig.fmap.dal.exception.ReadException;
45
import org.gvsig.fmap.geom.primitive.Envelope;
46
import org.gvsig.fmap.mapcontext.MapContext;
47
import org.gvsig.fmap.mapcontext.MapContextException;
48
import org.gvsig.fmap.mapcontext.ViewPort;
49
import org.gvsig.tools.ToolsLocator;
50
import org.gvsig.tools.dynobject.DynStruct;
51
import org.gvsig.tools.persistence.PersistenceManager;
52
import org.gvsig.tools.persistence.PersistentState;
53
import org.gvsig.tools.persistence.exception.PersistenceException;
54

    
55
/**
56
 * FFrame para introducir el localizador de una vista en el Layout.
57
 * 
58
 * @author Vicente Caballero Navarro
59
 */
60
public class FFrameOverView extends FFrameView implements IFFrameViewDependence {
61

    
62
    private static final Logger logger = LoggerFactory
63
        .getLogger(FFrameOverView.class);
64
    
65
    public static final String PERSISTENCE_DEFINITION_NAME = "FFrameOverView";
66

    
67
    private static final String FFRAMEVIEWDEPENDENCE_FIELD =
68
        "fframeViewDependence";
69

    
70
    private MapContext assoc_map;
71
    private Envelope extent;
72
    private FFrameView fframeViewDependence;
73

    
74
    protected void createImage(AffineTransform affineTransform,
75
                    int width, int height, Point mapOrigin) throws ReadException, MapContextException {
76
            // FIXME: we create the image on every redraw instead of implementing some optimizations
77
            // The overview should not be heavy to paint, but we could optimize the process by setting
78
            // the listeners to the right mapcontext, the image would be automaticall invalidated when
79
            // needed.
80
            m_image = null;
81
            
82
            super.createImage(affineTransform, width, height, mapOrigin);
83
            Graphics2D g = (Graphics2D) m_image.createGraphics();
84
            Dimension size = getMapContext().getViewPort().getImageSize();
85
            Rectangle2D r = new Rectangle2D.Double(0, 0, size.getWidth(), size.getHeight());
86
            drawAreaOfInterestBox(g, affineTransform, r, null);
87
            g.dispose();
88
    }
89

    
90
    protected void drawAreaOfInterestBox(
91
                    Graphics2D g,
92
                    AffineTransform at,
93
                    Rectangle2D clip,
94
                    PrintAttributes printAttributes) {
95

    
96
            if ((assoc_map != null)
97
                            && (assoc_map.getViewPort().getEnvelope() != null)
98
                            && (getMapContext().getViewPort().getEnvelope() != null)) {
99

    
100
                    ViewPort vp = getMapContext().getViewPort();
101
                    ViewPort vpOrig = assoc_map.getViewPort();
102

    
103
                    if (extent == null) {
104
                            extent = vpOrig.getAdjustedEnvelope();
105
                    }
106
                    
107
                    Image ov_img = new BufferedImage(
108
                                    vp.getImageWidth(),
109
                                    vp.getImageHeight(),
110
                                    BufferedImage.TYPE_INT_ARGB);
111
                    Graphics ov_img_gra = ov_img.getGraphics();
112

    
113
                    /*
114
                     * Fill with transparent color
115
                     */
116
                     ov_img_gra.setColor(new Color(0, 0, 0, 0));
117
                    ov_img_gra.fillRect(0, 0,
118
                                    vp.getImageWidth(), vp.getImageHeight());
119

    
120
                    /*
121
                     * Area of interest in pixels, inside ov_img image
122
                     */
123

    
124
                    Rectangle2D aux =
125
                                    new Rectangle2D.Double(extent.getMinimum(0),
126
                                                    extent.getMinimum(1), extent.getLength(0),
127
                                                    extent.getLength(1));
128
                    Rectangle2D extentPixels = vp.fromMapRectangle(aux);
129

    
130
                    /*
131
                     * Fill (gray) and perimeter (red)
132
                     */
133
                    ov_img_gra.setColor(new Color(100, 100, 100, 100));
134
                    ov_img_gra.fillRect((int) extentPixels.getMinX(),
135
                                    (int) extentPixels.getMinY(),
136
                                    (int) extentPixels.getWidth(),
137
                                    (int) extentPixels.getHeight());
138
                    ov_img_gra.setColor(Color.red);
139
                    ov_img_gra.drawRect((int) extentPixels.getMinX(),
140
                                    (int) extentPixels.getMinY(),
141
                                    (int) extentPixels.getWidth(),
142
                                    (int) extentPixels.getHeight());
143

    
144
                    int aoi_top = (int) extentPixels.getMinY();
145
                    int aoi_bottom = (int) extentPixels.getMaxY();
146
                    int aoi_left = (int) extentPixels.getMinX();
147
                    int aoi_right = (int) extentPixels.getMaxX();
148
                    /*
149
                     * Draw vertical, horizontal lines
150
                     */
151
                    ov_img_gra.setColor(Color.darkGray);
152
                    int cx = (int) extentPixels.getCenterX(); 
153
                    int cy = (int) extentPixels.getCenterY();
154
                    int maxx = vp.getImageWidth();
155
                    int maxy = vp.getImageHeight();
156

    
157
                    // vertical
158
                    ov_img_gra.drawLine(cx, 0, cx, aoi_top);
159
                    ov_img_gra.drawLine(cx, aoi_bottom, cx, maxy);
160
                    // horiz
161
                    ov_img_gra.drawLine(0, cy, aoi_left, cy);
162
                    ov_img_gra.drawLine(aoi_right, cy, maxx, cy);
163

    
164
                    /*
165
                     * Semi-transparent image is ready
166
                     */
167

    
168
                    /*
169
                     * Save clip area
170
                     */
171
                    Rectangle2D origClip = g.getClipBounds();
172
                    g.setClip((int) clip.getX(), (int) clip.getY(),
173
                                    (int) clip.getWidth(), (int) clip.getHeight());
174

    
175
                    /*
176
                     * Draw image
177
                     */
178
                    g.drawImage(ov_img, (int)clip.getX(), (int)clip.getY(), null);
179
                    ov_img_gra.dispose();
180

    
181
                    /*
182
                     * Restore clip area
183
                     */
184
                    if (origClip!=null) {
185
                    g.setClip((int) origClip.getX(), (int) origClip.getY(),
186
                                    (int) origClip.getWidth(), (int) origClip.getHeight());
187
                    }
188
                    else {
189
                            g.setClip(null);
190
                    }
191
                    extent = null;
192
            }
193
    }
194
    
195
    protected void print(Graphics2D g, AffineTransform at, PrintAttributes printAttributes) {        
196
        super.print(g, at, printAttributes);
197
                Rectangle2D r = getBoundingBox(at);
198
                if (getMapContext()!=null && getMapContext().getViewPort()!=null) {
199
                        ViewPort vp = getMapContext().getViewPort();
200
                        Dimension oldSize = vp.getImageSize();
201
                        vp.setImageSize(new Dimension((int)r.getWidth(), (int) r.getHeight()));
202
                        drawAreaOfInterestBox(g, at, r, printAttributes);
203
                        vp.setImageSize(oldSize);
204
                }
205
    }
206
    
207
    public void setView(ViewDocument v) {
208
            this.viewDocument = v;
209
        if (fframeViewDependence == null)
210
            return;
211
        assoc_map = fframeViewDependence.getMapContext();
212
        if (v!=null) {
213
                setViewMapContext(v.getMapOverViewContext());
214
        }
215
    }
216
    
217
    protected void setListeners() {
218
            // we don't need any listener for FFrameOverview
219
    }
220

    
221

    
222
    /**
223
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getNameFFrame()
224
     */
225
    public String getNameFFrame() {
226
        return PluginServices.getText(this, "Localizador") + num;
227
    }
228

    
229
    public String getName() {
230
        return PERSISTENCE_DEFINITION_NAME;
231
    }
232

    
233
    @Override
234
    public FFrameOverView clone() throws CloneNotSupportedException {
235
        FFrameOverView frame = (FFrameOverView) super.clone();
236
        frame.setSelected(this.isSelected());
237
        return frame;
238
    }
239

    
240
    public void setFFrameDependence(IFFrame f) {
241
        fframeViewDependence = (FFrameView) f;
242
    }
243

    
244
    public IFFrame[] getFFrameDependence() {
245
        return new IFFrame[] { fframeViewDependence };
246
    }
247

    
248
    public void refreshDependence(IFFrame fant, IFFrame fnew) {
249
        if ((fframeViewDependence != null) && fframeViewDependence.equals(fant)
250
                        && fnew instanceof FFrameView) {
251
                FFrameView newFFView = (FFrameView) fnew;
252
                setView(newFFView.getView());
253
        }
254
    }
255
 
256
    public static void registerPersistent() {
257
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
258
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
259
            DynStruct definition =
260
                manager.addDefinition(FFrameOverView.class,
261
                    PERSISTENCE_DEFINITION_NAME,
262
                    "FFrameOverView persistence definition", null, null);
263

    
264
            definition.extend(manager
265
                .getDefinition(FFrameView.PERSISTENCE_DEFINITION_NAME));
266

    
267
            definition.addDynFieldObject(FFRAMEVIEWDEPENDENCE_FIELD)
268
                .setClassOfValue(FFrameView.class);
269
        }
270
    }
271

    
272
    @Override
273
    public void loadFromState(PersistentState state)
274
        throws PersistenceException {
275
        super.loadFromState(state);
276
        fframeViewDependence =
277
            (FFrameView) state.get(FFRAMEVIEWDEPENDENCE_FIELD);
278
        
279
        /*
280
         * force refreshment of other fields
281
         */
282
        this.setView(viewDocument);
283
    }
284

    
285
    @Override
286
    public void saveToState(PersistentState state) throws PersistenceException {
287
        super.saveToState(state);
288
        state.set(FFRAMEVIEWDEPENDENCE_FIELD, fframeViewDependence);
289
    }
290
}