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