Hello Daniel,
Daniel Watkins escribió:
Hello all,
I want to be able to set the dimensions and UI components of a
document's frame before starting to load the document itself. So, for
example, I'm currently doing something very like:
doc = desktop.loadComponentFromUrl(<url>, "_blank")
frame = doc.getControllers().nextElement().getFrame()
why not simply
frame = doc.getCurrentController().getFrame()
as right after loadComponentFromUrl returns, there'll be likely only one
controller for this model
# Remove UI stuff
lmgr = frame.LayoutManager
lmgr.hideElement("private:resource/menubar/menubar")
lmgr.setVisible(False)
if you want to get rid of *every* UI element, the easiest thing is to
get rid of the LayoutManager itself (see below).
# Set window size
frame.getContainerWindow().setPosSize(x, y, w, h, 15)
I'd suggest you to use the fully qualified name of the constants (in
this case com.sun.star.awt.PosSize.POSSIZE): on one hand, this way code
is easier to understand; on the other hand, the constant value depends
on the implementation (which may change), and one should program against
the specification (which is stable if published).
What I would like to do is to set the window up, perform the removal of
UI stuff and window resizing, and then call
'desktop.loadComponentFromUrl',
It seemed like I might be able to do something in this vein using
'_default' as the target frame, as per [0], but I couldn't get that to
work.
Any pointers on how I could go about doing this?
you can do all by yourself: create your own top window, create your
frame, load the component in it.
Follow these steps:
1. create a TOP css.awt.XWindow via the Toolkit service
2. instatiate a css.frame.Frame and initialize it with your
css.awt.XWindow, this will be the container window you can then get with
css.XFrame.getContainerWindow()
3. get rid of the LayoutManager, by setting the Frame property
"LayoutManager" with an empty reference (you could even implement the
LayoutManager yourself - I guess == never tried myself)
4. the Frame is a component loader == it implements
css.frame.XComponentLoader, so use it to load the component in itself
(set the target frame name and the search flags properly)
Notice that if you follow this way, you must take care of
different/several other things, try with the Java code I attach.
Regards
Ariel.
--
Ariel Constenla-Haile
La Plata, Argentina
[EMAIL PROTECTED]
http://www.ArielConstenlaHaile.com.ar/ooo/
"Aus der Kriegsschule des Lebens
- Was mich nicht umbringt,
macht mich härter."
Nietzsche Götzendämmerung, Sprüche und Pfeile, 8.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.openoffice.sdk.awt.test;
import com.sun.star.awt.PosSize;
import com.sun.star.awt.Rectangle;
import com.sun.star.awt.WindowAttribute;
import com.sun.star.awt.WindowClass;
import com.sun.star.awt.WindowDescriptor;
import com.sun.star.awt.XToolkit;
import com.sun.star.awt.XVclWindowPeer;
import com.sun.star.awt.XWindow;
import com.sun.star.awt.XWindow2;
import com.sun.star.awt.XWindowPeer;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.container.XIndexAccess;
import com.sun.star.frame.FrameSearchFlag;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XFrame;
import com.sun.star.frame.XFrames;
import com.sun.star.frame.XFramesSupplier;
import com.sun.star.frame.XLayoutManager;
import com.sun.star.lang.XComponent;
import com.sun.star.text.XTextDocument;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
/**
*
* @author ariel
*/
public class FrameLoader {
private final XComponentContext m_xContext;
private XToolkit m_xToolkit;
public FrameLoader( XComponentContext xContext ){
m_xContext = xContext;
try {
m_xToolkit = (XToolkit) UnoRuntime.queryInterface(
XToolkit.class,
xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.awt.Toolkit", xContext));
} catch (Exception e) {
e.printStackTrace();
}
}
protected XComponent loadComponent(String sURL){
XComponent xComponent = null;
try {
// in order to center the window on the screen
// we get first the default display work area
// We could use Java AWT
// java.awt.Toolkit.getDefaultToolkit().getScreenSize()
// but to make this portable, use
// the UNPUBLISHED/UNDOCUMENTED com.sun.star.awt.DisplayAccess
Rectangle aWorkArea = getDefaultDisplayWorkArea();
// the window size
Rectangle aWinSize =
aWorkArea.Width > 0 && aWorkArea.Height > 0 ?
new Rectangle( 0, 0,
(int) (aWorkArea.Width / 1.5),
(int) (aWorkArea.Height / 1.5) ) :
new Rectangle( 0, 0, 400, 300);
// calculate the window position, in the center of the work area
calcCenterPosSize(aWinSize, aWorkArea);
// create a top window
XWindowPeer xWindowPeer = createTopWindow(null, aWinSize);
XWindow2 xWindow = (XWindow2) UnoRuntime.queryInterface(
XWindow2.class, xWindowPeer);
//*****************************************************************
// create a Frame
XFrame xFrame = (XFrame) UnoRuntime.queryInterface(
XFrame.class,
m_xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.frame.Frame", m_xContext));
// initialize the frame with our window
xFrame.initialize(xWindow);
//*****************************************************************
// do we want our frame to be part of the desktop hierarchy
// and so take part of the application framework?
XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
XDesktop.class,
m_xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.frame.Desktop", m_xContext));
// if NOT, don't append it!
// But we should, among other things, add an
// XTerminateListener at the XDesktop,
// an XTopWindowListener at our window, etc.
// [An example: if our component is modified and
// the user tries to close it, the user won't be warned with
// the Save/Discard/Cancel dialog
// So in short: we are by ourselves!]
XFramesSupplier xFramesSupplier = (XFramesSupplier) UnoRuntime.queryInterface(
XFramesSupplier.class, xDesktop);
XFrames xFrames = xFramesSupplier.getFrames();
// comment/un-comment to test what happens...
xFrames.append(xFrame);
//*****************************************************************
// get rid of the LayoutManager
XPropertySet xFramePropertySet = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xFrame);
XLayoutManager xLayoutManager = null;
// in our case, the Title should be set as a window property!
//xFramePropertySet.setPropertyValue("Title", "Frame loader demo");
xFramePropertySet.setPropertyValue("LayoutManager", xLayoutManager);
//*****************************************************************
// load the component
// as loading will inmediatly set the window visible,
// we should load the component hidden if we want to perform
// some tasks before showing the component to the user
PropertyValue[] aMediaDescriptor = new PropertyValue[1];
/*aMediaDescriptor[0] = new PropertyValue();
aMediaDescriptor[0].Name = "Hidden";
aMediaDescriptor[0].Value = Boolean.TRUE;*/
XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(
XComponentLoader.class, xFrame);
// set both target name and search flag
// to load the component in[side] itself
xComponent = xComponentLoader.loadComponentFromURL(
sURL, "_self", FrameSearchFlag.SELF, aMediaDescriptor);
//*****************************************************************
// unfortunatlly, the PosSize set when created the window
// will have no efect, so we must set it again
// once the window *is* *visible*
// the following CANNOT be false
XWindow xContainerWindow = xFrame.getContainerWindow();
boolean bAreSame = UnoRuntime.areSame(xContainerWindow, xWindow);
System.out.printf("XFrame.getContainerWindow() returns our window? %b\n", bAreSame );
// if the window was not visible ...
// xWindow/xFrame.getContainerWindow().setVisible(true);
xWindow.setPosSize(
aWinSize.X, aWinSize.X,
aWinSize.Width, aWinSize.Height,
PosSize.POSSIZE);
// set the Title
XVclWindowPeer xVclWindowPeer = (XVclWindowPeer) UnoRuntime.queryInterface(
XVclWindowPeer.class, xWindow);
xVclWindowPeer.setProperty("Title", "Frame loader demo");
} catch (Exception e) {
e.printStackTrace();
} finally {
return xComponent;
}
}
private XWindowPeer createTopWindow(XWindowPeer xParentWindowPeer, Rectangle aPosSize) {
XWindowPeer xTopWindow = null;
try {
if (m_xToolkit != null) {
WindowDescriptor aWindowDescriptor = new WindowDescriptor();
aWindowDescriptor.Bounds = aPosSize;
aWindowDescriptor.Parent = xParentWindowPeer;
aWindowDescriptor.ParentIndex = -1;
aWindowDescriptor.Type = WindowClass.TOP;
aWindowDescriptor.WindowAttributes =
WindowAttribute.BORDER |
WindowAttribute.CLOSEABLE |
/*WindowAttribute.FULLSIZE |*/
WindowAttribute.MOVEABLE |
WindowAttribute.SIZEABLE;
aWindowDescriptor.WindowServiceName = "workwindow";
xTopWindow = m_xToolkit.createWindow(aWindowDescriptor);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return xTopWindow;
}
}
public Rectangle getDefaultDisplayWorkArea() {
Rectangle aWorkArea = new Rectangle();
try {
XPropertySet xDisplayAccess = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, m_xContext.getServiceManager().
createInstanceWithContext(
"com.sun.star.awt.DisplayAccess", m_xContext));
XIndexAccess xDisplayIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
XIndexAccess.class, xDisplayAccess);
int nDefaultDisplay = -1;
try {
nDefaultDisplay = AnyConverter.toInt(
xDisplayAccess.getPropertyValue("DefaultDisplay"));
} catch (Exception e) {
e.printStackTrace();
}
XPropertySet xDefaultDisplay = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class,
xDisplayIndexAccess.getByIndex(nDefaultDisplay) );
aWorkArea = (Rectangle) AnyConverter.toObject(
Rectangle.class,
xDefaultDisplay.getPropertyValue("WorkArea"));
} catch (Exception e) {
e.printStackTrace();
}
return aWorkArea;
}
private void calcCenterPosSize(
Rectangle aWinPosSize /* IN-OUT */,
Rectangle aParentWinPosSize) {
try {
int aParentWinHeight = aParentWinPosSize.Height;
int aParentWinWidth = aParentWinPosSize.Width;
int aWinWidth = aWinPosSize.Width;
int aWinHeight = aWinPosSize.Height;
aWinPosSize.X = ((aParentWinWidth / 2) - (aWinWidth / 2));
aWinPosSize.Y = ((aParentWinHeight / 2) - (aWinHeight / 2));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
XComponentContext xContext = null;
try {
xContext = Bootstrap.bootstrap();
FrameLoader demo = new FrameLoader(xContext);
XComponent xComponent = demo.loadComponent(
"private:factory/swriter");
if ( xComponent != null ){
XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class, xComponent);
xTextDocument.getText().setString(
"Component loaded in our frame with our window!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]