Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / SLDListBoxCellRenderer.java @ 24390

History | View | Annotate | Download (6.54 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 com.iver.cit.gvsig.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
import com.iver.andami.PluginServices;
72

    
73
/**
74
*
75
* @author jaume dominguez faus - jaume.dominguez@iver.es
76
*
77
*/
78
public class SLDListBoxCellRenderer extends JPanel implements ListCellRenderer {
79

    
80
    private String str;
81
    private GVTBuilder gvtBuilder = new GVTBuilder();
82
    private StaticRenderer renderer = new StaticRenderer();
83
    private Element elt;
84
    private GraphicsNode gvtRoot;
85

    
86
    DocumentLoader loader;
87
    private UserAgentAdapter userAgent;
88
    BridgeContext  ctx;
89
    BufferedImage image;
90
    protected static RenderingHints defaultRenderingHints;
91
    static {
92
        defaultRenderingHints = new RenderingHints(null);
93
        defaultRenderingHints.put(RenderingHints.KEY_ANTIALIASING,
94
                                  RenderingHints.VALUE_ANTIALIAS_ON);
95

    
96
        defaultRenderingHints.put(RenderingHints.KEY_INTERPOLATION,
97
                                  RenderingHints.VALUE_INTERPOLATION_BILINEAR);
98
    }
99
    /**
100
     * Constructor method
101
     *
102
     */
103

    
104
    public SLDListBoxCellRenderer() {
105
        setOpaque(true);
106
        setLayout(new BorderLayout(3,3));
107

    
108
        userAgent = new UserAgentAdapter();
109
        loader    = new DocumentLoader(userAgent);
110
        ctx       = new BridgeContext(userAgent, loader);
111

    
112
        renderer.setDoubleBuffered(true);
113
        /* canvas = new JSVGCanvas();
114
        // canvas.setMySize(getWidth()-3, getHeight()-3);
115
        canvas.setMySize(new Dimension(getWidth()-3, getHeight()-3));
116
        // add(canvas,BorderLayout.CENTER);
117
        canvas.setVisible(true); */
118

    
119
        /* setHorizontalAlignment(CENTER);
120
        setVerticalAlignment(CENTER); */
121
    }
122
    public Component getListCellRendererComponent(JList list, Object value,
123
            int index, boolean isSelected, boolean cellHasFocus) {
124
//      Get the selected index. (The index param isn't
125
//      always valid, so just use the value.)
126
      String selectedSymbol = ((String)value).toString();
127
      str = selectedSymbol;
128

    
129
      if (isSelected) {
130
          setBackground(list.getSelectionBackground());
131
          setForeground(list.getSelectionForeground());
132
      } else {
133
          setBackground(list.getBackground());
134
          setForeground(list.getForeground());
135
      }
136

    
137
      /* try {
138
          canvas.setURI( new File("D:/java/eclipse30/eclipse/workspace/FMap 03/docs/fill1.svg").toURL().toString() );
139
      } catch (MalformedURLException e1) {
140
          // TODO Auto-generated catch block
141
          e1.printStackTrace();
142
      } */
143
      gvtRoot = null;
144
      try
145
      {
146
          File f = new File(str);
147
          Document svgDoc = loader.loadDocument(f.toURI().toString());
148
          gvtRoot = gvtBuilder.build(ctx, svgDoc);
149
          renderer.setTree(gvtRoot);
150
          elt = ((SVGDocument)svgDoc).getRootElement();
151

    
152
      } catch (Exception ex) {
153
          ex.printStackTrace();
154
      }
155

    
156

    
157
//      Set the icon and text.  If icon was null, say so.
158
      // ImageIcon icon = images[selectedIndex];
159
      String pet = selectedSymbol; // petStrings[selectedIndex];
160
      /* setIcon(icon);
161
      if (icon != null) { */
162
          // setText(pet);
163
          setFont(list.getFont());
164
          repaint();
165
      /* } else {
166
          // setUhOhText(pet + " (no image available)", list.getFont());
167
      } */
168

    
169
      return this;
170
    }
171
    /* (non-Javadoc)
172
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
173
     */
174
    protected void paintComponent(Graphics g) {
175
        super.paintComponent(g);
176
        /* renderer.setTransform
177
        (ViewBox.getViewTransform
178
         (null, elt, getWidth()-1, getHeight()-1));
179

180
        renderer.updateOffScreen(getWidth()-1,
181
                                 getHeight()-1);
182

183
        Rectangle r = new Rectangle(0, 0,
184
                                    getWidth()-1,
185
                                    getHeight()-1);
186
        renderer.repaint(r);
187

188
        image = renderer.getOffScreen();
189

190
        g.drawImage(image,0,0, null); */
191
        Graphics2D g2 = (Graphics2D) g;
192
        RenderingHints renderingHints = defaultRenderingHints;
193
        g2.setRenderingHints(renderingHints);
194
        try {
195
                gvtRoot.setTransform((ViewBox.getViewTransform(null, elt, getWidth()-1, getHeight()-1, ctx)));
196
                gvtRoot.paint(g2);
197
        } catch (Exception e) {
198
                Rectangle bounds = getBounds();
199
                JLabel errLbl = new JLabel("<html><b>"+"Cannot render north"+/*PluginServices.getText(this, "cannot_render_north")+*/"</b></html>");
200
                errLbl.setPreferredSize(bounds.getSize());
201
                errLbl.setSize(bounds.getSize());
202
                errLbl.setBounds(bounds);
203
                errLbl.paint(g);
204
        }
205
        // g2.setTransform(oldTx);
206

    
207
    }
208

    
209
}