Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / GUI / DialogDocument.java
blobc565007f8bc376cbe435e219af670ca5061bf216
1 /*************************************************************************
3 * $RCSfile: DialogDocument.java,v $
5 * $Revision: 1.2 $
7 * last change: $Author: hr $ $Date: 2007-07-31 13:54:36 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.awt.PushButtonType;
42 import com.sun.star.awt.Rectangle;
43 import com.sun.star.awt.VclWindowPeerAttribute;
44 import com.sun.star.awt.WindowAttribute;
45 import com.sun.star.awt.WindowClass;
46 import com.sun.star.awt.XToolkit;
47 import com.sun.star.awt.XView;
48 import com.sun.star.awt.XWindow;
49 import com.sun.star.awt.XWindowPeer;
50 import com.sun.star.beans.PropertyValue;
51 import com.sun.star.beans.XMultiPropertySet;
52 import com.sun.star.frame.XComponentLoader;
53 import com.sun.star.frame.XFrame;
54 import com.sun.star.lang.XMultiComponentFactory;
55 import com.sun.star.uno.UnoRuntime;
56 import com.sun.star.uno.XComponentContext;
59 public class DialogDocument extends UnoDialogSample {
61 public DialogDocument(XComponentContext _xContext, XMultiComponentFactory _xMCF) {
62 super(_xContext, _xMCF);
65 public static void main(String args[]){
66 DialogDocument oDialogDocument = null;
67 try {
68 XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
69 if(xContext != null )
70 System.out.println("Connected to a running office ...");
71 XMultiComponentFactory xMCF = xContext.getServiceManager();
72 oDialogDocument = new DialogDocument(xContext, xMCF);
73 oDialogDocument.initialize( new String[] {"Height", "Moveable", "Name","PositionX","PositionY", "Step", "TabIndex","Title","Width"},
74 new Object[] { new Integer(400), Boolean.TRUE, "Dialog1", new Integer(102),new Integer(41), new Integer(1), new Short((short) 0), "Document-Dialog", new Integer(300)});
75 oDialogDocument.createWindowPeer();
76 Object oFTHeaderModel = oDialogDocument.m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel");
77 XMultiPropertySet xFTHeaderModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oFTHeaderModel);
78 xFTHeaderModelMPSet.setPropertyValues(
79 new String[] {"Height", "Label", "Name", "PositionX", "PositionY", "Width"},
80 new Object[] { new Integer(8), "This code-sample demonstrates how to display an office document in a dialog window", "HeaderLabel", new Integer(6), new Integer(6), new Integer(300)});
81 // add the model to the NameContainer of the dialog model
82 oDialogDocument.m_xDlgModelNameContainer.insertByName("Headerlabel", oFTHeaderModel);
83 oDialogDocument.showDocumentinDialogWindow(oDialogDocument.m_xWindowPeer, new Rectangle(40, 50, 420, 550), "private:factory/swriter");
85 oDialogDocument.insertButton(oDialogDocument, 126, 370, 50, "~Close dialog", (short) PushButtonType.OK_value);
86 oDialogDocument.executeDialog();
87 }catch( Exception ex ) {
88 ex.printStackTrace(System.out);
89 } finally{
90 //make sure always to dispose the component and free the memory!
91 if (oDialogDocument != null){
92 if (oDialogDocument.m_xComponent != null) {
93 oDialogDocument.m_xComponent.dispose();
98 System.exit( 0 );
101 public void showDocumentinDialogWindow(XWindowPeer _xParentWindowPeer, Rectangle _aRectangle, String _sUrl){
102 try {
103 // The Toolkit is the creator of all windows...
104 Object oToolkit = m_xMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", m_xContext);
105 XToolkit xToolkit = (XToolkit) UnoRuntime.queryInterface(XToolkit.class, oToolkit);
107 // set up a window description and create the window. A parent window is always necessary for this...
108 com.sun.star.awt.WindowDescriptor aWindowDescriptor = new com.sun.star.awt.WindowDescriptor();
109 // a simple window is enough for this purpose...
110 aWindowDescriptor.Type = WindowClass.SIMPLE;
111 aWindowDescriptor.WindowServiceName = "dockingwindow";
112 // assign the parent window peer as described in the idl description...
113 aWindowDescriptor.Parent = _xParentWindowPeer;
114 aWindowDescriptor.ParentIndex = 1;
115 aWindowDescriptor.Bounds = _aRectangle;
117 // set the window attributes...
118 // The attribute CLIPCHILDREN causes the parent to not repaint the areas of the children...
119 aWindowDescriptor.WindowAttributes = VclWindowPeerAttribute.CLIPCHILDREN + WindowAttribute.BORDER + WindowAttribute.SHOW;
120 XWindowPeer xWindowPeer = xToolkit.createWindow(aWindowDescriptor);
121 XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, xWindowPeer);
122 XView xView = (XView) UnoRuntime.queryInterface(XView.class, xWindow);
124 // create a frame and initialize it with the created window...
125 Object oFrame = m_xMCF.createInstanceWithContext("com.sun.star.frame.Frame", m_xContext);
126 // The frame should be of global scope because it's within the responsibility to dispose it after usage
127 m_xFrame = (XFrame) UnoRuntime.queryInterface(XFrame.class, oFrame);
128 m_xFrame.initialize(xWindow);
130 // load the document and open it in preview mode
131 XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, m_xFrame);
132 PropertyValue[] aPropertyValues = new PropertyValue[2];
133 PropertyValue aPropertyValue = new PropertyValue();
134 aPropertyValue.Name = "Preview";
135 aPropertyValue.Value = Boolean.TRUE;
136 aPropertyValues[0] = aPropertyValue;
137 aPropertyValue = new PropertyValue();
138 aPropertyValue.Name = "ReadOnly";
139 aPropertyValue.Value = Boolean.TRUE;
140 aPropertyValues[1] = aPropertyValue;
141 xComponentLoader.loadComponentFromURL(_sUrl, "_self", 0, aPropertyValues);
142 } catch (com.sun.star.lang.IllegalArgumentException ex) {
143 ex.printStackTrace();
144 throw new java.lang.RuntimeException("cannot happen...");
145 } catch (com.sun.star.uno.Exception ex) {
146 ex.printStackTrace();
147 throw new java.lang.RuntimeException("cannot happen...");