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