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 
24 package com.sun.star.wizards.document;
25 
26 import com.sun.star.awt.Point;
27 import com.sun.star.beans.XPropertySet;
28 import com.sun.star.beans.XPropertySetInfo;
29 import com.sun.star.container.XNameContainer;
30 import com.sun.star.drawing.XShapes;
31 import com.sun.star.sdbc.DataType;
32 import com.sun.star.wizards.common.Desktop;
33 import com.sun.star.wizards.common.Helper;
34 import com.sun.star.wizards.common.PropertyNames;
35 import com.sun.star.wizards.db.FieldColumn;
36 
37 /**
38  * @author Administrator
39  *
40  * To change the template for this generated type comment go to
41  * Window>Preferences>Java>Code Generation>Code and Comments
42  */
43 public class DatabaseControl extends Control
44 {
45 
46     private int m_nFieldType;
47     // private int iMemofieldwidth = IIMGFIELDWIDTH;
48     // private int iMemofieldheight = -1;
49     // private FieldColumn m_FieldColumn;
50 
51     public DatabaseControl(GridControl _oGridControl, FieldColumn _curfieldcolumn)
52     {
53         super();
54         // m_FieldColumn = _curfieldcolumn;
55         if (_curfieldcolumn.getFieldType() != DataType.TIMESTAMP)
56         {
57             createGridColumn(_oGridControl, _curfieldcolumn, _curfieldcolumn.getFieldType(), _curfieldcolumn.getFieldTitle());
58         }
59     }
60 
61     public DatabaseControl(GridControl _oGridControl, FieldColumn _curfieldcolumn, int _fieldtype, String _columntitle)
62     {
63         super();
64         // m_FieldColumn = _curfieldcolumn;
65         createGridColumn(_oGridControl, _curfieldcolumn, _fieldtype, _columntitle);
66     }
67 
68     protected int getFieldType()
69     {
70         return m_nFieldType;
71     }
72 
73     private void createGridColumn(GridControl _oGridControl, FieldColumn _curfieldcolumn, int _fieldtype, String _columntitle)
74     {
75         try
76         {
77             m_nFieldType = _fieldtype;
78             String sFieldName = _curfieldcolumn.getFieldName();
79             String sUniqueName = Desktop.getUniqueName(_oGridControl.xNameAccess, sFieldName);
80 
81             String sGridColumnName = getGridColumnName();
82             XPropertySet xPropColumn = _oGridControl.xGridColumnFactory.createColumn(sGridColumnName);
83             xPropColumn.setPropertyValue(PropertyNames.PROPERTY_NAME, sUniqueName);
84             boolean bHidden = false;
85             if (_fieldtype == DataType.LONGVARBINARY)  //TODO CONTROLType abfragen!!!!!!
86             {
87                 bHidden = true;
88             }
89             xPropColumn.setPropertyValue("Hidden", Boolean.valueOf(bHidden));
90             xPropColumn.setPropertyValue("DataField", sFieldName);
91             xPropColumn.setPropertyValue(PropertyNames.PROPERTY_LABEL, _columntitle);
92             xPropColumn.setPropertyValue(PropertyNames.PROPERTY_WIDTH, 0);  // Width of column is adjusted to Columname
93 
94             XPropertySetInfo xPSI = xPropColumn.getPropertySetInfo();
95             if ( xPSI.hasPropertyByName( "MouseWheelBehavior" ) )
96                 xPropColumn.setPropertyValue( "MouseWheelBehavior", new Short( com.sun.star.awt.MouseWheelBehavior.SCROLL_DISABLED ) );
97 
98             setNumericLimits();
99             _oGridControl.xNameContainer.insertByName(sFieldName, xPropColumn);
100         }
101         catch (Exception e)
102         {
103             e.printStackTrace(System.out);
104         }
105     }
106 
107     public DatabaseControl(FormHandler _oFormHandler, String _sServiceName, Point _aPoint)
108     {
109         super(_oFormHandler, _sServiceName, _aPoint);
110     }
111 
112     public DatabaseControl(FormHandler _oFormHandler, XNameContainer _xFormName, String _curFieldName, int _fieldtype, Point _aPoint)
113     {
114         super(_oFormHandler, _xFormName, _oFormHandler.getControlType(_fieldtype), _curFieldName, _aPoint, null);
115         try
116         {
117             m_nFieldType = _fieldtype;
118             Helper.setUnoPropertyValue(xControlModel, "DataField", _curFieldName);
119             setNumericLimits();
120         }
121         catch (Exception e)
122         {
123             e.printStackTrace(System.out);
124         }
125     }
126 
127     public DatabaseControl(FormHandler _oFormHandler, XShapes _xGroupShapes, XNameContainer _xFormName, String _curFieldName, int _fieldtype, Point _aPoint)
128     {
129         super(_oFormHandler, _xGroupShapes, _xFormName, _oFormHandler.getControlType(_fieldtype), _aPoint, null);
130         try
131         {
132             m_nFieldType = _fieldtype;
133             Helper.setUnoPropertyValue(xControlModel, "DataField", _curFieldName);
134             setNumericLimits();
135         }
136         catch (Exception e)
137         {
138             e.printStackTrace(System.out);
139         }
140     }
141 
142     private String getGridColumnName()
143     {
144         for (int i = 0; i < FormHandler.oControlData.length; i++)
145         {
146             if (FormHandler.oControlData[i].DataType == getFieldType())
147             {
148                 return FormHandler.oControlData[i].GridColumnName;
149             }
150         }
151         return PropertyNames.EMPTY_STRING;
152     }
153 
154     public int getControlHeight()
155     {
156         if (getControlType() == FormHandler.SOIMAGECONTROL)
157         {
158             // return oFormHandler.getImageControlHeight();
159             return oFormHandler.getControlReferenceHeight() * 4;
160         }
161         else
162         {
163             if (getFieldType() == DataType.LONGVARCHAR)
164             {
165                 // Helper.setUnoPropertyValue(xControlModel, PropertyNames.PROPERTY_MULTILINE, Boolean.TRUE);
166                 return oFormHandler.getControlReferenceHeight() * 4;
167             }
168             else if (getControlType() == FormHandler.SOCHECKBOX)
169             {
170                 return super.getPreferredHeight();
171             }
172         }
173         return oFormHandler.getControlReferenceHeight();
174     }
175 
176     public int getControlWidth()
177     {
178         if (getControlType() == FormHandler.SOIMAGECONTROL)
179         {
180             return 2 * getControlHeight();
181         }
182         if (getFieldType() == DataType.LONGVARCHAR)
183         {
184             return 2 * getControlHeight();
185         }
186         else
187         {
188             return getPreferredWidth();
189         }
190     }
191 
192     private static long m_nLongMax = 0;
193     public static long getLongMax()
194     {
195         if (m_nLongMax == 0)
196         {
197         }
198         return m_nLongMax;
199     }
200     public void setNumericLimits()
201     {
202         try
203         {
204             if (getControlType() == FormHandler.SONUMERICCONTROL)
205             {
206                 xPropertySet.setPropertyValue("TreatAsNumber", Boolean.TRUE);
207                 // Math.
208                 // 2^63 - 1 = 9223372036854775807
209                 // int nIntMax = 0x7fffffff;
210                 // int nIntMin = -0x80000000;
211                 // long nLongMax = nIntMax;
212                 // nLongMax <<= (4*4);
213                 // nLongMax |= 0xffff;
214                 // nLongMax <<= (4*4);
215                 // nLongMax |= 0xffff;
216 
217                 // long nLongMin = nIntMin;
218                 // nLongMin <<= (8*4);
219 
220                 // long nLong2 = (long)9223372036854775807;
221 
222                 switch (getFieldType())
223                 {
224                     case DataType.BIGINT:
225                         xPropertySet.setPropertyValue("EffectiveMax", new Double(Long.MAX_VALUE));
226                         xPropertySet.setPropertyValue("EffectiveMin", new Double(Long.MIN_VALUE));
227                         break;
228                     case DataType.INTEGER:
229                         xPropertySet.setPropertyValue("EffectiveMax", new Double(Integer.MAX_VALUE));
230                         xPropertySet.setPropertyValue("EffectiveMin", new Double(Integer.MIN_VALUE));
231                         break;
232                     case DataType.SMALLINT:
233                         xPropertySet.setPropertyValue("EffectiveMax", new Double(Short.MAX_VALUE));
234                         xPropertySet.setPropertyValue("EffectiveMin", new Double(Short.MIN_VALUE));
235                         break;
236                     case DataType.TINYINT:
237                         xPropertySet.setPropertyValue("EffectiveMax", new Double(127));
238                         xPropertySet.setPropertyValue("EffectiveMin", new Double(-128));
239                         break;
240                     case DataType.FLOAT:
241                     case DataType.REAL:
242                     case DataType.DOUBLE:
243                     case DataType.DECIMAL:
244                     case DataType.NUMERIC:
245                         break;
246                 }
247             }
248             // else if (getControlType() == FormHandler.SOTEXTBOX)
249             // {     // com.sun.star.sdbc.DataType.CHAR, com.sun.star.sdbc.DataType.VARCHAR, com.sun.star.sdbc.DataType.LONGVARCHAR
250             // }
251             else if (getControlType() == FormHandler.SOIMAGECONTROL)
252             {
253                 xPropertySet.setPropertyValue("ScaleMode", com.sun.star.awt.ImageScaleMode.Isotropic);
254             }
255         }
256         catch (Exception e)
257         {
258             e.printStackTrace(System.out);
259         }
260     }
261     /**
262      * @return
263      */
264 }
265