xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sudhakar S <yessudha...@hotmail.com>
Subject Re: Multiple Group Elements Selection and Moving
Date Mon, 19 Jun 2006 06:24:05 GMT

Hi Thomas,

As per your suggestion i have changed the code. but still i haven't able to
resolve the problem. I think there is some problem in finding start points.

If i find the start position in mouse down action, it works for single group
elements move. but multiple elements it is not working.
SVGPoint startPt = localPt((Element)selElement.getParentNode(),
elEvt.getClientX(), elEvt.getClientY()); 


private class OnDownAction implements EventListener {

        public void handleEvent(Event evt) {
            DOMMouseEvent elEvt = (DOMMouseEvent)evt;
            action = DRAG;
            selElement = (Element)elEvt.getTarget();

           
if(!((Element)selElement.getParentNode()).getAttribute("id").endsWith("Grp"))
                return;

            actionNode = elEvt.getTarget();

            Point sPos = new Point((int)elEvt.getClientX(),
(int)elEvt.getClientY());

            if (elEvt.getCtrlKey()) {
                moveGroups.put(selElement.getParentNode(), sPos);
            } else {
                moveGroups = new Hashtable();
                moveGroups.put(selElement.getParentNode(), sPos);
            }
        }

    }

   private class OnMoveAction implements EventListener {
        public void handleEvent(Event evt) {

            if (actionNode == null)return;

            DOMMouseEvent elEvt = (DOMMouseEvent)evt;

            float currentX = elEvt.getClientX();
            float currentY = elEvt.getClientY();

            if (action == DRAG) {

                Enumeration enu = moveGroups.keys();
                while (enu.hasMoreElements()) {

                    Element ele = (Element)enu.nextElement();
                    Element pEle = (Element)ele.getParentNode();
                    
                    Point sPos = (Point)moveGroups.get(ele);
                    
                    SVGPoint sp = localPt(pEle, (int)sPos.getX(),
(int)sPos.getY());
                    SVGPoint cp = localPt(pEle, (int)currentX,
(int)currentY);

                    float dx = cp.getX() - sp.getX();
                    float dy = cp.getY() - sp.getY();
                    
                    ele.setAttributeNS(null, "transform", "translate(" + dx
+ "," + dy + ")");
                }
            }

        }
    }


Herewith i also attached the full sample applicaion file for your reference.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.w3c.dom.*;
import org.w3c.dom.svg.*;
import javax.swing.*;
import java.awt.*;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.bridge.UpdateManager;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.apache.batik.swing.*;
import org.apache.batik.dom.svg.*;
import org.apache.batik.dom.events.*;
import java.util.Hashtable;
import java.util.Enumeration;

public class SVGApplication10 {

    static JSVGCanvas svgCanvas = new JSVGCanvas();
    Hashtable moveGroups = new Hashtable();
    Hashtable offsetMap = new Hashtable();
    JFrame frame;
    String DRAG = "drag";
    String action;
    EventTarget actionNode;
    SVGPoint startPt;
    Element selElement = null;

    public static void main(String[] args) {
        SVGApplication10 app = new SVGApplication10();
    }

    public SVGApplication10() {
        JFrame f = new JFrame();
        f.getContentPane().add(createComponents());
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.setSize(600, 400);
        f.setVisible(true);
    }

