summaryrefslogtreecommitdiffstats
path: root/collaboration/GSoC-2011-Nirmal/CompositeDiagramShellPlugin/src/main/java/org/apache/tuscany/sca/diagram/shell/SVGViewer.java
blob: 4eb26d9e61529d8c75eada6d331a6a8ecb5ba171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.diagram.shell;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.net.MalformedURLException;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.w3c.dom.Document;

public class SVGViewer {

        public static void main(String[] args) {
//            new SVGViewer("../CompositeDiagramGeneratorUsingBatik/output/Calculator_diagram.svg");
        }
        
        protected JFrame frame;
        protected JLabel label = new JLabel();
        protected JSVGCanvas svgCanvas = new JSVGCanvas();
        protected String svgFile;
        private Document doc;

        public SVGViewer(Document doc) {
//            this.svgFile = svgFile;
            this.doc = doc;
            this.frame = new JFrame("Batik");
            frame.getContentPane().add(createComponents());
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
//                    System.exit(0);
                }
            });
            frame.setSize(1000, 1000);
            frame.setVisible(true);
        }

        public JComponent createComponents() {
            final JPanel panel = new JPanel(new BorderLayout());
            panel.add("Center", svgCanvas);
//            try {
//                svgCanvas.setURI(new File(svgFile).toURI().toURL().toString());
//            } catch (MalformedURLException e) {
//                e.printStackTrace();
//            }
            
            svgCanvas.setDocument(doc);
            
            svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
                public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
                    label.setText("Document Loading...");
                }
                public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
                    label.setText("Document Loaded.");
                }
            });

            svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
                public void gvtBuildStarted(GVTTreeBuilderEvent e) {
                    label.setText("Build Started...");
                }
                public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
                    label.setText("Build Done.");
                    frame.pack();
                }
            });

            svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
                public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
                    label.setText("Rendering Started...");
                }
                public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                    label.setText("");
                }
            });

            return panel;
        }
    }