update dev300-m58
[ooovba.git] / sw / source / ui / fldui / changedb.cxx
blob79ccb9ed2ca4fc33522bf2eca2bd12ece167a124
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: changedb.cxx,v $
10 * $Revision: 1.21 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #ifdef SW_DLLIMPLEMENTATION
36 #undef SW_DLLIMPLEMENTATION
37 #endif
40 #define _CHANGEDB_CXX
42 #include <svtools/stdctrl.hxx>
43 #ifndef _MSGBOX_HXX //autogen
44 #include <vcl/msgbox.hxx>
45 #endif
46 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47 #include <com/sun/star/container/XNameAccess.hpp>
48 #include <com/sun/star/sdb/XDatabaseAccess.hpp>
49 #include <comphelper/processfactory.hxx>
50 #include <sfx2/viewfrm.hxx>
52 #ifndef _VIEW_HXX
53 #include <view.hxx>
54 #endif
55 #include <wrtsh.hxx>
56 #ifndef _DBMGR_HXX
57 #include <dbmgr.hxx>
58 #endif
59 #include <fldmgr.hxx>
60 #include <expfld.hxx>
61 #include <txtatr.hxx>
62 #include <ndtxt.hxx>
63 #include <fldbas.hxx>
64 #include <dbfld.hxx>
65 #include <changedb.hxx>
67 #ifndef _FLDUI_HRC
68 #include <fldui.hrc>
69 #endif
70 #ifndef _UTLUI_HRC
71 #include <utlui.hrc>
72 #endif
73 #ifndef _CHANGEDB_HRC
74 #include <changedb.hrc>
75 #endif
77 #include <unomid.h>
79 using namespace ::com::sun::star::uno;
80 using namespace ::com::sun::star::container;
81 using namespace ::com::sun::star::lang;
84 /*--------------------------------------------------------------------
85 Beschreibung: Feldeinfuegen bearbeiten
86 --------------------------------------------------------------------*/
89 SwChangeDBDlg::SwChangeDBDlg(SwView& rVw) :
90 SvxStandardDialog(&rVw.GetViewFrame()->GetWindow(), SW_RES(DLG_CHANGE_DB)),
92 aDBListFL (this, SW_RES(FL_DBLIST )),
93 aUsedDBFT (this, SW_RES(FT_USEDDB )),
94 aAvailDBFT (this, SW_RES(FT_AVAILDB )),
95 aUsedDBTLB (this, SW_RES(TLB_USEDDB )),
96 aAvailDBTLB (this, SW_RES(TLB_AVAILDB ), 0),
97 aAddDBPB (this, SW_RES(PB_ADDDB)),
98 aDescFT (this, SW_RES(FT_DESC )),
99 aDocDBTextFT(this, SW_RES(FT_DOCDBTEXT )),
100 aDocDBNameFT(this, SW_RES(FT_DOCDBNAME )),
101 aOKBT (this, SW_RES(BT_OK )),
102 aCancelBT (this, SW_RES(BT_CANCEL )),
103 aHelpBT (this, SW_RES(BT_HELP )),
104 // aChangeBT (this, SW_RES(BT_CHANGEDB )),
105 aImageList (SW_RES(ILIST_DB_DLG )),
106 aImageListHC (SW_RES(ILIST_DB_DLG_HC )),
108 pSh(rVw.GetWrtShellPtr()),
109 pMgr( new SwFldMgr() )
111 aAvailDBTLB.SetWrtShell(*pSh);
112 FillDBPopup();
114 FreeResource();
116 ShowDBName(pSh->GetDBData());
117 aOKBT.SetClickHdl(LINK(this, SwChangeDBDlg, ButtonHdl));
118 aAddDBPB.SetClickHdl(LINK(this, SwChangeDBDlg, AddDBHdl));
120 aUsedDBTLB.SetSelectionMode(MULTIPLE_SELECTION);
121 aUsedDBTLB.SetWindowBits(WB_HASLINES|WB_CLIPCHILDREN|WB_SORT|WB_HASBUTTONS|WB_HASBUTTONSATROOT|WB_HSCROLL);
122 aUsedDBTLB.SetSpaceBetweenEntries(0);
124 aUsedDBTLB.SetNodeBitmaps( aImageList.GetImage(IMG_COLLAPSE),
125 aImageList.GetImage(IMG_EXPAND ), BMP_COLOR_NORMAL );
126 aUsedDBTLB.SetNodeBitmaps( aImageListHC.GetImage(IMG_COLLAPSE),
127 aImageListHC.GetImage(IMG_EXPAND ), BMP_COLOR_HIGHCONTRAST );
129 Link aLink = LINK(this, SwChangeDBDlg, TreeSelectHdl);
131 aUsedDBTLB.SetSelectHdl(aLink);
132 aUsedDBTLB.SetDeselectHdl(aLink);
133 aAvailDBTLB.SetSelectHdl(aLink);
134 aAvailDBTLB.SetDeselectHdl(aLink);
135 TreeSelectHdl();
138 /*--------------------------------------------------------------------
139 Beschreibung: Datenbank-Listboxen initialisieren
140 --------------------------------------------------------------------*/
143 void SwChangeDBDlg::FillDBPopup()
145 Reference<XNameAccess> xDBContext;
146 Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
147 if( xMgr.is() )
149 Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.sdb.DatabaseContext" ));
150 xDBContext = Reference<XNameAccess>(xInstance, UNO_QUERY) ;
152 DBG_ASSERT(xDBContext.is(), "com.sun.star.sdb.DataBaseContext: service not available");
154 const SwDBData& rDBData = pSh->GetDBData();
155 String sDBName(rDBData.sDataSource);
156 String sTableName(rDBData.sCommand);
157 aAvailDBTLB.Select(sDBName, sTableName, aEmptyStr);
159 SvStringsDtor aAllDBNames(5, 5);
161 Sequence< ::rtl::OUString > aDBNames = xDBContext->getElementNames();
162 const ::rtl::OUString* pDBNames = aDBNames.getConstArray();
163 sal_Int32 nDBCount = aDBNames.getLength();
164 for(sal_Int32 i = 0; i < nDBCount; i++)
166 aAllDBNames.Insert(new String(pDBNames[i]), aAllDBNames.Count());
169 SvStringsDtor aDBNameList(5, 1);
170 pSh->GetAllUsedDB( aDBNameList, &aAllDBNames );
172 USHORT nCount = aDBNameList.Count();
173 aUsedDBTLB.Clear();
174 SvLBoxEntry *pFirst = 0;
175 SvLBoxEntry *pLast = 0;
177 for (USHORT k = 0; k < nCount; k++)
179 sDBName = *aDBNameList.GetObject(k);
180 sDBName = sDBName.GetToken(0);
181 pLast = Insert(sDBName);
182 if (!pFirst)
183 pFirst = pLast;
186 if (pFirst)
188 aUsedDBTLB.MakeVisible(pFirst);
189 aUsedDBTLB.Select(pFirst);
194 /*--------------------------------------------------------------------
195 Beschreibung:
196 --------------------------------------------------------------------*/
199 SvLBoxEntry* SwChangeDBDlg::Insert(const String& rDBName)
201 String sDBName(rDBName.GetToken(0, DB_DELIM));
202 String sTableName(rDBName.GetToken(1, DB_DELIM));
203 int nCommandType = rDBName.GetToken(2, DB_DELIM).ToInt32();
204 SvLBoxEntry* pParent;
205 SvLBoxEntry* pChild;
207 USHORT nParent = 0;
208 USHORT nChild = 0;
210 Image aTableImg = aImageList.GetImage(IMG_DBTABLE);
211 Image aDBImg = aImageList.GetImage(IMG_DB);
212 Image aQueryImg = aImageList.GetImage(IMG_DBQUERY);
213 Image aHCTableImg = aImageListHC.GetImage(IMG_DBTABLE);
214 Image aHCDBImg = aImageListHC.GetImage(IMG_DB);
215 Image aHCQueryImg = aImageListHC.GetImage(IMG_DBQUERY);
216 Image& rToInsert = nCommandType ? aQueryImg : aTableImg;
217 Image& rHCToInsert = nCommandType ? aHCQueryImg : aHCTableImg;
218 while ((pParent = aUsedDBTLB.GetEntry(nParent++)) != NULL)
220 if (sDBName == aUsedDBTLB.GetEntryText(pParent))
222 while ((pChild = aUsedDBTLB.GetEntry(pParent, nChild++)) != NULL)
224 if (sTableName == aUsedDBTLB.GetEntryText(pChild))
225 return pChild;
227 SvLBoxEntry* pRet = aUsedDBTLB.InsertEntry(sTableName, rToInsert, rToInsert, pParent);
228 aUsedDBTLB.SetExpandedEntryBmp(pRet, rHCToInsert, BMP_COLOR_HIGHCONTRAST);
229 aUsedDBTLB.SetCollapsedEntryBmp(pRet, rHCToInsert, BMP_COLOR_HIGHCONTRAST);
230 pRet->SetUserData((void*)nCommandType);
231 return pRet;
234 pParent = aUsedDBTLB.InsertEntry(sDBName, aDBImg, aDBImg);
235 aUsedDBTLB.SetExpandedEntryBmp(pParent, aHCDBImg, BMP_COLOR_HIGHCONTRAST);
236 aUsedDBTLB.SetCollapsedEntryBmp(pParent, aHCDBImg, BMP_COLOR_HIGHCONTRAST);
238 SvLBoxEntry* pRet = aUsedDBTLB.InsertEntry(sTableName, rToInsert, rToInsert, pParent);
239 aUsedDBTLB.SetExpandedEntryBmp(pRet, rHCToInsert, BMP_COLOR_HIGHCONTRAST);
240 aUsedDBTLB.SetCollapsedEntryBmp(pRet, rHCToInsert, BMP_COLOR_HIGHCONTRAST);
241 pRet->SetUserData((void*)nCommandType);
242 return pRet;
245 /*--------------------------------------------------------------------
246 Beschreibung: Dialog zerstoeren
247 --------------------------------------------------------------------*/
248 __EXPORT SwChangeDBDlg::~SwChangeDBDlg()
250 delete pMgr;
253 /*--------------------------------------------------------------------
254 Beschreibung: Schliessen
255 --------------------------------------------------------------------*/
256 void __EXPORT SwChangeDBDlg::Apply()
258 UpdateFlds();
260 /*--------------------------------------------------------------------
261 Beschreibung:
262 --------------------------------------------------------------------*/
263 void SwChangeDBDlg::UpdateFlds()
265 SvStringsDtor aDBNames( (BYTE)aUsedDBTLB.GetSelectionCount(), 1 );
266 SvLBoxEntry* pEntry = aUsedDBTLB.FirstSelected();
268 while( pEntry )
270 if( aUsedDBTLB.GetParent( pEntry ))
272 String* pTmp = new String( aUsedDBTLB.GetEntryText(
273 aUsedDBTLB.GetParent( pEntry )));
274 *pTmp += DB_DELIM;
275 *pTmp += aUsedDBTLB.GetEntryText( pEntry );
276 *pTmp += DB_DELIM;
277 int nCommandType = (int)(ULONG)pEntry->GetUserData();
278 *pTmp += String::CreateFromInt32(nCommandType);
279 aDBNames.Insert(pTmp, aDBNames.Count() );
281 pEntry = aUsedDBTLB.NextSelected(pEntry);
284 pSh->StartAllAction();
285 String sTableName, sColumnName;
286 sal_Bool bIsTable = sal_False;
287 String sTemp(aAvailDBTLB.GetDBName(sTableName, sColumnName, &bIsTable));
288 sTemp += DB_DELIM;
289 sTemp += sTableName;
290 sTemp += DB_DELIM;
291 sTemp += bIsTable ? '0' : '1';
292 pSh->ChangeDBFields( aDBNames, sTemp);
293 pSh->EndAllAction();
296 /*------------------------------------------------------------------------
297 Beschreibung:
298 ------------------------------------------------------------------------*/
301 IMPL_LINK( SwChangeDBDlg, ButtonHdl, Button *, EMPTYARG )
303 String sTableName, sColumnName;
304 SwDBData aData;
305 sal_Bool bIsTable = sal_False;
306 aData.sDataSource = aAvailDBTLB.GetDBName(sTableName, sColumnName, &bIsTable);
307 aData.sCommand = sTableName;
308 aData.nCommandType = bIsTable ? 0 : 1;;
309 pSh->ChgDBData(aData);
310 ShowDBName(pSh->GetDBData());
311 EndDialog(RET_OK);
313 return 0;
316 /*------------------------------------------------------------------------
317 Beschreibung:
318 ------------------------------------------------------------------------*/
321 IMPL_LINK( SwChangeDBDlg, TreeSelectHdl, SvTreeListBox *, EMPTYARG )
323 BOOL bEnable = FALSE;
325 SvLBoxEntry* pEntry = aAvailDBTLB.GetCurEntry();
327 if (pEntry)
329 if (aAvailDBTLB.GetParent(pEntry))
330 bEnable = TRUE;
331 aOKBT.Enable( bEnable );
333 return 0;
336 /*--------------------------------------------------------------------
337 Beschreibung: Datenbankname fuer Anzeige wandeln
338 --------------------------------------------------------------------*/
340 void SwChangeDBDlg::ShowDBName(const SwDBData& rDBData)
342 String sTmp(rDBData.sDataSource);
343 String sName;
344 sTmp += '.';
345 sTmp += (String)rDBData.sCommand;
347 for (USHORT i = 0; i < sTmp.Len(); i++)
349 sName += sTmp.GetChar(i);
350 if (sTmp.GetChar(i) == '~')
351 sName += '~';
354 aDocDBNameFT.SetText(sName);
356 /*-- 27.05.2004 09:14:01---------------------------------------------------
358 -----------------------------------------------------------------------*/
359 IMPL_LINK( SwChangeDBDlg, AddDBHdl, PushButton *, EMPTYARG )
361 String sNewDB = SwNewDBMgr::LoadAndRegisterDataSource();
362 if(sNewDB.Len())
363 aAvailDBTLB.AddDataSource(sNewDB);
364 return 0;