/** * 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; }
@Override public void notifyDrawingAction(SVGHandle handle, Point2D point, int modifier, int type) { // according to the type of the event for the drawing action switch (type) { case DRAWING_MOUSE_RELEASED: // scaling the two points to fit a 1.1 zoom factor Point2D scaledPoint = handle.getTransformsManager().getScaledPoint(point, true); this.drawingPoint = scaledPoint; textDialog.showDialog(handle.getSVGFrame(), handle); resetDrawing(); break; } }
@Override public Set<SelectionItem> getSelectionItems(SVGHandle handle, Set<Element> elements, int level) { // clearing the stored values rotationSkewCenterPoint = null; // getting the first element of this set Element element = elements.iterator().next(); // the set of the items that will be returned Set<SelectionItem> items = new HashSet<SelectionItem>(); // getting the bounds of the element Rectangle2D bounds = getTransformedShape(handle, element, false).getBounds2D(); // scaling the bounds in the canvas space Rectangle2D scaledWholeBounds = handle.getTransformsManager().getScaledRectangle(bounds, false); // getting the selection items according to the level type switch (level) { case Selection.SELECTION_LEVEL_DRAWING: case Selection.SELECTION_LEVEL_1: items.addAll(getResizeSelectionItems(handle, elements, scaledWholeBounds)); break; case Selection.SELECTION_LEVEL_2: items.addAll(getRotateSelectionItems(handle, elements, scaledWholeBounds)); break; } return items; }
/** * 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); } }
@Override public CanvasPainter showTranslateAction( SVGHandle handle, Set<Element> elementSet, Point2D firstPoint, Point2D currentPoint) { CanvasPainter canvasPainter = null; // getting the element that will undergo the action Element element = elementSet.iterator().next(); // getting the initial line Shape shape = getTransformedShape(handle, element, true); if (shape != null) { Point2D translationCoefficients = new Point2D.Double( currentPoint.getX() - firstPoint.getX(), currentPoint.getY() - firstPoint.getY()); AffineTransform transform = AffineTransform.getTranslateInstance( translationCoefficients.getX(), translationCoefficients.getY()); // computing the screen scaled shape final Shape fshape = handle.getTransformsManager().getScaledShape(shape, false, transform); // creating the set of the clips final Set<Rectangle2D> fclips = new HashSet<Rectangle2D>(); fclips.add(fshape.getBounds2D()); canvasPainter = new CanvasPainter() { @Override public void paintToBeDone(Graphics2D g) { g = (Graphics2D) g.create(); g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(strokeColor); g.draw(fshape); g.dispose(); } @Override public Set<Rectangle2D> getClip() { return fclips; } }; } return canvasPainter; }
@Override public Shape getShape(SVGHandle handle, Element element, boolean isOutline) { Shape shape = handle.getSvgElementsManager().getGeometryOutline(element); if (shape == null || shape.getBounds().getWidth() == 0 || shape.getBounds().getHeight() == 0) { // getting the location of the text double x = EditorToolkit.getAttributeValue(element, xAtt); double y = EditorToolkit.getAttributeValue(element, yAtt); shape = new Rectangle2D.Double(x, y, 1, 1); } return shape; }
@Override public Shape getTransformedShape(SVGHandle handle, Element element, boolean isOutline) { Shape shape = getShape(handle, element, isOutline); if (shape != null) { // getting the transformed points AffineTransform af = handle.getSvgElementsManager().getTransform(element); if (af != null && !af.isIdentity()) { shape = af.createTransformedShape(shape); } } return shape; }
/** * returns the transformed shape corresponding to this element * * @param handle a svg handle * @param element an element * @param isOutline whether the returned shape should only be used for showing the action * @return the transformed shape corresponding to this element */ public Shape getTransformedShape(SVGHandle handle, Element element, boolean isOutline) { Shape shape = getShape(handle, element, isOutline); if (shape != null) { // getting the transformation of this element AffineTransform af = handle.getSvgElementsManager().getTransform(element); // transforming this shape if (!af.isIdentity()) { shape = af.createTransformedShape(shape); } } return shape; }
/** * computes the new coordinates of the element according to the transform a returns an undo/redo * action * * @param handle a svg handle * @param element the element that will be transformed * @param transform the transform to apply * @param actionUndoRedoLabel the action undo/redo label * @return an undo/redo action */ protected UndoRedoAction applyTransform( final SVGHandle handle, final Element element, AffineTransform transform, String actionUndoRedoLabel) { // getting the initial transform final AffineTransform initialTransform = handle.getSvgElementsManager().getTransform(element); // getting the new transform final AffineTransform newTransform = new AffineTransform(initialTransform); newTransform.preConcatenate(transform); // setting the new x and y attributes for the elements Runnable executeRunnable = new Runnable() { @Override public void run() { handle.getSvgElementsManager().setTransform(element, newTransform); } }; // the undo runnable Runnable undoRunnable = new Runnable() { @Override public void run() { handle.getSvgElementsManager().setTransform(element, initialTransform); } }; // executing the action and creating the undo/redo action HashSet<Element> elements = new HashSet<Element>(); elements.add(element); UndoRedoAction undoRedoAction = ShapeToolkit.getUndoRedoAction( actionUndoRedoLabel, executeRunnable, undoRunnable, elements); return undoRedoAction; }
@Override public CanvasPainter showAction( SVGHandle handle, int level, Set<Element> elementSet, SelectionItem item, Point2D firstPoint, Point2D currentPoint) { // getting the element that will undergo the action Element element = elementSet.iterator().next(); // the canvas painter that should be returned CanvasPainter painter = null; // whether the shape should be painted boolean canPaintShape = true; // the shape that will be painted Shape shape = null; // getting the action transform AffineTransform actionTransform = null; switch (level) { case 0: // getting the resize transform actionTransform = getResizeTransform(handle, element, item, firstPoint, currentPoint); break; case 1: if (item.getType() == SelectionItem.CENTER) { // storing the center point for the rotate action rotationSkewSelectionItemCenterPoint = currentPoint; item.setPoint(currentPoint); canPaintShape = false; } else if (item.getType() == SelectionItem.NORTH_WEST || item.getType() == SelectionItem.NORTH_EAST || item.getType() == SelectionItem.SOUTH_EAST || item.getType() == SelectionItem.SOUTH_WEST) { // getting the rotation transform actionTransform = getRotationTransform(handle, element, firstPoint, currentPoint); } else { // getting the skew transform actionTransform = getSkewTransform(handle, element, firstPoint, currentPoint, item); } break; } if (actionTransform != null) { // getting the initial shape shape = getShape(handle, element, true); // getting the element's transform AffineTransform transform = handle.getSvgElementsManager().getTransform(element); // concatenating the action transform to the element's transform transform.preConcatenate(actionTransform); // computing the screen scaled shape shape = handle.getTransformsManager().getScaledShape(shape, false, transform); } if (canPaintShape && shape != null) { final Shape fshape = shape; // creating the set of the clips final HashSet<Rectangle2D> fclips = new HashSet<Rectangle2D>(); fclips.add(fshape.getBounds2D()); painter = new CanvasPainter() { @Override public void paintToBeDone(Graphics2D g) { g = (Graphics2D) g.create(); g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(strokeColor); g.draw(fshape); g.dispose(); } @Override public Set<Rectangle2D> getClip() { return fclips; } }; } return painter; }