Hi Антон,

On Fri, Mar 15, 2013 at 02:51:26AM +0400, Антон Борисов wrote:
> Why this listener don't work for ConnectorShape's properties StartShape
> EndShape?

the base class of all shapes has an empty implementation
http://opengrok.adfinis-sygroup.org/source/xref/aoo-trunk/main/svx/source/unodraw/unoshape.cxx#2054
you'll find this in several places, for example
https://issues.apache.org/ooo/show_bug.cgi?id=121263

I'm not sure if this is for performance issues, or simply a bug. I'm not
aware of this being reported, so please open a new bug, this will help
improve the API.

> So is there exists workaround?

add an old css::document::XEventListener at the document model, you'll
get notified of ShapeModified event, EventObject.Source is the shape;
something like the attached source.


Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina
/****************************************************************
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 *************************************************************/
package com.example;

import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.document.DocumentEvent;
import com.sun.star.document.XDocumentEventBroadcaster;
import com.sun.star.document.XDocumentEventListener;
import com.sun.star.document.XEventBroadcaster;
import com.sun.star.document.XEventListener;
import com.sun.star.drawing.XConnectorShape;
import com.sun.star.drawing.XShape;
import com.sun.star.frame.FrameSearchFlag;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.EventObject;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ariel
 */
public class ConnectorShapeListener {

    private final XComponentContext m_xContext;
    private DocumentListener maListener;
    private XDocumentEventBroadcaster m_xNewBroadcaster;
    private XEventBroadcaster m_xOldBroadcaster;

    private ConnectorShapeListener(XComponentContext xContext) {
        m_xContext = xContext;
    }

    private void runDemo() throws Exception {
        XComponentLoader xLoader = UnoRuntime.queryInterface(
                XComponentLoader.class,
                m_xContext.getServiceManager().createInstanceWithContext(
                "com.sun.star.frame.Desktop", m_xContext));
        XComponent xDoc = UnoRuntime.queryInterface(
                XComponent.class,
                xLoader.loadComponentFromURL(
                "private:factory/sdraw", "_default",
                FrameSearchFlag.ALL, new PropertyValue[]{}));

        m_xNewBroadcaster = 
UnoRuntime.queryInterface(XDocumentEventBroadcaster.class, xDoc);
        m_xOldBroadcaster = UnoRuntime.queryInterface(XEventBroadcaster.class, 
xDoc);
        maListener = new DocumentListener();
        if (m_xNewBroadcaster != null) {
            m_xNewBroadcaster.addDocumentEventListener(maListener);
        }
        if (m_xOldBroadcaster != null) {
            m_xOldBroadcaster.addEventListener(maListener);
        }
    }

    private void finishDemo() {
        if (m_xNewBroadcaster != null) {
            m_xNewBroadcaster.removeDocumentEventListener(maListener);
        }
        if (m_xOldBroadcaster != null) {
            m_xOldBroadcaster.removeEventListener(maListener);
        }
    }

