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