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 @ 40560
History | View | Annotate | Download (5.41 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 inside
|
48 |
* the image, draws the outline of the image and so on.
|
49 |
*
|
50 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
51 |
*/
|
52 |
public class ImageStyle extends BackgroundFileStyle { |
53 |
public static final String IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME = "ImageStyle"; |
54 |
private static final String SOURCE = "source"; |
55 |
|
56 |
private BufferedImage img; |
57 |
/**
|
58 |
* Creates a rectangle with the dimensions of the buffered image
|
59 |
* @return Rectangle
|
60 |
*/
|
61 |
public Rectangle getBounds() { |
62 |
if (img == null) return new Rectangle(); |
63 |
return new Rectangle(new Dimension(img.getWidth(), img.getHeight())); |
64 |
} |
65 |
/**
|
66 |
* Defines the source (file) from where the buffered image will be taken.
|
67 |
* @param f,File
|
68 |
*/
|
69 |
public void setSource(URL url) throws IOException { |
70 |
source = url; |
71 |
img = ImageIO.read(url);
|
72 |
} |
73 |
|
74 |
public void drawInsideRectangle(Graphics2D g, Rectangle r, boolean keepAspectRatio) { |
75 |
if (img != null) { |
76 |
|
77 |
double xOffset = 0; |
78 |
double yOffset = 0; |
79 |
double xScale = 1; |
80 |
double yScale = 1; |
81 |
if (keepAspectRatio) {
|
82 |
double scale;
|
83 |
if (img.getWidth()>img.getHeight()) {
|
84 |
scale = r.getWidth()/img.getWidth(); |
85 |
yOffset = 0.5*(r.getHeight() - img.getHeight()*scale);
|
86 |
} else {
|
87 |
scale = r.getHeight()/img.getHeight(); |
88 |
xOffset = 0.5*(r.getWidth() - img.getWidth()*scale);
|
89 |
} |
90 |
xScale = yScale = scale; |
91 |
|
92 |
} else {
|
93 |
xScale = r.getWidth()/img.getWidth(); |
94 |
yScale = r.getHeight()/img.getHeight(); |
95 |
yOffset = img.getHeight()*0.5*yScale ;
|
96 |
|
97 |
} |
98 |
|
99 |
|
100 |
AffineTransform at = AffineTransform.getTranslateInstance(xOffset, yOffset); |
101 |
at.concatenate(AffineTransform.getScaleInstance(xScale, yScale));
|
102 |
g.drawRenderedImage(img, at); |
103 |
} |
104 |
} |
105 |
|
106 |
public boolean isSuitableFor(ISymbol symbol) { |
107 |
// TODO Implement it
|
108 |
throw new Error("Not yet implemented!"); |
109 |
|
110 |
} |
111 |
|
112 |
public void drawOutline(Graphics2D g, Rectangle r) throws SymbolDrawingException { |
113 |
drawInsideRectangle(g, r); |
114 |
} |
115 |
|
116 |
public void loadFromState(PersistentState state) |
117 |
throws PersistenceException {
|
118 |
try {
|
119 |
String sourceSymbolInLibrary = state.getString(SOURCE_SYMBOL_IN_LIBRARY);
|
120 |
if (sourceSymbolInLibrary != null){ |
121 |
setSource(new URL(getSymbolLibraryURL().toString()+sourceSymbolInLibrary)); |
122 |
} else {
|
123 |
setSource(state.getURL(SOURCE)); |
124 |
} |
125 |
} catch (Exception e) { |
126 |
throw new PersistenceCantSetSourceException(e); |
127 |
} |
128 |
} |
129 |
|
130 |
public void saveToState(PersistentState state) throws PersistenceException { |
131 |
if (isLibrarySymbol()){
|
132 |
state.set(SOURCE_SYMBOL_IN_LIBRARY, getSourceSymbolInLibrary()); |
133 |
} else {
|
134 |
state.setNull(SOURCE_SYMBOL_IN_LIBRARY); |
135 |
} |
136 |
state.set(SOURCE, source); |
137 |
} |
138 |
|
139 |
public static class RegisterPersistence implements Callable { |
140 |
|
141 |
public Object call() throws Exception { |
142 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
143 |
if( manager.getDefinition(IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME)==null ) { |
144 |
DynStruct definition = manager.addDefinition( |
145 |
ImageStyle.class, |
146 |
IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME, |
147 |
IMAGE_STYLE_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
148 |
null,
|
149 |
null
|
150 |
); |
151 |
|
152 |
// Extend the Style base definition
|
153 |
definition.extend(manager.getDefinition(BACKGROUND_FILE_STYLE_PERSISTENCE_DEFINITION_NAME)); |
154 |
|
155 |
definition.addDynFieldURL(SOURCE).setMandatory(true);
|
156 |
definition.addDynFieldString(SOURCE_SYMBOL_IN_LIBRARY).setMandatory(false);
|
157 |
} |
158 |
return Boolean.TRUE; |
159 |
} |
160 |
} |
161 |
} |
162 |
|
163 |
|