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 #ifdef SW_DLLIMPLEMENTATION
32 #undef SW_DLLIMPLEMENTATION
33 #endif
34 
35 
36 #include <swtypes.hxx>
37 #include <selectdbtabledialog.hxx>
38 #include <dbtablepreviewdialog.hxx>
39 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
40 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 #include <com/sun/star/beans/PropertyValue.hpp>
43 #include <com/sun/star/container/XChild.hpp>
44 #include <com/sun/star/sdbc/XDataSource.hpp>
45 
46 #include <unomid.h>
47 
48 #include <selectdbtabledialog.hrc>
49 #include <dbui.hrc>
50 #include <helpid.h>
51 
52 using namespace ::com::sun::star;
53 using namespace ::com::sun::star::sdbcx;
54 using namespace ::com::sun::star::sdbc;
55 using namespace ::com::sun::star::sdb;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::container;
58 using namespace ::com::sun::star::beans;
59 
60 /*-- 08.04.2004 14:33:56---------------------------------------------------
61 
62   -----------------------------------------------------------------------*/
63 SwSelectDBTableDialog::SwSelectDBTableDialog(Window* pParent,
64         const uno::Reference< sdbc::XConnection>& rConnection) :
65     SfxModalDialog(pParent, SW_RES(DLG_MM_SELECTDBTABLEDDIALOG)),
66 #ifdef MSC
67 #pragma warning (disable : 4355)
68 #endif
69     m_aSelectFI( this, SW_RES(       FI_SELECT     )),
70     m_aTableHB( this, WB_BUTTONSTYLE | WB_BOTTOMBORDER),
71     m_aTableLB( this, SW_RES(        LB_TABLE      )),
72     m_aPreviewPB( this, SW_RES(      PB_PREVIEW    )),
73     m_aSeparatorFL(this, SW_RES(    FL_SEPARATOR      )),
74     m_aOK( this, SW_RES(             PB_OK         )),
75     m_aCancel( this, SW_RES(         PB_CANCEL     )),
76     m_aHelp( this, SW_RES(           PB_HELP       )),
77 #ifdef MSC
78 #pragma warning (default : 4355)
79 #endif
80     m_sName( SW_RES( ST_NAME )),
81     m_sType( SW_RES( ST_TYPE )),
82     m_sTable( SW_RES( ST_TABLE )),
83     m_sQuery( SW_RES( ST_QUERY )),
84     m_xConnection(rConnection)
85 {
86     FreeResource();
87 
88     Size aLBSize(m_aTableLB.GetSizePixel());
89     m_aTableHB.SetSizePixel(aLBSize);
90     Size aHeadSize(m_aTableHB.CalcWindowSizePixel());
91     aHeadSize.Width() = aLBSize.Width();
92     m_aTableHB.SetSizePixel(aHeadSize);
93     Point aLBPos(m_aTableLB.GetPosPixel());
94     m_aTableHB.SetPosPixel(aLBPos);
95     aLBPos.Y() += aHeadSize.Height();
96     aLBSize.Height() -= aHeadSize.Height();
97     m_aTableLB.SetPosSizePixel(aLBPos, aLBSize);
98 
99     Size aSz(m_aTableHB.GetOutputSizePixel());
100     m_aTableHB.InsertItem( 1, m_sName,
101                             aSz.Width()/2,
102                             HIB_LEFT | HIB_VCENTER /*| HIB_CLICKABLE | HIB_UPARROW */);
103     m_aTableHB.InsertItem( 2, m_sType,
104                             aSz.Width()/2,
105                             HIB_LEFT | HIB_VCENTER /*| HIB_CLICKABLE | HIB_UPARROW */);
106     m_aTableHB.SetHelpId(HID_MM_ADDRESSLIST_HB );
107     m_aTableHB.Show();
108 
109     static long nTabs[] = {3, 0, aSz.Width()/2, aSz.Width() };
110     m_aTableLB.SetTabs(&nTabs[0], MAP_PIXEL);
111     m_aTableLB.SetHelpId(HID_MM_SELECTDBTABLEDDIALOG_LISTBOX);
112     m_aTableLB.SetStyle( m_aTableLB.GetStyle() | WB_CLIPCHILDREN );
113     m_aTableLB.SetSpaceBetweenEntries(3);
114     m_aTableLB.SetSelectionMode( SINGLE_SELECTION );
115     m_aTableLB.SetDragDropMode(   0 );
116     m_aTableLB.EnableAsyncDrag(sal_False);
117 
118     m_aPreviewPB.SetClickHdl(LINK(this, SwSelectDBTableDialog, PreviewHdl));
119 
120     Reference<XTablesSupplier> xTSupplier(m_xConnection, UNO_QUERY);
121     if(xTSupplier.is())
122     {
123         Reference<XNameAccess> xTbls = xTSupplier->getTables();
124         Sequence<rtl::OUString> aTbls = xTbls->getElementNames();
125         const rtl::OUString* pTbls = aTbls.getConstArray();
126         for(long i = 0; i < aTbls.getLength(); i++)
127         {
128             String sEntry = pTbls[i];
129             sEntry += '\t';
130             sEntry += m_sTable;
131             SvLBoxEntry* pEntry = m_aTableLB.InsertEntry(sEntry);
132             pEntry->SetUserData((void*)0);
133         }
134     }
135     Reference<XQueriesSupplier> xQSupplier(m_xConnection, UNO_QUERY);
136     if(xQSupplier.is())
137     {
138         Reference<XNameAccess> xQueries = xQSupplier->getQueries();
139         Sequence<rtl::OUString> aQueries = xQueries->getElementNames();
140         const rtl::OUString* pQueries = aQueries.getConstArray();
141         for(long i = 0; i < aQueries.getLength(); i++)
142         {
143             String sEntry = pQueries[i];
144             sEntry += '\t';
145             sEntry += m_sQuery;
146             SvLBoxEntry* pEntry = m_aTableLB.InsertEntry(sEntry);
147             pEntry->SetUserData((void*)1);
148         }
149     }
150 }
151 /*-- 08.04.2004 14:33:57---------------------------------------------------
152 
153   -----------------------------------------------------------------------*/
154 SwSelectDBTableDialog::~SwSelectDBTableDialog()
155 {
156 }
157 /*-- 08.04.2004 14:33:57---------------------------------------------------
158 
159   -----------------------------------------------------------------------*/
160 IMPL_LINK(SwSelectDBTableDialog, PreviewHdl, PushButton*, pButton)
161 {
162     SvLBoxEntry* pEntry = m_aTableLB.FirstSelected();
163     if(pEntry)
164     {
165         ::rtl::OUString sTableOrQuery = m_aTableLB.GetEntryText(pEntry, 0);
166         sal_Int32 nCommandType = 0 == pEntry->GetUserData() ? 0 : 1;
167 
168         ::rtl::OUString sDataSourceName;
169         Reference<XChild> xChild(m_xConnection, UNO_QUERY);
170         if(xChild.is())
171         {
172             Reference<XDataSource> xSource(xChild->getParent(), UNO_QUERY);
173             Reference<XPropertySet> xPrSet(xSource, UNO_QUERY);
174             xPrSet->getPropertyValue(C2U("Name")) >>= sDataSourceName;
175         }
176         DBG_ASSERT(sDataSourceName.getLength(), "no data source found");
177         Sequence<PropertyValue> aProperties(5);
178         PropertyValue* pProperties = aProperties.getArray();
179         pProperties[0].Name = C2U("DataSourceName");
180         pProperties[0].Value <<= sDataSourceName;
181         pProperties[1].Name = C2U("Command");
182         pProperties[1].Value <<= sTableOrQuery;
183         pProperties[2].Name = C2U("CommandType");
184         pProperties[2].Value <<= nCommandType;
185         pProperties[3].Name = C2U("ShowTreeView");
186         sal_Bool bFalse = sal_False;
187         pProperties[3].Value <<= bFalse;
188         pProperties[4].Name = C2U("ShowTreeViewButton");
189         pProperties[4].Value <<= bFalse;
190 
191         SwDBTablePreviewDialog* pDlg = new SwDBTablePreviewDialog(pButton, aProperties);
192         pDlg->Execute();
193         delete pDlg;
194     }
195 
196     return 0;
197 }
198 /*-- 19.04.2004 10:03:26---------------------------------------------------
199 
200   -----------------------------------------------------------------------*/
201 String      SwSelectDBTableDialog::GetSelectedTable(bool& bIsTable)
202 {
203     SvLBoxEntry* pEntry = m_aTableLB.FirstSelected();
204     bIsTable = pEntry->GetUserData() ? false : true;
205     return pEntry ? m_aTableLB.GetEntryText(pEntry, 0) : String();
206 }
207 /*-- 13.05.2004 12:58:26---------------------------------------------------
208 
209   -----------------------------------------------------------------------*/
210 void   SwSelectDBTableDialog::SetSelectedTable(const String& rTable, bool bIsTable)
211 {
212     SvLBoxEntry*    pEntry = m_aTableLB.First();
213     while(pEntry)
214     {
215         if((m_aTableLB.GetEntryText(pEntry, 0) == rTable) &&
216                  ((pEntry->GetUserData() == 0 ) == bIsTable))
217         {
218             m_aTableLB.Select(pEntry);
219             break;
220         }
221         pEntry = m_aTableLB.Next( pEntry );
222     }
223 }
224