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  * To change this template, choose Tools | Templates
24  * and open the template in the editor.
25  */
26 
27 package org.openoffice.xforms;
28 
29 import com.sun.star.beans.PropertyVetoException;
30 import com.sun.star.beans.UnknownPropertyException;
31 import com.sun.star.beans.XPropertySet;
32 import com.sun.star.lang.IllegalArgumentException;
33 import com.sun.star.lang.WrappedTargetException;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.xforms.XFormsUIHelper1;
36 import com.sun.star.xforms.XModel;
37 import com.sun.star.xml.dom.XNode;
38 
39 /** encapsulates an XForms model
40  *
41  * @author fs93730
42  */
43 public class Model
44 {
45     private XModel          m_model;
46     private XPropertySet    m_modelProps;
47     private XFormsUIHelper1 m_helper;
48 
Model( Object _model )49     protected Model( Object _model )
50     {
51         m_model = (XModel)UnoRuntime.queryInterface( XModel.class, _model );
52         m_modelProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, _model );
53         m_helper = (XFormsUIHelper1)UnoRuntime.queryInterface( XFormsUIHelper1.class,
54             m_model );
55     }
56 
getXModel()57     protected XModel getXModel()
58     {
59         return m_model;
60     }
61 
getUIHelper()62     protected XFormsUIHelper1 getUIHelper()
63     {
64         return m_helper;
65     }
66 
getDefaultInstance()67     public Instance getDefaultInstance()
68     {
69         return new Instance( this, m_model.getDefaultInstance() );
70     }
71 
72     /** creates a binding for the given DOM node
73      *
74      * @param _node
75      *      the DOM node to create a binding for
76      * @param _dataType
77      *      the data type to be used for the binding
78      * @return
79      */
createBindingForNode( XNode _node, short _dataTypeClass )80     public XPropertySet createBindingForNode( XNode _node, short _dataTypeClass )
81     {
82         XPropertySet binding = m_helper.getBindingForNode(_node, true);
83         try
84         {
85             String basicTypeName = (String)m_model.getDataTypeRepository().getBasicDataType( _dataTypeClass ).
86                 getPropertyValue( "Name" );
87             binding.setPropertyValue( "Type", basicTypeName );
88         }
89         catch (Exception ex)
90         {
91             ex.printStackTrace();
92         }
93         return binding;
94     }
95 
setIsDocumentInternalData( boolean _internalData )96     public void setIsDocumentInternalData( boolean _internalData )
97     {
98         try
99         {
100             m_modelProps.setPropertyValue("ExternalData", new Boolean(!_internalData));
101         }
102         catch (Exception ex)
103         {
104             ex.printStackTrace();
105         }
106     }
107 
getIsDocumentInternalData()108     public boolean getIsDocumentInternalData()
109     {
110         boolean isInternalData = false;
111         try
112         {
113             isInternalData = !((Boolean)m_modelProps.getPropertyValue( "ExternalData" )).booleanValue();
114         }
115         catch (Exception ex)
116         {
117             ex.printStackTrace();
118         }
119         return isInternalData;
120     }
121 }
122