Statistics
| Revision:

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

History | View | Annotate | Download (6.09 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.RenderingHints;
51
import java.awt.image.BufferedImage;
52
import java.io.File;
53

    
54
import javax.swing.JList;
55
import javax.swing.JPanel;
56
import javax.swing.ListCellRenderer;
57

    
58
import org.apache.batik.bridge.BridgeContext;
59
import org.apache.batik.bridge.DocumentLoader;
60
import org.apache.batik.bridge.GVTBuilder;
61
import org.apache.batik.bridge.UserAgentAdapter;
62
import org.apache.batik.bridge.ViewBox;
63
import org.apache.batik.gvt.GraphicsNode;
64
import org.apache.batik.gvt.renderer.StaticRenderer;
65
import org.w3c.dom.Document;
66
import org.w3c.dom.Element;
67
import org.w3c.dom.svg.SVGDocument;
68

    
69
/**
70
*
71
* @author jaume dominguez faus - jaume.dominguez@iver.es
72
*
73
*/
74
public class SLDListBoxCellRenderer extends JPanel implements ListCellRenderer {
75

    
76
    private String str;
77
    private GVTBuilder gvtBuilder = new GVTBuilder();
78
    private StaticRenderer renderer = new StaticRenderer();
79
    private Element elt;
80
    private GraphicsNode gvtRoot;
81
    
82
    DocumentLoader loader;
83
    private UserAgentAdapter userAgent;
84
    BridgeContext  ctx;
85
    BufferedImage image;
86
    protected static RenderingHints defaultRenderingHints;
87
    static {
88
        defaultRenderingHints = new RenderingHints(null);
89
        defaultRenderingHints.put(RenderingHints.KEY_ANTIALIASING,
90
                                  RenderingHints.VALUE_ANTIALIAS_ON);
91

    
92
        defaultRenderingHints.put(RenderingHints.KEY_INTERPOLATION,
93
                                  RenderingHints.VALUE_INTERPOLATION_BILINEAR);
94
    }
95
    
96
    
97
    public SLDListBoxCellRenderer() {
98
        setOpaque(true);
99
        setLayout(new BorderLayout(3,3));
100

    
101
        userAgent = new UserAgentAdapter();
102
        loader    = new DocumentLoader(userAgent);
103
        ctx       = new BridgeContext(userAgent, loader);
104

    
105
        renderer.setDoubleBuffered(true);
106
        /* canvas = new JSVGCanvas();
107
        // canvas.setMySize(getWidth()-3, getHeight()-3);
108
        canvas.setMySize(new Dimension(getWidth()-3, getHeight()-3));
109
        // add(canvas,BorderLayout.CENTER);
110
        canvas.setVisible(true); */
111
        
112
        /* setHorizontalAlignment(CENTER);
113
        setVerticalAlignment(CENTER); */
114
    }
115
    public Component getListCellRendererComponent(JList list, Object value,
116
            int index, boolean isSelected, boolean cellHasFocus) {
117
//      Get the selected index. (The index param isn't
118
//      always valid, so just use the value.)
119
      String selectedSymbol = ((String)value).toString();
120
      str = selectedSymbol;
121

    
122
      if (isSelected) {
123
          setBackground(list.getSelectionBackground());
124
          setForeground(list.getSelectionForeground());
125
      } else {
126
          setBackground(list.getBackground());
127
          setForeground(list.getForeground());
128
      }
129
       
130
      /* try {
131
          canvas.setURI( new File("D:/java/eclipse30/eclipse/workspace/FMap 03/docs/fill1.svg").toURL().toString() );
132
      } catch (MalformedURLException e1) {
133
          // TODO Auto-generated catch block
134
          e1.printStackTrace();
135
      } */
136
      gvtRoot = null;
137
      try 
138
      {
139
          File f = new File(str);
140
          Document svgDoc = loader.loadDocument(f.toURI().toString());
141
          gvtRoot = gvtBuilder.build(ctx, svgDoc);
142
          renderer.setTree(gvtRoot);
143
          elt = ((SVGDocument)svgDoc).getRootElement();
144
          
145
      } catch (Exception ex) {
146
          ex.printStackTrace();
147
      }
148
      
149

    
150
//      Set the icon and text.  If icon was null, say so.
151
      // ImageIcon icon = images[selectedIndex];
152
      String pet = selectedSymbol; // petStrings[selectedIndex];
153
      /* setIcon(icon);
154
      if (icon != null) { */
155
          // setText(pet);
156
          setFont(list.getFont());
157
          repaint();
158
      /* } else {
159
          // setUhOhText(pet + " (no image available)", list.getFont());
160
      } */
161

    
162
      return this;
163
    }
164
    /* (non-Javadoc)
165
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
166
     */
167
    protected void paintComponent(Graphics g) {
168
        super.paintComponent(g);
169
        /* renderer.setTransform
170
        (ViewBox.getViewTransform
171
         (null, elt, getWidth()-1, getHeight()-1));
172

173
        renderer.updateOffScreen(getWidth()-1, 
174
                                 getHeight()-1);
175
    
176
        Rectangle r = new Rectangle(0, 0,
177
                                    getWidth()-1, 
178
                                    getHeight()-1);
179
        renderer.repaint(r);
180
        
181
        image = renderer.getOffScreen(); 
182
        
183
        g.drawImage(image,0,0, null); */
184
        Graphics2D g2 = (Graphics2D) g;
185
        RenderingHints renderingHints = defaultRenderingHints;
186
        g2.setRenderingHints(renderingHints);
187
        gvtRoot.setTransform((ViewBox.getViewTransform(null, elt, getWidth()-1, getHeight()-1)));
188
        gvtRoot.paint(g2);
189
        // g2.setTransform(oldTx);
190
        
191
    }
192

    
193
}