Search in sources :

Example 1 with Pair

use of net.sf.sdedit.util.Pair in project abstools by abstools.

the class DiagramLoader method load.

/**
 * Loads a diagram from the text transmitted through the given
 * <tt>stream</tt>. If the text contains a line that starts with
 * <tt>&lt;?xml</tt>, it is interpreted as an XML file, containing the
 * diagram source as a CDATA section along with a configuration. Otherwise
 * the whole of the text is interpreted as a diagram source, and a default
 * configuration is used.
 *
 * @param stream
 *            the stream from where the diagram specification is read
 * @param encoding
 *            the encoding of the diagram specification
 * @return a pair of the diagram source and the configuration to be used for
 *         generating the diagram
 *
 * @throws IOException
 * @throws DocUtil.XMLException
 */
public static Pair<String, Bean<Configuration>> load(InputStream stream, String encoding) throws IOException, DocUtil.XMLException {
    InputStreamReader reader = new InputStreamReader(stream, encoding);
    BufferedReader buffered = new BufferedReader(reader);
    StringWriter stringWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(stringWriter);
    boolean xml = false;
    String line = buffered.readLine();
    while (line != null) {
        xml |= line.trim().startsWith("<?xml");
        writer.println(line);
        line = buffered.readLine();
    }
    writer.close();
    String source;
    Bean<Configuration> configuration = ConfigurationManager.createNewDefaultConfiguration();
    if (xml) {
        InputStream inputStream = new ByteArrayInputStream(stringWriter.toString().getBytes(encoding));
        try {
            Document document = DocUtil.readDocument(inputStream, encoding);
            source = DocUtil.evaluateCDATA(document, "/diagram/source");
            Element confElement = (Element) DocUtil.evalXPathAsNode(document, "/diagram/configuration");
            BeanConverter converter = new BeanConverter(configuration, document);
            converter.setValues(confElement);
        } finally {
            inputStream.close();
        }
    } else {
        source = stringWriter.toString();
    }
    return new Pair<String, Bean<Configuration>>(source, configuration);
}
Also used : InputStreamReader(java.io.InputStreamReader) Configuration(net.sf.sdedit.config.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) BeanConverter(net.sf.sdedit.ui.components.configuration.BeanConverter) PrintWriter(java.io.PrintWriter) Pair(net.sf.sdedit.util.Pair)

Example 2 with Pair

use of net.sf.sdedit.util.Pair in project abstools by abstools.

the class NoteManager method computeArrowAssociations.

public void computeArrowAssociations() {
    for (Note description : notes) {
        List<Message> msgs = messageAssociation.get(description.getNumber());
        if (msgs != null) {
            for (Message msg : msgs) {
                // note to this.
                if (msg.getArrow() != null) {
                    description.addTarget(msg.getArrow().getAnchor());
                }
            }
        }
        List<Pair<Lifeline, Integer>> pairs = eventAssociation.get(description.getNumber());
        if (pairs != null) {
            for (Pair<Lifeline, Integer> pair : pairs) {
                int x = pair.getFirst().getView().getLeft() + pair.getFirst().getView().getWidth() / 2;
                Point p = new Point(x, pair.getSecond());
                description.addTarget(p);
            }
        }
    }
}
Also used : Message(net.sf.sdedit.message.Message) Note(net.sf.sdedit.drawable.Note) Point(java.awt.Point) Point(java.awt.Point) Pair(net.sf.sdedit.util.Pair)

Example 3 with Pair

use of net.sf.sdedit.util.Pair in project abstools by abstools.

the class TextHandler method getEventAssociation.

/**
 * If the current line specifies an association of a note to the current
 * vertical position of a lifeline, this method returns a pair consisting of
 * the lifeline and the note number.
 *
 * @return a pair of a lifeline and a note number, if the current line
 *         specifies and association between the note and the lifeline,
 *         otherwise <tt>null</tt>
 */
public Pair<Lifeline, Integer> getEventAssociation() throws SyntaxError {
    if (section == 0) {
        throw new IllegalStateException("not all objects have been read");
    }
    if (currentLine == null) {
        throw new IllegalStateException("nothing to read");
    }
    String[] parts = Grep.parse("\\((\\d+)\\)\\s*(\\w+)", currentLine);
    if (parts == null) {
        return null;
    }
    int number = Integer.parseInt(parts[0]);
    String obj = parts[1];
    Lifeline line = diagram.getLifeline(obj);
    if (line == null) {
        throw new SyntaxError(this, obj + " does not exist");
    }
    return new Pair<Lifeline, Integer>(line, number);
}
Also used : SyntaxError(net.sf.sdedit.error.SyntaxError) Lifeline(net.sf.sdedit.diagram.Lifeline) Pair(net.sf.sdedit.util.Pair)

Aggregations

Pair (net.sf.sdedit.util.Pair)3 Point (java.awt.Point)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Configuration (net.sf.sdedit.config.Configuration)1 Lifeline (net.sf.sdedit.diagram.Lifeline)1 Note (net.sf.sdedit.drawable.Note)1 SyntaxError (net.sf.sdedit.error.SyntaxError)1 Message (net.sf.sdedit.message.Message)1 BeanConverter (net.sf.sdedit.ui.components.configuration.BeanConverter)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1