svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / SymbolPreviewer.java @ 45527
History | View | Annotate | Download (8.17 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.app.gui.styling; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.Font; |
28 |
import java.awt.FontMetrics; |
29 |
import java.awt.Graphics; |
30 |
import java.awt.Graphics2D; |
31 |
import java.awt.Rectangle; |
32 |
import java.awt.RenderingHints; |
33 |
import java.awt.event.MouseListener; |
34 |
import java.awt.event.MouseMotionListener; |
35 |
import java.awt.geom.AffineTransform; |
36 |
|
37 |
import javax.swing.JPanel; |
38 |
import org.apache.commons.lang3.mutable.MutableObject; |
39 |
import org.gvsig.fmap.dal.feature.Feature; |
40 |
|
41 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
42 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
43 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol_v2; |
44 |
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol; |
45 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
46 |
import org.gvsig.i18n.Messages; |
47 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol; |
48 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol_v2; |
49 |
import org.slf4j.Logger; |
50 |
import org.slf4j.LoggerFactory; |
51 |
|
52 |
/**
|
53 |
* SymbolPreviewer creates a JPanel used for the user to watch the preview of a
|
54 |
* symbol.It has an square form with a white background and the symbol is
|
55 |
* inserted in the middle.
|
56 |
*
|
57 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
58 |
*
|
59 |
*/
|
60 |
public class SymbolPreviewer extends JPanel { |
61 |
|
62 |
private static final Logger LOGGER = LoggerFactory.getLogger(SymbolPreviewer.class); |
63 |
/**
|
64 |
*
|
65 |
*/
|
66 |
private static final long serialVersionUID = 8095930506630873031L; |
67 |
private int hGap = 3, vGap = 3; |
68 |
private ISymbol symbol;
|
69 |
private EditorTool prevTool;
|
70 |
private boolean useAllRoom = false; |
71 |
private MutableObject<Feature> sampleFeature;
|
72 |
|
73 |
/**
|
74 |
* constructor method
|
75 |
*
|
76 |
* @param sampleFeature
|
77 |
*/
|
78 |
public SymbolPreviewer(MutableObject<Feature> sampleFeature) {
|
79 |
super(true); |
80 |
setBackground(Color.WHITE);
|
81 |
this.sampleFeature = sampleFeature;
|
82 |
} |
83 |
|
84 |
public SymbolPreviewer(MutableObject<Feature> sampleFeature, boolean all_room) { |
85 |
this(sampleFeature);
|
86 |
useAllRoom = all_room; |
87 |
} |
88 |
|
89 |
public SymbolPreviewer(boolean all_room) { |
90 |
this(new MutableObject<>(null), all_room); |
91 |
} |
92 |
|
93 |
public SymbolPreviewer() {
|
94 |
this(new MutableObject<>(null)); |
95 |
} |
96 |
|
97 |
/**
|
98 |
* Returns the symbol printed inside
|
99 |
*
|
100 |
* @return symbol
|
101 |
*/
|
102 |
public ISymbol getSymbol() {
|
103 |
return symbol;
|
104 |
} |
105 |
|
106 |
/**
|
107 |
* Establishes the symbol to be showed in the symbolpreviewer panel
|
108 |
*
|
109 |
* @param symbol
|
110 |
*/
|
111 |
public void setSymbol(ISymbol symbol) { |
112 |
this.symbol = symbol;
|
113 |
repaint(); |
114 |
} |
115 |
|
116 |
/**
|
117 |
* Draws the symbol in the middle of the pane in order to create a preview
|
118 |
* of the final one.
|
119 |
*/
|
120 |
public void paint(Graphics g) { |
121 |
try {
|
122 |
super.paint(g);
|
123 |
Graphics2D g2 = (Graphics2D) g; |
124 |
RenderingHints old = g2.getRenderingHints();
|
125 |
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
126 |
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); |
127 |
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
128 |
|
129 |
g2.translate(hGap, vGap); |
130 |
Rectangle r = getBounds();
|
131 |
r = new Rectangle( |
132 |
0,
|
133 |
0,
|
134 |
(int) (r.getWidth() - (hGap * 2)), |
135 |
(int) (r.getHeight() - (vGap * 2)) |
136 |
); |
137 |
|
138 |
Double forcesize = this.forceSize(symbol); |
139 |
if (forcesize != null && !useAllRoom) { |
140 |
long difx = Math.round(0.5 * (r.getWidth() - forcesize)); |
141 |
long dify = Math.round(0.5 * (r.getHeight() - forcesize)); |
142 |
|
143 |
r = new Rectangle((int) difx, (int) dify, |
144 |
(int) forcesize.doubleValue(),
|
145 |
(int) forcesize.doubleValue());
|
146 |
} |
147 |
|
148 |
if (symbol != null) { |
149 |
try {
|
150 |
if (symbol instanceof ISymbol_v2) { |
151 |
((ISymbol_v2) this.symbol).setFeature(sampleFeature.getValue());
|
152 |
} |
153 |
symbol.drawInsideRectangle(g2, new AffineTransform(), r, null); |
154 |
} catch (SymbolDrawingException e) {
|
155 |
if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
|
156 |
try {
|
157 |
IWarningSymbol warnsym = MapContextLocator.getSymbolManager().getWarningSymbol( |
158 |
SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS, |
159 |
"",
|
160 |
SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS |
161 |
); |
162 |
warnsym.drawInsideRectangle(g2, null, r, null); |
163 |
} catch (SymbolDrawingException e1) {
|
164 |
// IMPOSSIBLE TO REACH THIS
|
165 |
} |
166 |
} else {
|
167 |
// should be unreachable code
|
168 |
throw new RuntimeException(Messages.getText("symbol_shapetype_mismatch")); |
169 |
} |
170 |
} |
171 |
} else {
|
172 |
String noneSelected = "[" + Messages.getText("preview_not_available") + "]"; |
173 |
FontMetrics fm = g2.getFontMetrics();
|
174 |
int lineWidth = fm.stringWidth(noneSelected);
|
175 |
float scale = (float) r.getWidth() / lineWidth; |
176 |
Font f = g2.getFont();
|
177 |
float fontSize = f.getSize() * scale;
|
178 |
g2.setFont(f.deriveFont(fontSize)); |
179 |
|
180 |
g2.drawString(noneSelected, (r.x * scale) - (hGap / 2), r.height / 2 + vGap * scale); |
181 |
} |
182 |
g2.setRenderingHints(old); |
183 |
} catch (Throwable t) { |
184 |
LOGGER.warn("Can't preview symbol", t);
|
185 |
} |
186 |
} |
187 |
|
188 |
/**
|
189 |
* Sets the EditorTool for the pane.
|
190 |
*
|
191 |
* @param tool
|
192 |
* @return
|
193 |
*/
|
194 |
public EditorTool setEditorTool(EditorTool tool) {
|
195 |
EditorTool previous = prevTool; |
196 |
|
197 |
MouseListener[] ml = getMouseListeners(); |
198 |
for (int i = 0; i < ml.length; i++) { |
199 |
super.removeMouseListener(ml[i]);
|
200 |
} |
201 |
MouseMotionListener[] mml = getMouseMotionListeners(); |
202 |
for (int i = 0; i < mml.length; i++) { |
203 |
super.removeMouseMotionListener(mml[i]);
|
204 |
} |
205 |
|
206 |
if (tool != null) { |
207 |
super.addMouseListener(tool);
|
208 |
setCursor(tool.getCursor()); |
209 |
super.addMouseMotionListener(tool);
|
210 |
} |
211 |
prevTool = tool; |
212 |
return previous;
|
213 |
} |
214 |
|
215 |
private Double forceSize(ISymbol sym) { |
216 |
|
217 |
if (sym == null) { |
218 |
return null; |
219 |
} |
220 |
|
221 |
if (!(sym instanceof IMarkerSymbol)) { |
222 |
return null; |
223 |
} |
224 |
|
225 |
IMarkerSymbol msym = (IMarkerSymbol) sym; |
226 |
if (msym.getUnit() == -1) { |
227 |
/*
|
228 |
* Pixels
|
229 |
*/
|
230 |
if (msym instanceof IMarkerSymbol_v2) { |
231 |
return ((IMarkerSymbol_v2) msym).getEfectiveSize(sampleFeature.getValue());
|
232 |
} |
233 |
return msym.getSize();
|
234 |
} else {
|
235 |
return null; |
236 |
} |
237 |
|
238 |
} |
239 |
|
240 |
// public void setSampleFeature(Feature feature) {
|
241 |
// this.sampleFeature = feature;
|
242 |
// }
|
243 |
} |