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_extensions.hxx"
30 
31 #ifndef BIB_HRC
32 #include "bib.hrc"
33 #endif
34 #include "bibcont.hxx"
35 #include "bibbeam.hxx"
36 #include "bibmod.hxx"
37 #include "general.hxx"
38 #include "bibview.hxx"
39 #include "datman.hxx"
40 #include "bibresid.hxx"
41 #include "bibmod.hxx"
42 #include "sections.hrc"
43 #include "bibconfig.hxx"
44 
45 
46 #include <vcl/svapp.hxx>
47 #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
48 #include <com/sun/star/form/XLoadable.hpp>
49 #include <vcl/msgbox.hxx>
50 #include <tools/debug.hxx>
51 
52 using namespace ::com::sun::star;
53 using namespace ::com::sun::star::form;
54 using namespace ::com::sun::star::beans;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::uno;
57 
58 #define C2U( cChar ) ::rtl::OUString::createFromAscii( cChar )
59 
60 //.........................................................................
61 namespace bib
62 {
63 //.........................................................................
64 
65 	// -----------------------------------------------------------------------
66 	BibView::BibView( Window* _pParent, BibDataManager* _pManager, WinBits _nStyle )
67 		:BibWindow( _pParent, _nStyle )
68 		,m_pDatMan( _pManager )
69 		,m_xDatMan( _pManager )
70 		,m_pGeneralPage( NULL )
71 	{
72 		if ( m_xDatMan.is() )
73 			connectForm( m_xDatMan );
74 	}
75 
76 	// -----------------------------------------------------------------------
77 	BibView::~BibView()
78 	{
79 		BibGeneralPage* pGeneralPage = m_pGeneralPage;
80 		m_pGeneralPage = NULL;
81 
82 		pGeneralPage->CommitActiveControl();
83 		Reference< XForm > xForm = m_pDatMan->getForm();
84 		Reference< XPropertySet > xProps( xForm, UNO_QUERY );
85 		Reference< sdbc::XResultSetUpdate > xResUpd( xProps, UNO_QUERY );
86 		DBG_ASSERT( xResUpd.is(), "BibView::~BibView: invalid form!" );
87 
88 		if ( xResUpd.is() )
89 		{
90 			Any aModified = xProps->getPropertyValue( C2U( "IsModified" ) );
91 			sal_Bool bFlag = sal_False;
92 			if ( ( aModified >>= bFlag ) && bFlag )
93 			{
94 
95                 try
96                 {
97                     Any aNew = xProps->getPropertyValue( C2U( "IsNew" ) );
98                     aNew >>= bFlag;
99                     if ( bFlag )
100                         xResUpd->insertRow();
101                     else
102                         xResUpd->updateRow();
103                 }
104                 catch( const uno::Exception& rEx)
105                 {
106                    (void) rEx;
107                 }
108 			}
109 		}
110 
111 		if ( isFormConnected() )
112 			disconnectForm();
113 
114 		pGeneralPage->RemoveListeners();
115 		m_xGeneralPage = NULL;
116 	}
117 
118 	/* -----------------16.11.99 13:13-------------------
119 
120 	 --------------------------------------------------*/
121 	void BibView::UpdatePages()
122 	{
123 		// TODO:
124 		// this is _strange_: Why not updating the existent general page?
125 		// I consider the current behaviour a HACK.
126 		// frank.schoenheit@sun.com
127 		if ( m_pGeneralPage )
128 		{
129 			m_pGeneralPage->Hide();
130 			m_pGeneralPage->RemoveListeners();
131 			m_xGeneralPage = 0;
132 		}
133 
134 		m_xGeneralPage = m_pGeneralPage = new BibGeneralPage( this, m_pDatMan );
135 
136 		Resize();
137 
138 		if( HasFocus() )
139 			// "delayed" GetFocus() because GetFocus() is initially called before GeneralPage is created
140 			m_pGeneralPage->GrabFocus();
141 
142 		String sErrorString( m_pGeneralPage->GetErrorString() );
143 		if ( sErrorString.Len() )
144 		{
145             sal_Bool bExecute = BibModul::GetConfig()->IsShowColumnAssignmentWarning();
146             if(!m_pDatMan->HasActiveConnection())
147             {
148                 //no connection is available -> the data base has to be assigned
149                 m_pDatMan->DispatchDBChangeDialog();
150                 bExecute = sal_False;
151             }
152             else if(bExecute)
153             {
154                 sErrorString += '\n';
155                 sErrorString += String( BibResId( RID_MAP_QUESTION ) );
156                 QueryBox aQuery( this, WB_YES_NO, sErrorString );
157                 aQuery.SetDefaultCheckBoxText();
158                 short nResult = aQuery.Execute();
159                 BibModul::GetConfig()->SetShowColumnAssignmentWarning(
160                     !aQuery.GetCheckBoxState());
161                 if( RET_YES != nResult )
162                 {
163                     bExecute = sal_False;
164                 }
165             }
166             if(bExecute)
167 			{
168 				Application::PostUserEvent( STATIC_LINK( this, BibView, CallMappingHdl ) );
169 			}
170 		}
171 	}
172 	//---------------------------------------------------------------------
173 	//--- 19.10.01 16:55:49 -----------------------------------------------
174 
175 	void BibView::_loaded( const EventObject& _rEvent )
176 	{
177 		UpdatePages();
178 		FormControlContainer::_loaded( _rEvent );
179 	}
180 
181 	void BibView::_reloaded( const EventObject& _rEvent )
182 	{
183 		UpdatePages();
184 		FormControlContainer::_loaded( _rEvent );
185 	}
186 
187 	/* -----------------------------02.02.00 16:49--------------------------------
188 
189 	 ---------------------------------------------------------------------------*/
190 	IMPL_STATIC_LINK( BibView, CallMappingHdl, BibView*, EMPTYARG )
191 	{
192 		pThis->m_pDatMan->CreateMappingDialog( pThis );
193 		return 0;
194 	}
195 	/* -----------------------------13.04.00 16:12--------------------------------
196 
197 	 ---------------------------------------------------------------------------*/
198 	void BibView::Resize()
199 	{
200 		if ( m_pGeneralPage )
201 		{
202 			::Size aSz( GetOutputSizePixel() );
203 			m_pGeneralPage->SetSizePixel( aSz );
204 		}
205 		Window::Resize();
206 	}
207 
208 	//---------------------------------------------------------------------
209 	//--- 18.10.01 18:52:45 -----------------------------------------------
210 
211 	Reference< awt::XControlContainer > BibView::getControlContainer()
212 	{
213 		Reference< awt::XControlContainer > xReturn;
214 		if ( m_pGeneralPage )
215 			xReturn = m_pGeneralPage->GetControlContainer();
216 		return xReturn;
217 	}
218 
219 	void BibView::GetFocus()
220 	{
221 		if( m_pGeneralPage )
222 			m_pGeneralPage->GrabFocus();
223 	}
224 
225 	sal_Bool BibView::HandleShortCutKey( const KeyEvent& rKeyEvent )
226 	{
227 		return m_pGeneralPage? m_pGeneralPage->HandleShortCutKey( rKeyEvent ) : sal_False;
228 	}
229 
230 //.........................................................................
231 }	// namespace bib
232 //.........................................................................
233