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