update dev300-m58
[ooovba.git] / reportdesign / source / ui / dlg / AddField.cxx
blobe054c804046ba00db637070dc3bf87c74e7008b3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: AddField.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "precompiled_reportdesign.hxx"
31 #include "AddField.hxx"
32 #include "UITools.hxx"
33 #include <svx/dbaexchange.hxx>
34 #include <svx/svdpagv.hxx>
35 #include <com/sun/star/sdb/CommandType.hpp>
36 #include <com/sun/star/util/URL.hpp>
37 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
38 #include <com/sun/star/util/URL.hpp>
39 #include <com/sun/star/i18n/XCollator.hpp>
41 #include <vcl/waitobj.hxx>
42 #include <vcl/svapp.hxx>
43 #include <tools/diagnose_ex.h>
44 #include <comphelper/stl_types.hxx>
45 #include "rptui_slotid.hrc"
47 #include <connectivity/dbtools.hxx>
48 #include "helpids.hrc"
49 #include "RptResId.hrc"
50 #include "CondFormat.hrc"
51 #include "ModuleHelper.hxx"
52 #include "uistrings.hrc"
53 #include <comphelper/property.hxx>
54 #include <svtools/imgdef.hxx>
56 namespace rptui
58 const long STD_WIN_SIZE_X = 180;
59 const long STD_WIN_SIZE_Y = 320;
61 const long LISTBOX_BORDER = 2;
63 using namespace ::com::sun::star;
64 using namespace sdbc;
65 using namespace sdb;
66 using namespace uno;
67 using namespace datatransfer;
68 using namespace beans;
69 using namespace lang;
70 using namespace container;
71 using namespace ::svx;
72 class OAddFieldWindowListBox : public SvTreeListBox
74 OAddFieldWindow* m_pTabWin;
76 OAddFieldWindowListBox(const OAddFieldWindowListBox&);
77 void operator =(const OAddFieldWindowListBox&);
78 protected:
79 // virtual void Command( const CommandEvent& rEvt );
81 public:
82 OAddFieldWindowListBox( OAddFieldWindow* _pParent );
83 virtual ~OAddFieldWindowListBox();
85 sal_Int8 AcceptDrop( const AcceptDropEvent& rEvt );
86 sal_Int8 ExecuteDrop( const ExecuteDropEvent& rEvt );
88 uno::Sequence< beans::PropertyValue > getSelectedFieldDescriptors();
90 protected:
91 // DragSourceHelper
92 virtual void StartDrag( sal_Int8 nAction, const Point& rPosPixel );
94 private:
95 using SvTreeListBox::ExecuteDrop;
97 // -----------------------------------------------------------------------------
98 uno::Sequence< beans::PropertyValue > OAddFieldWindowListBox::getSelectedFieldDescriptors()
100 uno::Sequence< beans::PropertyValue > aArgs(GetSelectionCount());
101 sal_Int32 i = 0;
102 SvLBoxEntry* pSelected = FirstSelected();
103 while( pSelected )
105 // build a descriptor for the currently selected field
106 ::svx::ODataAccessDescriptor aDescriptor;
107 m_pTabWin->fillDescriptor(pSelected,aDescriptor);
108 aArgs[i++].Value <<= aDescriptor.createPropertyValueSequence();
109 pSelected = NextSelected(pSelected);
111 return aArgs;
113 //==================================================================
114 // class OAddFieldWindowListBox
115 //==================================================================
116 DBG_NAME( rpt_OAddFieldWindowListBox );
117 //------------------------------------------------------------------------------
118 OAddFieldWindowListBox::OAddFieldWindowListBox( OAddFieldWindow* _pParent )
119 :SvTreeListBox( _pParent, WB_TABSTOP|WB_BORDER|WB_SORT )
120 ,m_pTabWin( _pParent )
122 DBG_CTOR( rpt_OAddFieldWindowListBox,NULL);
123 SetHelpId( HID_RPT_FIELD_SEL );
124 SetSelectionMode(MULTIPLE_SELECTION);
125 SetDragDropMode( 0xFFFF );
126 SetHighlightRange( );
129 //------------------------------------------------------------------------------
130 OAddFieldWindowListBox::~OAddFieldWindowListBox()
132 DBG_DTOR( rpt_OAddFieldWindowListBox,NULL);
135 //------------------------------------------------------------------------------
136 sal_Int8 OAddFieldWindowListBox::AcceptDrop( const AcceptDropEvent& /*rEvt*/ )
138 return DND_ACTION_NONE;
141 //------------------------------------------------------------------------------
142 sal_Int8 OAddFieldWindowListBox::ExecuteDrop( const ExecuteDropEvent& /*rEvt*/ )
144 return DND_ACTION_NONE;
147 //------------------------------------------------------------------------------
148 void OAddFieldWindowListBox::StartDrag( sal_Int8 /*_nAction*/, const Point& /*_rPosPixel*/ )
150 if ( GetSelectionCount() < 1 )
151 // no drag without a field
152 return;
154 OMultiColumnTransferable* pDataContainer = new OMultiColumnTransferable(getSelectedFieldDescriptors());
155 Reference< XTransferable> xEnsureDelete = pDataContainer;
157 EndSelection();
158 pDataContainer->StartDrag( this, DND_ACTION_COPYMOVE | DND_ACTION_LINK );
160 //========================================================================
161 // class OAddFieldWindow
162 //========================================================================
163 DBG_NAME( rpt_OAddFieldWindow );
164 //-----------------------------------------------------------------------
165 OAddFieldWindow::OAddFieldWindow(Window* pParent
166 ,const uno::Reference< beans::XPropertySet >& _xRowSet
168 :FloatingWindow(pParent, WinBits(WB_STDMODELESS|WB_SIZEABLE))
169 ,::comphelper::OPropertyChangeListener(m_aMutex)
170 ,::comphelper::OContainerListener(m_aMutex)
171 ,m_xRowSet(_xRowSet)
172 ,m_aActions(this,ModuleRes(RID_TB_SORTING))
173 ,m_pListBox(new OAddFieldWindowListBox( this ))
174 ,m_aFixedLine(this, ModuleRes(ADDFIELD_FL_HELP_SEPARATOR) )
175 ,m_aHelpText(this, ModuleRes(ADDFIELD_HELP_FIELD) )
176 ,m_aInsertButton(this, WB_TABSTOP|WB_CENTER)
177 ,m_nCommandType(0)
178 ,m_bEscapeProcessing(sal_False)
179 ,m_pChangeListener(NULL)
180 ,m_pContainerListener(NULL)
182 DBG_CTOR( rpt_OAddFieldWindow,NULL);
183 SetHelpId( HID_RPT_FIELD_SEL_WIN );
184 SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetFaceColor()) );
185 SetMinOutputSizePixel(Size(STD_WIN_SIZE_X,STD_WIN_SIZE_Y));
187 m_aActions.SetStyle(m_aActions.GetStyle()|WB_LINESPACING);
188 m_aActions.SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetFaceColor()) );
190 m_aActions.SetSelectHdl(LINK(this, OAddFieldWindow, OnSortAction));
191 setToolBox(&m_aActions);
192 m_aActions.CheckItem(SID_FM_SORTUP);
193 m_aActions.EnableItem(SID_ADD_CONTROL_PAIR, FALSE);
195 m_pListBox->SetDoubleClickHdl(LINK( this, OAddFieldWindow, OnDoubleClickHdl ) );
196 m_pListBox->SetSelectHdl(LINK( this, OAddFieldWindow, OnSelectHdl ) );
197 m_pListBox->SetDeselectHdl(LINK( this, OAddFieldWindow, OnSelectHdl ) );
198 m_pListBox->SetDoubleClickHdl(LINK( this, OAddFieldWindow, OnDoubleClickHdl ) );
199 m_pListBox->Show();
200 const String sTitle(ModuleRes(RID_STR_INSERT));
201 m_aInsertButton.SetText(sTitle);
202 m_aInsertButton.SetClickHdl(LINK( this, OAddFieldWindow, OnDoubleClickHdl ) );
203 m_aInsertButton.Show();
205 m_aFixedLine.SetControlBackground( GetSettings().GetStyleSettings().GetFaceColor() );
206 m_aHelpText.SetControlBackground( GetSettings().GetStyleSettings().GetFaceColor() );
208 SetSizePixel(Size(STD_WIN_SIZE_X,STD_WIN_SIZE_Y));
209 //Show();
211 if ( m_xRowSet.is() )
215 // be notified when the settings of report definition change
216 m_pChangeListener = new ::comphelper::OPropertyChangeMultiplexer( this, m_xRowSet );
217 m_pChangeListener->addProperty( PROPERTY_COMMAND );
218 m_pChangeListener->addProperty( PROPERTY_COMMANDTYPE );
219 m_pChangeListener->addProperty( PROPERTY_ESCAPEPROCESSING );
220 m_pChangeListener->addProperty( PROPERTY_FILTER );
222 catch( const Exception& )
224 DBG_UNHANDLED_EXCEPTION();
229 //-----------------------------------------------------------------------
230 OAddFieldWindow::~OAddFieldWindow()
232 if (m_pChangeListener.is())
233 m_pChangeListener->dispose();
234 if ( m_pContainerListener.is() )
235 m_pContainerListener->dispose();
236 DBG_DTOR( rpt_OAddFieldWindow,NULL);
239 //-----------------------------------------------------------------------
240 void OAddFieldWindow::GetFocus()
242 if ( m_pListBox.get() )
243 m_pListBox->GrabFocus();
244 else
245 FloatingWindow::GetFocus();
247 //-----------------------------------------------------------------------
248 uno::Sequence< beans::PropertyValue > OAddFieldWindow::getSelectedFieldDescriptors()
250 return m_pListBox->getSelectedFieldDescriptors();
253 //-----------------------------------------------------------------------
254 long OAddFieldWindow::PreNotify( NotifyEvent& _rNEvt )
256 if ( EVENT_KEYINPUT == _rNEvt.GetType() )
258 const KeyCode& rKeyCode = _rNEvt.GetKeyEvent()->GetKeyCode();
259 if ( ( 0 == rKeyCode.GetModifier() ) && ( KEY_RETURN == rKeyCode.GetCode() ) )
261 if ( m_aCreateLink.IsSet() )
263 m_aCreateLink.Call(this);
264 return 1;
269 return FloatingWindow::PreNotify( _rNEvt );
271 //-----------------------------------------------------------------------
272 void OAddFieldWindow::_propertyChanged( const beans::PropertyChangeEvent& _evt ) throw( uno::RuntimeException )
274 OSL_ENSURE( _evt.Source == m_xRowSet, "OAddFieldWindow::_propertyChanged: where did this come from?" );
275 (void)_evt;
276 Update();
279 //-----------------------------------------------------------------------
280 namespace
282 void lcl_addToList( OAddFieldWindowListBox& _rListBox, const uno::Sequence< ::rtl::OUString >& _rEntries )
284 const ::rtl::OUString* pEntries = _rEntries.getConstArray();
285 sal_Int32 nEntries = _rEntries.getLength();
286 for ( sal_Int32 i = 0; i < nEntries; ++i, ++pEntries )
287 _rListBox.InsertEntry( *pEntries );
291 //-----------------------------------------------------------------------
292 void OAddFieldWindow::Update()
294 if ( m_pContainerListener.is() )
295 m_pContainerListener->dispose();
296 m_pContainerListener = NULL;
297 m_xColumns.clear();
301 // ListBox loeschen
302 m_pListBox->Clear();
303 const USHORT nItemCount = m_aActions.GetItemCount();
304 for (USHORT j = 0; j< nItemCount; ++j)
306 m_aActions.EnableItem(m_aActions.GetItemId(j),FALSE);
309 String aTitle(ModuleRes(RID_STR_FIELDSELECTION));
310 SetText(aTitle);
311 if ( m_xRowSet.is() )
313 ::rtl::OUString sCommand( m_aCommandName );
314 sal_Int32 nCommandType( m_nCommandType );
315 sal_Bool bEscapeProcessing( m_bEscapeProcessing );
316 ::rtl::OUString sFilter( m_sFilter );
318 OSL_VERIFY( m_xRowSet->getPropertyValue( PROPERTY_COMMAND ) >>= sCommand );
319 OSL_VERIFY( m_xRowSet->getPropertyValue( PROPERTY_COMMANDTYPE ) >>= nCommandType );
320 OSL_VERIFY( m_xRowSet->getPropertyValue( PROPERTY_ESCAPEPROCESSING ) >>= bEscapeProcessing );
321 OSL_VERIFY( m_xRowSet->getPropertyValue( PROPERTY_FILTER ) >>= sFilter );
323 m_aCommandName = sCommand;
324 m_nCommandType = nCommandType;
325 m_bEscapeProcessing = bEscapeProcessing;
326 m_sFilter = sFilter;
328 // add the columns to the list
329 uno::Reference< sdbc::XConnection> xCon = getConnection();
330 if ( xCon.is() && m_aCommandName.getLength() )
331 m_xColumns = dbtools::getFieldsByCommandDescriptor( xCon, GetCommandType(), GetCommand(), m_xHoldAlive );
332 if ( m_xColumns.is() )
334 lcl_addToList( *m_pListBox, m_xColumns->getElementNames() );
335 uno::Reference< container::XContainer> xContainer(m_xColumns,uno::UNO_QUERY);
336 if ( xContainer.is() )
337 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
340 // add the parameter columns to the list
341 uno::Reference< ::com::sun::star::sdbc::XRowSet > xRowSet(m_xRowSet,uno::UNO_QUERY);
342 Sequence< ::rtl::OUString > aParamNames( getParameterNames( xRowSet ) );
343 lcl_addToList( *m_pListBox, aParamNames );
345 // set title
346 aTitle.AppendAscii(" ");
347 aTitle += m_aCommandName.getStr();
348 SetText( aTitle );
349 if ( m_aCommandName.getLength() )
351 for (USHORT i = 0; i < nItemCount; ++i)
353 m_aActions.EnableItem(m_aActions.GetItemId(i));
356 OnSelectHdl(NULL);
359 catch( const Exception& )
361 DBG_UNHANDLED_EXCEPTION();
365 //-----------------------------------------------------------------------
366 void OAddFieldWindow::Resize()
368 FloatingWindow::Resize();
370 const Size aWindowSize( GetOutputSizePixel() );
373 const Size aRelated(LogicToPixel( Size( RELATED_CONTROLS, RELATED_CONTROLS ), MAP_APPFONT ));
374 const Size aFixedTextSize(LogicToPixel( Size( FIXEDTEXT_WIDTH, FIXEDTEXT_HEIGHT ), MAP_APPFONT ));
376 // ToolBar
377 Size aToolbarSize( m_aActions.GetSizePixel() );
378 Point aToolbarPos( aRelated.Width(), aRelated.Height());
379 m_aActions.SetPosPixel(Point(aToolbarPos.X(), aToolbarPos.Y()));
381 Size aLBSize( aWindowSize );
382 aLBSize.Width() -= ( 2 * aRelated.Width() );
384 // help text
385 const Size aHelpTextSize = m_aHelpText.CalcMinimumSize(aLBSize.Width());
387 // ListBox
388 Point aLBPos( aRelated.Width(), aRelated.Height() + aToolbarSize.Height() + aRelated.Height() );
390 aLBSize.Height() -= aToolbarSize.Height(); // Toolbar
391 aLBSize.Height() -= (6*aRelated.Height()); // 6 * gap
392 aLBSize.Height() -= aFixedTextSize.Height(); // fixed line
393 aLBSize.Height() -= aHelpTextSize.Height(); // help text
394 m_pListBox->SetPosSizePixel( aLBPos, aLBSize );
396 // FixedLine
397 Size aFLSize( aLBSize.Width(),aFixedTextSize.Height() );
398 Point aFLPos( aRelated.Width(), aLBPos.Y() + aLBSize.Height() + aRelated.Height());
399 m_aFixedLine.SetPosSizePixel( aFLPos, aFLSize );
401 // Help text
402 Point aFTPos( aRelated.Width(), aFLPos.Y() + aFLSize.Height() + aRelated.Height() );
403 m_aHelpText.SetPosSizePixel( aFTPos, aHelpTextSize );
405 // -----------------------------------------------------------------------------
406 uno::Reference< sdbc::XConnection> OAddFieldWindow::getConnection() const
408 return uno::Reference< sdbc::XConnection>(m_xRowSet->getPropertyValue( PROPERTY_ACTIVECONNECTION ),uno::UNO_QUERY);
410 // -----------------------------------------------------------------------------
411 void OAddFieldWindow::fillDescriptor(SvLBoxEntry* _pSelected,::svx::ODataAccessDescriptor& _rDescriptor)
413 if ( _pSelected && m_xColumns.is() )
415 uno::Reference<container::XChild> xChild(getConnection(),uno::UNO_QUERY);
416 if ( xChild.is( ) )
418 uno::Reference<sdb::XDocumentDataSource> xDocument( xChild->getParent(), uno::UNO_QUERY );
419 if ( xDocument.is() )
421 uno::Reference<frame::XModel> xModel(xDocument->getDatabaseDocument(),uno::UNO_QUERY);
422 if ( xModel.is() )
423 _rDescriptor[ daDatabaseLocation ] <<= xModel->getURL();
424 } // if ( xDocument.is() )
427 _rDescriptor[ ::svx::daCommand ] <<= GetCommand();
428 _rDescriptor[ ::svx::daCommandType ] <<= GetCommandType();
429 _rDescriptor[ ::svx::daEscapeProcessing ] <<= GetEscapeProcessing();
430 _rDescriptor[ ::svx::daConnection ] <<= getConnection();
432 ::rtl::OUString sColumnName = m_pListBox->GetEntryText( _pSelected );
433 _rDescriptor[ ::svx::daColumnName ] <<= sColumnName;
434 if ( m_xColumns->hasByName( sColumnName ) )
435 _rDescriptor[ ::svx::daColumnObject ] <<= m_xColumns->getByName(sColumnName);
438 // -----------------------------------------------------------------------------
439 void OAddFieldWindow::_elementInserted( const container::ContainerEvent& _rEvent ) throw(::com::sun::star::uno::RuntimeException)
441 if ( m_pListBox.get() )
443 ::rtl::OUString sName;
444 if ( _rEvent.Accessor >>= sName )
445 m_pListBox->InsertEntry(sName);
448 // -----------------------------------------------------------------------------
449 void OAddFieldWindow::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException)
451 if ( m_pListBox.get() )
453 m_pListBox->Clear();
454 if ( m_xColumns.is() )
455 lcl_addToList( *m_pListBox, m_xColumns->getElementNames() );
458 // -----------------------------------------------------------------------------
459 void OAddFieldWindow::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException)
462 // -----------------------------------------------------------------------------
463 IMPL_LINK( OAddFieldWindow, OnSelectHdl, void* ,/*_pAddFieldDlg*/)
465 m_aActions.EnableItem(SID_ADD_CONTROL_PAIR, ( m_pListBox.get() && m_pListBox->GetSelectionCount() > 0 ));
467 return 0L;
469 // -----------------------------------------------------------------------------
470 IMPL_LINK( OAddFieldWindow, OnDoubleClickHdl, void* ,/*_pAddFieldDlg*/)
472 if ( m_aCreateLink.IsSet() )
473 m_aCreateLink.Call(this);
475 return 0L;
477 //------------------------------------------------------------------------------
478 ImageList OAddFieldWindow::getImageList(sal_Int16 _eBitmapSet,sal_Bool _bHiContast) const
480 sal_Int16 nN = IMG_ADDFIELD_DLG_SC;
481 sal_Int16 nH = IMG_ADDFIELD_DLG_SCH;
482 if ( _eBitmapSet == SFX_SYMBOLS_SIZE_LARGE )
484 nN = IMG_ADDFIELD_DLG_LC;
485 nH = IMG_ADDFIELD_DLG_LCH;
487 return ImageList(ModuleRes( _bHiContast ? nH : nN ));
489 //------------------------------------------------------------------
490 void OAddFieldWindow::resizeControls(const Size& _rDiff)
492 // we use large images so we must change them
493 if ( _rDiff.Width() || _rDiff.Height() )
495 Invalidate();
498 //------------------------------------------------------------------
499 IMPL_LINK( OAddFieldWindow, OnSortAction, ToolBox*, /*NOTINTERESTEDIN*/ )
501 const USHORT nCurItem = m_aActions.GetCurItemId();
502 if ( SID_ADD_CONTROL_PAIR == nCurItem )
503 OnDoubleClickHdl(NULL);
504 else
506 if ( SID_FM_REMOVE_FILTER_SORT == nCurItem || !m_aActions.IsItemChecked(nCurItem) )
508 const USHORT nItemCount = m_aActions.GetItemCount();
509 for (USHORT j = 0; j< nItemCount; ++j)
511 const USHORT nItemId = m_aActions.GetItemId(j);
512 if ( nCurItem != nItemId )
513 m_aActions.CheckItem(nItemId,FALSE);
515 SvSortMode eSortMode = SortNone;
516 if ( SID_FM_REMOVE_FILTER_SORT != nCurItem )
518 m_aActions.CheckItem(nCurItem,!m_aActions.IsItemChecked(nCurItem));
519 if ( m_aActions.IsItemChecked(SID_FM_SORTUP) )
520 eSortMode = SortAscending;
521 else if ( m_aActions.IsItemChecked(SID_FM_SORTDOWN) )
522 eSortMode = SortDescending;
523 } // if ( SID_FM_REMOVE_FILTER_SORT != nCurItem )
525 m_pListBox->GetModel()->SetSortMode(eSortMode);
526 if ( SID_FM_REMOVE_FILTER_SORT == nCurItem )
527 Update();
529 m_pListBox->GetModel()->Resort();
532 return 0L;
534 // -----------------------------------------------------------------------------
535 // =============================================================================
536 } // namespace rptui
537 // =============================================================================