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: DocumentViewHelper.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 ************************************************************************/
30 package org
.openoffice
.sdk
.forms
;
32 import com
.sun
.star
.awt
.XControl
;
33 import com
.sun
.star
.awt
.XControlModel
;
34 import com
.sun
.star
.awt
.XWindow
;
35 import com
.sun
.star
.beans
.PropertyValue
;
36 import com
.sun
.star
.beans
.XPropertySet
;
37 import com
.sun
.star
.container
.XIndexContainer
;
38 import com
.sun
.star
.form
.FormComponentType
;
39 import com
.sun
.star
.form
.XForm
;
40 import com
.sun
.star
.form
.XFormController
;
41 import com
.sun
.star
.frame
.XController
;
42 import com
.sun
.star
.frame
.XDispatch
;
43 import com
.sun
.star
.frame
.XDispatchProvider
;
44 import com
.sun
.star
.lang
.XMultiServiceFactory
;
45 import com
.sun
.star
.uno
.UnoRuntime
;
46 import com
.sun
.star
.util
.URL
;
47 import com
.sun
.star
.util
.XURLTransformer
;
48 import com
.sun
.star
.view
.XControlAccess
;
49 import com
.sun
.star
.view
.XFormLayerAccess
;
52 /**************************************************************************/
53 /** provides a small wrapper around a document view
55 class DocumentViewHelper
57 private XMultiServiceFactory m_orb
;
58 private XController m_controller
;
59 private DocumentHelper m_document
;
61 /* ------------------------------------------------------------------ */
62 final protected XController
getController()
67 /* ------------------------------------------------------------------ */
68 final protected DocumentHelper
getDocument()
73 /* ------------------------------------------------------------------ */
74 public DocumentViewHelper( XMultiServiceFactory orb
, DocumentHelper document
, XController controller
)
77 m_document
= document
;
78 m_controller
= controller
;
81 /* ------------------------------------------------------------------ */
82 /** Quick access to a given interface of the view
83 @param aInterfaceClass
84 the class of the interface which shall be returned
86 public Object
get( Class aInterfaceClass
)
88 return UnoRuntime
.queryInterface( aInterfaceClass
, m_controller
);
91 /* ------------------------------------------------------------------ */
92 /** retrieves a dispatcher for the given URL, obtained at the current view of the document
94 a one-element array. The first element must contain a valid
95 <member scope="com.sun.star.util">URL::Complete</member> value. Upon return, the URL is correctly
98 the dispatcher for the URL in question
100 public XDispatch
getDispatcher( URL
[] aURL
) throws java
.lang
.Exception
102 XDispatch xReturn
= null;
104 // go get the current view
105 XController xController
= (XController
)get( XController
.class );
106 // go get the dispatch provider of it's frame
107 XDispatchProvider xProvider
= (XDispatchProvider
)UnoRuntime
.queryInterface(
108 XDispatchProvider
.class, xController
.getFrame() );
109 if ( null != xProvider
)
111 // need an URLTransformer
112 XURLTransformer xTransformer
= (XURLTransformer
)UnoRuntime
.queryInterface(
113 XURLTransformer
.class, m_orb
.createInstance( "com.sun.star.util.URLTransformer" ) );
114 xTransformer
.parseStrict( aURL
);
116 xReturn
= xProvider
.queryDispatch( aURL
[0], new String( ), 0 );
121 /* ------------------------------------------------------------------ */
122 /** retrieves a dispatcher for the given URL, obtained at the current view of the document
124 public XDispatch
getDispatcher( String sURL
) throws java
.lang
.Exception
126 URL
[] aURL
= new URL
[] { new URL() };
127 aURL
[0].Complete
= sURL
;
128 return getDispatcher( aURL
);
131 /* ------------------------------------------------------------------ */
132 /* retrieves the form controller belonging to a given logical form
134 public XFormController
getFormController( Object _form
)
136 XFormLayerAccess formLayer
= (XFormLayerAccess
)get( XFormLayerAccess
.class );
137 return formLayer
.getFormController( (XForm
)UnoRuntime
.queryInterface( XForm
.class, _form
) );
140 /* ------------------------------------------------------------------ */
141 /** retrieves a control within the current view of a document
143 specifies the control model whose control should be located
145 the control tied to the model
147 public XControl
getFormControl( XControlModel xModel
) throws com
.sun
.star
.uno
.Exception
149 // the current view of the document
150 XControlAccess xCtrlAcc
= (XControlAccess
)get( XControlAccess
.class );
151 // delegate the task of looking for the control
152 return xCtrlAcc
.getControl( xModel
);
155 /* ------------------------------------------------------------------ */
156 public XControl
getFormControl( Object aModel
) throws com
.sun
.star
.uno
.Exception
158 XControlModel xModel
= (XControlModel
)UnoRuntime
.queryInterface( XControlModel
.class, aModel
);
159 return getFormControl( xModel
);
162 /* ------------------------------------------------------------------ */
163 public Object
getFormControl( Object aModel
, Class aInterfaceClass
) throws com
.sun
.star
.uno
.Exception
165 XControlModel xModel
= (XControlModel
)UnoRuntime
.queryInterface( XControlModel
.class, aModel
);
166 return UnoRuntime
.queryInterface( aInterfaceClass
, getFormControl( xModel
) );
169 /* ------------------------------------------------------------------ */
170 /** toggles the design mode of the form layer of active view of our sample document
172 protected void toggleFormDesignMode( ) throws java
.lang
.Exception
174 // get a dispatcher for the toggle URL
175 URL
[] aToggleURL
= new URL
[] { new URL() };
176 aToggleURL
[0].Complete
= new String( ".uno:SwitchControlDesignMode" );
177 XDispatch xDispatcher
= getDispatcher( aToggleURL
);
179 // dispatch the URL - this will result in toggling the mode
180 PropertyValue
[] aDummyArgs
= new PropertyValue
[] { };
181 xDispatcher
.dispatch( aToggleURL
[0], aDummyArgs
);
184 /* ------------------------------------------------------------------ */
185 /** sets the focus to a specific control
187 a control model. The focus is set to that control which is part of our view
188 and associated with the given model.
190 public void grabControlFocus( Object xModel
) throws com
.sun
.star
.uno
.Exception
192 // look for the control from the current view which belongs to the model
193 XControl xControl
= getFormControl( xModel
);
195 // the focus can be set to an XWindow only
196 XWindow xControlWindow
= (XWindow
)UnoRuntime
.queryInterface( XWindow
.class,
200 xControlWindow
.setFocus();
203 /* ------------------------------------------------------------------ */
204 /** sets the focus to the first control
206 protected void grabControlFocus( ) throws java
.lang
.Exception
208 // the forms container of our document
209 XIndexContainer xForms
= UNO
.queryIndexContainer( m_document
.getFormComponentTreeRoot( ) );
211 XIndexContainer xForm
= UNO
.queryIndexContainer( xForms
.getByIndex( 0 ) );
213 // the first control model which is no FixedText (FixedText's can't have the focus)
214 for ( int i
= 0; i
<xForm
.getCount(); ++i
)
216 XPropertySet xControlProps
= UNO
.queryPropertySet( xForm
.getByIndex( i
) );
217 if ( FormComponentType
.FIXEDTEXT
!= ((Short
)xControlProps
.getPropertyValue( "ClassId" )).shortValue() )
219 XControlModel xControlModel
= (XControlModel
)UnoRuntime
.queryInterface(
220 XControlModel
.class, xControlProps
);
221 // set the focus to this control
222 grabControlFocus( xControlModel
);
228 // Note that we simply took the first control model from the hierarchy. This does state nothing
229 // about the location of the respective control in the view. A control model is tied to a control
230 // shape, and the shapes are where the geometry information such as position and size is hung up.
231 // So you could easily have a document where the first control model is bound to a shape which
232 // has a greater ordinate than any other control model.