Search in sources :

Example 1 with ImagePaintDevice

use of net.sf.sdedit.ui.ImagePaintDevice in project abstools by abstools.

the class Main method createImage.

private static void createImage(CommandLine cmd) throws IOException, XMLException, SyntaxError, SemanticError {
    File inFile = new File(getInputFiles(cmd)[0]);
    File outFile = new File(cmd.getOptionValue('o'));
    String type = "png";
    if (cmd.getOptionValue('t') != null) {
        type = cmd.getOptionValue('t').toLowerCase();
    }
    String format = "A4";
    if (cmd.getOptionValue('f') != null) {
        format = cmd.getOptionValue('f').toUpperCase();
    }
    String orientation = "Portrait";
    if (cmd.getOptionValue('r') != null) {
        orientation = cmd.getOptionValue('r').toLowerCase();
        if (orientation.length() > 0) {
            orientation = orientation.substring(0, 1).toUpperCase() + orientation.substring(1);
        }
    }
    InputStream in = null;
    OutputStream out = null;
    in = new FileInputStream(inFile);
    try {
        out = new FileOutputStream(outFile);
        try {
            Pair<String, Bean<Configuration>> pair = DiagramLoader.load(in, ConfigurationManager.getGlobalConfiguration().getFileEncoding());
            TextHandler th = new TextHandler(pair.getFirst());
            Bean<Configuration> conf = pair.getSecond();
            configure(conf, cmd);
            if (type.equals("png")) {
                ImagePaintDevice paintDevice = new ImagePaintDevice();
                new Diagram(conf.getDataObject(), th, paintDevice).generate();
                paintDevice.writeToStream(out);
            } else {
                Exporter paintDevice = Exporter.getExporter(type, orientation, format, out);
                new Diagram(conf.getDataObject(), th, paintDevice).generate();
                paintDevice.export();
            }
            out.flush();
        } finally {
            out.close();
        }
    } finally {
        in.close();
    }
}
Also used : Configuration(net.sf.sdedit.config.Configuration) ImagePaintDevice(net.sf.sdedit.ui.ImagePaintDevice) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Exporter(net.sf.sdedit.server.Exporter) FileInputStream(java.io.FileInputStream) Bean(net.sf.sdedit.ui.components.configuration.Bean) Diagram(net.sf.sdedit.diagram.Diagram) FileOutputStream(java.io.FileOutputStream) TextHandler(net.sf.sdedit.text.TextHandler) File(java.io.File)

Example 2 with ImagePaintDevice

use of net.sf.sdedit.ui.ImagePaintDevice in project abstools by abstools.

the class SequenceTaglet method generateOutput.

/**
 * Creates a sequence diagram image from a part of the contents of a
 * sequence.diagram tag, saves it in the image directory and returns HTML
 * code that references the image.
 *
 * @param path
 *            the path to the directory where the diagram image is to be
 *            stored (usually a path going upwards, i. e. containing ../'s)
 * @param imageBaseName
 *            the base name of the image to be stored
 * @param source
 *            string array containing the lines of the diagram specification
 * @return the output that is to appear on the javadoc page (i. e. the
 *         &lt;img&gt; tag referencing the image
 * @throws SequenceTagletException
 *             if the creation of the diagram fails
 */
