Search in sources :

Example 6 with Configuration

use of net.sf.sdedit.config.Configuration 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
 *         <img> 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 7 with Configuration

use of net.sf.sdedit.config.Configuration in project abstools by abstools.

the class PrintDialog method reinitialize.

private void reinitialize() {
    String source = ui.getCode();
    Configuration configuration = ui.getConfiguration().getDataObject();
    exporter = new MultipageExporter(printerProperties.getDataObject(), source, configuration);
    try {
        exporter.init();
    } catch (RuntimeException re) {
        throw re;
    } catch (SemanticError se) {
    /* ignored */
    } catch (SyntaxError se) {
    /* ignored */
    }
    int scale = (int) (100 * exporter.getScale());
    scaleLabel.setText("Zoom factor: " + scale + " %");
    preview.setViewportView(exporter);
}
Also used : MultipageExporter(net.sf.sdedit.multipage.MultipageExporter) PrintConfiguration(net.sf.sdedit.config.PrintConfiguration) Configuration(net.sf.sdedit.config.Configuration) SyntaxError(net.sf.sdedit.error.SyntaxError) SemanticError(net.sf.sdedit.error.SemanticError)

Example 8 with Configuration

use of net.sf.sdedit.config.Configuration 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

Configuration (net.sf.sdedit.config.Configuration)8 File (java.io.File)4 GlobalConfiguration (net.sf.sdedit.config.GlobalConfiguration)3 Diagram (net.sf.sdedit.diagram.Diagram)3 TextHandler (net.sf.sdedit.text.TextHandler)3 ImagePaintDevice (net.sf.sdedit.ui.ImagePaintDevice)3 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Image (java.awt.Image)1 Point (java.awt.Point)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Action (javax.swing.Action)1 PrintConfiguration (net.sf.sdedit.config.PrintConfiguration)1