001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of AutomataLib, http://www.automatalib.net/.
003 * 
004 * AutomataLib is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License version 3.0 as published by the Free Software Foundation.
007 * 
008 * AutomataLib is distributed in the hope that it will be useful,
009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
011 * Lesser General Public License for more details.
012 * 
013 * You should have received a copy of the GNU Lesser General Public
014 * License along with AutomataLib; if not, see
015 * http://www.gnu.de/documents/lgpl.en.html.
016 */
017package net.automatalib.commons.dotutil;
018
019import java.awt.event.ActionEvent;
020import java.awt.image.BufferedImage;
021import java.io.BufferedWriter;
022import java.io.FileWriter;
023import java.io.IOException;
024import java.io.Reader;
025import java.io.StringWriter;
026import java.io.Writer;
027
028import javax.swing.AbstractAction;
029import javax.swing.Action;
030import javax.swing.JFileChooser;
031import javax.swing.JOptionPane;
032
033import net.automatalib.commons.util.IOUtil;
034
035public class DOTComponent extends ImageComponent {
036        /**
037         * 
038         */
039        private static final long serialVersionUID = 1L;
040        
041        private String dot;
042        
043        private final Action saveDotAction = new AbstractAction("Save DOT") {
044                private static final long serialVersionUID = 1L;
045                @Override
046                public void actionPerformed(ActionEvent e) {
047                        JFileChooser saveDlg = new JFileChooser();
048                        saveDlg.setFileFilter(DOTMisc.DOT_FILTER);
049                        int result = saveDlg.showSaveDialog(DOTComponent.this);
050                        if(result != JFileChooser.APPROVE_OPTION)
051                                return;
052                        try {
053                                Writer w = new BufferedWriter(new FileWriter(saveDlg.getSelectedFile()));
054                                w.write(dot);
055                                w.close();
056                        }
057                        catch(IOException ex) {
058                                JOptionPane.showMessageDialog(DOTComponent.this, "Could not save DOT file: " + ex.getMessage(), "Cannot save DOT", JOptionPane.ERROR_MESSAGE);
059                        }
060                }
061                
062        };
063        
064        public DOTComponent() {}
065        
066        public DOTComponent(Reader dotReader) throws IOException {
067                renderDot(dotReader);
068        }
069        
070        
071        private void renderDot(Reader dotReader) throws IOException {
072                StringWriter w = new StringWriter();
073                
074                IOUtil.copy(dotReader, w);
075                String dot = w.getBuffer().toString();
076                 
077                BufferedImage img = DOT.renderDOTImage(dot);
078                
079                super.setImage(img);
080                this.dot = dot;
081        }
082
083        public String getDot() {
084                return dot;
085        }
086        
087        public Action getSaveDotAction() {
088                return saveDotAction;
089        }
090
091        public void toggleScale() {
092                // TODO Auto-generated method stub
093                
094        }
095}