001/*
002 * SVG Salamander
003 * Copyright (c) 2004, Mark McKay
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or 
007 * without modification, are permitted provided that the following
008 * conditions are met:
009 *
010 *   - Redistributions of source code must retain the above 
011 *     copyright notice, this list of conditions and the following
012 *     disclaimer.
013 *   - Redistributions in binary form must reproduce the above
014 *     copyright notice, this list of conditions and the following
015 *     disclaimer in the documentation and/or other materials 
016 *     provided with the distribution.
017 *
018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
029 * OF THE POSSIBILITY OF SUCH DAMAGE. 
030 * 
031 * Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
032 * projects can be found at http://www.kitfox.com
033 *
034 * Created on February 20, 2004, 12:29 PM
035 */
036
037package com.kitfox.svg;
038
039import javax.swing.*;
040import java.awt.*;
041import java.awt.geom.*;
042import java.util.logging.Level;
043import java.util.logging.Logger;
044
045/**
046 * @author Mark McKay
047 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
048 */
049public class SVGDisplayPanel extends javax.swing.JPanel implements Scrollable
050{
051    public static final long serialVersionUID = 1;
052    
053    SVGDiagram diagram = null;
054    float scale = 1f;
055    Color bgColor = null;
056
057    /** Creates new form SVGDisplayPanel */
058    public SVGDisplayPanel()
059    {
060        initComponents();
061    }
062
063    public SVGDiagram getDiagram()
064    {
065        return diagram;
066    }
067    
068    public void setDiagram(SVGDiagram diagram)
069    {
070        this.diagram = diagram;
071        diagram.setDeviceViewport(getBounds());
072        
073        setDimension();
074    }
075
076    public void setScale(float scale)
077    {
078        this.scale = scale;
079        setDimension();
080    }
081
082    public void setBgColor(Color col)
083    {
084        bgColor = col;
085    }
086
087    private void setDimension()
088    {
089        if (diagram == null)
090        {
091            setPreferredSize(new Dimension(1, 1));
092            revalidate();
093            return;
094        }
095
096        final Rectangle2D.Float rect = new Rectangle2D.Float();
097        diagram.getViewRect(rect);
098
099        int w = (int)(rect.width * scale);
100        int h = (int)(rect.height * scale);
101
102        setPreferredSize(new Dimension(w, h));
103        revalidate();
104    }
105
106    /**
107     * Update this image to reflect the passed time
108     * @param curTime
109     * @throws com.kitfox.svg.SVGException
110     */
111    public void updateTime(double curTime) throws SVGException
112    {
113        if (diagram == null) return;
114        
115        diagram.updateTime(curTime);
116    }
117    
118    @Override
119    public void paintComponent(Graphics gg)
120    {
121        Graphics2D g = (Graphics2D)gg;
122
123        if (bgColor != null)
124        {
125            Dimension dim = getSize();
126            g.setColor(bgColor);
127            g.fillRect(0, 0, dim.width, dim.height);
128        }
129
130        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
131        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
132        if (diagram != null) 
133        {
134            try
135            {
136                diagram.render(g);
137            }
138            catch (SVGException e)
139            {
140                Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 
141                    "Could not render diagram", e);
142            }
143        }
144    }
145
146    /** This method is called from within the constructor to
147     * initialize the form.
148     * WARNING: Do NOT modify this code. The content of this method is
149     * always regenerated by the Form Editor.
150     */
151    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
152    private void initComponents()
153    {
154
155        setLayout(new java.awt.BorderLayout());
156
157        addComponentListener(new java.awt.event.ComponentAdapter()
158        {
159            @Override
160            public void componentResized(java.awt.event.ComponentEvent evt)
161            {
162                formComponentResized(evt);
163            }
164        });
165
166    }// </editor-fold>//GEN-END:initComponents
167
168    private void formComponentResized(java.awt.event.ComponentEvent evt)//GEN-FIRST:event_formComponentResized
169    {//GEN-HEADEREND:event_formComponentResized
170        if (diagram != null)
171        {
172            diagram.setDeviceViewport(getBounds());
173            setDimension();
174        }
175
176    }//GEN-LAST:event_formComponentResized
177
178    public Dimension getPreferredScrollableViewportSize()
179    {
180        return getPreferredSize();
181    }
182
183    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
184    {
185        if (orientation == SwingConstants.HORIZONTAL)
186        {
187            return visibleRect.width;
188        }
189        else return visibleRect.height;
190    }
191
192    public boolean getScrollableTracksViewportHeight()
193    {
194        return false;
195    }
196
197    public boolean getScrollableTracksViewportWidth()
198    {
199        return false;
200    }
201
202    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
203    {
204        return getScrollableBlockIncrement(visibleRect, orientation, direction) / 16;
205    }
206
207
208    // Variables declaration - do not modify//GEN-BEGIN:variables
209    // End of variables declaration//GEN-END:variables
210
211}