Hi;

I'm trying to write sidebar extension for openoffice. I've read sidebar for developers from wiki. (https://wiki.openoffice.org/wiki/Sidebar_for_Developers) And i modify like below, but i can not add button control. How can i add control to XSidebarPanel ?

package org.apache.openoffice.sidebar;

import java.util.Vector;

import com.sun.star.accessibility.XAccessible;
import com.sun.star.awt.Rectangle;
import com.sun.star.awt.Size;
import com.sun.star.awt.WindowAttribute;
import com.sun.star.awt.WindowClass;
import com.sun.star.awt.WindowDescriptor;
import com.sun.star.awt.WindowEvent;
import com.sun.star.awt.XToolkit;
import com.sun.star.awt.XWindow;
import com.sun.star.awt.XWindowListener;
import com.sun.star.awt.XWindowPeer;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XNameContainer;
import com.sun.star.lang.DisposedException;
import com.sun.star.lang.EventObject;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XEventListener;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.ui.LayoutSize;
import com.sun.star.ui.XSidebarPanel;
import com.sun.star.ui.XToolPanel;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public abstract class PanelBase implements XToolPanel, XWindowListener,
    XSidebarPanel, XComponent {
    /**
     * This is the one method that a derived class has to implement: how to
     * react to size changes of the content window.
     */
    abstract protected void Layout(final Size aWindowSize);

    protected PanelBase() {
        maDisposeListeners = new Vector<XEventListener>();
    }

    protected void Initialize(final XWindow xParentWindow,
        final XComponentContext xContext) {
        Log.Instance().println("creating panel");

        // Create the content window of the panel.
        try {
            XWindowPeer xParentPeer =
                (XWindowPeer) UnoRuntime.queryInterface(XWindowPeer.class,
                    xParentWindow);
            if (xParentPeer != null) {
                xParentWindow.addWindowListener(this);

                if (xContext == null)
                    throw new RuntimeException("got null XContext");
XMultiComponentFactory xFactory = xContext.getServiceManager();
                if (xFactory == null)
                    throw new RuntimeException(
                        "can not acquire factor from XContext");

                XToolkit xToolkit =
                    (XToolkit) UnoRuntime.queryInterface(XToolkit.class,
                        xFactory.createInstanceWithContext(
                            "com.sun.star.awt.Toolkit", xContext));
                WindowDescriptor aWindowDescriptor =
                    new WindowDescriptor(WindowClass.CONTAINER, "",
xParentPeer, (short) -1, // parent index not available new Rectangle(0, 0, 10, 10), WindowAttribute.SIZEABLE
                            | WindowAttribute.MOVEABLE
                            | WindowAttribute.NODECORATION);
                mxWindow =
                    (XWindow) UnoRuntime.queryInterface(XWindow.class,
                        xToolkit.createWindow(aWindowDescriptor));
                if (mxWindow == null)
                    throw new RuntimeException(
"can not create XWindow for parent " + xParentPeer);

// Make the background transparent. The slide show paints its
                // own background.
                final XWindowPeer xPeer =
(XWindowPeer) UnoRuntime.queryInterface(XWindowPeer.class,
                        mxWindow);
                if (xPeer != null) {
                    // Make the window background transparent to avoid some
                    // flickering,
                    // when first the window background and then the canvas
                    // content is painted.
                    // xPeer.setBackground(0xffffff);
                    xPeer.setBackground(0xfff000);
                }

                XMultiServiceFactory xMultiServiceFactory =
                    (XMultiServiceFactory) xContext.getServiceManager();

                try {
                    String _buttonName = "mahooo";
                    // create the button model and set the properties
                    Object buttonModel =
                        xMultiServiceFactory
.createInstance("com.sun.star.awt.UnoControlButtonModel");
                    XPropertySet xPSetButton =
                        (XPropertySet) UnoRuntime.queryInterface(
                            XPropertySet.class, buttonModel);
xPSetButton.setPropertyValue("PositionX", new Integer(20)); xPSetButton.setPropertyValue("PositionY", new Integer(70));
                    xPSetButton.setPropertyValue("Width", new Integer(50));
xPSetButton.setPropertyValue("Height", new Integer(14));
                    xPSetButton.setPropertyValue("Name", _buttonName);
                    xPSetButton.setPropertyValue("TabIndex", new Short(
                        (short) 0));
                    xPSetButton.setPropertyValue("Label",
                        new String("Click Me"));

                    XNameContainer xNameCont =
                        (XNameContainer) UnoRuntime.queryInterface(
                            XNameContainer.class, xPeer);
                    xNameCont.insertByName(_buttonName, buttonModel);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                mxWindow.setVisible(true);
            }
        } catch (Exception aException) {
            Log.Instance().PrintStackTrace(aException);
            throw new RuntimeException("can not create window for Panel: "
                + aException.getMessage());
        }
    }

    // ----- Implementation of UNO interface XWindowListener -----

    @Override
    public void windowHidden(final EventObject aEvent) {
        CallLayout(0, 0);
    }

    @Override
    public void windowMoved(final WindowEvent aEvent) {
        CallLayout(aEvent.Width, aEvent.Height);
    }

    @Override
    public void windowResized(final WindowEvent aEvent) {
        CallLayout(aEvent.Width, aEvent.Height);
    }

    @Override
    public void windowShown(final EventObject aEvent) {
CallLayout(mxWindow.getPosSize().Width, mxWindow.getPosSize().Height);
    }

    @Override
    public void disposing(final EventObject aEvent) {
        mxWindow = null;
    }

    // ----- Implementation of UNO interface XToolPanel -----

    @Override
    public XAccessible createAccessible(XAccessible arg0) {
        return (XAccessible) UnoRuntime.queryInterface(XAccessible.class,
            getWindow());
    }

    @Override
    public XWindow getWindow() {
        if (mxWindow == null)
            throw new DisposedException("Panel is already disposed", this);

        return mxWindow;
    }

    // ----- Implementation of UNO interface XSidebarPanel -----

    @Override
    public LayoutSize getHeightForWidth(final int nWidth) {
        return new LayoutSize(0, 0, 0);
    }

    // ----- Implementation of UNO interface XComponent -----

    @Override
    public void dispose() {
        EventObject aEvent = new EventObject(this);
        for (final XEventListener xListener : maDisposeListeners)
            xListener.disposing(aEvent);
    }

    @Override
    public void addEventListener(final XEventListener xListener) {
        maDisposeListeners.add(xListener);
    }

    @Override
    public void removeEventListener(final XEventListener xListener) {
        maDisposeListeners.remove(xListener);
    }

    // ----- private methods -----

    private void CallLayout(final int nWidth, final int nHeight) {
        Layout(new Size(nWidth, nHeight));
    }

    protected XWindow mxWindow;
    private final Vector<XEventListener> maDisposeListeners;
}


---------------------------------------------------------------------
To unsubscribe, e-mail: api-unsubscr...@openoffice.apache.org
For additional commands, e-mail: api-h...@openoffice.apache.org

Reply via email to