1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WriterFrame.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import complexlib
.ComplexTestCase
;
34 import java
.awt
.Rectangle
;
35 import java
.awt
.Insets
;
36 import java
.awt
.BorderLayout
;
37 import java
.awt
.event
.*;
38 import java
.awt
.Frame
;
39 import java
.awt
.Dimension
;
40 import com
.sun
.star
.comp
.beans
.OOoBean
;
41 import com
.sun
.star
.uno
.UnoRuntime
;
42 import com
.sun
.star
.text
.XTextDocument
;
45 class WriterFrame
extends java
.awt
.Frame
47 com
.sun
.star
.comp
.beans
.OOoBean m_bean
;
48 String m_sDocURL
= "private:factory/swriter";
51 @param loadBeforeVisible
52 the OOoBean is added to the frame before it is displayable. Then the Java Frame does
53 not have a native window peer yet.
55 public WriterFrame(int x
, int y
, int width
, int height
, boolean loadBeforeVisible
) throws Exception
58 if (loadBeforeVisible
== false)
60 m_bean
= new com
.sun
.star
.comp
.beans
.OOoBean();
61 add(m_bean
, BorderLayout
.CENTER
);
63 setBounds(x
, y
, width
, height
);
65 m_bean
.loadFromURL(m_sDocURL
, null);
70 m_bean
= new com
.sun
.star
.comp
.beans
.OOoBean();
71 m_bean
.loadFromURL(m_sDocURL
, null);
72 add(m_bean
, BorderLayout
.CENTER
);
74 setBounds(x
, y
, width
, height
);
76 m_bean
.aquireSystemWindow();
80 public WriterFrame() throws Exception
82 this(0, 0, 800, 400, false);
85 public void setText(String s
) throws Exception
87 com
.sun
.star
.frame
.XModel model
= (com
.sun
.star
.frame
.XModel
)m_bean
.getDocument();
88 com
.sun
.star
.text
.XTextDocument myDoc
=
89 (XTextDocument
) UnoRuntime
.queryInterface(com
.sun
.star
.text
.XTextDocument
.class,
91 com
.sun
.star
.text
.XText xText
= myDoc
.getText();
92 com
.sun
.star
.text
.XTextCursor xTCursor
= xText
.createTextCursor();
94 xText
.insertString( xTCursor
, s
, false );
97 public String
getText() throws Exception
99 com
.sun
.star
.frame
.XModel model
= (com
.sun
.star
.frame
.XModel
)m_bean
.getDocument();
100 com
.sun
.star
.text
.XTextDocument myDoc
=
101 (XTextDocument
) UnoRuntime
.queryInterface(com
.sun
.star
.text
.XTextDocument
.class,
103 com
.sun
.star
.text
.XText xText
= myDoc
.getText();
104 return xText
.getString();
107 public void dispose() {
108 m_bean
.stopOOoConnection();
118 /** Makes sure the document is displayed at the beginning.
119 * This is important for comparing screenshots.
121 public void goToStart() throws Exception
123 com
.sun
.star
.frame
.XModel xModel
= (com
.sun
.star
.frame
.XModel
)m_bean
.getDocument();
124 com
.sun
.star
.frame
.XController xController
= xModel
.getCurrentController();
126 com
.sun
.star
.text
.XTextViewCursorSupplier xVCSupplier
=
127 (com
.sun
.star
.text
.XTextViewCursorSupplier
) UnoRuntime
.queryInterface (
128 com
.sun
.star
.text
.XTextViewCursorSupplier
.class, xController
);
130 com
.sun
.star
.text
.XTextViewCursor xTViewCursor
= xVCSupplier
.getViewCursor ( );
131 xTViewCursor
.gotoStart(false);
135 public void pageDown() throws Exception
137 com
.sun
.star
.frame
.XModel xModel
= (com
.sun
.star
.frame
.XModel
)m_bean
.getDocument();
138 com
.sun
.star
.frame
.XController xController
= xModel
.getCurrentController();
140 com
.sun
.star
.text
.XTextViewCursorSupplier xVCSupplier
=
141 (com
.sun
.star
.text
.XTextViewCursorSupplier
) UnoRuntime
.queryInterface (
142 com
.sun
.star
.text
.XTextViewCursorSupplier
.class, xController
);
143 com
.sun
.star
.text
.XTextViewCursor xTViewCursor
= xVCSupplier
.getViewCursor ( );
144 com
.sun
.star
.view
.XScreenCursor xScreenCursor
=
145 (com
.sun
.star
.view
.XScreenCursor
) UnoRuntime
.queryInterface (
146 com
.sun
.star
.view
.XScreenCursor
.class, xTViewCursor
);
147 xScreenCursor
.screenDown();
150 public Rectangle
getClientArea()
153 Insets i
= getInsets();
154 Rectangle r
= getBounds();
155 Rectangle rc
= new Rectangle(r
.x
+ i
.left
, r
.y
+ i
.top
,
156 r
.width
- i
.left
- i
.right
,
157 r
.height
- i
.top
- i
.bottom
);
161 public Rectangle
getUnoFramePosition() throws Exception
163 com
.sun
.star
.awt
.XWindow win
= m_bean
.getFrame().getContainerWindow();
164 com
.sun
.star
.awt
.Rectangle rect
= win
.getPosSize();
165 return new Rectangle(rect
.X
, rect
.Y
, rect
.Width
, rect
.Height
);
168 /** After resizing the Java frame, the UNO window shall be resized
169 * as well, which is checked by this method.
171 public boolean checkUnoFramePosition() throws Exception
173 Rectangle client
= getClientArea();
174 Rectangle unoWin
= getUnoFramePosition();
176 if (client
.x
!= unoWin
.x
177 || client
.y
!= unoWin
.y
178 || client
.width
!= unoWin
.width
179 || client
.height
!= unoWin
.height
)
181 System
.out
.println("\nPosition of client are of Java frame does not match the postion" +
182 "of the UNO window. These are the values of Java frame, followed by" +
184 System
.out
.println(client
);
185 System
.out
.println(unoWin
);
186 System
.out
.println("");
193 public void removeOOoBean() throws Exception
195 //OOoBean.releaseSystemWindow need not be called because
196 //LocalOfficeWindow overrides removeNotify.
197 //However because of bt4745222 which was fixed in 1.4.2_04,
198 //this is very very slow. The workaround is use releaseSystemWindow
200 m_bean
.releaseSystemWindow();
204 public void addOOoBean() throws Exception
206 add(m_bean
, BorderLayout
.CENTER
);
207 m_bean
.aquireSystemWindow();