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;
027import java.util.List;
028
029import javax.swing.AbstractAction;
030import javax.swing.Action;
031import javax.swing.JFileChooser;
032import javax.swing.JOptionPane;
033
034import net.automatalib.commons.util.IOUtil;
035
036public class DOTComponent extends ImageComponent {
037        /**
038         * 
039         */
040        private static final long serialVersionUID = 1L;
041        
042        private String dot;
043        
044        private final Action saveDotAction = new AbstractAction("Save DOT") {
045                private static final long serialVersionUID = 1L;
046                @Override
047                public void actionPerformed(ActionEvent e) {
048                        JFileChooser saveDlg = new JFileChooser();
049                        saveDlg.setFileFilter(DOTMisc.DOT_FILTER);
050                        int result = saveDlg.showSaveDialog(DOTComponent.this);
051                        if(result != JFileChooser.APPROVE_OPTION)
052                                return;
053                        try {
054                                Writer w = new BufferedWriter(new FileWriter(saveDlg.getSelectedFile()));
055                                w.write(dot);
056                                w.close();
057                        }
058                        catch(IOException ex) {
059                                JOptionPane.showMessageDialog(DOTComponent.this, "Could not save DOT file: " + ex.getMessage(), "Cannot save DOT", JOptionPane.ERROR_MESSAGE);
060                        }
061                }
062                
063        };
064        
065        public DOTComponent() {}
066        
067        public DOTComponent(Reader dotReader) throws IOException {
068                renderDot(dotReader);
069        }
070        
071        
072        
073        
074        /* (non-Javadoc)
075         * @see net.automatalib.commons.dotutil.ImageComponent#listActions(java.util.List)
076         */
077        @Override
078        public void listActions(List<Action> actions) {
079                super.listActions(actions);
080                actions.add(saveDotAction);
081        }
082
083        private void renderDot(Reader dotReader) throws IOException {
084                StringWriter w = new StringWriter();
085                
086                IOUtil.copy(dotReader, w);
087                String dot = w.getBuffer().toString();
088                 
089                BufferedImage img = DOT.renderDOTImage(dot);
090                
091                super.setImage(img);
092                this.dot = dot;
093        }
094
095        public String getDot() {
096                return dot;
097        }
098        
099        public Action getSaveDotAction() {
100                return saveDotAction;
101        }
102
103}