1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.wizards.document;
29 
30 import com.sun.star.awt.Point;
31 import com.sun.star.beans.XPropertySet;
32 import com.sun.star.beans.XPropertySetInfo;
33 import com.sun.star.container.XNameContainer;
34 import com.sun.star.drawing.XShapes;
35 import com.sun.star.sdbc.DataType;
36 import com.sun.star.wizards.common.Desktop;
37 import com.sun.star.wizards.common.Helper;
38 import com.sun.star.wizards.common.PropertyNames;
39 import com.sun.star.wizards.db.FieldColumn;
40 
41 /**
42  * @author Administrator
43  *
44  * To change the template for this generated type comment go to
45  * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
46  */
47 public class DatabaseControl extends Control
48 {
49 
50     private int m_nFieldType;
51     // private int iMemofieldwidth = IIMGFIELDWIDTH;
52     // private int iMemofieldheight = -1;
53     // private FieldColumn m_FieldColumn;
54 
55     public DatabaseControl(GridControl _oGridControl, FieldColumn _curfieldcolumn)
56     {
57         super();
58         // m_FieldColumn = _curfieldcolumn;
59         if (_curfieldcolumn.getFieldType() != DataType.TIMESTAMP)
60         {
61             createGridColumn(_oGridControl, _curfieldcolumn, _curfieldcolumn.getFieldType(), _curfieldcolumn.getFieldTitle());
62         }
63     }
64 
65     public DatabaseControl(GridControl _oGridControl, FieldColumn _curfieldcolumn, int _fieldtype, String _columntitle)
66     {
67         super();
68         // m_FieldColumn = _curfieldcolumn;
69         createGridColumn(_oGridControl, _curfieldcolumn, _fieldtype, _columntitle);
70     }
71 
72     protected int getFieldType()
73     {
74         return m_nFieldType;
75     }
76 
77     private void createGridColumn(GridControl _oGridControl, FieldColumn _curfieldcolumn, int _fieldtype, String _columntitle)
78     {
79         try
80         {
81             m_nFieldType = _fieldtype;
82             String sFieldName = _curfieldcolumn.getFieldName();
83             String sUniqueName = Desktop.getUniqueName(_oGridControl.xNameAccess, sFieldName);
84 
85             String sGridColumnName = getGridColumnName();
86             XPropertySet xPropColumn = _oGridControl.xGridColumnFactory.createColumn(sGridColumnName);
87             xPropColumn.setPropertyValue(PropertyNames.PROPERTY_NAME, sUniqueName);
88             boolean bHidden = false;
89             if (_fieldtype == DataType.LONGVARBINARY)  //TODO CONTROLType abfragen!!!!!!
90             {
91                 bHidden = true;
92             }
93             xPropColumn.setPropertyValue("Hidden", Boolean.valueOf(bHidden));
94             xPropColumn.setPropertyValue("DataField", sFieldName);
95             xPropColumn.setPropertyValue(PropertyNames.PROPERTY_LABEL, _columntitle);
96             xPropColumn.setPropertyValue(PropertyNames.PROPERTY_WIDTH, 0);  // Width of column is adjusted to Columname
97 
98             XPropertySetInfo xPSI = xPropColumn.getPropertySetInfo();
99             if ( xPSI.hasPropertyByName( "MouseWheelBehavior" ) )
100                 xPropColumn.setPropertyValue( "MouseWheelBehavior", new Short( com.sun.star.awt.MouseWheelBehavior.SCROLL_DISABLED ) );
101 
102             setNumericLimits();
103             _oGridControl.xNameContainer.insertByName(sFieldName, xPropColumn);
104         }
105         catch (Exception e)
106         {
107             e.printStackTrace(System.out);
108         }
109     }
110 
111     public DatabaseControl(FormHandler _oFormHandler, String _sServiceName, Point _aPoint)
112     {
113         super(_oFormHandler, _sServiceName, _aPoint);
114     }
115 
116     public DatabaseControl(FormHandler _oFormHandler, XNameContainer _xFormName, String _curFieldName, int _fieldtype, Point _aPoint)
117     {
118         super(_oFormHandler, _xFormName, _oFormHandler.getControlType(_fieldtype), _curFieldName, _aPoint, null);
119         try
120         {
121             m_nFieldType = _fieldtype;
122             Helper.setUnoPropertyValue(xControlModel, "DataField", _curFieldName);
123             setNumericLimits();
124         }
125         catch (Exception e)
126         {
127             e.printStackTrace(System.out);
128         }
129     }
130 
131     public DatabaseControl(FormHandler _oFormHandler, XShapes _xGroupShapes, XNameContainer _xFormName, String _curFieldName, int _fieldtype, Point _aPoint)
132     {
133         super(_oFormHandler, _xGroupShapes, _xFormName, _oFormHandler.getControlType(_fieldtype), _aPoint, null);
134         try
135         {
136             m_nFieldType = _fieldtype;
137             Helper.setUnoPropertyValue(xControlModel, "DataField", _curFieldName);
138             setNumericLimits();
139         }
140         catch (Exception e)
141         {
142             e.printStackTrace(System.out);
143         }
144     }
145 
146     private String getGridColumnName()
147     {
148         for (int i = 0; i < FormHandler.oControlData.length; i++)
149         {
150             if (FormHandler.oControlData[i].DataType == getFieldType())
151             {
152                 return FormHandler.oControlData[i].GridColumnName;
153             }
154         }
155         return PropertyNames.EMPTY_STRING;
156     }
157 
158     public int getControlHeight()
159     {
160         if (getControlType() == FormHandler.SOIMAGECONTROL)
161         {
162             // return oFormHandler.getImageControlHeight();
163             return oFormHandler.getControlReferenceHeight() * 4;
164         }
165         else
166         {
167             if (getFieldType() == DataType.LONGVARCHAR)
168             {
169                 // Helper.setUnoPropertyValue(xControlModel, PropertyNames.PROPERTY_MULTILINE, Boolean.TRUE);
170                 return oFormHandler.getControlReferenceHeight() * 4;
171             }
172             else if (getControlType() == FormHandler.SOCHECKBOX)
173             {
174                 return super.getPreferredHeight();
175             }
176         }
177         return oFormHandler.getControlReferenceHeight();
178     }
179 
180     public int getControlWidth()
181     {
182         if (getControlType() == FormHandler.SOIMAGECONTROL)
183         {
184             return 2 * getControlHeight();
185         }
186         if (getFieldType() == DataType.LONGVARCHAR)
187         {
188             return 2 * getControlHeight();
189         }
190         else
191         {
192             return getPreferredWidth();
193         }
194     }
195 
196     private static long m_nLongMax = 0;
197     public static long getLongMax()
198     {
199         if (m_nLongMax == 0)
200         {
201         }
202         return m_nLongMax;
203     }
204     public void setNumericLimits()
205     {
206         try
207         {
208             if (getControlType() == FormHandler.SONUMERICCONTROL)
209             {
210                 xPropertySet.setPropertyValue("TreatAsNumber", Boolean.TRUE);
211                 // Math.
212                 // 2^63 - 1 = 9223372036854775807
213                 // int nIntMax = 0x7fffffff;
214                 // int nIntMin = -0x80000000;
215                 // long nLongMax = nIntMax;
216                 // nLongMax <<= (4*4);
217                 // nLongMax |= 0xffff;
218                 // nLongMax <<= (4*4);
219                 // nLongMax |= 0xffff;
220 
221                 // long nLongMin = nIntMin;
222                 // nLongMin <<= (8*4);
223 
224                 // long nLong2 = (long)9223372036854775807;
225 
226                 switch (getFieldType())
227                 {
228                     case DataType.BIGINT:
229                         xPropertySet.setPropertyValue("EffectiveMax", new Double(Long.MAX_VALUE));
230                         xPropertySet.setPropertyValue("EffectiveMin", new Double(Long.MIN_VALUE));
231                         break;
232                     case DataType.INTEGER:
233                         xPropertySet.setPropertyValue("EffectiveMax", new Double(Integer.MAX_VALUE));
234                         xPropertySet.setPropertyValue("EffectiveMin", new Double(Integer.MIN_VALUE));
235                         break;
236                     case DataType.SMALLINT:
237                         xPropertySet.setPropertyValue("EffectiveMax", new Double(Short.MAX_VALUE));
238                         xPropertySet.setPropertyValue("EffectiveMin", new Double(Short.MIN_VALUE));
239                         break;
240                     case DataType.TINYINT:
241                         xPropertySet.setPropertyValue("EffectiveMax", new Double(127));
242                         xPropertySet.setPropertyValue("EffectiveMin", new Double(-128));
243                         break;
244                     case DataType.FLOAT:
245                     case DataType.REAL:
246                     case DataType.DOUBLE:
247                     case DataType.DECIMAL:
248                     case DataType.NUMERIC:
249                         break;
250                 }
251             }
252             // else if (getControlType() == FormHandler.SOTEXTBOX)
253             // {     // com.sun.star.sdbc.DataType.CHAR, com.sun.star.sdbc.DataType.VARCHAR, com.sun.star.sdbc.DataType.LONGVARCHAR
254             // }
255             else if (getControlType() == FormHandler.SOIMAGECONTROL)
256             {
257                 xPropertySet.setPropertyValue("ScaleMode", com.sun.star.awt.ImageScaleMode.Isotropic);
258             }
259         }
260         catch (Exception e)
261         {
262             e.printStackTrace(System.out);
263         }
264     }
265     /**
266      * @return
267      */
268 }
269