1*c7cc89feSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c7cc89feSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c7cc89feSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c7cc89feSAndrew Rist  * distributed with this work for additional information
6*c7cc89feSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c7cc89feSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c7cc89feSAndrew Rist  * "License"); you may not use this file except in compliance
9*c7cc89feSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c7cc89feSAndrew Rist  *
11*c7cc89feSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c7cc89feSAndrew Rist  *
13*c7cc89feSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c7cc89feSAndrew Rist  * software distributed under the License is distributed on an
15*c7cc89feSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c7cc89feSAndrew Rist  * KIND, either express or implied.  See the License for the
17*c7cc89feSAndrew Rist  * specific language governing permissions and limitations
18*c7cc89feSAndrew Rist  * under the License.
19*c7cc89feSAndrew Rist  *
20*c7cc89feSAndrew Rist  *************************************************************/
21*c7cc89feSAndrew Rist 
22*c7cc89feSAndrew Rist 
23cdf0e10cSrcweir package complex.persistent_window_states;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
27cdf0e10cSrcweir import com.sun.star.awt.PosSize;
28cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
29cdf0e10cSrcweir import com.sun.star.lang.XComponent;
30cdf0e10cSrcweir import com.sun.star.awt.XWindow;
31cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
32cdf0e10cSrcweir import com.sun.star.beans.PropertyState;
33cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
34cdf0e10cSrcweir import com.sun.star.frame.XFrame;
35cdf0e10cSrcweir import com.sun.star.frame.FrameSearchFlag;
36cdf0e10cSrcweir import helper.WindowListener;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir /**
39cdf0e10cSrcweir  * Load and resize a document.
40cdf0e10cSrcweir  *
41cdf0e10cSrcweir  */
42cdf0e10cSrcweir public class DocumentHandle {
43cdf0e10cSrcweir     // the component loader to load a document
44cdf0e10cSrcweir     XComponentLoader xCompLoader = null;
45cdf0e10cSrcweir     // the document
46cdf0e10cSrcweir     XComponent xComp = null;
47cdf0e10cSrcweir     // the current window
48cdf0e10cSrcweir     XWindow xWin = null;
49cdf0e10cSrcweir     // a own window listener
50cdf0e10cSrcweir     WindowListener wl = null;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     /**
53cdf0e10cSrcweir      * Constructor
54cdf0e10cSrcweir      * @param xCompLoader  A loader to load a document
55cdf0e10cSrcweir      */
DocumentHandle(XComponentLoader xCompLoader)56cdf0e10cSrcweir     public DocumentHandle(XComponentLoader xCompLoader) {
57cdf0e10cSrcweir         this.xCompLoader = xCompLoader;
58cdf0e10cSrcweir         wl = new WindowListener();
59cdf0e10cSrcweir     }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     /**
62cdf0e10cSrcweir      * Load/Create a document.
63cdf0e10cSrcweir      * @param docName The name of a document as file URL
64cdf0e10cSrcweir      * @param hidden If true, the document is loaded hidden.
65cdf0e10cSrcweir      * @return The size of the opened/created document.
66cdf0e10cSrcweir      * @throws Exception
67cdf0e10cSrcweir      */
loadDocument(String docName, boolean hidden)68cdf0e10cSrcweir     public Rectangle loadDocument(String docName, boolean hidden)
69cdf0e10cSrcweir                                                             throws Exception{
70cdf0e10cSrcweir         wl.resetTrigger();
71cdf0e10cSrcweir         try {
72cdf0e10cSrcweir             PropertyValue [] szArgs = null;
73cdf0e10cSrcweir 			if (hidden) {
74cdf0e10cSrcweir 				szArgs = new PropertyValue [1];
75cdf0e10cSrcweir 				PropertyValue Arg = new PropertyValue();
76cdf0e10cSrcweir 				Arg.Name = "Hidden";
77cdf0e10cSrcweir 				Arg.Value = hidden?"True":"False";
78cdf0e10cSrcweir 				Arg.Handle = -1;
79cdf0e10cSrcweir 				Arg.State = PropertyState.DEFAULT_VALUE;
80cdf0e10cSrcweir 				szArgs[0] = Arg;
81cdf0e10cSrcweir 			}
82cdf0e10cSrcweir 			else {
83cdf0e10cSrcweir 				szArgs = new PropertyValue [0];
84cdf0e10cSrcweir 			}
85cdf0e10cSrcweir 
86cdf0e10cSrcweir             // get the current active window
87cdf0e10cSrcweir             XFrame xCurFrame = UnoRuntime.queryInterface(XFrame.class, xCompLoader);
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 			// create a new frame
90cdf0e10cSrcweir             XFrame xFrame = xCurFrame.findFrame("_blank", FrameSearchFlag.CREATE);
91cdf0e10cSrcweir 
92cdf0e10cSrcweir             // load document in this frame
93cdf0e10cSrcweir             XComponentLoader xFrameLoader = UnoRuntime.queryInterface(XComponentLoader.class, xFrame);
94cdf0e10cSrcweir             xComp = xFrameLoader.loadComponentFromURL(
95cdf0e10cSrcweir                                                 docName, "_self", 0, szArgs);
96cdf0e10cSrcweir             // wait for the document to load.
97cdf0e10cSrcweir             try {
98cdf0e10cSrcweir                 Thread.sleep(10000);
99cdf0e10cSrcweir             }
100cdf0e10cSrcweir             catch(java.lang.InterruptedException e) {}
101cdf0e10cSrcweir 
102cdf0e10cSrcweir             xWin = xFrame.getContainerWindow();
103cdf0e10cSrcweir             xWin.addWindowListener(wl);
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir         catch(com.sun.star.io.IOException e) {
106cdf0e10cSrcweir             e.printStackTrace();
107cdf0e10cSrcweir             return null;
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir         catch(com.sun.star.lang.IllegalArgumentException e) {
110cdf0e10cSrcweir             e.printStackTrace();
111cdf0e10cSrcweir             return null;
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir         catch(java.lang.Exception e) {
114cdf0e10cSrcweir             System.out.println("DH3");
115cdf0e10cSrcweir             e.printStackTrace();
116cdf0e10cSrcweir             throw e;
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir         return xWin.getPosSize();
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     /**
123cdf0e10cSrcweir      * Get the size of the current window.
124cdf0e10cSrcweir      * @return The size of the window as Rectangle.
125cdf0e10cSrcweir      */
getDocumentPosSize()126cdf0e10cSrcweir     public Rectangle getDocumentPosSize() {
127cdf0e10cSrcweir         return xWin.getPosSize();
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     /**
131cdf0e10cSrcweir      * Resize the window in defined steps:
132cdf0e10cSrcweir      * width -10 pixel;
133cdf0e10cSrcweir      * height -10 pixel;
134cdf0e10cSrcweir      * X-Position +10 pixel;
135cdf0e10cSrcweir      * Y-Position +10 pixel
136cdf0e10cSrcweir      * @return True if resize worked.
137cdf0e10cSrcweir      */
resizeDocument()138cdf0e10cSrcweir     public boolean resizeDocument() {
139cdf0e10cSrcweir         Rectangle newPosSize = xWin.getPosSize();
140cdf0e10cSrcweir         newPosSize.Width = newPosSize.Width - 20;
141cdf0e10cSrcweir         newPosSize.Height = newPosSize.Height - 20;
142cdf0e10cSrcweir         newPosSize.X = newPosSize.X + 80;
143cdf0e10cSrcweir         newPosSize.Y = newPosSize.Y + 80;
144cdf0e10cSrcweir         return resizeDocument(newPosSize);
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     /**
148cdf0e10cSrcweir      * Resize window to the given Rectangle
149cdf0e10cSrcweir      * @param newPosSize The new position and size of the window.
150cdf0e10cSrcweir      * @return True if resize worked.
151cdf0e10cSrcweir      */
resizeDocument(Rectangle newPosSize)152cdf0e10cSrcweir     public boolean resizeDocument(Rectangle newPosSize){
153cdf0e10cSrcweir         wl.resetTrigger();
154cdf0e10cSrcweir         xWin.setPosSize(newPosSize.X, newPosSize.Y, newPosSize.Width,
155cdf0e10cSrcweir                                     newPosSize.Height, PosSize.POSSIZE);
156cdf0e10cSrcweir         try {
157cdf0e10cSrcweir             Thread.sleep(3000);
158cdf0e10cSrcweir         }
159cdf0e10cSrcweir         catch(java.lang.InterruptedException e) {}
160cdf0e10cSrcweir         return wl.resizedTrigger;
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir }
163