root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / SLDListBoxCellRenderer.java @ 34294
History | View | Annotate | Download (6.57 KB)
1 |
/*
|
---|---|
2 |
* Created on 26-abr-2005
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
package org.gvsig.app.gui.styling; |
45 |
|
46 |
import java.awt.BorderLayout; |
47 |
import java.awt.Component; |
48 |
import java.awt.Graphics; |
49 |
import java.awt.Graphics2D; |
50 |
import java.awt.Rectangle; |
51 |
import java.awt.RenderingHints; |
52 |
import java.awt.image.BufferedImage; |
53 |
import java.io.File; |
54 |
|
55 |
import javax.swing.JLabel; |
56 |
import javax.swing.JList; |
57 |
import javax.swing.JPanel; |
58 |
import javax.swing.ListCellRenderer; |
59 |
|
60 |
import org.apache.batik.bridge.BridgeContext; |
61 |
import org.apache.batik.bridge.DocumentLoader; |
62 |
import org.apache.batik.bridge.GVTBuilder; |
63 |
import org.apache.batik.bridge.UserAgentAdapter; |
64 |
import org.apache.batik.bridge.ViewBox; |
65 |
import org.apache.batik.gvt.GraphicsNode; |
66 |
import org.apache.batik.gvt.renderer.StaticRenderer; |
67 |
import org.w3c.dom.Document; |
68 |
import org.w3c.dom.Element; |
69 |
import org.w3c.dom.svg.SVGDocument; |
70 |
|
71 |
/**
|
72 |
*
|
73 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
74 |
*
|
75 |
*/
|
76 |
public class SLDListBoxCellRenderer extends JPanel implements ListCellRenderer { |
77 |
|
78 |
/**
|
79 |
*
|
80 |
*/
|
81 |
private static final long serialVersionUID = 1458566622433802796L; |
82 |
private String str; |
83 |
private GVTBuilder gvtBuilder = new GVTBuilder(); |
84 |
private StaticRenderer renderer = new StaticRenderer(); |
85 |
private Element elt; |
86 |
private GraphicsNode gvtRoot;
|
87 |
|
88 |
DocumentLoader loader; |
89 |
private UserAgentAdapter userAgent;
|
90 |
BridgeContext ctx; |
91 |
BufferedImage image;
|
92 |
protected static RenderingHints defaultRenderingHints; |
93 |
static {
|
94 |
defaultRenderingHints = new RenderingHints(null); |
95 |
defaultRenderingHints.put(RenderingHints.KEY_ANTIALIASING,
|
96 |
RenderingHints.VALUE_ANTIALIAS_ON);
|
97 |
|
98 |
defaultRenderingHints.put(RenderingHints.KEY_INTERPOLATION,
|
99 |
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
100 |
} |
101 |
/**
|
102 |
* Constructor method
|
103 |
*
|
104 |
*/
|
105 |
|
106 |
public SLDListBoxCellRenderer() {
|
107 |
setOpaque(true);
|
108 |
setLayout(new BorderLayout(3,3)); |
109 |
|
110 |
userAgent = new UserAgentAdapter();
|
111 |
loader = new DocumentLoader(userAgent);
|
112 |
ctx = new BridgeContext(userAgent, loader);
|
113 |
|
114 |
renderer.setDoubleBuffered(true);
|
115 |
/* canvas = new JSVGCanvas();
|
116 |
// canvas.setMySize(getWidth()-3, getHeight()-3);
|
117 |
canvas.setMySize(new Dimension(getWidth()-3, getHeight()-3));
|
118 |
// add(canvas,BorderLayout.CENTER);
|
119 |
canvas.setVisible(true); */
|
120 |
|
121 |
/* setHorizontalAlignment(CENTER);
|
122 |
setVerticalAlignment(CENTER); */
|
123 |
} |
124 |
public Component getListCellRendererComponent(JList list, Object value, |
125 |
int index, boolean isSelected, boolean cellHasFocus) { |
126 |
// Get the selected index. (The index param isn't
|
127 |
// always valid, so just use the value.)
|
128 |
String selectedSymbol = ((String)value).toString(); |
129 |
str = selectedSymbol; |
130 |
|
131 |
if (isSelected) {
|
132 |
setBackground(list.getSelectionBackground()); |
133 |
setForeground(list.getSelectionForeground()); |
134 |
} else {
|
135 |
setBackground(list.getBackground()); |
136 |
setForeground(list.getForeground()); |
137 |
} |
138 |
|
139 |
/* try {
|
140 |
canvas.setURI( new File("D:/java/eclipse30/eclipse/workspace/FMap 03/docs/fill1.svg").toURL().toString() );
|
141 |
} catch (MalformedURLException e1) {
|
142 |
// TODO Auto-generated catch block
|
143 |
e1.printStackTrace();
|
144 |
} */
|
145 |
gvtRoot = null;
|
146 |
try
|
147 |
{ |
148 |
File f = new File(str); |
149 |
Document svgDoc = loader.loadDocument(f.toURI().toString());
|
150 |
gvtRoot = gvtBuilder.build(ctx, svgDoc); |
151 |
renderer.setTree(gvtRoot); |
152 |
elt = ((SVGDocument)svgDoc).getRootElement(); |
153 |
|
154 |
} catch (Exception ex) { |
155 |
ex.printStackTrace(); |
156 |
} |
157 |
|
158 |
|
159 |
// Set the icon and text. If icon was null, say so.
|
160 |
// ImageIcon icon = images[selectedIndex];
|
161 |
String pet = selectedSymbol; // petStrings[selectedIndex]; |
162 |
/* setIcon(icon);
|
163 |
if (icon != null) { */
|
164 |
// setText(pet);
|
165 |
setFont(list.getFont()); |
166 |
repaint(); |
167 |
/* } else {
|
168 |
// setUhOhText(pet + " (no image available)", list.getFont());
|
169 |
} */
|
170 |
|
171 |
return this; |
172 |
} |
173 |
/* (non-Javadoc)
|
174 |
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
|
175 |
*/
|
176 |
protected void paintComponent(Graphics g) { |
177 |
super.paintComponent(g);
|
178 |
/* renderer.setTransform
|
179 |
(ViewBox.getViewTransform
|
180 |
(null, elt, getWidth()-1, getHeight()-1));
|
181 |
|
182 |
renderer.updateOffScreen(getWidth()-1,
|
183 |
getHeight()-1);
|
184 |
|
185 |
Rectangle r = new Rectangle(0, 0,
|
186 |
getWidth()-1,
|
187 |
getHeight()-1);
|
188 |
renderer.repaint(r);
|
189 |
|
190 |
image = renderer.getOffScreen();
|
191 |
|
192 |
g.drawImage(image,0,0, null); */
|
193 |
Graphics2D g2 = (Graphics2D) g; |
194 |
RenderingHints renderingHints = defaultRenderingHints;
|
195 |
g2.setRenderingHints(renderingHints); |
196 |
try {
|
197 |
gvtRoot.setTransform((ViewBox.getViewTransform(null, elt, getWidth()-1, getHeight()-1, ctx))); |
198 |
gvtRoot.paint(g2); |
199 |
} catch (Exception e) { |
200 |
Rectangle bounds = getBounds();
|
201 |
JLabel errLbl = new JLabel("<html><b>"+"Cannot render north"+/*PluginServices.getText(this, "cannot_render_north")+*/"</b></html>"); |
202 |
errLbl.setPreferredSize(bounds.getSize()); |
203 |
errLbl.setSize(bounds.getSize()); |
204 |
errLbl.setBounds(bounds); |
205 |
errLbl.paint(g); |
206 |
} |
207 |
// g2.setTransform(oldTx);
|
208 |
|
209 |
} |
210 |
|
211 |
} |