    public JComponent createComponents() {
        final JPanel panel = new JPanel(new BorderLayout());
        JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
        svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        DOMImplementation impl =
SVGDOMImplementation.getDOMImplementation();
        SVGDocument doc =
(SVGDocument)impl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI,
"svg", null);
        panel.add("North", p);
        JSVGScrollPane scroll = new JSVGScrollPane(svgCanvas);
        panel.add("Center", scroll);
        svgCanvas.setDocument(doc);
        registerListeners();
        return panel;
    }

    public void registerListeners() {
        svgCanvas.addSVGDocumentLoaderListener(new
SVGDocumentLoaderAdapter() {
            public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
                System.out.println("Document Loading...");
            }
            public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
                System.out.println("Document Loaded.");
            }
        });
        svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
            public void gvtBuildStarted(GVTTreeBuilderEvent e) {
                System.out.println("gvt bUILD Started...");
            }
            public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
                System.out.println("GVT Build Done...");
                frame.pack();
            }
        });


        svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
            public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
                System.out.println("Renderer Started...");
            }
            public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                System.out.println("Renderer Completed...");

                UpdateManager um = svgCanvas.getUpdateManager();
                um.getUpdateRunnableQueue().invokeLater(new Runnable() {
                    public void run() {
                        resizeCircle();
                    }
                });
            }
        });

        SVGDocument doc = svgCanvas.getSVGDocument();

        SVGSVGElement svgRoot = doc.getRootElement();
        EventTarget t = (EventTarget)svgRoot;
        t.addEventListener("SVGLoad", new OnLoadAction(), false);
    }


    public class OnLoadAction implements EventListener {
        public void handleEvent(Event evt) {
        }
    }

    private void resizeCircle() throws DOMException {
        try {
            SVGDocument doc = svgCanvas.getSVGDocument();
            SVGSVGElement svgRoot = doc.getRootElement();

            Element rect =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "rect");
            rect.setAttributeNS(null, "id", "bgrectangle");
            rect.setAttributeNS(null, "x", "0");
            rect.setAttributeNS(null, "y", "0");
            rect.setAttributeNS(null, "width", "100%");
            rect.setAttributeNS(null, "height", "100%");
            rect.setAttributeNS(null, "pointer-events", "fill");
            rect.setAttributeNS(null, "style", "fill:none");
            rect.setAttributeNS(null, "stroke", "darkblue");
            rect.setAttributeNS(null, "stroke-width", "1");
            doc.getRootElement().appendChild(rect);

            Element rect1Grp =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "g");
            rect1Grp.setAttributeNS(null, "id", "rect1Grp");

            Element rect1 =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "rect");
            rect1.setAttributeNS(null, "id", "rec");
            rect1.setAttributeNS(null, "x", "100");
            rect1.setAttributeNS(null, "y", "100");
            rect1.setAttributeNS(null, "width", "20");
            rect1.setAttributeNS(null, "height", "20");
            rect1.setAttributeNS(null, "style", "fill:red; stroke:black");
            rect1Grp.appendChild(rect1);
            doc.getRootElement().appendChild(rect1Grp);

            Element rect2Grp =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "g");
            rect2Grp.setAttributeNS(null, "id", "rect2Grp");
            Element rect2 =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "rect");
            rect2.setAttributeNS(null, "id", "rec");
            rect2.setAttributeNS(null, "x", "150");
            rect2.setAttributeNS(null, "y", "150");
            rect2.setAttributeNS(null, "width", "20");
            rect2.setAttributeNS(null, "height", "20");
            rect2.setAttributeNS(null, "style", "fill:green; stroke:black");
            rect2Grp.appendChild(rect2);
            doc.getRootElement().appendChild(rect2Grp);


            Element rect3Grp =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "g");
            rect3Grp.setAttributeNS(null, "id", "rect3Grp");

            Element rect3 =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "rect");
            rect3.setAttributeNS(null, "id", "rec");
            rect3.setAttributeNS(null, "x", "200");
            rect3.setAttributeNS(null, "y", "200");
            rect3.setAttributeNS(null, "width", "20");
            rect3.setAttributeNS(null, "height", "20");
            rect3.setAttributeNS(null, "style", "fill:blue; stroke:black");
            rect3Grp.appendChild(rect3);
            doc.getRootElement().appendChild(rect3Grp);

            EventTarget t = (EventTarget)svgRoot;
            t.addEventListener("mousemove", new OnMoveAction(), false);
            t.addEventListener("mousedown", new OnDownAction(), false);
            t.addEventListener("mouseup", new OnUpAction(), false);


            EventTarget t1 = (EventTarget)rect;
            t1.addEventListener("mousemove", new OnMoveAction(), false);
            t1.addEventListener("mousedown", new OnUpAction(), false);
            t1.addEventListener("mouseup", new OnUpAction(), false);

        } catch (Exception e) {}
    }

    private SVGPoint localPt(Element elem, int x, int y) {
        SVGDocument svgDocument = svgCanvas.getSVGDocument();
        SVGMatrix mat = ((SVGLocatable)elem).getScreenCTM();
        SVGMatrix imat = mat.inverse();
        SVGPoint cPt = svgDocument.getRootElement().createSVGPoint();
        cPt.setX(x);
        cPt.setY(y);
        cPt = cPt.matrixTransform(imat);
        return cPt;
    }

    SVGPoint getPosition(Element selElement) {
       SVGLocatable recLoc = (SVGLocatable)selElement;
       SVGMatrix mat =
recLoc.getTransformToElement((SVGElement)selElement.getParentNode());
       SVGPoint cPt =
svgCanvas.getSVGDocument().getRootElement().createSVGPoint();
       SVGRect bbox = recLoc.getBBox();
       cPt.setX(bbox.getX());
       cPt.setY(bbox.getY());
       cPt = cPt.matrixTransform(mat);
       return cPt;
   }

   int startX, startY; 

   private class OnDownAction implements EventListener {

        public void handleEvent(Event evt) {
            DOMMouseEvent elEvt = (DOMMouseEvent)evt;
            action = DRAG;
            selElement = (Element)elEvt.getTarget();

           
if(!((Element)selElement.getParentNode()).getAttribute("id").endsWith("Grp"))
                return;

            actionNode = elEvt.getTarget();

            Point sPos = new Point((int)elEvt.getClientX(),
(int)elEvt.getClientY());

            if (elEvt.getCtrlKey()) {
                moveGroups.put(selElement.getParentNode(), sPos);
            } else {
                moveGroups = new Hashtable();
                moveGroups.put(selElement.getParentNode(), sPos);
            }
        }

    }

   private class OnMoveAction implements EventListener {
        public void handleEvent(Event evt) {

            if (actionNode == null)return;

            DOMMouseEvent elEvt = (DOMMouseEvent)evt;

            float currentX = elEvt.getClientX();
            float currentY = elEvt.getClientY();

            if (action == DRAG) {

                Enumeration enu = moveGroups.keys();
                while (enu.hasMoreElements()) {

                    Element ele = (Element)enu.nextElement();
                    Element pEle = (Element)ele.getParentNode();
                    
                    Point sPos = (Point)moveGroups.get(ele);
                    
                    SVGPoint sp = localPt(pEle, (int)sPos.getX(),
(int)sPos.getY());
                    SVGPoint cp = localPt(pEle, (int)currentX,
(int)currentY);

                    float dx = cp.getX() - sp.getX();
                    float dy = cp.getY() - sp.getY();
                    
                    ele.setAttributeNS(null, "transform", "translate(" + dx
+ "," + dy + ")");
                }
            }

        }
    }
    
    
    private class OnUpAction implements EventListener {
        public void handleEvent(Event evt) {
            if (actionNode != null) {
                actionNode = null;
            }
        }
    }
}


Thanks,
Sudhakar


--
View this message in context: http://www.nabble.com/Multiple-Group-Elements-Selection-and-Moving-t1799038.html#a4931198
Sent from the Batik - Users forum at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Mime
View raw message