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.bean;
24 
25 
26 // import com.sun.star.comp.beans.LocalOfficeConnection;
27 import com.sun.star.uno.XComponentContext;
28 import java.awt.Rectangle;
29 import java.awt.Insets;
30 import java.awt.BorderLayout;
31 import com.sun.star.comp.beans.OOoBean;
32 import com.sun.star.uno.UnoRuntime;
33 
34 
35 
36 class WriterFrame extends java.awt.Frame
37 {
38     com.sun.star.comp.beans.OOoBean m_bean;
39     final static String m_sDocURL = "private:factory/swriter";
40 
41     /**
42       @param loadBeforeVisible
43           the OOoBean is added to the frame before it is displayable. Then the Java Frame does
44           not have a native window peer yet.
45      */
WriterFrame(int x, int y, int width, int height, boolean loadBeforeVisible, XComponentContext _xConn)46     public WriterFrame(int x, int y, int width, int height, boolean loadBeforeVisible, XComponentContext _xConn) throws Exception
47     {
48 
49         try
50         {
51             if (loadBeforeVisible == false)
52             {
53                 m_bean = new com.sun.star.comp.beans.OOoBean(new PrivateLocalOfficeConnection(_xConn));
54                 add(m_bean, BorderLayout.CENTER);
55                 pack();
56                 setBounds(x, y, width, height);
57                 setVisible(true);
58                 m_bean.loadFromURL(m_sDocURL, null);
59                 validate();
60             }
61             else
62             {
63                 m_bean = new com.sun.star.comp.beans.OOoBean(new PrivateLocalOfficeConnection(_xConn));
64                 m_bean.loadFromURL(m_sDocURL, null);
65                 add(m_bean, BorderLayout.CENTER);
66                 pack();
67                 setBounds(x, y, width, height);
68                 setVisible(true);
69                 m_bean.aquireSystemWindow();
70             }
71         }
72         catch (Exception e)
73         {
74             System.out.println("Exception caught: " + e.getMessage());
75         }
76     }
77 
WriterFrame()78     public WriterFrame() throws Exception
79     {
80         this(0, 0, 800, 400, false, null);
81     }
82 
setText(String s)83     public void setText(String s) throws Exception
84     {
85         com.sun.star.frame.XModel model = (com.sun.star.frame.XModel)m_bean.getDocument();
86         com.sun.star.text.XTextDocument myDoc =
87             UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, model);
88         com.sun.star.text.XText xText = myDoc.getText();
89         com.sun.star.text.XTextCursor xTCursor = xText.createTextCursor();
90         //inserting some Text
91         xText.insertString( xTCursor, s, false );
92     }
93 
getText()94     public String getText() throws Exception
95     {
96         com.sun.star.frame.XModel model = (com.sun.star.frame.XModel)m_bean.getDocument();
97         com.sun.star.text.XTextDocument myDoc =
98             UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, model);
99         com.sun.star.text.XText xText = myDoc.getText();
100         return xText.getString();
101     }
102 
103     @Override
dispose()104     public void dispose() {
105         m_bean.stopOOoConnection();
106         setVisible(false);
107         super.dispose();
108     }
109 
getBean()110     OOoBean getBean()
111     {
112         return m_bean;
113     }
114 
115     /** Makes sure the document is displayed at the beginning.
116      * This is important for comparing screenshots.
117      */
goToStart()118     public void goToStart() throws Exception
119     {
120         com.sun.star.frame.XModel xModel = (com.sun.star.frame.XModel)m_bean.getDocument();
121         com.sun.star.frame.XController xController = xModel.getCurrentController();
122 
123         com.sun.star.text.XTextViewCursorSupplier xVCSupplier =
124             UnoRuntime.queryInterface(com.sun.star.text.XTextViewCursorSupplier.class, xController);
125 
126         com.sun.star.text.XTextViewCursor xTViewCursor = xVCSupplier.getViewCursor ( );
127         xTViewCursor.gotoStart(false);
128     }
129 
130 
pageDown()131     public void pageDown() throws Exception
132     {
133         com.sun.star.frame.XModel xModel = (com.sun.star.frame.XModel)m_bean.getDocument();
134         com.sun.star.frame.XController xController = xModel.getCurrentController();
135 
136         com.sun.star.text.XTextViewCursorSupplier xVCSupplier =
137             UnoRuntime.queryInterface(com.sun.star.text.XTextViewCursorSupplier.class, xController);
138         com.sun.star.text.XTextViewCursor xTViewCursor = xVCSupplier.getViewCursor ( );
139         com.sun.star.view.XScreenCursor xScreenCursor =
140             UnoRuntime.queryInterface(com.sun.star.view.XScreenCursor.class, xTViewCursor);
141         xScreenCursor.screenDown();
142     }
143 
getClientArea()144     public Rectangle getClientArea()
145     {
146 
147         Insets i = getInsets();
148         Rectangle r = getBounds();
149         Rectangle rc = new Rectangle(r.x + i.left, r.y + i.top,
150                                      r.width - i.left - i.right,
151                                      r.height - i.top - i.bottom);
152         return rc;
153     }
154 
getUnoFramePosition()155     public Rectangle getUnoFramePosition() throws Exception
156     {
157         com.sun.star.awt.XWindow win = m_bean.getFrame().getContainerWindow();
158         com.sun.star.awt.Rectangle rect = win.getPosSize();
159         return new Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
160     }
161 
162     /** After resizing the Java frame, the UNO window shall be resized
163      * as well, which is checked by this method.
164      */
checkUnoFramePosition()165     public boolean checkUnoFramePosition() throws Exception
166     {
167         Rectangle client = getClientArea();
168         Rectangle unoWin = getUnoFramePosition();
169 
170         if (client.x != unoWin.x
171             || client.y != unoWin.y
172             || client.width != unoWin.width
173             || client.height != unoWin.height)
174         {
175             System.out.println("\nPosition of client are of Java frame does not match the postion" +
176                                "of the UNO window. These are the values of Java frame, followed by" +
177                                "the UNO window: ");
178             System.out.println(client);
179             System.out.println(unoWin);
180             System.out.println("");
181             return false;
182         }
183 
184         return true;
185     }
186 
removeOOoBean()187     public void removeOOoBean() throws Exception
188     {
189         //OOoBean.releaseSystemWindow need not be called because
190         //LocalOfficeWindow overrides removeNotify.
191         //However because of bt4745222 which was fixed in 1.4.2_04,
192         //this is very very slow. The workaround is use releaseSystemWindow
193         //beforehand.
194         m_bean.releaseSystemWindow();
195         remove(m_bean);
196     }
197 
addOOoBean()198     public void addOOoBean() throws Exception
199     {
200         add(m_bean, BorderLayout.CENTER);
201         m_bean.aquireSystemWindow();
202         validate();
203     }
204 
205 }
206 
207