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