private String generateOutput(String path, String imageBaseName, String[] source) throws SequenceTagletException {
    if (source == null || source.length == 0) {
        return "";
    }
    // Set custom diagram title if first line is quoted
    String diagramTitle = null;
    if (source[0].trim().matches("^[\"'].*[\"']$")) {
        // Set the title and remove the title quote
        diagramTitle = source[0].replaceAll("[\"']", "");
        // Remove the custom title from the sd spec
        source[0] = "";
    }
    StringBuffer buffer = new StringBuffer();
    for (String string : source) {
        string = string.trim();
        if (string.startsWith("<") && string.endsWith(">")) {
            continue;
        }
        buffer.append(string + "\n");
    }
    String specification = buffer.toString().trim();
    if (specification.length() == 0) {
        return "";
    }
    Configuration conf = ConfigurationManager.createNewDefaultConfiguration().getDataObject();
    conf.setHeadWidth(25);
    conf.setMainLifelineWidth(5);
    conf.setSubLifelineWidth(5);
    conf.setThreaded(true);
    conf.setGlue(3);
    ImagePaintDevice device = new ImagePaintDevice();
    TextHandler handler = new TextHandler(specification);
    try {
        new Diagram(conf, handler, device).generate();
    } catch (Exception e) {
        int error = handler.getLineNumber();
        StringBuffer code = new StringBuffer("<br><tt>");
        for (int i = 0; i < source.length; i++) {
            String html = source[i].replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\"", "&quot;");
            if (i == error) {
                html = "<FONT COLOR=\"red\"><U><B>" + html + "</B></U></FONT>";
            }
            code.append(html + "<br>");
        }
        throw new SequenceTagletException("Malformed diagram specification: " + e.getMessage(), "<DT><HR><B>Sequence Diagram:</B></DT>" + "<DD><B>Could not create sequence diagram: " + "<font color=\"red\">" + e.getMessage() + "</font></B>" + code.toString() + "</DD>");
    }
    String fileName = imageBaseName + ".png";
    File imageFile = new File(diagramDirectory, fileName);
    try {
        device.saveImage(imageFile.getAbsolutePath());
    } catch (IOException ioe) {
        throw new SequenceTagletException("Could not save diagram image: " + ioe.getMessage(), "");
    }
    if (diagramTitle == null) {
        diagramTitle = "Sequence Diagram " + imageBaseName;
    }
    String imageUrl = path + imageSubDirectory + "/" + fileName;
    String anchor = "<A name=\"" + imageBaseName + "\"/>";
    return "<DT><HR><B>" + anchor + diagramTitle + ":</B><P></DT>" + "<DD><img src='" + imageUrl + "'></DD>";
}
Also used : Configuration(net.sf.sdedit.config.Configuration) ImagePaintDevice(net.sf.sdedit.ui.ImagePaintDevice) TextHandler(net.sf.sdedit.text.TextHandler) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) Diagram(net.sf.sdedit.diagram.Diagram)

Example 3 with ImagePaintDevice

use of net.sf.sdedit.ui.ImagePaintDevice in project abstools by abstools.

the class Editor method saveImage.

/**
 * Saves the current diagram as a PNG image file whose name is chosen by the
 * user. Asks for confirmation, if a file would be overwritten.
 *
 * @throws IOException
 *             if the image file cannot be written due to an i/o error
 */
void saveImage() throws IOException {
    String code = getUI().getCode().trim();
    if (code.equals("")) {
        return;
    }
    ImagePaintDevice ipd = new ImagePaintDevice();
    TextHandler handler = new TextHandler(code);
    Configuration conf = getUI().getConfiguration().getDataObject();
    try {
        new Diagram(conf, handler, ipd).generate();
    } catch (Exception ex) {
        ui.errorMessage("The diagram source text has errors.");
        return;
    }
    Image image = ipd.getImage();
    if (image != null) {
        File current = null;
        if (!firstImageSaved) {
            current = ui.getCurrentFile();
            if (current != null) {
                current = current.getParentFile();
            }
            firstImageSaved = true;
        }
        String currentFile = null;
        if (ui.getCurrentFile() != null) {
            currentFile = ui.getCurrentFile().getName();
            int dot = currentFile.lastIndexOf('.');
            if (dot >= 0) {
                currentFile = currentFile.substring(0, dot + 1) + "png";
            }
        }
        File[] files = ui.getFiles(false, false, "save as PNG", currentFile, current, "PNG image", "png");
        File imageFile = files != null ? files[0] : null;
        if (imageFile != null && (!imageFile.exists() || 1 == ui.confirmOrCancel("Overwrite existing file " + imageFile.getName() + "?"))) {
            ipd.saveImage(imageFile);
            ui.message("Exported image as\n" + imageFile.getAbsolutePath());
        }
    }
}
Also used : ImagePaintDevice(net.sf.sdedit.ui.ImagePaintDevice) GlobalConfiguration(net.sf.sdedit.config.GlobalConfiguration) Configuration(net.sf.sdedit.config.Configuration) TextHandler(net.sf.sdedit.text.TextHandler) Image(java.awt.Image) File(java.io.File) IOException(java.io.IOException) XMLException(net.sf.sdedit.util.DocUtil.XMLException) Diagram(net.sf.sdedit.diagram.Diagram)

Aggregations

File (java.io.File)3 Configuration (net.sf.sdedit.config.Configuration)3 Diagram (net.sf.sdedit.diagram.Diagram)3 TextHandler (net.sf.sdedit.text.TextHandler)3 ImagePaintDevice (net.sf.sdedit.ui.ImagePaintDevice)3 IOException (java.io.IOException)2 Image (java.awt.Image)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 GlobalConfiguration (net.sf.sdedit.config.GlobalConfiguration)1 Exporter (net.sf.sdedit.server.Exporter)1 Bean (net.sf.sdedit.ui.components.configuration.Bean)1 XMLException (net.sf.sdedit.util.DocUtil.XMLException)1