xref: /AOO41X/main/sw/source/ui/fldui/changedb.cxx (revision e9b3637dc0ef10af2c3f6cdf114e1ea7d8a8352f)
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 
SwChangeDBDlg(SwView & rVw)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     aApplDBFL   (this, SW_RES(FL_APPLDB     )),
98 //  aChangeBT   (this, SW_RES(BT_CHANGEDB    )),
99     aImageList      (SW_RES(ILIST_DB_DLG    )),
100     aImageListHC    (SW_RES(ILIST_DB_DLG_HC )),
101 
102     pSh(rVw.GetWrtShellPtr()),
103     pMgr( new SwFldMgr() )
104 {
105     aAvailDBTLB.SetWrtShell(*pSh);
106     FillDBPopup();
107 
108     FreeResource();
109 
110     ShowDBName(pSh->GetDBData());
111     aOKBT.SetClickHdl(LINK(this, SwChangeDBDlg, ButtonHdl));
112     aAddDBPB.SetClickHdl(LINK(this, SwChangeDBDlg, AddDBHdl));
113 
114     aUsedDBTLB.SetSelectionMode(MULTIPLE_SELECTION);
115     aUsedDBTLB.SetStyle(aUsedDBTLB.GetStyle()|WB_HASLINES|WB_CLIPCHILDREN|WB_SORT|WB_HASBUTTONS|WB_HASBUTTONSATROOT|WB_HSCROLL);
116     aUsedDBTLB.SetSpaceBetweenEntries(0);
117 
118     aUsedDBTLB.SetNodeBitmaps( aImageList.GetImage(IMG_COLLAPSE),
119                     aImageList.GetImage(IMG_EXPAND  ), BMP_COLOR_NORMAL );
120     aUsedDBTLB.SetNodeBitmaps( aImageListHC.GetImage(IMG_COLLAPSE),
121                     aImageListHC.GetImage(IMG_EXPAND  ), BMP_COLOR_HIGHCONTRAST );
122 
123     Link aLink = LINK(this, SwChangeDBDlg, TreeSelectHdl);
124 
125     aUsedDBTLB.SetSelectHdl(aLink);
126     aUsedDBTLB.SetDeselectHdl(aLink);
127     aAvailDBTLB.SetSelectHdl(aLink);
128     aAvailDBTLB.SetDeselectHdl(aLink);
129     TreeSelectHdl();
130 }
131 
132 /*--------------------------------------------------------------------
133     Beschreibung: Datenbank-Listboxen initialisieren
134  --------------------------------------------------------------------*/
135 
136 
FillDBPopup()137 void SwChangeDBDlg::FillDBPopup()
138 {
139     Reference<XNameAccess> xDBContext;
140     Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
141     if( xMgr.is() )
142     {
143         Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.sdb.DatabaseContext" ));
144         xDBContext = Reference<XNameAccess>(xInstance, UNO_QUERY) ;
145     }
146     DBG_ASSERT(xDBContext.is(), "com.sun.star.sdb.DataBaseContext: service not available");
147 
148     const SwDBData& rDBData = pSh->GetDBData();
149     String sDBName(rDBData.sDataSource);
150     String sTableName(rDBData.sCommand);
151     aAvailDBTLB.Select(sDBName, sTableName, aEmptyStr);
152 
153     SvStringsDtor aAllDBNames(5, 5);
154 
155     Sequence< ::rtl::OUString > aDBNames = xDBContext->getElementNames();
156     const ::rtl::OUString* pDBNames = aDBNames.getConstArray();
157     sal_Int32 nDBCount = aDBNames.getLength();
158     for(sal_Int32 i = 0; i < nDBCount; i++)
159     {
160         aAllDBNames.Insert(new String(pDBNames[i]), aAllDBNames.Count());
161     }
162 
163     SvStringsDtor aDBNameList(5, 1);
164     pSh->GetAllUsedDB( aDBNameList, &aAllDBNames );
165 
166     sal_uInt16 nCount = aDBNameList.Count();
167     aUsedDBTLB.Clear();
168     SvLBoxEntry *pFirst = 0;
169     SvLBoxEntry *pLast = 0;
170 
171     for (sal_uInt16 k = 0; k < nCount; k++)
172     {
173         sDBName = *aDBNameList.GetObject(k);
174         sDBName = sDBName.GetToken(0);
175         pLast = Insert(sDBName);
176         if (!pFirst)
177             pFirst = pLast;
178     }
179 
180     if (pFirst)
181     {
182         aUsedDBTLB.MakeVisible(pFirst);
183         aUsedDBTLB.Select(pFirst);
184     }
185 
186 }
187 
188 /*--------------------------------------------------------------------
189     Beschreibung:
190  --------------------------------------------------------------------*/
191 
192 
Insert(const String & rDBName)193 SvLBoxEntry* SwChangeDBDlg::Insert(const String& rDBName)
194 {
195     String sDBName(rDBName.GetToken(0, DB_DELIM));
196     String sTableName(rDBName.GetToken(1, DB_DELIM));
197     int nCommandType = rDBName.GetToken(2, DB_DELIM).ToInt32();
198     SvLBoxEntry* pParent;
199     SvLBoxEntry* pChild;
200 
201     sal_uInt16 nParent = 0;
202     sal_uInt16 nChild = 0;
203 
204     Image aTableImg = aImageList.GetImage(IMG_DBTABLE);
205     Image aDBImg = aImageList.GetImage(IMG_DB);
206     Image aQueryImg = aImageList.GetImage(IMG_DBQUERY);
207     Image aHCTableImg = aImageListHC.GetImage(IMG_DBTABLE);
208     Image aHCDBImg = aImageListHC.GetImage(IMG_DB);
209     Image aHCQueryImg = aImageListHC.GetImage(IMG_DBQUERY);
210     Image& rToInsert = nCommandType ? aQueryImg : aTableImg;
211     Image& rHCToInsert = nCommandType ? aHCQueryImg : aHCTableImg;
212     while ((pParent = aUsedDBTLB.GetEntry(nParent++)) != NULL)
213     {
214         if (sDBName == aUsedDBTLB.GetEntryText(pParent))
215         {
216             while ((pChild = aUsedDBTLB.GetEntry(pParent, nChild++)) != NULL)
217             {
218                 if (sTableName == aUsedDBTLB.GetEntryText(pChild))
219                     return pChild;
220             }
221             SvLBoxEntry* pRet = aUsedDBTLB.InsertEntry(sTableName, rToInsert, rToInsert, pParent);
222             aUsedDBTLB.SetExpandedEntryBmp(pRet, rHCToInsert, BMP_COLOR_HIGHCONTRAST);
223             aUsedDBTLB.SetCollapsedEntryBmp(pRet, rHCToInsert, BMP_COLOR_HIGHCONTRAST);
224             pRet->SetUserData((void*)nCommandType);
225             return pRet;
226         }
227     }
228     pParent = aUsedDBTLB.InsertEntry(sDBName, aDBImg, aDBImg);
229     aUsedDBTLB.SetExpandedEntryBmp(pParent, aHCDBImg, BMP_COLOR_HIGHCONTRAST);
230     aUsedDBTLB.SetCollapsedEntryBmp(pParent, aHCDBImg, BMP_COLOR_HIGHCONTRAST);
231 
232     SvLBoxEntry* pRet = aUsedDBTLB.InsertEntry(sTableName, rToInsert, rToInsert, pParent);
233     aUsedDBTLB.SetExpandedEntryBmp(pRet, rHCToInsert, BMP_COLOR_HIGHCONTRAST);
234     aUsedDBTLB.SetCollapsedEntryBmp(pRet, rHCToInsert, BMP_COLOR_HIGHCONTRAST);
235     pRet->SetUserData((void*)nCommandType);
236     return pRet;
237 }
238 
239 /*--------------------------------------------------------------------
240     Beschreibung: Dialog zerstoeren
241  --------------------------------------------------------------------*/
~SwChangeDBDlg()242 __EXPORT SwChangeDBDlg::~SwChangeDBDlg()
243 {
244     delete pMgr;
245 }
246 
247 /*--------------------------------------------------------------------
248      Beschreibung:  Schliessen
249  --------------------------------------------------------------------*/
Apply()250 void __EXPORT SwChangeDBDlg::Apply()
251 {
252     UpdateFlds();
253 }
254 /*--------------------------------------------------------------------
255      Beschreibung:
256  --------------------------------------------------------------------*/
UpdateFlds()257 void SwChangeDBDlg::UpdateFlds()
258 {
259     SvStringsDtor aDBNames( (sal_uInt8)aUsedDBTLB.GetSelectionCount(), 1 );
260     SvLBoxEntry* pEntry = aUsedDBTLB.FirstSelected();
261 
262     while( pEntry )
263     {
264         if( aUsedDBTLB.GetParent( pEntry ))
265         {
266             String* pTmp = new String( aUsedDBTLB.GetEntryText(
267                                             aUsedDBTLB.GetParent( pEntry )));
268             *pTmp += DB_DELIM;
269             *pTmp += aUsedDBTLB.GetEntryText( pEntry );
270             *pTmp += DB_DELIM;
271             int nCommandType = (int)(sal_uLong)pEntry->GetUserData();
272             *pTmp += String::CreateFromInt32(nCommandType);
273             aDBNames.Insert(pTmp, aDBNames.Count() );
274         }
275         pEntry = aUsedDBTLB.NextSelected(pEntry);
276     }
277 
278     pSh->StartAllAction();
279     String sTableName, sColumnName;
280     sal_Bool bIsTable = sal_False;
281     String sTemp(aAvailDBTLB.GetDBName(sTableName, sColumnName, &bIsTable));
282     sTemp += DB_DELIM;
283     sTemp += sTableName;
284     sTemp += DB_DELIM;
285     sTemp += bIsTable ? '0' : '1';
286     pSh->ChangeDBFields( aDBNames, sTemp);
287     pSh->EndAllAction();
288 }
289 
290 /*------------------------------------------------------------------------
291  Beschreibung:
292 ------------------------------------------------------------------------*/
293 
294 
IMPL_LINK(SwChangeDBDlg,ButtonHdl,Button *,EMPTYARG)295 IMPL_LINK( SwChangeDBDlg, ButtonHdl, Button *, EMPTYARG )
296 {
297     String sTableName, sColumnName;
298     SwDBData aData;
299     sal_Bool bIsTable = sal_False;
300     aData.sDataSource = aAvailDBTLB.GetDBName(sTableName, sColumnName, &bIsTable);
301     aData.sCommand = sTableName;
302     aData.nCommandType = bIsTable ? 0 : 1;;
303     pSh->ChgDBData(aData);
304     ShowDBName(pSh->GetDBData());
305     EndDialog(RET_OK);
306 
307     return 0;
308 }
309 
310 /*------------------------------------------------------------------------
311  Beschreibung:
312 ------------------------------------------------------------------------*/
313 
314 
IMPL_LINK(SwChangeDBDlg,TreeSelectHdl,SvTreeListBox *,EMPTYARG)315 IMPL_LINK( SwChangeDBDlg, TreeSelectHdl, SvTreeListBox *, EMPTYARG )
316 {
317     sal_Bool bEnable = sal_False;
318 
319     SvLBoxEntry* pEntry = aAvailDBTLB.GetCurEntry();
320 
321     if (pEntry)
322     {
323         if (aAvailDBTLB.GetParent(pEntry))
324             bEnable = sal_True;
325         aOKBT.Enable( bEnable );
326     }
327     return 0;
328 }
329 
330 /*--------------------------------------------------------------------
331     Description: transform databasename for screen
332  --------------------------------------------------------------------*/
333 
ShowDBName(const SwDBData & rDBData)334 void SwChangeDBDlg::ShowDBName(const SwDBData& rDBData)
335 {
336     String sName;
337     String sTmp(rDBData.sDataSource);
338     if (!rDBData.sDataSource.isEmpty() && !rDBData.sCommand.isEmpty()) {
339         sTmp += '.';
340     }
341     sTmp += (String)rDBData.sCommand;
342 
343     for (sal_uInt16 i = 0; i < sTmp.Len(); i++)
344     {
345         sName += sTmp.GetChar(i);
346         if (sTmp.GetChar(i) == '~')
347             sName += '~';
348     }
349 
350     aDocDBNameFT.SetText(sName);
351 }
352 /*-- 27.05.2004 09:14:01---------------------------------------------------
353 
354   -----------------------------------------------------------------------*/
IMPL_LINK(SwChangeDBDlg,AddDBHdl,PushButton *,EMPTYARG)355 IMPL_LINK( SwChangeDBDlg, AddDBHdl, PushButton *, EMPTYARG )
356 {
357     String sNewDB = SwNewDBMgr::LoadAndRegisterDataSource();
358     if(sNewDB.Len())
359         aAvailDBTLB.AddDataSource(sNewDB);
360     return 0;
361 }
362 
363