Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / layout / mapbox / fframe / FFrameMapBox.java @ 1714

History | View | Annotate | Download (9.48 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.layout.mapbox.fframe;
23

    
24
import java.awt.BasicStroke;
25
import java.awt.Color;
26
import java.awt.Graphics2D;
27
import java.awt.geom.AffineTransform;
28
import java.awt.geom.Point2D;
29
import java.awt.geom.Rectangle2D;
30
import java.awt.image.BufferedImage;
31
import java.util.Collections;
32
import java.util.List;
33
import org.apache.commons.math.util.MathUtils;
34
import org.gvsig.andami.PluginServices;
35
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
36
import org.gvsig.app.project.documents.layout.fframes.*;
37
import org.gvsig.compat.print.PrintAttributes;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
40
import org.gvsig.layout.mapbox.model.Cell;
41
import org.gvsig.layout.mapbox.model.Dimension2DDouble;
42
import org.gvsig.layout.mapbox.model.MapBoxModel;
43
import org.gvsig.layout.mapbox.model.SelectionCell;
44
import org.gvsig.layout.mapbox.model.SelectionCellImpl;
45
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.dynobject.DynStruct;
47
import org.gvsig.tools.lang.CloneableUtils;
48
import org.gvsig.tools.persistence.PersistenceManager;
49
import org.gvsig.tools.persistence.PersistentState;
50
import org.gvsig.tools.persistence.exception.PersistenceException;
51

    
52
/**
53
 * FFrame para introducir una imagen en el Layout o para dibujar sobre el
54
 * graphics un SVG.
55
 * 
56
 * @author gvSIG Team
57
 */
58
public class FFrameMapBox extends FFrame {
59

    
60
    public static final String PERSISTENCE_DEFINITION_NAME = "FFrameMapBox";
61
    public static final double PIXELS_TOLERANCE = 2.0;
62
    public static final double MAPBOX_EPSILON = 0.001;
63
    
64
    
65
    private MapBoxModel model;
66
    private SelectionCell selection;
67

    
68
    /**
69
     * Creates a new FFrameMapBox object.
70
     */
71
    public FFrameMapBox() {
72
        this.selection = new SelectionCellImpl();
73
    }
74

    
75
    public void setModel(MapBoxModel model) {
76
        this.model = model;
77
    }
78

    
79
    public MapBoxModel getModel() {
80
        return this.model;
81
    }
82

    
83
    public SelectionCell getSelection() {
84
        return this.selection;
85
    }
86

    
87
    public List<Cell> getSelectedCells() {
88
        Rectangle2D r = ((SelectionCellImpl)selection).getRectangle();
89
        if(r == null){
90
            return Collections.EMPTY_LIST;
91
        }
92
        return this.model.getCells(r);
93
    }
94
    
95

    
96
    /**
97
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
98
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
99
     * de dibujar.
100
     * 
101
     * @param g
102
     *            Graphics
103
     * @param at
104
     *            Transformada afin.
105
     * @param rv
106
     *            rect?ngulo sobre el que hacer un clip.
107
     * @param imgBase
108
     *            Imagen para acelerar el dibujado.
109
     */
110
    @Override
111
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
112
        BufferedImage imgBase) {
113
        draw(g, at);
114
    }
115
    
116
    public void draw(Graphics2D g, AffineTransform at) {
117
        if(this.model == null) {
118
            return;
119
        }
120
        Rectangle2D.Double r = getBoundingBox(at);
121
        g.rotate(
122
            Math.toRadians(getRotation()), 
123
            r.x + (r.width / 2), 
124
            r.y + (r.height / 2)
125
        );
126

    
127
        g.setColor(Color.BLACK);
128
        List<Cell> selectedCells = this.getSelectedCells();
129
        for (Cell cell : this.model.getCells()) {
130
            drawCell(g, cell, getBoundBox(), selectedCells.contains(cell));
131
        }
132
        for (Cell cell : selectedCells) {
133
            drawCell(g, cell, getBoundBox(), true);
134
        }
135
        drawFrame(g, r);
136
        
137
        g.rotate(Math.toRadians(-getRotation()), r.x + (r.width / 2), r.y
138
            + (r.height / 2));
139
    }
140

    
141
    public void drawCell(Graphics2D g, Cell cell, Rectangle2D rect, boolean selected) {
142
        Rectangle2D cellRect = cell.getRectangle();
143
        Rectangle2D r2 = new Rectangle2D.Double(
144
            (cellRect.getX()+rect.getX()), 
145
            (cellRect.getY()+rect.getY()), 
146
            cellRect.getWidth(), 
147
            cellRect.getHeight());
148
        Rectangle2D r = FLayoutUtilities.fromSheetRect(r2, lastAT);
149
        if(selected){
150
            g.setColor(Color.yellow.brighter());
151
            g.fillRect((int) (r.getX()), (int) (r.getY()), (int) r.getWidth(), (int) r.getHeight());
152
            g.setColor(Color.yellow.darker());
153
        } else {
154
            g.setColor(Color.black);
155
        }
156
        g.setStroke(new BasicStroke(2));
157
        g.drawRect((int) (r.getX()), (int) (r.getY()), (int) r.getWidth(), (int) r.getHeight());
158
        g.setColor(Color.black);
159

    
160
    }
161

    
162
    private void drawFrame(Graphics2D g, Rectangle2D r) {
163
        g.setColor(Color.black);
164
        g.setStroke(new BasicStroke(2));
165
        g.drawRect((int) (r.getX()), (int) (r.getY()), (int) r.getWidth(), (int) r.getHeight());
166
        g.setColor(Color.black);
167

    
168
    }
169

    
170
    /**
171
     * @see org.gvsig.app.project.documents.layout.fframes.IFFrame#getNameFFrame()
172
     */
173
    @Override
174
    public String getNameFFrame() {
175
        return PluginServices.getText(this, "_Map_box") + num;
176
    }
177

    
178
    @Override
179
    public String getName() {
180
        return PERSISTENCE_DEFINITION_NAME;
181
    }
182

    
183
    public void initialize() {
184
        // TODO Auto-generated method stub
185

    
186
    }
187

    
188
    public void cloneActions(IFFrame frame) {
189
    }
190

    
191
    @Override
192
    public void print(Graphics2D g, AffineTransform at, Geometry geom,
193
        PrintAttributes properties) {
194
        draw(g, at, null, null);
195
    }
196

    
197
    public static void registerPersistent() {
198
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
199
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
200
            DynStruct definition =
201
                manager.addDefinition(FFrameMapBox.class,
202
                    PERSISTENCE_DEFINITION_NAME,
203
                    "FFrameMapBox persistence definition", null, null);
204

    
205
            definition.extend(manager
206
                .getDefinition(FFrame.PERSISTENCE_DEFINITION_NAME));
207

    
208
            definition.addDynFieldObject("model").setClassOfValue(MapBoxModel.class).setMandatory(true);
209
            definition.addDynFieldObject("selection").setClassOfValue(SelectionCell.class).setDefaultFieldValue(null).setMandatory(false);
210
        }
211
    }
