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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 #ifndef DBAUI_COLUMNCONTROLWINDOW_HXX
31 #include "ColumnControlWindow.hxx"
32 #endif
33 #ifndef DBAUI_FIELDCONTROLS_HXX
34 #include "FieldControls.hxx"
35 #endif
36 #ifndef INCLUDED_SVTOOLS_SYSLOCALE_HXX
37 #include <unotools/syslocale.hxx>
38 #endif
39 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
40 #include <connectivity/dbtools.hxx>
41 #endif
42 #ifndef DBAUI_TOOLS_HXX
43 #include "UITools.hxx"
44 #endif
45 #ifndef _DBU_RESOURCE_HRC_
46 #include "dbu_resource.hrc"
47 #endif
48 
49 
50 using namespace ::dbaui;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::container;
54 using namespace ::com::sun::star::util;
55 using namespace ::com::sun::star::sdbc;
56 using namespace ::com::sun::star::lang;
57 
58 //========================================================================
59 // OColumnControlWindow
60 DBG_NAME(OColumnControlWindow)
61 //========================================================================
62 OColumnControlWindow::OColumnControlWindow(Window* pParent
63 										   ,const Reference<XMultiServiceFactory>& _rxFactory)
64 			: OFieldDescControl(pParent,NULL)
65 			, m_xORB(_rxFactory)
66 			, m_sTypeNames(ModuleRes(STR_TABLEDESIGN_DBFIELDTYPES))
67 			, m_bAutoIncrementEnabled(sal_True)
68 {
69     DBG_CTOR(OColumnControlWindow,NULL);
70 
71 	setRightAligned();
72 	m_aLocale = SvtSysLocale().GetLocaleData().getLocale();
73 }
74 // -----------------------------------------------------------------------------
75 OColumnControlWindow::~OColumnControlWindow()
76 {
77 
78     DBG_DTOR(OColumnControlWindow,NULL);
79 }
80 // -----------------------------------------------------------------------
81 void OColumnControlWindow::ActivateAggregate( EControlType eType )
82 {
83 	switch(eType )
84 	{
85 		case tpFormat:
86 		case tpDefault:
87 //		case tpAutoIncrement:
88 		case tpColumnName:
89 			break;
90 		default:
91 			OFieldDescControl::ActivateAggregate( eType );
92 	}
93 }
94 // -----------------------------------------------------------------------
95 void OColumnControlWindow::DeactivateAggregate( EControlType eType )
96 {
97 	switch(eType )
98 	{
99 		case tpFormat:
100 		case tpDefault:
101 //		case tpAutoIncrement:
102 		case tpColumnName:
103 			break;
104 		default:
105 			OFieldDescControl::DeactivateAggregate( eType );
106 	}
107 }
108 // -----------------------------------------------------------------------------
109 void OColumnControlWindow::CellModified(long /*nRow*/, sal_uInt16 /*nColId*/ )
110 {
111 	saveCurrentFieldDescData();
112 }
113 // -----------------------------------------------------------------------------
114 ::com::sun::star::lang::Locale	OColumnControlWindow::GetLocale() const
115 {
116 	return m_aLocale;
117 }
118 // -----------------------------------------------------------------------------
119 Reference< XNumberFormatter > OColumnControlWindow::GetFormatter() const
120 {
121 	if ( !m_xFormatter.is() )
122 		try
123 		{
124 			Reference< XNumberFormatsSupplier >  xSupplier(::dbtools::getNumberFormats(m_xConnection, sal_True,m_xORB));
125 
126 			if ( xSupplier.is() )
127 			{
128 				// create a new formatter
129 				m_xFormatter.set( m_xORB->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.util.NumberFormatter"))), UNO_QUERY);
130 				if (m_xFormatter.is())
131 					m_xFormatter->attachNumberFormatsSupplier(xSupplier);
132 			}
133 		}
134 		catch(Exception&)
135 		{
136 		}
137 	return m_xFormatter;
138 }
139 // -----------------------------------------------------------------------------
140 TOTypeInfoSP OColumnControlWindow::getTypeInfo(sal_Int32 _nPos)
141 {
142 	return ( _nPos >= 0 && _nPos < static_cast<sal_Int32>(m_aDestTypeInfoIndex.size())) ? m_aDestTypeInfoIndex[_nPos]->second : TOTypeInfoSP();
143 }
144 // -----------------------------------------------------------------------------
145 const OTypeInfoMap*	OColumnControlWindow::getTypeInfo() const
146 {
147 	return &m_aDestTypeInfo;
148 }
149 // -----------------------------------------------------------------------------
150 Reference< XDatabaseMetaData> OColumnControlWindow::getMetaData()
151 {
152 	if ( m_xConnection.is() )
153 		return m_xConnection->getMetaData();
154 	return Reference< XDatabaseMetaData>();
155 }
156 // -----------------------------------------------------------------------------
157 Reference< XConnection> OColumnControlWindow::getConnection()
158 {
159 	return m_xConnection;
160 }
161 // -----------------------------------------------------------------------------
162 void OColumnControlWindow::setConnection(const Reference< XConnection>& _xCon)
163 {
164 	m_xConnection = _xCon;
165 	m_xFormatter = NULL;
166 	m_aDestTypeInfoIndex.clear();
167 	m_aDestTypeInfo.clear();
168 
169 	if ( m_xConnection.is() )
170 	{
171 		Init();
172 
173 		::dbaui::fillTypeInfo(m_xConnection,m_sTypeNames,m_aDestTypeInfo,m_aDestTypeInfoIndex);
174         // read autoincrement value set in the datasource
175 		::dbaui::fillAutoIncrementValue(m_xConnection,m_bAutoIncrementEnabled,m_sAutoIncrementValue);
176 	}
177 }
178 // -----------------------------------------------------------------------------
179 sal_Bool OColumnControlWindow::isAutoIncrementValueEnabled() const
180 {
181 	return m_bAutoIncrementEnabled;
182 }
183 // -----------------------------------------------------------------------------
184 ::rtl::OUString OColumnControlWindow::getAutoIncrementValue() const
185 {
186 	return m_sAutoIncrementValue;
187 }
188 // -----------------------------------------------------------------------------
189 TOTypeInfoSP OColumnControlWindow::getDefaultTyp() const
190 {
191 	if ( !m_pTypeInfo.get() )
192 	{
193 		m_pTypeInfo = TOTypeInfoSP(new OTypeInfo());
194 		m_pTypeInfo->aUIName = m_sTypeNames.GetToken(TYPE_OTHER);
195 	}
196 	return m_pTypeInfo;
197 }
198 // -----------------------------------------------------------------------------
199