1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
package integration.forms;
/**************************************************************************/
import com.sun.star.awt.XControl;
import com.sun.star.awt.XControlModel;
import com.sun.star.awt.XWindow;
import com.sun.star.awt.XToolkitExperimental;
import com.sun.star.beans.PropertyValue;
import com.sun.star.form.XForm;
import com.sun.star.form.runtime.XFormController;
import com.sun.star.frame.XController;
import com.sun.star.frame.XDispatch;
import com.sun.star.frame.XDispatchProvider;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.util.URL;
import com.sun.star.util.XURLTransformer;
import com.sun.star.view.XControlAccess;
import com.sun.star.view.XFormLayerAccess;
import org.openoffice.xforms.XMLDocument;
/**************************************************************************/
/** provides a small wrapper around a document view
*/
public class DocumentViewHelper
{
private final XMultiServiceFactory m_orb;
private final XController m_controller;
private final DocumentHelper m_document;
/* ------------------------------------------------------------------ */
final protected XController getController()
{
return m_controller;
}
/* ------------------------------------------------------------------ */
final protected DocumentHelper getDocument()
{
return m_document;
}
/* ------------------------------------------------------------------ */
public DocumentViewHelper( XMultiServiceFactory orb, DocumentHelper document, XController controller )
{
m_orb = orb;
m_document = document;
m_controller = controller;
}
/* ------------------------------------------------------------------ */
/** Quick access to a given interface of the view
@param aInterfaceClass
the class of the interface which shall be returned
*/
public <T> T query( Class<T> aInterfaceClass )
{
return UnoRuntime.queryInterface( aInterfaceClass, m_controller );
}
/* ------------------------------------------------------------------ */
/** retrieves a dispatcher for the given URL, obtained at the current view of the document
@param aURL
a one-element array. The first element must contain a valid
<member scope="com.sun.star.util">URL::Complete</member> value. Upon return, the URL is correctly
parsed.
@return
the dispatcher for the URL in question
*/
private XDispatch getDispatcher( URL[] aURL ) throws java.lang.Exception
{
XDispatch xReturn = null;
// go get the current view
XController xController = query( XController.class );
// go get the dispatch provider of its frame
XDispatchProvider xProvider = UnoRuntime.queryInterface(
XDispatchProvider.class, xController.getFrame() );
if ( null != xProvider )
{
// need a URLTransformer
XURLTransformer xTransformer = UnoRuntime.queryInterface(
XURLTransformer.class, m_orb.createInstance( "com.sun.star.util.URLTransformer" ) );
xTransformer.parseStrict( aURL );
xReturn = xProvider.queryDispatch( aURL[0], "", 0 );
}
return xReturn;
}
/* ------------------------------------------------------------------ */
/** retrieves a dispatcher for the given URL, obtained at the current view of the document
*/
public XDispatch getDispatcher( String url ) throws java.lang.Exception
{
URL[] aURL = new URL[] { new URL() };
aURL[0].Complete = url;
return getDispatcher( aURL );
}
/* ------------------------------------------------------------------ */
/** dispatches the given URL into the view, if there's a dispatcher for it
@return
<TRUE/> if the URL was successfully dispatched
*/
public boolean dispatch( String url ) throws java.lang.Exception
{
URL[] completeURL = new URL[] { new URL() };
completeURL[0].Complete = url;
XDispatch dispatcher = getDispatcher( completeURL );
if ( dispatcher == null )
return false;
PropertyValue[] aDummyArgs = new PropertyValue[] { };
dispatcher.dispatch( completeURL[0], aDummyArgs );
return true;
}
/* ------------------------------------------------------------------ */
/** retrieves a control within the current view of a document
@param xModel
specifies the control model whose control should be located
@return
the control tied to the model
*/
public XControl getControl( XControlModel xModel ) throws com.sun.star.uno.Exception
{
// the current view of the document
XControlAccess xCtrlAcc = query( XControlAccess.class );
// delegate the task of looking for the control
return xCtrlAcc.getControl( xModel );
}
/* ------------------------------------------------------------------ */
public XControl getControl( Object aModel ) throws com.sun.star.uno.Exception
{
XControlModel xModel = UnoRuntime.queryInterface( XControlModel.class, aModel );
return getControl( xModel );
}
/* ------------------------------------------------------------------ */
public <T> T getControl( Object aModel, Class<T> aInterfaceClass ) throws com.sun.star.uno.Exception
{
XControlModel xModel = UnoRuntime.queryInterface( XControlModel.class, aModel );
return UnoRuntime.queryInterface( aInterfaceClass, getControl( xModel ) );
}
/* ------------------------------------------------------------------ */
/** retrieves the form controller for a given logical form
*/
private XFormController getFormController( XForm _form )
{
XFormLayerAccess formLayerAccess = query( XFormLayerAccess.class );
return formLayerAccess.getFormController( _form );
}
/* ------------------------------------------------------------------ */
/** retrieves the form controller for a given logical form
*/
public XFormController getFormController( Object _form )
{
return getFormController( UnoRuntime.queryInterface( XForm.class, _form ));
}
/* ------------------------------------------------------------------ */
/** toggles the design mode of the form layer of active view of our sample document
*/
protected void toggleFormDesignMode( ) throws java.lang.Exception
{
if ( m_document instanceof XMLDocument )
dispatch( ".uno:SwitchXFormsDesignMode" );
else
dispatch( ".uno:SwitchControlDesignMode" );
// at least SwitchControlDesignMode is async, so wait for it to be done
XToolkitExperimental xToolkit = UnoRuntime.queryInterface(
XToolkitExperimental.class,
m_orb.createInstance("com.sun.star.awt.Toolkit"));
xToolkit.processEventsToIdle();
}
/* ------------------------------------------------------------------ */
/** sets the focus to a specific control
@param xModel
a control model. The focus is set to that control which is part of our view
and associated with the given model.
*/
public void grabControlFocus( Object xModel ) throws com.sun.star.uno.Exception
{
// look for the control from the current view which belongs to the model
XControl xControl = getControl( xModel );
// the focus can be set to an XWindow only
XWindow xControlWindow = UnoRuntime.queryInterface( XWindow.class,
xControl );
// grab the focus
xControlWindow.setFocus();
}
}
|