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