Exemplo n.º 1
0
  /**
   * creates a svg element by specifiying its parameters
   *
   * @param handle the current svg handle
   * @param text the text for the new element
   * @return the created svg element
   */
  public Element createElement(SVGHandle handle, String text) {

    // the edited document
    Document doc = handle.getScrollPane().getSVGCanvas().getDocument();

    // creating the text element
    final Element element =
        doc.createElementNS(doc.getDocumentElement().getNamespaceURI(), handledElementTagName);

    // getting the last color that has been used by the user
    String colorString = Editor.getColorChooser().getColorString(ColorManager.getCurrentColor());
    element.setAttributeNS(null, "style", "fill:" + colorString + ";stroke:none;");
    element.setAttributeNS(null, "style", "font-size:12pt;fill:" + colorString + ";");

    EditorToolkit.setAttributeValue(element, xAtt, drawingPoint.getX());
    EditorToolkit.setAttributeValue(element, yAtt, drawingPoint.getY());

    // creating the text node
    Text textValue = doc.createTextNode(text);
    element.appendChild(textValue);

    // inserting the element in the document and handling the undo/redo
    // support
    insertShapeElement(handle, element);
    handle.getSelection().handleSelection(element, false, false);

    return element;
  }
Exemplo n.º 2
0
  /**
   * inserts the given shape element in a svg document
   *
   * @param handle the current svg handle
   * @param shapeElement the shape element to be inserted in the document
   */
  protected void insertShapeElement(final SVGHandle handle, final Element shapeElement) {

    if (shapeElement != null) {

      // getting the current parent element of all the edited nodes
      final Element parentElement = handle.getSelection().getParentElement();

      // the execute runnable
      Runnable executeRunnable =
          new Runnable() {

            @Override
            public void run() {

              parentElement.appendChild(shapeElement);
              handle.getSelection().clearSelection();
              handle.getSelection().handleSelection(shapeElement, false, false);
            }
          };

      // the undo runnable
      Runnable undoRunnable =
          new Runnable() {

            @Override
            public void run() {

              parentElement.removeChild(shapeElement);
            }
          };

      // executing the action and creating the undo/redo action
      HashSet<Element> elements = new HashSet<Element>();
      elements.add(shapeElement);
      UndoRedoAction undoRedoAction =
          ShapeToolkit.getUndoRedoAction(undoRedoLabel, executeRunnable, undoRunnable, elements);

      UndoRedoActionList actionlist = new UndoRedoActionList(undoRedoLabel, false);
      actionlist.add(undoRedoAction);
      handle.getUndoRedo().addActionList(actionlist, false);
    }
  }