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_chart2.hxx"
30 
31 #include "dlg_DataSource.hxx"
32 #include "dlg_DataSource.hrc"
33 #include "Strings.hrc"
34 #include "ResId.hxx"
35 #include "ChartTypeTemplateProvider.hxx"
36 #include "DiagramHelper.hxx"
37 #include "DialogModel.hxx"
38 #include "HelpIds.hrc"
39 
40 #include "tp_RangeChooser.hxx"
41 #include "tp_DataSource.hxx"
42 
43 // for RET_OK
44 #include <vcl/msgbox.hxx>
45 
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::chart2;
48 using namespace ::chart;
49 
50 using ::com::sun::star::uno::Reference;
51 using ::com::sun::star::uno::Sequence;
52 using ::rtl::OUString;
53 
54 // --------------------------------------------------------------------------------
55 
56 
57 namespace chart
58 {
59 
60 // ----------------------------------------
61 
62 class DocumentChartTypeTemplateProvider : public ChartTypeTemplateProvider
63 {
64 public:
65     DocumentChartTypeTemplateProvider(
66         const Reference< chart2::XChartDocument > & xDoc );
67     virtual ~DocumentChartTypeTemplateProvider();
68 
69     // ____ ChartTypeTemplateProvider ____
70     virtual Reference< chart2::XChartTypeTemplate > getCurrentTemplate() const;
71 
72 private:
73     Reference< chart2::XChartTypeTemplate > m_xTemplate;
74 };
75 
76 DocumentChartTypeTemplateProvider::DocumentChartTypeTemplateProvider(
77     const Reference< chart2::XChartDocument > & xDoc )
78 {
79     if( xDoc.is())
80     {
81         Reference< chart2::XDiagram > xDia( xDoc->getFirstDiagram());
82         if( xDia.is())
83         {
84             DiagramHelper::tTemplateWithServiceName aResult(
85                 DiagramHelper::getTemplateForDiagram(
86                     xDia,
87                     Reference< lang::XMultiServiceFactory >(
88                         xDoc->getChartTypeManager(), uno::UNO_QUERY ) ));
89             m_xTemplate.set( aResult.first );
90         }
91     }
92 }
93 
94 DocumentChartTypeTemplateProvider::~DocumentChartTypeTemplateProvider()
95 {}
96 
97 Reference< chart2::XChartTypeTemplate > DocumentChartTypeTemplateProvider::getCurrentTemplate() const
98 {
99     return m_xTemplate;
100 }
101 
102 // ----------------------------------------
103 
104 class DataSourceTabControl : public TabControl
105 {
106 public:
107     DataSourceTabControl( Window* pParent, const ResId& rResId );
108     ~DataSourceTabControl();
109 
110     virtual long DeactivatePage();
111 
112     void DisableTabToggling();
113     void EnableTabToggling();
114 
115 private:
116     bool m_bTogglingEnabled;
117 };
118 
119 DataSourceTabControl::DataSourceTabControl( Window* pParent, const ResId& rResId ) :
120         TabControl( pParent, rResId ),
121         m_bTogglingEnabled( true )
122 {}
123 
124 DataSourceTabControl::~DataSourceTabControl()
125 {}
126 
127 // Note that the result is long, but is intended to be a bool
128 long DataSourceTabControl::DeactivatePage()
129 {
130     bool bCanDeactivate( TabControl::DeactivatePage() != 0 );
131 
132     bCanDeactivate = (bCanDeactivate && m_bTogglingEnabled);
133 
134     return (bCanDeactivate ? 1 : 0 );
135 }
136 
137 void DataSourceTabControl::DisableTabToggling()
138 {
139     m_bTogglingEnabled = false;
140 }
141 
142 void DataSourceTabControl::EnableTabToggling()
143 {
144     m_bTogglingEnabled = true;
145 }
146 
147 // ----------------------------------------
148 
149 sal_uInt16 DataSourceDialog::m_nLastPageId = 0;
150 
151 DataSourceDialog::DataSourceDialog(
152     Window * pParent,
153     const Reference< XChartDocument > & xChartDocument,
154     const Reference< uno::XComponentContext > & xContext ) :
155 
156         TabDialog( pParent, SchResId( DLG_DATA_SOURCE )),
157 
158         m_xChartDocument( xChartDocument ),
159         m_xContext( xContext ),
160         m_apDocTemplateProvider( new DocumentChartTypeTemplateProvider( xChartDocument )),
161         m_apDialogModel( new DialogModel( xChartDocument, xContext )),
162 
163         m_pTabControl( new DataSourceTabControl( this, SchResId( TABCTRL ) )),
164         m_aBtnOK( this, SchResId( BTN_OK ) ),
165         m_aBtnCancel( this, SchResId( BTN_CANCEL ) ),
166         m_aBtnHelp( this, SchResId( BTN_HELP ) ),
167 
168         m_pRangeChooserTabePage(0),
169         m_pDataSourceTabPage(0),
170         m_bRangeChooserTabIsValid( true ),
171         m_bDataSourceTabIsValid( true )
172 {
173     FreeResource();
174 
175     //don't create the tabpages before FreeResource, otherwise the help ids are not matched correctly
176     m_pRangeChooserTabePage = new RangeChooserTabPage( m_pTabControl, *(m_apDialogModel.get()),
177                                      m_apDocTemplateProvider.get(), this, true /* bHideDescription */ );
178     m_pDataSourceTabPage = new DataSourceTabPage( m_pTabControl, *(m_apDialogModel.get()),
179                                     m_apDocTemplateProvider.get(), this, true /* bHideDescription */ );
180 
181     m_pTabControl->InsertPage( TP_RANGECHOOSER, String( SchResId( STR_PAGE_DATA_RANGE )));
182     m_pTabControl->InsertPage( TP_DATA_SOURCE,  String( SchResId( STR_OBJECT_DATASERIES_PLURAL )));
183 
184     m_pTabControl->SetTabPage( TP_RANGECHOOSER, m_pRangeChooserTabePage );
185     m_pTabControl->SetTabPage( TP_DATA_SOURCE,  m_pDataSourceTabPage );
186 
187     m_pTabControl->SelectTabPage( m_nLastPageId );
188 
189     SetHelpId( HID_SCH_DLG_RANGES );
190 }
191 
192 DataSourceDialog::~DataSourceDialog()
193 {
194     delete m_pRangeChooserTabePage;
195     delete m_pDataSourceTabPage;
196 
197     m_nLastPageId = m_pTabControl->GetCurPageId();
198     delete m_pTabControl;
199 }
200 
201 short DataSourceDialog::Execute()
202 {
203     short nResult = TabDialog::Execute();
204     if( nResult == RET_OK )
205     {
206         if( m_pRangeChooserTabePage )
207             m_pRangeChooserTabePage->commitPage();
208         if( m_pDataSourceTabPage )
209             m_pDataSourceTabPage->commitPage();
210     }
211     return nResult;
212 }
213 
214 void DataSourceDialog::setInvalidPage( TabPage * pTabPage )
215 {
216     if( pTabPage == m_pRangeChooserTabePage )
217         m_bRangeChooserTabIsValid = false;
218     else if( pTabPage == m_pDataSourceTabPage )
219         m_bDataSourceTabIsValid = false;
220 
221     if( ! (m_bRangeChooserTabIsValid && m_bDataSourceTabIsValid ))
222     {
223         m_aBtnOK.Enable( sal_False );
224         OSL_ASSERT( m_pTabControl );
225         // note: there seems to be no suitable mechanism to address pages by
226         // identifier, at least it is unclear what the page identifiers are.
227         // @todo: change the fixed numbers to identifiers
228         if( m_bRangeChooserTabIsValid )
229             m_pTabControl->SetCurPageId( m_pTabControl->GetPageId( 1 ));
230         else if( m_bDataSourceTabIsValid )
231             m_pTabControl->SetCurPageId( m_pTabControl->GetPageId( 0 ));
232         m_pTabControl->DisableTabToggling();
233     }
234 }
235 
236 void DataSourceDialog::setValidPage( TabPage * pTabPage )
237 {
238     if( pTabPage == m_pRangeChooserTabePage )
239         m_bRangeChooserTabIsValid = true;
240     else if( pTabPage == m_pDataSourceTabPage )
241         m_bDataSourceTabIsValid = true;
242 
243     if( m_bRangeChooserTabIsValid && m_bDataSourceTabIsValid )
244     {
245         m_aBtnOK.Enable( sal_True );
246         OSL_ASSERT( m_pTabControl );
247         m_pTabControl->EnableTabToggling();
248     }
249 }
250 
251 
252 } //  namespace chart
253