212

    
213
    @Override
214
    public void loadFromState(PersistentState state)
215
        throws PersistenceException {
216
        super.loadFromState(state);
217
        model = (MapBoxModel) state.get("model");
218
        selection = (SelectionCell) state.get("selection");
219
        if(selection == null){
220
            this.selection = new SelectionCellImpl();
221
        }
222
    }
223

    
224
    @Override
225
    public void saveToState(PersistentState state) throws PersistenceException {
226
        super.saveToState(state);
227
        state.set("model", model);
228
        state.set("selection", selection);
229
    }
230
    
231
    @Override
232
    public Object clone() throws CloneNotSupportedException {
233
        FFrameMapBox x = (FFrameMapBox) super.clone();
234
        
235
        x.model = (MapBoxModel) CloneableUtils.cloneQuietly(this.model);
236
        x.selection = (SelectionCell) CloneableUtils.cloneQuietly(this.selection);
237
        return x;
238
    }
239

    
240
    @Override
241
    public void setBoundBox(Rectangle2D r) {
242
        if(model != null) {
243
            model.resize(new Dimension2DDouble(r.getWidth(), r.getHeight()));
244
        }
245
        super.setBoundBox(r);
246
    }
247

    
248
    public void moveVerticalLine(double x0, double x1){
249
        model.moveVerticalLine(x0, x1);
250
        if (MathUtils.equals(x0, 0d, MAPBOX_EPSILON)) {
251
            this.m_BoundBox.x += x1;
252
            this.m_BoundBox.width -= (x1-x0);
253
        }
254
        if(MathUtils.equals(x0, this.m_BoundBox.width, MAPBOX_EPSILON)) {
255
            this.m_BoundBox.width = x1;
256
        }
257
    }
258

    
259
    public void moveHorizontalLine(double y0, double y1){
260
        model.moveHorizontalLine(y0, y1);
261
        if (MathUtils.equals(y0, 0d, MAPBOX_EPSILON)) {
262
            this.m_BoundBox.y += y1;
263
            this.m_BoundBox.height -= (y1-y0);
264
        }
265
        if(MathUtils.equals(y0, this.m_BoundBox.height, MAPBOX_EPSILON)) {
266
            this.m_BoundBox.height = y1;
267
        }
268
    }
269

    
270
    public void click(PointEvent event) {
271
        
272
    }
273

    
274
    public void drag(Point2D.Double toSheetPoint) {
275
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
276
    }
277

    
278
    public Point2D.Double fromSheetPointToMapBoxPoint(Point2D.Double sheetPoint) {
279
        return new Point2D.Double(
280
            sheetPoint.x - this.m_BoundBox.x,
281
            sheetPoint.y - this.m_BoundBox.y
282
        );
283
    }
284

    
285
    public Point2D.Double fromMapBoxPointToSheetPoint(Point2D.Double mapBoxPoint) {
286
        return new Point2D.Double(
287
            this.m_BoundBox.x + mapBoxPoint.x,
288
            this.m_BoundBox.y + mapBoxPoint.y
289
        );
290
    }
291

    
292
}