Statistics
| Revision:

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 / SLDListBoxCellRenderer.java @ 40560

History | View | Annotate | Download (6.29 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.BorderLayout;
27
import java.awt.Component;
28
import java.awt.Graphics;
29
import java.awt.Graphics2D;
30
import java.awt.Rectangle;
31
import java.awt.RenderingHints;
32
import java.awt.image.BufferedImage;
33
import java.io.File;
34

    
35
import javax.swing.JLabel;
36
import javax.swing.JList;
37
import javax.swing.JPanel;
38
import javax.swing.ListCellRenderer;
39

    
40
import org.apache.batik.bridge.BridgeContext;
41
import org.apache.batik.bridge.DocumentLoader;
42
import org.apache.batik.bridge.GVTBuilder;
43
import org.apache.batik.bridge.UserAgentAdapter;
44
import org.apache.batik.bridge.ViewBox;
45
import org.apache.batik.gvt.GraphicsNode;
46
import org.apache.batik.gvt.renderer.StaticRenderer;
47
import org.w3c.dom.Document;
48
import org.w3c.dom.Element;
49
import org.w3c.dom.svg.SVGDocument;
50

    
51
/**
52
*
53
* @author jaume dominguez faus - jaume.dominguez@iver.es
54
*
55
*/
56
public class SLDListBoxCellRenderer extends JPanel implements ListCellRenderer {
57

    
58
    /**
59
         * 
60
         */
61
        private static final long serialVersionUID = 1458566622433802796L;
62
        private String str;
63
    private GVTBuilder gvtBuilder = new GVTBuilder();
64
    private StaticRenderer renderer = new StaticRenderer();
65
    private Element elt;
66
    private GraphicsNode gvtRoot;
67

    
68
    DocumentLoader loader;
69
    private UserAgentAdapter userAgent;
70
    BridgeContext  ctx;
71
    BufferedImage image;
72
    protected static RenderingHints defaultRenderingHints;
73
    static {
74
        defaultRenderingHints = new RenderingHints(null);
75
        defaultRenderingHints.put(RenderingHints.KEY_ANTIALIASING,
76
                                  RenderingHints.VALUE_ANTIALIAS_ON);
77

    
78
        defaultRenderingHints.put(RenderingHints.KEY_INTERPOLATION,
79
                                  RenderingHints.VALUE_INTERPOLATION_BILINEAR);
80
    }
81
    /**
82
     * Constructor method
83
     *
84
     */
85

    
86
    public SLDListBoxCellRenderer() {
87
        setOpaque(true);
88
        setLayout(new BorderLayout(3,3));
89

    
90
        userAgent = new UserAgentAdapter();
91
        loader    = new DocumentLoader(userAgent);
92
        ctx       = new BridgeContext(userAgent, loader);
93

    
94
        renderer.setDoubleBuffered(true);
95
        /* canvas = new JSVGCanvas();
96
        // canvas.setMySize(getWidth()-3, getHeight()-3);
97
        canvas.setMySize(new Dimension(getWidth()-3, getHeight()-3));
98
        // add(canvas,BorderLayout.CENTER);
99
        canvas.setVisible(true); */
100

    
101
        /* setHorizontalAlignment(CENTER);
102
        setVerticalAlignment(CENTER); */
103
    }
104
    public Component getListCellRendererComponent(JList list, Object value,
105
            int index, boolean isSelected, boolean cellHasFocus) {
106
//      Get the selected index. (The index param isn't
107
//      always valid, so just use the value.)
108
      String selectedSymbol = ((String)value).toString();
109
      str = selectedSymbol;
110

    
111
      if (isSelected) {
112
          setBackground(list.getSelectionBackground());
113
          setForeground(list.getSelectionForeground());
114
      } else {
115
          setBackground(list.getBackground());
116
          setForeground(list.getForeground());
117
      }
118

    
119
      /* try {
120
          canvas.setURI( new File("D:/java/eclipse30/eclipse/workspace/FMap 03/docs/fill1.svg").toURL().toString() );
121
      } catch (MalformedURLException e1) {
122
          // TODO Auto-generated catch block
123
          e1.printStackTrace();
124
      } */
125
      gvtRoot = null;
126
      try
127
      {
128
          File f = new File(str);
129
          Document svgDoc = loader.loadDocument(f.toURI().toString());
130
          gvtRoot = gvtBuilder.build(ctx, svgDoc);
131
          renderer.setTree(gvtRoot);
132
          elt = ((SVGDocument)svgDoc).getRootElement();
133

    
134
      } catch (Exception ex) {
135
          ex.printStackTrace();
136
      }
137

    
138

    
139
//      Set the icon and text.  If icon was null, say so.
140
      // ImageIcon icon = images[selectedIndex];
141
      String pet = selectedSymbol; // petStrings[selectedIndex];
142
      /* setIcon(icon);
143
      if (icon != null) { */
144
          // setText(pet);
145
          setFont(list.getFont());
146
          repaint();
147
      /* } else {
148
          // setUhOhText(pet + " (no image available)", list.getFont());
149
      } */
150

    
151
      return this;
152
    }
153
    /* (non-Javadoc)
154
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
155
     */
156
    protected void paintComponent(Graphics g) {
157
        super.paintComponent(g);
158
        /* renderer.setTransform
159
        (ViewBox.getViewTransform
160
         (null, elt, getWidth()-1, getHeight()-1));
161

162
        renderer.updateOffScreen(getWidth()-1,
163
                                 getHeight()-1);
164

165
        Rectangle r = new Rectangle(0, 0,
166
                                    getWidth()-1,
167
                                    getHeight()-1);
168
        renderer.repaint(r);
169

170
        image = renderer.getOffScreen();
171

172
        g.drawImage(image,0,0, null); */
173
        Graphics2D g2 = (Graphics2D) g;
174
        RenderingHints renderingHints = defaultRenderingHints;
175
        g2.setRenderingHints(renderingHints);
176
        try {
177
                gvtRoot.setTransform((ViewBox.getViewTransform(null, elt, getWidth()-1, getHeight()-1, ctx)));
178
                gvtRoot.paint(g2);
179
        } catch (Exception e) {
180
                Rectangle bounds = getBounds();
181
                JLabel errLbl = new JLabel("<html><b>"+"Cannot render north"+/*PluginServices.getText(this, "cannot_render_north")+*/"</b></html>");
182
                errLbl.setPreferredSize(bounds.getSize());
183
                errLbl.setSize(bounds.getSize());
184
                errLbl.setBounds(bounds);
185
                errLbl.paint(g);
186
        }
187
        // g2.setTransform(oldTx);
188

    
189
    }
190

    
191
}