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 / StylePreviewer.java @ 47476
History | View | Annotate | Download (4.63 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 org.apache.commons.lang3.mutable.MutableObject; |
33 |
import org.gvsig.fmap.dal.feature.Feature; |
34 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
35 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
36 |
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol; |
37 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
38 |
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle; |
39 |
import org.gvsig.i18n.Messages; |
40 |
|
41 |
|
42 |
/**
|
43 |
* Defines the properties of the symbols that are showed in the
|
44 |
* SymbolPreviewer panel.Also the user has methods to set this attributes.
|
45 |
*
|
46 |
* @author gvSIG Team
|
47 |
*
|
48 |
*/
|
49 |
public class StylePreviewer extends SymbolPreviewer{ |
50 |
/**
|
51 |
*
|
52 |
*/
|
53 |
private static final long serialVersionUID = -1690697926703748637L; |
54 |
private int hGap, vGap; |
55 |
private IStyle style;
|
56 |
private boolean showOutline; |
57 |
|
58 |
/**
|
59 |
* Constructor method
|
60 |
*
|
61 |
* @param feature
|
62 |
*/
|
63 |
public StylePreviewer(MutableObject<Feature> feature) {
|
64 |
super(feature);
|
65 |
initialize(); |
66 |
} |
67 |
|
68 |
public StylePreviewer() {
|
69 |
super();
|
70 |
initialize(); |
71 |
} |
72 |
|
73 |
private void initialize() { |
74 |
this.hGap = 5; |
75 |
this.vGap = 5; |
76 |
setBackground(Color.WHITE);
|
77 |
} |
78 |
|
79 |
/**
|
80 |
* Obtains the style of the symbol showed in the SymbolPreviewer panel
|
81 |
* @return style,IStyle
|
82 |
*/
|
83 |
public IStyle getStyle() {
|
84 |
return style;
|
85 |
} |
86 |
|
87 |
public void setShowOutline(boolean so) { |
88 |
this.showOutline = so;
|
89 |
} |
90 |
|
91 |
@Override
|
92 |
public ISymbol getSymbol() {
|
93 |
throw new Error(Messages.getText("undefined_for_StylePreviewer_use") |
94 |
+" getStyle() "+
|
95 |
Messages.getText("instead") );
|
96 |
|
97 |
} |
98 |
|
99 |
@Override
|
100 |
public void setSymbol(ISymbol symbol) { |
101 |
throw new Error(Messages.getText("undefined_for_StylePreviewer_use") |
102 |
+" setStyle(IStyle) "+
|
103 |
Messages.getText("instead") );
|
104 |
|
105 |
} |
106 |
/**
|
107 |
* Defines the style of the symbol showed in the SymbolPreviewer panel
|
108 |
* @param style,IStyle
|
109 |
*/
|
110 |
|
111 |
public void setStyle(IStyle style) { |
112 |
this.style = style;
|
113 |
} |
114 |
|
115 |
@Override
|
116 |
public void paint(Graphics g) { |
117 |
Graphics2D g2 = (Graphics2D) g; |
118 |
Rectangle r = getBounds();
|
119 |
g2.translate(hGap, vGap); |
120 |
r = new Rectangle(0, 0, (int) (r.getWidth()-(hGap*2)), (int) (r.getHeight()-(vGap*2))); |
121 |
|
122 |
if (style != null) { |
123 |
try {
|
124 |
if (showOutline) {
|
125 |
// this is a trick for the editor
|
126 |
style.drawOutline(g2, r); |
127 |
|
128 |
} else {
|
129 |
style.drawInsideRectangle(g2, r); |
130 |
} |
131 |
} catch (SymbolDrawingException e) {
|
132 |
if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
|
133 |
try {
|
134 |
IWarningSymbol warning = |
135 |
(IWarningSymbol) MapContextLocator.getSymbolManager() |
136 |
.getWarningSymbol( |
137 |
SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS, |
138 |
"",
|
139 |
SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS); |
140 |
warning.drawInsideRectangle(g2, null, r);
|
141 |
} catch (SymbolDrawingException e1) {
|
142 |
// IMPOSSIBLE TO REACH THIS
|
143 |
} |
144 |
} else {
|
145 |
// should be unreachable code
|
146 |
throw new Error(Messages.getText("symbol_shapetype_mismatch")); |
147 |
} |
148 |
} |
149 |
} else {
|
150 |
String noneSelected = "["+Messages.getText("preview_not_available")+"]"; |
151 |
FontMetrics fm = g2.getFontMetrics();
|
152 |
int lineWidth = fm.stringWidth(noneSelected);
|
153 |
float scale = (float) r.getWidth() / lineWidth; |
154 |
Font f = g2.getFont();
|
155 |
float fontSize = f.getSize()*scale;
|
156 |
g2.setFont( f.deriveFont( fontSize ) ); |
157 |
|
158 |
g2.drawString(noneSelected, (r.x*scale) - (hGap/2), r.height/2+vGap*scale); |
159 |
} |
160 |
} |
161 |
|
162 |
public int getHGap() { |
163 |
return hGap;
|
164 |
} |
165 |
|
166 |
public int getVGap() { |
167 |
return vGap;
|
168 |
} |
169 |
|
170 |
} |