xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fransemail <fransem...@libero.it>
Subject Re: Drag and drop
Date Sat, 15 May 2004 11:38:37 GMT
Thanks Andres Toussaint! I'm not able and I've use your suggests without 
result. Please check this source code, it's my implementation of DnD.

public class DnD {

        .......

    SketchPanel svgCanvas = new SketchPanel(); // A class that extends 
JSVGCanvas whit a method to draw a selection box
    Window window;
    Document document;  // current document
    protected SVGOMElement svgRoot; // root element
    protected SVGOMElement svgCurrentElement; //selected element


    public JComponent createComponents() {
        .......

        // Set the JSVGCanvas listeners.
        svgCanvas.addSVGDocumentLoaderListener(new 
SVGDocumentLoaderAdapter() {
            public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
                label.setText("Document Loading...");
            }
            public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
                label.setText("Document Loaded.");
            }
        });

        svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
            public void gvtBuildStarted(GVTTreeBuilderEvent e) {
            }
            public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
                label.setText("Build Done.");                              
                
svgCanvas.setMySize((Dimension)svgCanvas.getSVGDocumentSize());              
 
                frame.setSize((Dimension)svgCanvas.getSVGDocumentSize());
                frame.pack();
            }
        });

        svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
            public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
                label.setText("Rendering Started...");
            }
            public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                label.setText("");
            }
        });

        svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        svgCanvas.addSVGLoadEventDispatcherListener
            (new SVGLoadEventDispatcherAdapter() {
                    public void svgLoadEventDispatchStarted
                        (SVGLoadEventDispatcherEvent e) {
                        document = svgCanvas.getSVGDocument();
                        svgCanvas.setSVGRoot();
                        window = svgCanvas.getUpdateManager().  // 
Window window
                        getScriptingEnvironment().createWindow();
                        registerListeners();
                    }
                });
            ...........
    }

    public void registerListeners() {
        // Gets an element from the loaded document.
        svgRoot = (SVGOMElement)svgCanvas.getSVGDocument().getRootElement();
        EventTarget root = (EventTarget)svgRoot;

        // Adds a 'onmousedown' listener
        root.addEventListener( SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE , 
new OnNewClickAction(), false);
   }

    public void removeListeners() {
        // Gets an element from the loaded document.
        svgRoot = (SVGOMElement)svgCanvas.getSVGDocument().getRootElement();
        EventTarget root = (EventTarget)svgRoot;
        // remove listener
        root.removeEventListener( SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE 
, new OnDownAction(), false);
   }   

    public void registerListeners(EventTarget aTarget) {
        aTarget.addEventListener( SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE 
, new OnDownAction(), false);
        aTarget.addEventListener( SVGConstants.SVG_MOUSEUP_EVENT_TYPE , 
new OnUpAction(), false);
        aTarget.addEventListener( SVGConstants.SVG_MOUSEMOVE_EVENT_TYPE 
, new OnMoveAction(), false);
   }

    public void removeListeners(EventTarget aTarget) {
        aTarget.removeEventListener( 
SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE , new OnDownAction(), false);
        aTarget.removeEventListener( SVGConstants.SVG_MOUSEUP_EVENT_TYPE 
, new OnUpAction(), false);
        aTarget.removeEventListener( 
SVGConstants.SVG_MOUSEMOVE_EVENT_TYPE , new OnMoveAction(), false);
   }
   
public class OnMoveAction implements EventListener {
     public void handleEvent(Event evt) {
            DOMMouseEvent q = (DOMMouseEvent)evt;        
         if(selected){
             NamedNodeMap map = node.getAttributes();
             //place it so that the mouse-pointer is in the center of 
the object
             SVGOMElement svgCurrentElement = (SVGOMElement)target;
             SVGContext context = svgCurrentElement.getSVGContext();
             Rectangle2D box2D = context.getBBox();
             int height = (int)box2D.getX();
             int width = (int)box2D.getY();
            
             AffineTransform AT = new AffineTransform();
             
AT.translate((double)(q.getClientX()-width/2),(double)(q.getClientY()-height/2));
             SVGGraphicsElement rect = 
(SVGGraphicsElement)svgCurrentElement;
             context.setScreenTransform( AT );
         }
     }
 }   
   
    public class OnDownAction implements EventListener {
        public void handleEvent(Event evt) {
            DOMMouseEvent q = (DOMMouseEvent)evt;
            target = q.getTarget();
            removeListeners();
            registerListeners(target);
            node = (Node)target;
            selected = true;   
        }
    }

    public class OnUpAction implements EventListener {
        public void handleEvent(Event evt) {   
            removeListeners(target);
            registerListeners();
            target = null;
            selected = false;    }
    }
   
}
 
 
 --
 Email.it, the professional e-mail, gratis per te: http://www.email.it/f
 
 Sponsor:
 
 Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=&d=15-5

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


Mime
View raw message