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 
31 #ifndef _SBA_UNODATBR_HXX_
32 #include "unodatbr.hxx"
33 #endif
34 #ifndef DBACCESS_UI_BROWSER_ID_HXX
35 #include "browserids.hxx"
36 #endif
37 #ifndef _DBAUI_LISTVIEWITEMS_HXX_
38 #include "listviewitems.hxx"
39 #endif
40 #ifndef DBACCESS_IMAGEPROVIDER_HXX
41 #include "imageprovider.hxx"
42 #endif
43 #ifndef _TOOLS_DEBUG_HXX
44 #include <tools/debug.hxx>
45 #endif
46 #ifndef DBACCESS_UI_DBTREEVIEW_HXX
47 #include "dbtreeview.hxx"
48 #endif
49 #ifndef DBAUI_DBTREELISTBOX_HXX
50 #include "dbtreelistbox.hxx"
51 #endif
52 #ifndef _DBU_BRW_HRC_
53 #include "dbu_brw.hrc"
54 #endif
55 #ifndef DBAUI_DBTREEMODEL_HXX
56 #include "dbtreemodel.hxx"
57 #endif
58 
59 using namespace ::com::sun::star::frame;
60 using namespace ::dbtools;
61 using namespace ::svx;
62 
63 // .........................................................................
64 namespace dbaui
65 {
66 // .........................................................................
67 // -----------------------------------------------------------------------------
68 SbaTableQueryBrowser::EntryType SbaTableQueryBrowser::getChildType( SvLBoxEntry* _pEntry ) const
69 {
70 	DBG_ASSERT(isContainer(_pEntry), "SbaTableQueryBrowser::getChildType: invalid entry!");
71 	switch (getEntryType(_pEntry))
72 	{
73 		case etTableContainer:
74 			return etTableOrView;
75 		case etQueryContainer:
76 			return etQuery;
77         default:
78             break;
79 	}
80 	return etUnknown;
81 }
82 
83 // -----------------------------------------------------------------------------
84 String SbaTableQueryBrowser::GetEntryText( SvLBoxEntry* _pEntry ) const
85 {
86 	return m_pTreeView->getListBox().GetEntryText(_pEntry);
87 }
88 
89 // -----------------------------------------------------------------------------
90 SbaTableQueryBrowser::EntryType SbaTableQueryBrowser::getEntryType( SvLBoxEntry* _pEntry ) const
91 {
92 	if (!_pEntry)
93 		return etUnknown;
94 
95 	SvLBoxEntry* pRootEntry 	= m_pTreeView->getListBox().GetRootLevelParent(_pEntry);
96 	SvLBoxEntry* pEntryParent	= m_pTreeView->getListBox().GetParent(_pEntry);
97 	SvLBoxEntry* pTables		= m_pTreeView->getListBox().GetEntry(pRootEntry, CONTAINER_TABLES);
98 	SvLBoxEntry* pQueries		= m_pTreeView->getListBox().GetEntry(pRootEntry, CONTAINER_QUERIES);
99 
100 #ifdef DBG_UTIL
101 	String sTest;
102 	if (pTables) sTest = m_pTreeView->getListBox().GetEntryText(pTables);
103 	if (pQueries) sTest = m_pTreeView->getListBox().GetEntryText(pQueries);
104 #endif
105 
106 	if (pRootEntry == _pEntry)
107 		return etDatasource;
108 
109 	if (pTables == _pEntry)
110 		return etTableContainer;
111 
112 	if (pQueries == _pEntry)
113 		return etQueryContainer;
114 
115 	if (pTables == pEntryParent)
116 		return etTableOrView;
117 
118 	if (pQueries == pEntryParent)
119     {
120         DBTreeListUserData* pEntryData = static_cast<DBTreeListUserData*>(_pEntry->GetUserData());
121         if ( pEntryData )
122             return pEntryData->eType;
123 
124 		return etQuery;
125     }
126     while( pEntryParent != pQueries )
127     {
128         pEntryParent = m_pTreeView->getListBox().GetParent(pEntryParent);
129         if ( !pEntryParent )
130             return etUnknown;
131     }
132 
133 	return etQueryContainer;
134 }
135 //------------------------------------------------------------------------------
136 void SbaTableQueryBrowser::select(SvLBoxEntry* _pEntry, sal_Bool _bSelect)
137 {
138 	SvLBoxItem* pTextItem = _pEntry ? _pEntry->GetFirstItem(SV_ITEM_ID_BOLDLBSTRING) : NULL;
139 	if (pTextItem)
140 	{
141 		static_cast<OBoldListboxString*>(pTextItem)->emphasize(_bSelect);
142 		m_pTreeModel->InvalidateEntry(_pEntry);
143 	}
144 	else {
145 		DBG_ERROR("SbaTableQueryBrowser::select: invalid entry!");
146     }
147 }
148 
149 //------------------------------------------------------------------------------
150 void SbaTableQueryBrowser::selectPath(SvLBoxEntry* _pEntry, sal_Bool _bSelect)
151 {
152 	while (_pEntry)
153 	{
154 		select(_pEntry, _bSelect);
155 		_pEntry = m_pTreeModel->GetParent(_pEntry);
156 	}
157 }
158 //------------------------------------------------------------------------------
159 sal_Bool SbaTableQueryBrowser::isSelected(SvLBoxEntry* _pEntry) const
160 {
161 	SvLBoxItem* pTextItem = _pEntry ? _pEntry->GetFirstItem(SV_ITEM_ID_BOLDLBSTRING) : NULL;
162 	if (pTextItem)
163 		return static_cast<OBoldListboxString*>(pTextItem)->isEmphasized();
164 	else {
165 		DBG_ERROR("SbaTableQueryBrowser::isSelected: invalid entry!");
166     }
167 	return sal_False;
168 }
169 //------------------------------------------------------------------------------
170 void SbaTableQueryBrowser::SelectionChanged()
171 {
172     if ( !m_bShowMenu )
173     {
174 	    InvalidateFeature(ID_BROWSER_INSERTCOLUMNS);
175 	    InvalidateFeature(ID_BROWSER_INSERTCONTENT);
176 	    InvalidateFeature(ID_BROWSER_FORMLETTER);
177     } // if ( !m_bShowMenu )
178     InvalidateFeature(ID_BROWSER_COPY);
179     InvalidateFeature(ID_BROWSER_CUT);
180 }
181 //------------------------------------------------------------------------------
182 void SbaTableQueryBrowser::describeSupportedFeatures()
183 {
184 	SbaXDataBrowserController::describeSupportedFeatures();
185 
186 	implDescribeSupportedFeature( ".uno:Title",                             ID_BROWSER_TITLE );
187 	if ( !m_bShowMenu )
188 	{
189 	    implDescribeSupportedFeature( ".uno:DSBrowserExplorer",                 ID_BROWSER_EXPLORER, CommandGroup::VIEW );
190 
191         implDescribeSupportedFeature( ".uno:DSBFormLetter",                     ID_BROWSER_FORMLETTER, CommandGroup::DOCUMENT );
192 	    implDescribeSupportedFeature( ".uno:DSBInsertColumns",                  ID_BROWSER_INSERTCOLUMNS, CommandGroup::INSERT );
193         implDescribeSupportedFeature( ".uno:DSBInsertContent",                  ID_BROWSER_INSERTCONTENT, CommandGroup::INSERT );
194 	    implDescribeSupportedFeature( ".uno:DSBDocumentDataSource",             ID_BROWSER_DOCUMENT_DATASOURCE, CommandGroup::VIEW );
195 
196         implDescribeSupportedFeature( ".uno:DataSourceBrowser/FormLetter",          ID_BROWSER_FORMLETTER );
197 	    implDescribeSupportedFeature( ".uno:DataSourceBrowser/InsertColumns",       ID_BROWSER_INSERTCOLUMNS );
198         implDescribeSupportedFeature( ".uno:DataSourceBrowser/InsertContent",       ID_BROWSER_INSERTCONTENT );
199 	    implDescribeSupportedFeature( ".uno:DataSourceBrowser/DocumentDataSource",  ID_BROWSER_DOCUMENT_DATASOURCE );
200 	}
201 
202     implDescribeSupportedFeature( ".uno:CloseWin",      ID_BROWSER_CLOSE, CommandGroup::DOCUMENT );
203 	implDescribeSupportedFeature( ".uno:DBRebuildData", ID_BROWSER_REFRESH_REBUILD, CommandGroup::DATA );
204 }
205 
206 // -----------------------------------------------------------------------------
207 sal_Int32 SbaTableQueryBrowser::getDatabaseObjectType( EntryType _eType )
208 {
209     switch ( _eType )
210     {
211     case etQuery:
212     case etQueryContainer:
213         return DatabaseObject::QUERY;
214     case etTableOrView:
215     case etTableContainer:
216         return DatabaseObject::TABLE;
217     default:
218         break;
219     }
220     OSL_ENSURE( false, "SbaTableQueryBrowser::getDatabaseObjectType: folder types and 'Unknown' not allowed here!" );
221     return DatabaseObject::TABLE;
222 }
223 
224 // -----------------------------------------------------------------------------
225 void SbaTableQueryBrowser::notifyHiContrastChanged()
226 {
227 	if ( m_pTreeView )
228 	{
229 		// change all bitmap entries
230 		SvLBoxEntry* pEntryLoop = m_pTreeModel->First();
231 		while ( pEntryLoop )
232 		{
233 			DBTreeListUserData* pData = static_cast<DBTreeListUserData*>(pEntryLoop->GetUserData());
234 			if ( !pData )
235             {
236 				pEntryLoop = m_pTreeModel->Next(pEntryLoop);
237                 continue;
238             }
239 
240             // the connection to which this entry belongs, if any
241             ::std::auto_ptr< ImageProvider > pImageProvider( getImageProviderFor( pEntryLoop ) );
242 
243             // the images for this entry
244             Image aImage, aImageHC;
245             if ( pData->eType == etDatasource )
246             {
247                 aImage = pImageProvider->getDatabaseImage( false );
248                 aImageHC = pImageProvider->getDatabaseImage( true );
249             }
250             else
251             {
252                 bool bIsFolder = !isObject( pData->eType );
253                 if ( bIsFolder )
254                 {
255                     sal_Int32 nObjectType( getDatabaseObjectType( pData->eType ) );
256                     aImage = pImageProvider->getFolderImage( nObjectType, false );
257                     aImageHC = pImageProvider->getFolderImage( nObjectType, true );
258                 }
259                 else
260                 {
261                     sal_Int32 nObjectType( getDatabaseObjectType( pData->eType ) );
262                     pImageProvider->getImages( GetEntryText( pEntryLoop ), nObjectType, aImage, aImageHC );
263                 }
264             }
265 
266             // find the proper item, and set its icons
267 			sal_uInt16 nCount = pEntryLoop->ItemCount();
268 			for (sal_uInt16 i=0;i<nCount;++i)
269 			{
270 				SvLBoxItem* pItem = pEntryLoop->GetItem(i);
271 				if ( !pItem || ( pItem->IsA() != SV_ITEM_ID_LBOXCONTEXTBMP ) )
272                     continue;
273 
274                 SvLBoxContextBmp* pContextBitmapItem = static_cast< SvLBoxContextBmp* >( pItem );
275 
276 				pContextBitmapItem->SetBitmap1( aImage, BMP_COLOR_NORMAL );
277 				pContextBitmapItem->SetBitmap2( aImage, BMP_COLOR_NORMAL );
278 				pContextBitmapItem->SetBitmap1( aImageHC, BMP_COLOR_HIGHCONTRAST );
279 				pContextBitmapItem->SetBitmap2( aImageHC, BMP_COLOR_HIGHCONTRAST );
280 				break;
281 			}
282 
283 			pEntryLoop = m_pTreeModel->Next(pEntryLoop);
284 		}
285 	}
286 }
287 // -----------------------------------------------------------------------------
288 // .........................................................................
289 }	// namespace dbaui
290 // .........................................................................
291 
292