xref: /trunk/main/sw/source/ui/fldui/changedb.cxx (revision cdf0e10c)
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_sw.hxx"
30 
31 
32 #ifdef SW_DLLIMPLEMENTATION
33 #undef SW_DLLIMPLEMENTATION
34 #endif
35 
36 
37 #define _CHANGEDB_CXX
38 
39 #include <svtools/stdctrl.hxx>
40 #ifndef _MSGBOX_HXX //autogen
41 #include <vcl/msgbox.hxx>
42 #endif
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 #include <com/sun/star/container/XNameAccess.hpp>
45 #include <com/sun/star/sdb/XDatabaseAccess.hpp>
46 #include <comphelper/processfactory.hxx>
47 #include <sfx2/viewfrm.hxx>
48 
49 #ifndef _VIEW_HXX
50 #include <view.hxx>
51 #endif
52 #include <wrtsh.hxx>
53 #ifndef _DBMGR_HXX
54 #include <dbmgr.hxx>
55 #endif
56 #include <fldmgr.hxx>
57 #include <expfld.hxx>
58 #include <txtatr.hxx>
59 #include <ndtxt.hxx>
60 #include <fldbas.hxx>
61 #include <dbfld.hxx>
62 #include <changedb.hxx>
63 
64 #ifndef _FLDUI_HRC
65 #include <fldui.hrc>
66 #endif
67 #ifndef _UTLUI_HRC
68 #include <utlui.hrc>
69 #endif
70 #ifndef _CHANGEDB_HRC
71 #include <changedb.hrc>
72 #endif
73 
74 #include <unomid.h>
75 
76 using namespace ::com::sun::star::uno;
77 using namespace ::com::sun::star::container;
78 using namespace ::com::sun::star::lang;
79 
80 
81 /*--------------------------------------------------------------------
82 	Beschreibung: Feldeinfuegen bearbeiten
83  --------------------------------------------------------------------*/
84 
85 
86 SwChangeDBDlg::SwChangeDBDlg(SwView& rVw) :
87 	SvxStandardDialog(&rVw.GetViewFrame()->GetWindow(), SW_RES(DLG_CHANGE_DB)),
88 
89     aDBListFL   (this, SW_RES(FL_DBLIST     )),
90 	aUsedDBFT	(this, SW_RES(FT_USEDDB  	)),
91 	aAvailDBFT	(this, SW_RES(FT_AVAILDB 	)),
92 	aUsedDBTLB	(this, SW_RES(TLB_USEDDB  	)),
93     aAvailDBTLB (this, SW_RES(TLB_AVAILDB   ), 0),
94     aAddDBPB    (this, SW_RES(PB_ADDDB)),
95     aDescFT     (this, SW_RES(FT_DESC       )),
96 	aDocDBTextFT(this, SW_RES(FT_DOCDBTEXT	)),
97 	aDocDBNameFT(this, SW_RES(FT_DOCDBNAME	)),
98 	aOKBT		(this, SW_RES(BT_OK      	)),
99 	aCancelBT	(this, SW_RES(BT_CANCEL  	)),
100 	aHelpBT		(this, SW_RES(BT_HELP    	)),
101 //	aChangeBT	(this, SW_RES(BT_CHANGEDB    )),
102     aImageList      (SW_RES(ILIST_DB_DLG    )),
103     aImageListHC    (SW_RES(ILIST_DB_DLG_HC )),
104 
105     pSh(rVw.GetWrtShellPtr()),
106     pMgr( new SwFldMgr() )
107 {
108     aAvailDBTLB.SetWrtShell(*pSh);
109     FillDBPopup();
110 
111 	FreeResource();
112 
113 	ShowDBName(pSh->GetDBData());
114 	aOKBT.SetClickHdl(LINK(this, SwChangeDBDlg, ButtonHdl));
115     aAddDBPB.SetClickHdl(LINK(this, SwChangeDBDlg, AddDBHdl));
116 
117 	aUsedDBTLB.SetSelectionMode(MULTIPLE_SELECTION);
118 	aUsedDBTLB.SetStyle(aUsedDBTLB.GetStyle()|WB_HASLINES|WB_CLIPCHILDREN|WB_SORT|WB_HASBUTTONS|WB_HASBUTTONSATROOT|WB_HSCROLL);
119 	aUsedDBTLB.SetSpaceBetweenEntries(0);
120 
121     aUsedDBTLB.SetNodeBitmaps( aImageList.GetImage(IMG_COLLAPSE),
122                     aImageList.GetImage(IMG_EXPAND  ), BMP_COLOR_NORMAL );
123     aUsedDBTLB.SetNodeBitmaps( aImageListHC.GetImage(IMG_COLLAPSE),
124                     aImageListHC.GetImage(IMG_EXPAND  ), BMP_COLOR_HIGHCONTRAST );
125 
126 	Link aLink = LINK(this, SwChangeDBDlg, TreeSelectHdl);
127 
128 	aUsedDBTLB.SetSelectHdl(aLink);
129 	aUsedDBTLB.SetDeselectHdl(aLink);
130 	aAvailDBTLB.SetSelectHdl(aLink);
131 	aAvailDBTLB.SetDeselectHdl(aLink);
132 	TreeSelectHdl();
133 }
134 
135 /*--------------------------------------------------------------------
136 	Beschreibung: Datenbank-Listboxen initialisieren
137  --------------------------------------------------------------------*/
138 
139 
140 void SwChangeDBDlg::FillDBPopup()
141 {
142 	Reference<XNameAccess> xDBContext;
143 	Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
144 	if( xMgr.is() )
145 	{
146 		Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.sdb.DatabaseContext" ));
147 		xDBContext = Reference<XNameAccess>(xInstance, UNO_QUERY) ;
148 	}
149 	DBG_ASSERT(xDBContext.is(), "com.sun.star.sdb.DataBaseContext: service not available");
150 
151 	const SwDBData& rDBData = pSh->GetDBData();
152 	String sDBName(rDBData.sDataSource);
153 	String sTableName(rDBData.sCommand);
154 	aAvailDBTLB.Select(sDBName, sTableName, aEmptyStr);
155 
156 	SvStringsDtor aAllDBNames(5, 5);
157 
158     Sequence< ::rtl::OUString > aDBNames = xDBContext->getElementNames();
159     const ::rtl::OUString* pDBNames = aDBNames.getConstArray();
160     sal_Int32 nDBCount = aDBNames.getLength();
161     for(sal_Int32 i = 0; i < nDBCount; i++)
162 	{
163         aAllDBNames.Insert(new String(pDBNames[i]), aAllDBNames.Count());
164 	}
165 
166 	SvStringsDtor aDBNameList(5, 1);
167 	pSh->GetAllUsedDB( aDBNameList, &aAllDBNames );
168 
169 	sal_uInt16 nCount = aDBNameList.Count();
170 	aUsedDBTLB.Clear();
171 	SvLBoxEntry *pFirst = 0;
172 	SvLBoxEntry *pLast = 0;
173 
174     for (sal_uInt16 k = 0; k < nCount; k++)
175 	{
176         sDBName = *aDBNameList.GetObject(k);
177 		sDBName = sDBName.GetToken(0);
178 		pLast = Insert(sDBName);
179 		if (!pFirst)
180 			pFirst = pLast;
181 	}
182 
183 	if (pFirst)
184 	{
185 		aUsedDBTLB.MakeVisible(pFirst);
186 		aUsedDBTLB.Select(pFirst);
187 	}
188 
189 }
190 
191 /*--------------------------------------------------------------------
192 	Beschreibung:
193  --------------------------------------------------------------------*/
194 
195 
196 SvLBoxEntry* SwChangeDBDlg::Insert(const String& rDBName)
197 {
198 	String sDBName(rDBName.GetToken(0, DB_DELIM));
199 	String sTableName(rDBName.GetToken(1, DB_DELIM));
200 	int nCommandType = rDBName.GetToken(2, DB_DELIM).ToInt32();
201 	SvLBoxEntry* pParent;
202 	SvLBoxEntry* pChild;
203 
204 	sal_uInt16 nParent = 0;
205 	sal_uInt16 nChild = 0;
206 
207     Image aTableImg = aImageList.GetImage(IMG_DBTABLE);
208     Image aDBImg = aImageList.GetImage(IMG_DB);
209     Image aQueryImg = aImageList.GetImage(IMG_DBQUERY);
210     Image aHCTableImg = aImageListHC.GetImage(IMG_DBTABLE);
211     Image aHCDBImg = aImageListHC.GetImage(IMG_DB);
212     Image aHCQueryImg = aImageListHC.GetImage(IMG_DBQUERY);
213     Image& rToInsert = nCommandType ? aQueryImg : aTableImg;
214     Image& rHCToInsert = nCommandType ? aHCQueryImg : aHCTableImg;
215     while ((pParent = aUsedDBTLB.GetEntry(nParent++)) != NULL)
216 	{
217 		if (sDBName == aUsedDBTLB.GetEntryText(pParent))
218 		{
219 			while ((pChild = aUsedDBTLB.GetEntry(pParent, nChild++)) != NULL)
220 			{
221 				if (sTableName == aUsedDBTLB.GetEntryText(pChild))
222 					return pChild;
223 			}
224             SvLBoxEntry* pRet = aUsedDBTLB.InsertEntry(sTableName, rToInsert, rToInsert, pParent);
225             aUsedDBTLB.SetExpandedEntryBmp(pRet, rHCToInsert, BMP_COLOR_HIGHCONTRAST);
226             aUsedDBTLB.SetCollapsedEntryBmp(pRet, rHCToInsert, BMP_COLOR_HIGHCONTRAST);
227             pRet->SetUserData((void*)nCommandType);
228 			return pRet;
229 		}
230 	}
231     pParent = aUsedDBTLB.InsertEntry(sDBName, aDBImg, aDBImg);
232     aUsedDBTLB.SetExpandedEntryBmp(pParent, aHCDBImg, BMP_COLOR_HIGHCONTRAST);
233     aUsedDBTLB.SetCollapsedEntryBmp(pParent, aHCDBImg, BMP_COLOR_HIGHCONTRAST);
234 
235     SvLBoxEntry* pRet = aUsedDBTLB.InsertEntry(sTableName, rToInsert, rToInsert, pParent);
236     aUsedDBTLB.SetExpandedEntryBmp(pRet, rHCToInsert, BMP_COLOR_HIGHCONTRAST);
237     aUsedDBTLB.SetCollapsedEntryBmp(pRet, rHCToInsert, BMP_COLOR_HIGHCONTRAST);
238     pRet->SetUserData((void*)nCommandType);
239 	return pRet;
240 }
241 
242 /*--------------------------------------------------------------------
243 	Beschreibung: Dialog zerstoeren
244  --------------------------------------------------------------------*/
245 __EXPORT SwChangeDBDlg::~SwChangeDBDlg()
246 {
247 	delete pMgr;
248 }
249 
250 /*--------------------------------------------------------------------
251 	 Beschreibung:	Schliessen
252  --------------------------------------------------------------------*/
253 void __EXPORT SwChangeDBDlg::Apply()
254 {
255 	UpdateFlds();
256 }
257 /*--------------------------------------------------------------------
258 	 Beschreibung:
259  --------------------------------------------------------------------*/
260 void SwChangeDBDlg::UpdateFlds()
261 {
262 	SvStringsDtor aDBNames( (sal_uInt8)aUsedDBTLB.GetSelectionCount(), 1 );
263 	SvLBoxEntry* pEntry = aUsedDBTLB.FirstSelected();
264 
265 	while( pEntry )
266 	{
267 		if( aUsedDBTLB.GetParent( pEntry ))
268 		{
269 			String* pTmp = new String( aUsedDBTLB.GetEntryText(
270 											aUsedDBTLB.GetParent( pEntry )));
271 			*pTmp += DB_DELIM;
272 			*pTmp += aUsedDBTLB.GetEntryText( pEntry );
273 			*pTmp += DB_DELIM;
274 			int nCommandType = (int)(sal_uLong)pEntry->GetUserData();
275             *pTmp += String::CreateFromInt32(nCommandType);
276 			aDBNames.Insert(pTmp, aDBNames.Count() );
277 		}
278 		pEntry = aUsedDBTLB.NextSelected(pEntry);
279 	}
280 
281 	pSh->StartAllAction();
282 	String sTableName, sColumnName;
283     sal_Bool bIsTable = sal_False;
284     String sTemp(aAvailDBTLB.GetDBName(sTableName, sColumnName, &bIsTable));
285 	sTemp += DB_DELIM;
286 	sTemp += sTableName;
287     sTemp += DB_DELIM;
288     sTemp += bIsTable ? '0' : '1';
289 	pSh->ChangeDBFields( aDBNames, sTemp);
290 	pSh->EndAllAction();
291 }
292 
293 /*------------------------------------------------------------------------
294  Beschreibung:
295 ------------------------------------------------------------------------*/
296 
297 
298 IMPL_LINK( SwChangeDBDlg, ButtonHdl, Button *, EMPTYARG )
299 {
300 	String sTableName, sColumnName;
301 	SwDBData aData;
302     sal_Bool bIsTable = sal_False;
303     aData.sDataSource = aAvailDBTLB.GetDBName(sTableName, sColumnName, &bIsTable);
304 	aData.sCommand = sTableName;
305     aData.nCommandType = bIsTable ? 0 : 1;;
306     pSh->ChgDBData(aData);
307 	ShowDBName(pSh->GetDBData());
308 	EndDialog(RET_OK);
309 
310 	return 0;
311 }
312 
313 /*------------------------------------------------------------------------
314  Beschreibung:
315 ------------------------------------------------------------------------*/
316 
317 
318 IMPL_LINK( SwChangeDBDlg, TreeSelectHdl, SvTreeListBox *, EMPTYARG )
319 {
320 	sal_Bool bEnable = sal_False;
321 
322 	SvLBoxEntry* pEntry = aAvailDBTLB.GetCurEntry();
323 
324 	if (pEntry)
325 	{
326 		if (aAvailDBTLB.GetParent(pEntry))
327 			bEnable = sal_True;
328 		aOKBT.Enable( bEnable );
329 	}
330 	return 0;
331 }
332 
333 /*--------------------------------------------------------------------
334 	Beschreibung: Datenbankname fuer Anzeige wandeln
335  --------------------------------------------------------------------*/
336 
337 void SwChangeDBDlg::ShowDBName(const SwDBData& rDBData)
338 {
339 	String sTmp(rDBData.sDataSource);
340 	String sName;
341 	sTmp += '.';
342 	sTmp += (String)rDBData.sCommand;
343 
344 	for (sal_uInt16 i = 0; i < sTmp.Len(); i++)
345 	{
346 		sName += sTmp.GetChar(i);
347 		if (sTmp.GetChar(i) == '~')
348 			sName += '~';
349 	}
350 
351 	aDocDBNameFT.SetText(sName);
352 }
353 /*-- 27.05.2004 09:14:01---------------------------------------------------
354 
355   -----------------------------------------------------------------------*/
356 IMPL_LINK( SwChangeDBDlg, AddDBHdl, PushButton *, EMPTYARG )
357 {
358     String sNewDB = SwNewDBMgr::LoadAndRegisterDataSource();
359     if(sNewDB.Len())
360         aAvailDBTLB.AddDataSource(sNewDB);
361     return 0;
362 }
363 
364