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