View Javadoc

1   /*
2    * Copyright (c) 2003-2008, by Henrik Arro and Contributors
3    *
4    * This file is part of JSeq, a tool to automatically create
5    * sequence diagrams by tracing program execution.
6    *
7    * See <http://jseq.sourceforge.net> for more information.
8    *
9    * JSeq is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of
12   * the License, or (at your option) any later version.
13   *
14   * JSeq is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   * GNU Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public License
20   * along with JSeq. If not, see <http://www.gnu.org/licenses/>.
21   */
22  
23  package th.co.edge.jseq.sdedit;
24  
25  import java.io.File;
26  import java.io.IOException;
27  
28  import net.sf.sdedit.Main;
29  import th.co.edge.jseq.ActivationList;
30  import th.co.edge.jseq.Diagram;
31  
32  /**
33   * An <code>SdeditPngDiagram</code> is a <code>Diagram</code> that can be
34   * used to generate sequence diagrams in PNG format, using the <a
35   * href="http://sdedit.sourceforge.net/" target="new">Quick Sequence Diagram
36   * Editor</a> to create the image.
37   *
38   * @author jacek.ratzinger
39   */
40  public class SdeditPngDiagram implements Diagram {
41  
42      private static final String DEFAULT_PNG_FILE_EXTENSION = ".png";
43  
44      private final SdeditTextDiagram textDiagram;
45  
46      /**
47       * Creates a new <code>SdeditPngDiagram</code>, using the given
48       * <code>ActivationList</code> as the basis for the sequence diagram.
49       *
50       * @param activationList
51       *            a list of root <code>Activation</code>s to use when
52       *            generating the sequence diagram
53       */
54      public SdeditPngDiagram(ActivationList activationList) {
55          this.textDiagram = new SdeditTextDiagram(activationList);
56      }
57  
58      /**
59       * Creates a sequence diagram and writes it to file as a PNG image.
60       *
61       * @param pngFile
62       *            the <code>File</code> to write to
63       *
64       * @throws IOException
65       *             if something went wrong when creating or saving the diagram
66       */
67      @Override
68      public void save(File pngFile) throws IOException {
69          String outputFilePrefix =
70                  pngFile.getName().substring(
71                          0,
72                          pngFile.getName().length() -
73                                  DEFAULT_PNG_FILE_EXTENSION.length());
74          File tempTextFile =
75                  new File(pngFile.getParentFile(), outputFilePrefix + ".txt");
76          textDiagram.save(tempTextFile);
77          String[] sdeditArgs =
78                  { "-t", "png", "-o", pngFile.getAbsolutePath(),
79                          tempTextFile.getAbsolutePath() };
80          try {
81              Main.main(sdeditArgs);
82          } catch (Exception e) {
83              throw new IOException(e);
84          }
85      }
86  
87  }