1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir package complex.persistent_window_states;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
31*cdf0e10cSrcweir import com.sun.star.awt.PosSize;
32*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
33*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
34*cdf0e10cSrcweir import com.sun.star.awt.XWindow;
35*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
36*cdf0e10cSrcweir import com.sun.star.beans.PropertyState;
37*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38*cdf0e10cSrcweir import com.sun.star.frame.XFrame;
39*cdf0e10cSrcweir import com.sun.star.frame.FrameSearchFlag;
40*cdf0e10cSrcweir import helper.WindowListener;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir /**
43*cdf0e10cSrcweir  * Load and resize a document.
44*cdf0e10cSrcweir  *
45*cdf0e10cSrcweir  */
46*cdf0e10cSrcweir public class DocumentHandle {
47*cdf0e10cSrcweir     // the component loader to load a document
48*cdf0e10cSrcweir     XComponentLoader xCompLoader = null;
49*cdf0e10cSrcweir     // the document
50*cdf0e10cSrcweir     XComponent xComp = null;
51*cdf0e10cSrcweir     // the current window
52*cdf0e10cSrcweir     XWindow xWin = null;
53*cdf0e10cSrcweir     // a own window listener
54*cdf0e10cSrcweir     WindowListener wl = null;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir     /**
57*cdf0e10cSrcweir      * Constructor
58*cdf0e10cSrcweir      * @param xCompLoader  A loader to load a document
59*cdf0e10cSrcweir      */
60*cdf0e10cSrcweir     public DocumentHandle(XComponentLoader xCompLoader) {
61*cdf0e10cSrcweir         this.xCompLoader = xCompLoader;
62*cdf0e10cSrcweir         wl = new WindowListener();
63*cdf0e10cSrcweir     }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     /**
66*cdf0e10cSrcweir      * Load/Create a document.
67*cdf0e10cSrcweir      * @param docName The name of a document as file URL
68*cdf0e10cSrcweir      * @param hidden If true, the document is loaded hidden.
69*cdf0e10cSrcweir      * @return The size of the opened/created document.
70*cdf0e10cSrcweir      * @throws Exception
71*cdf0e10cSrcweir      */
72*cdf0e10cSrcweir     public Rectangle loadDocument(String docName, boolean hidden)
73*cdf0e10cSrcweir                                                             throws Exception{
74*cdf0e10cSrcweir         wl.resetTrigger();
75*cdf0e10cSrcweir         try {
76*cdf0e10cSrcweir             PropertyValue [] szArgs = null;
77*cdf0e10cSrcweir 			if (hidden) {
78*cdf0e10cSrcweir 				szArgs = new PropertyValue [1];
79*cdf0e10cSrcweir 				PropertyValue Arg = new PropertyValue();
80*cdf0e10cSrcweir 				Arg.Name = "Hidden";
81*cdf0e10cSrcweir 				Arg.Value = hidden?"True":"False";
82*cdf0e10cSrcweir 				Arg.Handle = -1;
83*cdf0e10cSrcweir 				Arg.State = PropertyState.DEFAULT_VALUE;
84*cdf0e10cSrcweir 				szArgs[0] = Arg;
85*cdf0e10cSrcweir 			}
86*cdf0e10cSrcweir 			else {
87*cdf0e10cSrcweir 				szArgs = new PropertyValue [0];
88*cdf0e10cSrcweir 			}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir             // get the current active window
91*cdf0e10cSrcweir             XFrame xCurFrame = UnoRuntime.queryInterface(XFrame.class, xCompLoader);
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 			// create a new frame
94*cdf0e10cSrcweir             XFrame xFrame = xCurFrame.findFrame("_blank", FrameSearchFlag.CREATE);
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir             // load document in this frame
97*cdf0e10cSrcweir             XComponentLoader xFrameLoader = UnoRuntime.queryInterface(XComponentLoader.class, xFrame);
98*cdf0e10cSrcweir             xComp = xFrameLoader.loadComponentFromURL(
99*cdf0e10cSrcweir                                                 docName, "_self", 0, szArgs);
100*cdf0e10cSrcweir             // wait for the document to load.
101*cdf0e10cSrcweir             try {
102*cdf0e10cSrcweir                 Thread.sleep(10000);
103*cdf0e10cSrcweir             }
104*cdf0e10cSrcweir             catch(java.lang.InterruptedException e) {}
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir             xWin = xFrame.getContainerWindow();
107*cdf0e10cSrcweir             xWin.addWindowListener(wl);
108*cdf0e10cSrcweir         }
109*cdf0e10cSrcweir         catch(com.sun.star.io.IOException e) {
110*cdf0e10cSrcweir             e.printStackTrace();
111*cdf0e10cSrcweir             return null;
112*cdf0e10cSrcweir         }
113*cdf0e10cSrcweir         catch(com.sun.star.lang.IllegalArgumentException e) {
114*cdf0e10cSrcweir             e.printStackTrace();
115*cdf0e10cSrcweir             return null;
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir         catch(java.lang.Exception e) {
118*cdf0e10cSrcweir             System.out.println("DH3");
119*cdf0e10cSrcweir             e.printStackTrace();
120*cdf0e10cSrcweir             throw e;
121*cdf0e10cSrcweir         }
122*cdf0e10cSrcweir         return xWin.getPosSize();
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     /**
127*cdf0e10cSrcweir      * Get the size of the current window.
128*cdf0e10cSrcweir      * @return The size of the window as Rectangle.
129*cdf0e10cSrcweir      */
130*cdf0e10cSrcweir     public Rectangle getDocumentPosSize() {
131*cdf0e10cSrcweir         return xWin.getPosSize();
132*cdf0e10cSrcweir     }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     /**
135*cdf0e10cSrcweir      * Resize the window in defined steps:
136*cdf0e10cSrcweir      * width -10 pixel;
137*cdf0e10cSrcweir      * height -10 pixel;
138*cdf0e10cSrcweir      * X-Position +10 pixel;
139*cdf0e10cSrcweir      * Y-Position +10 pixel
140*cdf0e10cSrcweir      * @return True if resize worked.
141*cdf0e10cSrcweir      */
142*cdf0e10cSrcweir     public boolean resizeDocument() {
143*cdf0e10cSrcweir         Rectangle newPosSize = xWin.getPosSize();
144*cdf0e10cSrcweir         newPosSize.Width = newPosSize.Width - 20;
145*cdf0e10cSrcweir         newPosSize.Height = newPosSize.Height - 20;
146*cdf0e10cSrcweir         newPosSize.X = newPosSize.X + 80;
147*cdf0e10cSrcweir         newPosSize.Y = newPosSize.Y + 80;
148*cdf0e10cSrcweir         return resizeDocument(newPosSize);
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     /**
152*cdf0e10cSrcweir      * Resize window to the given Rectangle
153*cdf0e10cSrcweir      * @param newPosSize The new position and size of the window.
154*cdf0e10cSrcweir      * @return True if resize worked.
155*cdf0e10cSrcweir      */
156*cdf0e10cSrcweir     public boolean resizeDocument(Rectangle newPosSize){
157*cdf0e10cSrcweir         wl.resetTrigger();
158*cdf0e10cSrcweir         xWin.setPosSize(newPosSize.X, newPosSize.Y, newPosSize.Width,
159*cdf0e10cSrcweir                                     newPosSize.Height, PosSize.POSSIZE);
160*cdf0e10cSrcweir         try {
161*cdf0e10cSrcweir             Thread.sleep(3000);
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir         catch(java.lang.InterruptedException e) {}
164*cdf0e10cSrcweir         return wl.resizedTrigger;
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir }
167