    private class DocumentListener implements XDocumentEventListener, 
XEventListener {

        private final HashMap< XConnectorShape, ConnectorShapes> 
maConnectorsMap = new HashMap<XConnectorShape, ConnectorShapes>();

        private class ConnectorShapes {

            private XPropertySet mxConnector;
            private XShape mxStart;
            private XShape mxEnd;

            private ConnectorShapes(XConnectorShape xShape) {
                try {
                    mxConnector = UnoRuntime.queryInterface(XPropertySet.class, 
xShape);
                    mxStart = UnoRuntime.queryInterface(XShape.class, 
mxConnector.getPropertyValue("StartShape"));
                    mxEnd = UnoRuntime.queryInterface(XShape.class, 
mxConnector.getPropertyValue("EndShape"));
                } catch (UnknownPropertyException ex) {
                    
Logger.getLogger(ConnectorShapeListener.class.getName()).log(Level.SEVERE, 
null, ex);
                } catch (WrappedTargetException ex) {
                    
Logger.getLogger(ConnectorShapeListener.class.getName()).log(Level.SEVERE, 
null, ex);
                }
            }

            private XShape getEnd() {
                return mxEnd;
            }

            private XShape getStart() {
                return mxStart;
            }

            private void setEnd(XShape xEnd) {
                mxEnd = xEnd;
            }

            private void setStart(XShape xStart) {
                mxStart = xStart;
            }

            private boolean hasChanged() {
                try {
                    XShape xStart = UnoRuntime.queryInterface(XShape.class, 
mxConnector.getPropertyValue("StartShape"));
                    XShape xEnd = UnoRuntime.queryInterface(XShape.class, 
mxConnector.getPropertyValue("EndShape"));

                    return xStart != mxStart || xEnd != mxEnd;
                } catch (UnknownPropertyException ex) {
                    
Logger.getLogger(ConnectorShapeListener.class.getName()).log(Level.SEVERE, 
null, ex);
                } catch (WrappedTargetException ex) {
                    
Logger.getLogger(ConnectorShapeListener.class.getName()).log(Level.SEVERE, 
null, ex);
                }

                return false;
            }

            private boolean update() {
                try {
                    mxStart = UnoRuntime.queryInterface(XShape.class, 
mxConnector.getPropertyValue("StartShape"));
                    mxEnd = UnoRuntime.queryInterface(XShape.class, 
mxConnector.getPropertyValue("EndShape"));
                    return true;
                } catch (UnknownPropertyException ex) {
                    
Logger.getLogger(ConnectorShapeListener.class.getName()).log(Level.SEVERE, 
null, ex);
                } catch (WrappedTargetException ex) {
                    
Logger.getLogger(ConnectorShapeListener.class.getName()).log(Level.SEVERE, 
null, ex);
                }

                return false;
            }
        }

        public void documentEventOccured(DocumentEvent aEvent) {
            System.out.printf("NEW Event: %s", aEvent.EventName);
            XServiceInfo xInfo = UnoRuntime.queryInterface(XServiceInfo.class, 
aEvent.Source);
            if (xInfo != null) {
                System.out.printf(" - %s", xInfo.getImplementationName());
            }
            System.out.println();
        }

        public void notifyEvent(com.sun.star.document.EventObject aEvent) {
            System.out.printf("OLD Event - Event: %s", aEvent.EventName);
            XServiceInfo xInfo = UnoRuntime.queryInterface(XServiceInfo.class, 
aEvent.Source);
            if (xInfo != null) {
                System.out.printf(" - %s", xInfo.getImplementationName());
            }
            System.out.println();
            if (aEvent.EventName.equals("ShapeInserted")) {
                XConnectorShape xConnectorShape = 
UnoRuntime.queryInterface(XConnectorShape.class, aEvent.Source);
                if (xConnectorShape != null) {
                    if (!maConnectorsMap.containsKey(xConnectorShape)) {
                        maConnectorsMap.put(xConnectorShape, new 
ConnectorShapes(xConnectorShape));
                        System.out.println("Inserted connector shape");
                    }
                }
            } else if (aEvent.EventName.equals("ShapeRemoved")) {
                XConnectorShape xConnectorShape = 
UnoRuntime.queryInterface(XConnectorShape.class, aEvent.Source);
                if (xConnectorShape != null) {
                    if (maConnectorsMap.containsKey(xConnectorShape)) {
                        maConnectorsMap.remove(xConnectorShape);
                        System.out.println("Removed connector shape");
                    }
                }
            } else if (aEvent.EventName.equals("ShapeModified")) {
                XConnectorShape xConnectorShape = 
UnoRuntime.queryInterface(XConnectorShape.class, aEvent.Source);
                if (xConnectorShape != null) {
                    if (maConnectorsMap.containsKey(xConnectorShape)) {
                        ConnectorShapes aShapes = 
maConnectorsMap.get(xConnectorShape);
                        if (aShapes.hasChanged()) {
                            System.out.println("Connected shapes have changed");
                            aShapes.update();
                        }
                    }
                }
            }
        }

        public void disposing(EventObject arg0) {
            System.out.println("disposing...");
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            // get the remote office component context
            XComponentContext xContext = Bootstrap.bootstrap();
            if (xContext != null) {
                ConnectorShapeListener aDemo = new 
ConnectorShapeListener(xContext);
                aDemo.runDemo();

                System.out.print("Press enter to finish the example...");
                BufferedReader aReader = new BufferedReader(new 
InputStreamReader(System.in));
                aReader.readLine();
                aDemo.finishDemo();
            }
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }
}

Attachment: pgph9leSz8I0F.pgp
Description: PGP signature

Reply via email to