1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.awt.PushButtonType;
25cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
26cdf0e10cSrcweir import com.sun.star.awt.VclWindowPeerAttribute;
27cdf0e10cSrcweir import com.sun.star.awt.WindowAttribute;
28cdf0e10cSrcweir import com.sun.star.awt.WindowClass;
29cdf0e10cSrcweir import com.sun.star.awt.XToolkit;
30cdf0e10cSrcweir import com.sun.star.awt.XView;
31cdf0e10cSrcweir import com.sun.star.awt.XWindow;
32cdf0e10cSrcweir import com.sun.star.awt.XWindowPeer;
33cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
34cdf0e10cSrcweir import com.sun.star.beans.XMultiPropertySet;
35cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
36cdf0e10cSrcweir import com.sun.star.frame.XFrame;
37cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
38cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
39cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir public class DialogDocument extends UnoDialogSample {
43cdf0e10cSrcweir 
DialogDocument(XComponentContext _xContext, XMultiComponentFactory _xMCF)44cdf0e10cSrcweir     public DialogDocument(XComponentContext _xContext, XMultiComponentFactory _xMCF) {
45cdf0e10cSrcweir         super(_xContext, _xMCF);
46cdf0e10cSrcweir     }
47cdf0e10cSrcweir 
main(String args[])48cdf0e10cSrcweir     public static void main(String args[]){
49cdf0e10cSrcweir         DialogDocument oDialogDocument = null;
50cdf0e10cSrcweir         try {
51cdf0e10cSrcweir             XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
52cdf0e10cSrcweir             if(xContext != null )
53cdf0e10cSrcweir                 System.out.println("Connected to a running office ...");
54cdf0e10cSrcweir             XMultiComponentFactory xMCF = xContext.getServiceManager();
55cdf0e10cSrcweir             oDialogDocument = new DialogDocument(xContext, xMCF);
56cdf0e10cSrcweir             oDialogDocument.initialize( new String[] {"Height", "Moveable", "Name","PositionX","PositionY", "Step", "TabIndex","Title","Width"},
57cdf0e10cSrcweir                     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)});
58cdf0e10cSrcweir             oDialogDocument.createWindowPeer();
59cdf0e10cSrcweir             Object oFTHeaderModel = oDialogDocument.m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel");
60cdf0e10cSrcweir             XMultiPropertySet xFTHeaderModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oFTHeaderModel);
61cdf0e10cSrcweir             xFTHeaderModelMPSet.setPropertyValues(
62cdf0e10cSrcweir                     new String[] {"Height", "Label", "Name", "PositionX", "PositionY", "Width"},
63cdf0e10cSrcweir                     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)});
64cdf0e10cSrcweir             // add the model to the NameContainer of the dialog model
65cdf0e10cSrcweir             oDialogDocument.m_xDlgModelNameContainer.insertByName("Headerlabel", oFTHeaderModel);
66cdf0e10cSrcweir             oDialogDocument.showDocumentinDialogWindow(oDialogDocument.m_xWindowPeer, new Rectangle(40, 50, 420, 550), "private:factory/swriter");
67cdf0e10cSrcweir 
68cdf0e10cSrcweir             oDialogDocument.insertButton(oDialogDocument, 126, 370, 50, "~Close dialog", (short) PushButtonType.OK_value);
69cdf0e10cSrcweir             oDialogDocument.executeDialog();
70cdf0e10cSrcweir         }catch( Exception ex ) {
71cdf0e10cSrcweir             ex.printStackTrace(System.out);
72cdf0e10cSrcweir         } finally{
73cdf0e10cSrcweir             //make sure always to dispose the component and free the memory!
74cdf0e10cSrcweir             if (oDialogDocument != null){
75cdf0e10cSrcweir                 if (oDialogDocument.m_xComponent != null) {
76cdf0e10cSrcweir                     oDialogDocument.m_xComponent.dispose();
77cdf0e10cSrcweir                 }
78cdf0e10cSrcweir             }
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         System.exit( 0 );
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
showDocumentinDialogWindow(XWindowPeer _xParentWindowPeer, Rectangle _aRectangle, String _sUrl)84cdf0e10cSrcweir     public void showDocumentinDialogWindow(XWindowPeer _xParentWindowPeer, Rectangle _aRectangle, String _sUrl){
85cdf0e10cSrcweir         try {
86cdf0e10cSrcweir             // The Toolkit is the creator of all windows...
87cdf0e10cSrcweir             Object oToolkit = m_xMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", m_xContext);
88cdf0e10cSrcweir             XToolkit xToolkit = (XToolkit) UnoRuntime.queryInterface(XToolkit.class, oToolkit);
89cdf0e10cSrcweir 
90cdf0e10cSrcweir             // set up a window description and create the window. A parent window is always necessary for this...
91cdf0e10cSrcweir             com.sun.star.awt.WindowDescriptor aWindowDescriptor = new com.sun.star.awt.WindowDescriptor();
92cdf0e10cSrcweir             // a simple window is enough for this purpose...
93cdf0e10cSrcweir             aWindowDescriptor.Type = WindowClass.SIMPLE;
94cdf0e10cSrcweir             aWindowDescriptor.WindowServiceName = "dockingwindow";
95cdf0e10cSrcweir             // assign the parent window peer as described in the idl description...
96cdf0e10cSrcweir             aWindowDescriptor.Parent = _xParentWindowPeer;
97cdf0e10cSrcweir             aWindowDescriptor.ParentIndex = 1;
98cdf0e10cSrcweir             aWindowDescriptor.Bounds = _aRectangle;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir             // set the window attributes...
101cdf0e10cSrcweir             // The attribute CLIPCHILDREN causes the parent to not repaint the areas of the children...
102cdf0e10cSrcweir             aWindowDescriptor.WindowAttributes = VclWindowPeerAttribute.CLIPCHILDREN + WindowAttribute.BORDER + WindowAttribute.SHOW;
103cdf0e10cSrcweir             XWindowPeer xWindowPeer = xToolkit.createWindow(aWindowDescriptor);
104cdf0e10cSrcweir             XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, xWindowPeer);
105cdf0e10cSrcweir             XView xView = (XView) UnoRuntime.queryInterface(XView.class, xWindow);
106cdf0e10cSrcweir 
107cdf0e10cSrcweir             // create a frame and initialize it with the created window...
108cdf0e10cSrcweir             Object oFrame = m_xMCF.createInstanceWithContext("com.sun.star.frame.Frame", m_xContext);
109cdf0e10cSrcweir             // The frame should be of global scope because it's within the responsibility to dispose it after usage
110cdf0e10cSrcweir             m_xFrame = (XFrame) UnoRuntime.queryInterface(XFrame.class, oFrame);
111cdf0e10cSrcweir             m_xFrame.initialize(xWindow);
112cdf0e10cSrcweir 
113cdf0e10cSrcweir             // load the document and open it in preview mode
114cdf0e10cSrcweir             XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, m_xFrame);
115cdf0e10cSrcweir             PropertyValue[] aPropertyValues = new PropertyValue[2];
116cdf0e10cSrcweir             PropertyValue aPropertyValue = new PropertyValue();
117cdf0e10cSrcweir             aPropertyValue.Name = "Preview";
118cdf0e10cSrcweir             aPropertyValue.Value = Boolean.TRUE;
119cdf0e10cSrcweir             aPropertyValues[0] = aPropertyValue;
120cdf0e10cSrcweir             aPropertyValue = new PropertyValue();
121cdf0e10cSrcweir             aPropertyValue.Name = "ReadOnly";
122cdf0e10cSrcweir             aPropertyValue.Value = Boolean.TRUE;
123cdf0e10cSrcweir             aPropertyValues[1] = aPropertyValue;
124cdf0e10cSrcweir             xComponentLoader.loadComponentFromURL(_sUrl, "_self", 0, aPropertyValues);
125cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException ex) {
126cdf0e10cSrcweir             ex.printStackTrace();
127cdf0e10cSrcweir             throw new java.lang.RuntimeException("cannot happen...");
128cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception ex) {
129cdf0e10cSrcweir             ex.printStackTrace();
130cdf0e10cSrcweir             throw new java.lang.RuntimeException("cannot happen...");
131cdf0e10cSrcweir         }
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir }
135