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 $
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
42 #include <svtools/stdctrl.hxx>
43 #ifndef _MSGBOX_HXX //autogen
44 #include <vcl/msgbox.hxx>
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>
65 #include <changedb.hxx>
74 #include <changedb.hrc>
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
);
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
);
138 /*--------------------------------------------------------------------
139 Beschreibung: Datenbank-Listboxen initialisieren
140 --------------------------------------------------------------------*/
143 void SwChangeDBDlg::FillDBPopup()
145 Reference
<XNameAccess
> xDBContext
;
146 Reference
< XMultiServiceFactory
> xMgr( ::comphelper::getProcessServiceFactory() );
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();
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
);
188 aUsedDBTLB
.MakeVisible(pFirst
);
189 aUsedDBTLB
.Select(pFirst
);
194 /*--------------------------------------------------------------------
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
;
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
))
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
);
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
);
245 /*--------------------------------------------------------------------
246 Beschreibung: Dialog zerstoeren
247 --------------------------------------------------------------------*/
248 __EXPORT
SwChangeDBDlg::~SwChangeDBDlg()
253 /*--------------------------------------------------------------------
254 Beschreibung: Schliessen
255 --------------------------------------------------------------------*/
256 void __EXPORT
SwChangeDBDlg::Apply()
260 /*--------------------------------------------------------------------
262 --------------------------------------------------------------------*/
263 void SwChangeDBDlg::UpdateFlds()
265 SvStringsDtor
aDBNames( (BYTE
)aUsedDBTLB
.GetSelectionCount(), 1 );
266 SvLBoxEntry
* pEntry
= aUsedDBTLB
.FirstSelected();
270 if( aUsedDBTLB
.GetParent( pEntry
))
272 String
* pTmp
= new String( aUsedDBTLB
.GetEntryText(
273 aUsedDBTLB
.GetParent( pEntry
)));
275 *pTmp
+= aUsedDBTLB
.GetEntryText( pEntry
);
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
));
291 sTemp
+= bIsTable
? '0' : '1';
292 pSh
->ChangeDBFields( aDBNames
, sTemp
);
296 /*------------------------------------------------------------------------
298 ------------------------------------------------------------------------*/
301 IMPL_LINK( SwChangeDBDlg
, ButtonHdl
, Button
*, EMPTYARG
)
303 String sTableName
, sColumnName
;
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());
316 /*------------------------------------------------------------------------
318 ------------------------------------------------------------------------*/
321 IMPL_LINK( SwChangeDBDlg
, TreeSelectHdl
, SvTreeListBox
*, EMPTYARG
)
323 BOOL bEnable
= FALSE
;
325 SvLBoxEntry
* pEntry
= aAvailDBTLB
.GetCurEntry();
329 if (aAvailDBTLB
.GetParent(pEntry
))
331 aOKBT
.Enable( bEnable
);
336 /*--------------------------------------------------------------------
337 Beschreibung: Datenbankname fuer Anzeige wandeln
338 --------------------------------------------------------------------*/
340 void SwChangeDBDlg::ShowDBName(const SwDBData
& rDBData
)
342 String
sTmp(rDBData
.sDataSource
);
345 sTmp
+= (String
)rDBData
.sCommand
;
347 for (USHORT i
= 0; i
< sTmp
.Len(); i
++)
349 sName
+= sTmp
.GetChar(i
);
350 if (sTmp
.GetChar(i
) == '~')
354 aDocDBNameFT
.SetText(sName
);
356 /*-- 27.05.2004 09:14:01---------------------------------------------------
358 -----------------------------------------------------------------------*/
359 IMPL_LINK( SwChangeDBDlg
, AddDBHdl
, PushButton
*, EMPTYARG
)
361 String sNewDB
= SwNewDBMgr::LoadAndRegisterDataSource();
363 aAvailDBTLB
.AddDataSource(sNewDB
);