Search in sources :

Example 1 with TextHandler

use of net.sf.sdedit.text.TextHandler in project abstools by abstools.

the class Engine method _render.

private synchronized void _render(final boolean syntaxCheckOnly) {
    editor.getUI().leaveFilterMode();
    Diagram diagram = editor.getUI().renderDiagram();
    // method is not synchronized. Why?
    if (diagram == null) {
        return;
    }
    DiagramError error = editor.getUI().getDiagramError();
    if (error == null) {
        editor.getUI().setErrorStatus(false, "", -1, -1);
        if (diagram.getFragmentManager().openFragmentsExist()) {
            editor.getUI().setErrorStatus(true, "Warning: There are open comments. Use [c:<type> <text>]...[/c]", -1, -1);
        }
        int noteNumber = diagram.getNextFreeNoteNumber();
        if (noteNumber == 0) {
            editor.getUI().setStatus("");
        } else {
            editor.getUI().setStatus("Next note number: " + diagram.getNextFreeNoteNumber());
        }
    } else {
        editor.getUI().setStatus("");
        if (error instanceof FatalError) {
            FatalError fatal = (FatalError) error;
            System.err.println("********************************************************");
            System.err.println("*                                                      *");
            System.err.println("*            A FATAL ERROR HAS OCCURED.                *");
            System.err.println("*                                                      *");
            System.err.println("********************************************************");
            error.getCause().printStackTrace();
            // cautiously embedding this call into a try/catch-block
            try {
                handle(diagram, fatal.getCause());
            } catch (Throwable t) {
                t.printStackTrace();
            }
        } else {
            TextHandler handler = (TextHandler) error.getProvider();
            String prefix = "";
            if (error instanceof SemanticError) {
                prefix = diagram.isThreaded() && diagram.getCallerThread() != -1 ? "Thread " + diagram.getCallerThread() + ": " : "";
            }
            editor.getUI().setErrorStatus(false, prefix + error.getMessage(), handler.getLineBegin() - 1, handler.getLineEnd());
        }
    }
    if (!syntaxCheckOnly) {
        editor.getUI().redraw();
    }
}
Also used : FatalError(net.sf.sdedit.error.FatalError) SemanticError(net.sf.sdedit.error.SemanticError) TextHandler(net.sf.sdedit.text.TextHandler) DiagramError(net.sf.sdedit.error.DiagramError) Diagram(net.sf.sdedit.diagram.Diagram)

Example 2 with TextHandler

use of net.sf.sdedit.text.TextHandler in project abstools by abstools.

the class ExportMapAction method actionPerformed.

/**
 * See {@linkplain ExportMapAction}.
 *
 * @param e
 */
public void actionPerformed(ActionEvent e) {
    Diagram diagram = editor.getUI().getDiagram();
    if (diagram == null || diagram.getLifelines().isEmpty()) {
        return;
    }
    TextHandler textHandler = (TextHandler) diagram.getDataProvider();
    File currentFile = editor.getUI().getCurrentFile();
    if (currentFile == null) {
        editor.getUI().message("Please save the diagram as a file first.");
        return;
    }
    String name = currentFile.getName();
    int dot = Math.min(name.length(), name.lastIndexOf('.'));
    name = currentFile.getName().substring(0, dot);
    if (directory == null) {
        directory = currentFile.getParentFile();
    }
    File[] target = editor.getUI().getFiles(false, false, "Export HTML map file", name + ".html", directory, "HTML files", "html");
    if (target != null && target.length > 0) {
        directory = target[0].getParentFile();
        if (!target[0].exists() || 1 == editor.getUI().confirmOrCancel("Overwrite existing file:\n" + target[0].getAbsolutePath() + "?")) {
            try {
                generateMapFile(diagram, textHandler, name, target[0]);
            } catch (IOException ex) {
                editor.getUI().errorMessage("The map file could not be saved due to an exception of type\n" + ex.getClass().getSimpleName() + " with the message: " + ex.getMessage());
            }
        }
    }
}
Also used : TextHandler(net.sf.sdedit.text.TextHandler) IOException(java.io.IOException) File(java.io.File) Diagram(net.sf.sdedit.diagram.Diagram)

Example 3 with TextHandler

use of net.sf.sdedit.text.TextHandler 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 4 with TextHandler

use of net.sf.sdedit.text.TextHandler 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 5 with TextHandler

use of net.sf.sdedit.text.TextHandler in project abstools by abstools.

the class Tab method renderDiagram.

void renderDiagram() {
    PanelPaintDevice paintDevice = new PanelPaintDevice(true);
    for (PanelPaintDeviceListener listener : ppdListeners) {
        paintDevice.addListener(listener);
    }
    TextHandler textHandler = new TextHandler(getCode());
    Diagram diagram = new Diagram(configuration.getDataObject(), textHandler, paintDevice);
    DiagramError newError = null;
    try {
        diagram.generate();
    } catch (RuntimeException e) {
        newError = new FatalError(textHandler, e);
    } catch (DiagramError e) {
        newError = e;
    }
    synchronized (diagramStack) {
        diagramStack.addLast(diagram);
        synchronized (this) {
            error = newError;
        }
    }
}
Also used : FatalError(net.sf.sdedit.error.FatalError) PanelPaintDeviceListener(net.sf.sdedit.ui.PanelPaintDeviceListener) PanelPaintDevice(net.sf.sdedit.ui.PanelPaintDevice) TextHandler(net.sf.sdedit.text.TextHandler) DiagramError(net.sf.sdedit.error.DiagramError) Diagram(net.sf.sdedit.diagram.Diagram)

Aggregations

Diagram (net.sf.sdedit.diagram.Diagram)7 TextHandler (net.sf.sdedit.text.TextHandler)7 File (java.io.File)4 IOException (java.io.IOException)3 Configuration (net.sf.sdedit.config.Configuration)3 ImagePaintDevice (net.sf.sdedit.ui.ImagePaintDevice)3 DiagramError (net.sf.sdedit.error.DiagramError)2 FatalError (net.sf.sdedit.error.FatalError)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 BoxLayout (javax.swing.BoxLayout)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 GlobalConfiguration (net.sf.sdedit.config.GlobalConfiguration)1 SemanticError (net.sf.sdedit.error.SemanticError)1 Exporter (net.sf.sdedit.server.Exporter)1 PanelPaintDevice (net.sf.sdedit.ui.PanelPaintDevice)1