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