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_dbaccess.hxx"
30 #ifndef DBAUI_QUERYVIEWSWITCH_HXX
31 #include "QueryViewSwitch.hxx"
32 #endif
33 #ifndef DBAUI_QUERYDESIGNVIEW_HXX
34 #include "QueryDesignView.hxx"
35 #endif
36 #ifndef DBAUI_QUERYVIEW_TEXT_HXX
37 #include "QueryTextView.hxx"
38 #endif
39 #ifndef DBAUI_QUERYCONTAINERWINDOW_HXX
40 #include "querycontainerwindow.hxx"
41 #endif
42 #ifndef _DBU_QRY_HRC_
43 #include "dbu_qry.hrc"
44 #endif
45 #ifndef DBACCESS_UI_BROWSER_ID_HXX
46 #include "browserids.hxx"
47 #endif
48 #ifndef DBAUI_QYDLGTAB_HXX
49 #include "adtabdlg.hxx"
50 #endif
51 #ifndef DBAUI_QUERYCONTROLLER_HXX
52 #include "querycontroller.hxx"
53 #endif
54 #ifndef DBAUI_SQLEDIT_HXX
55 #include "sqledit.hxx"
56 #endif
57 #ifndef DBAUI_QUERYCONTAINERWINDOW_HXX
58 #include "querycontainerwindow.hxx"
59 #endif
60 
61 using namespace dbaui;
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::lang;
64 
65 DBG_NAME(OQueryViewSwitch)
66 OQueryViewSwitch::OQueryViewSwitch(OQueryContainerWindow* _pParent, OQueryController& _rController,const Reference< XMultiServiceFactory >& _rFactory)
67 : m_bAddTableDialogWasVisible(sal_False)
68 {
69 	DBG_CTOR(OQueryViewSwitch,NULL);
70 
71 	m_pTextView		= new OQueryTextView(_pParent);
72 	m_pDesignView	= new OQueryDesignView( _pParent, _rController, _rFactory );
73 }
74 // -----------------------------------------------------------------------------
75 OQueryViewSwitch::~OQueryViewSwitch()
76 {
77 	DBG_DTOR(OQueryViewSwitch,NULL);
78 	{
79 		::std::auto_ptr<Window> aTemp(m_pTextView);
80 		m_pTextView = NULL;
81 	}
82 	{
83 		::std::auto_ptr<Window> aTemp(m_pDesignView);
84 		m_pDesignView = NULL;
85 	}
86 }
87 // -------------------------------------------------------------------------
88 void OQueryViewSwitch::Construct()
89 {
90 	m_pDesignView->Construct( );
91 }
92 // -----------------------------------------------------------------------------
93 void OQueryViewSwitch::initialize()
94 {
95 	// initially be in SQL mode
96 	m_pTextView->Show();
97 	m_pDesignView->initialize();
98 }
99 // -------------------------------------------------------------------------
100 void OQueryViewSwitch::resizeDocumentView(Rectangle& _rPlayground)
101 {
102 	m_pTextView->SetPosSizePixel( _rPlayground.TopLeft(), _rPlayground.GetSize() );
103 	m_pDesignView->SetPosSizePixel( _rPlayground.TopLeft(), _rPlayground.GetSize() );
104 
105 	// just for completeness: there is no space left, we occupied it all ...
106 	_rPlayground.SetPos( _rPlayground.BottomRight() );
107 	_rPlayground.SetSize( Size( 0, 0 ) );
108 }
109 // -----------------------------------------------------------------------------
110 sal_Bool OQueryViewSwitch::checkStatement()
111 {
112 	if(m_pTextView->IsVisible())
113 		return m_pTextView->checkStatement();
114 	return m_pDesignView->checkStatement();
115 }
116 // -----------------------------------------------------------------------------
117 ::rtl::OUString OQueryViewSwitch::getStatement()
118 {
119 	if(m_pTextView->IsVisible())
120 		return m_pTextView->getStatement();
121 	return m_pDesignView->getStatement();
122 }
123 // -----------------------------------------------------------------------------
124 void OQueryViewSwitch::setReadOnly(sal_Bool _bReadOnly)
125 {
126 	if(m_pTextView->IsVisible())
127 		m_pTextView->setReadOnly(_bReadOnly);
128 	else
129 		m_pDesignView->setReadOnly(_bReadOnly);
130 }
131 // -----------------------------------------------------------------------------
132 void OQueryViewSwitch::clear()
133 {
134 	if(m_pTextView->IsVisible())
135 		m_pTextView->clear();
136 	else
137 		m_pDesignView->clear();
138 }
139 // -----------------------------------------------------------------------------
140 void OQueryViewSwitch::GrabFocus()
141 {
142 	if ( m_pTextView && m_pTextView->IsVisible() )
143 		m_pTextView->GrabFocus();
144 	else if ( m_pDesignView && m_pDesignView->IsVisible() )
145 		m_pDesignView->GrabFocus();
146 }
147 // -----------------------------------------------------------------------------
148 void OQueryViewSwitch::setStatement(const ::rtl::OUString& _rsStatement)
149 {
150 	if(m_pTextView->IsVisible())
151 		m_pTextView->setStatement(_rsStatement);
152 	else
153 		m_pDesignView->setStatement(_rsStatement);
154 }
155 // -----------------------------------------------------------------------------
156 void OQueryViewSwitch::copy()
157 {
158 	if(m_pTextView->IsVisible())
159 		m_pTextView->copy();
160 	else
161 		m_pDesignView->copy();
162 }
163 // -----------------------------------------------------------------------------
164 sal_Bool OQueryViewSwitch::isCutAllowed()
165 {
166 	if(m_pTextView->IsVisible())
167 		return m_pTextView->isCutAllowed();
168 	return m_pDesignView->isCutAllowed();
169 }
170 // -----------------------------------------------------------------------------
171 sal_Bool OQueryViewSwitch::isCopyAllowed()
172 {
173 	if(m_pTextView->IsVisible())
174 		return m_pTextView->isCopyAllowed();
175 	return m_pDesignView->isCopyAllowed();
176 }
177 // -----------------------------------------------------------------------------
178 sal_Bool OQueryViewSwitch::isPasteAllowed()
179 {
180 	if(m_pTextView->IsVisible())
181 		return m_pTextView->isPasteAllowed();
182 	return m_pDesignView->isPasteAllowed();
183 }
184 // -----------------------------------------------------------------------------
185 void OQueryViewSwitch::cut()
186 {
187 	if(m_pTextView->IsVisible())
188 		m_pTextView->cut();
189 	else
190 		m_pDesignView->cut();
191 }
192 // -----------------------------------------------------------------------------
193 void OQueryViewSwitch::paste()
194 {
195 	if(m_pTextView->IsVisible())
196 		m_pTextView->paste();
197 	else
198 		m_pDesignView->paste();
199 }
200 // -----------------------------------------------------------------------------
201 OQueryContainerWindow* OQueryViewSwitch::getContainer() const
202 {
203 	Window* pDesignParent = getDesignView() ? getDesignView()->GetParent() : NULL;
204 	return static_cast< OQueryContainerWindow* >( pDesignParent );
205 }
206 
207 // -----------------------------------------------------------------------------
208 void OQueryViewSwitch::impl_forceSQLView()
209 {
210     OAddTableDlg* pAddTabDialog( getAddTableDialog() );
211 
212     // hide the "Add Table" dialog
213     m_bAddTableDialogWasVisible = pAddTabDialog ? pAddTabDialog->IsVisible() : false;
214     if ( m_bAddTableDialogWasVisible )
215         pAddTabDialog->Hide();
216 
217     // tell the views they're in/active
218 	m_pDesignView->stopTimer();
219 	m_pTextView->getSqlEdit()->startTimer();
220 
221     // set the most recent statement at the text view
222 	m_pTextView->clear();
223 	m_pTextView->setStatement(static_cast<OQueryController&>(m_pDesignView->getController()).getStatement());
224 }
225 
226 // -----------------------------------------------------------------------------
227 void OQueryViewSwitch::forceInitialView()
228 {
229     OQueryController& rQueryController( static_cast< OQueryController& >( m_pDesignView->getController() ) );
230 	const sal_Bool bGraphicalDesign = rQueryController.isGraphicalDesign();
231     if ( !bGraphicalDesign )
232         impl_forceSQLView();
233     else
234     {
235         // tell the text view it's inactive now
236 		m_pTextView->getSqlEdit()->stopTimer();
237 
238         // update the "Add Table" dialog
239         OAddTableDlg* pAddTabDialog( getAddTableDialog() );
240         if ( pAddTabDialog )
241 		    pAddTabDialog->Update();
242 
243         // initialize the design view
244 		m_pDesignView->initByFieldDescriptions( rQueryController.getFieldInformation() );
245 
246 		// tell the design view it's active now
247 		m_pDesignView->startTimer();
248     }
249 
250     impl_postViewSwitch( bGraphicalDesign, true );
251 }
252 
253 // -----------------------------------------------------------------------------
254 bool OQueryViewSwitch::switchView( ::dbtools::SQLExceptionInfo* _pErrorInfo )
255 {
256 	sal_Bool bRet = sal_True;
257 	sal_Bool bGraphicalDesign = static_cast<OQueryController&>(m_pDesignView->getController()).isGraphicalDesign();
258 
259 	if ( !bGraphicalDesign )
260 	{
261         impl_forceSQLView();
262 	}
263 	else
264 	{
265         // tell the text view it's inactive now
266 		m_pTextView->getSqlEdit()->stopTimer();
267 
268         // update the "Add Table" dialog
269         OAddTableDlg* pAddTabDialog( getAddTableDialog() );
270         if ( pAddTabDialog )
271 		    pAddTabDialog->Update();
272 
273         // initialize the design view
274 		bRet = m_pDesignView->initByParseIterator( _pErrorInfo );
275 
276 		// tell the design view it's active now
277 		m_pDesignView->startTimer();
278 	}
279 
280     return impl_postViewSwitch( bGraphicalDesign, bRet );
281 }
282 
283 // -----------------------------------------------------------------------------
284 bool OQueryViewSwitch::impl_postViewSwitch( const bool i_bGraphicalDesign, const bool i_bSuccess )
285 {
286 	if ( i_bSuccess )
287 	{
288 		m_pTextView->Show	( !i_bGraphicalDesign );
289 		m_pDesignView->Show	( i_bGraphicalDesign );
290         OAddTableDlg* pAddTabDialog( getAddTableDialog() );
291         if ( pAddTabDialog )
292 		    if ( i_bGraphicalDesign && m_bAddTableDialogWasVisible )
293 			    pAddTabDialog->Show();
294 
295 		GrabFocus();
296 	}
297 
298 	OQueryContainerWindow* pContainer = getContainer();
299 	if ( pContainer )
300 		pContainer->Resize();
301 
302 	m_pDesignView->getController().ClearUndoManager();
303 	m_pDesignView->getController().InvalidateAll();
304 
305 	return i_bSuccess;
306 }
307 
308 // -----------------------------------------------------------------------------
309 OAddTableDlg* OQueryViewSwitch::getAddTableDialog()
310 {
311     if ( !m_pDesignView )
312         return NULL;
313     return m_pDesignView->getController().getAddTableDialog();
314 }
315 // -----------------------------------------------------------------------------
316 sal_Bool OQueryViewSwitch::isSlotEnabled(sal_Int32 _nSlotId)
317 {
318 	return m_pDesignView->isSlotEnabled(_nSlotId);
319 }
320 // -----------------------------------------------------------------------------
321 void OQueryViewSwitch::setSlotEnabled(sal_Int32 _nSlotId,sal_Bool _bEnable)
322 {
323 	m_pDesignView->setSlotEnabled(_nSlotId,_bEnable);
324 }
325 // -----------------------------------------------------------------------------
326 void OQueryViewSwitch::SaveUIConfig()
327 {
328 	if(m_pDesignView->IsVisible())
329 		m_pDesignView->SaveUIConfig();
330 }
331 // -----------------------------------------------------------------------------
332 void OQueryViewSwitch::SetPosSizePixel( Point _rPt,Size _rSize)
333 {
334 	m_pDesignView->SetPosSizePixel( _rPt,_rSize);
335 	m_pDesignView->Resize();
336 	m_pTextView->SetPosSizePixel( _rPt,_rSize);
337 }
338 // -----------------------------------------------------------------------------
339 Reference< XMultiServiceFactory > OQueryViewSwitch::getORB() const
340 {
341 	return m_pDesignView->getORB();
342 }
343 // -----------------------------------------------------------------------------
344 bool OQueryViewSwitch::reset( ::dbtools::SQLExceptionInfo* _pErrorInfo )
345 {
346 	m_pDesignView->reset();
347 	if ( !m_pDesignView->initByParseIterator( _pErrorInfo ) )
348         return false;
349 
350     if ( switchView( _pErrorInfo ) )
351         return false;
352 
353     return true;
354 }
355 // -----------------------------------------------------------------------------
356 void OQueryViewSwitch::setNoneVisbleRow(sal_Int32 _nRows)
357 {
358 	if(m_pDesignView)
359 		m_pDesignView->setNoneVisbleRow(_nRows);
360 }
361 // -----------------------------------------------------------------------------
362