Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / style / ImageStyle.java @ 45524

History | View | Annotate | Download (6.11 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style;
25

    
26
import java.awt.Dimension;
27
import java.awt.Graphics2D;
28
import java.awt.Rectangle;
29
import java.awt.geom.AffineTransform;
30
import java.awt.image.BufferedImage;
31
import java.io.IOException;
32
import java.net.URL;
33

    
34
import javax.imageio.ImageIO;
35

    
36
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
37
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
38
import org.gvsig.tools.ToolsLocator;
39
import org.gvsig.tools.dynobject.DynStruct;
40
import org.gvsig.tools.persistence.PersistenceManager;
41
import org.gvsig.tools.persistence.PersistentState;
42
import org.gvsig.tools.persistence.exception.PersistenceException;
43
import org.gvsig.tools.util.Callable;
44

    
45
/**
46
 * Controls the style of an image to be correctly painted. This class controls
47
 * aspects like the source path of the image, creates a rectangle to paint
48
 * inside the image, draws the outline of the image and so on.
49
 *
50
 * @author gvSIG Team
51
 */
52
@SuppressWarnings("UseSpecificCatch")
53
public class ImageStyle extends BackgroundFileStyle {
54

    
55
    public static final String IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME = "ImageStyle";
56
    private static final String SOURCE = "source";
57

    
58
    private BufferedImage img;
59

    
60
    /**
61
     * Creates a rectangle with the dimensions of the buffered image
62
     *
63
     * @return Rectangle
64
     */
65
    @Override
66
    public Rectangle getBounds() {
67
        if (img == null) {
68
            return new Rectangle();
69
        }
70
        return new Rectangle(new Dimension(img.getWidth(), img.getHeight()));
71
    }
72

    
73
    /**
74
     * Defines the source (file) from where the buffered image will be taken.
75
     *
76
     * @param url
77
     */
78
    @Override
79
    public void setSource(URL url) throws IOException {
80
        source = url;
81
        img = ImageIO.read(url);
82
    }
83

    
84
    @Override
85
    public void drawInsideRectangle(Graphics2D g, Rectangle r, boolean keepAspectRatio) {
86
        if (img != null) {
87

    
88
            double xOffset = 0;
89
            double yOffset = 0;
90
            @SuppressWarnings("UnusedAssignment")
91
            double xScale = 1;
92
            @SuppressWarnings("UnusedAssignment")
93
            double yScale = 1;
94
            if (keepAspectRatio) {
95
                double scale;
96
                if (img.getWidth() > img.getHeight()) {
97
                    scale = r.getWidth() / img.getWidth();
98
                    yOffset = 0.5 * (r.getHeight() - img.getHeight() * scale);
99
                } else {
100
                    scale = r.getHeight() / img.getHeight();
101
                    xOffset = 0.5 * (r.getWidth() - img.getWidth() * scale);
102
                }
103
                xScale = yScale = scale;
104

    
105
            } else {
106
                xScale = r.getWidth() / img.getWidth();
107
                yScale = r.getHeight() / img.getHeight();
108
                yOffset = img.getHeight() * 0.5 * yScale;
109

    
110
            }
111

    
112
            AffineTransform at = AffineTransform.getTranslateInstance(xOffset, yOffset);
113
            at.concatenate(AffineTransform.getScaleInstance(xScale, yScale));
114
            g.drawRenderedImage(img, at);
115
        }
116
    }
117

    
118
    @Override
119
    public boolean isSuitableFor(ISymbol symbol) {
120
        // TODO Implement it
121
        throw new Error("Not yet implemented!");
122

    
123
    }
124

    
125
    @Override
126
    public void drawOutline(Graphics2D g, Rectangle r) throws SymbolDrawingException {
127
        drawInsideRectangle(g, r);
128
    }
129

    
130
    @Override
131
    public void loadFromState(PersistentState state)
132
            throws PersistenceException {
133
        try {
134
            String sourceSymbolInLibrary = state.getString(SOURCE_SYMBOL_IN_LIBRARY);
135
            if (sourceSymbolInLibrary != null) {
136
                setSource(new URL(getSymbolLibraryURL().toString() + sourceSymbolInLibrary));
137
            } else {
138
                setSource(state.getURL(SOURCE));
139
            }
140
        } catch (Exception e) {
141
            throw new PersistenceCantSetSourceException(e);
142
        }
143
    }
144

    
145
    @Override
146
    public void saveToState(PersistentState state) throws PersistenceException {
147
        if (isLibrarySymbol()) {
148
            state.set(SOURCE_SYMBOL_IN_LIBRARY, getSourceSymbolInLibrary());
149
        } else {
150
            state.setNull(SOURCE_SYMBOL_IN_LIBRARY);
151
        }
152
        state.set(SOURCE, source);
153
    }
154

    
155
    public static class RegisterPersistence implements Callable {
156

    
157
        @Override
158
        public Object call() throws Exception {
159
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
160
            if (manager.getDefinition(IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME) == null) {
161
                DynStruct definition = manager.addDefinition(
162
                        ImageStyle.class,
163
                        IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME,
164
                        IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
165
                        null,
166
                        null
167
                );
168

    
169
                // Extend the Style base definition
170
                definition.extend(manager.getDefinition(BACKGROUND_FILE_STYLE_PERSISTENCE_DEFINITION_NAME));
171

    
172
                definition.addDynFieldURL(SOURCE).setMandatory(true);
173
                definition.addDynFieldString(SOURCE_SYMBOL_IN_LIBRARY).setMandatory(false);
174
            }
175
            return Boolean.TRUE;
176
        }
177
    }
178
}