1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "selectlabeldialog.hxx"
31 #ifndef _EXTENSIONS_PROPCTRLR_FORMRESID_HRC_
32 #include "formresid.hrc"
34 #include "formbrowsertools.hxx"
35 #include "formstrings.hxx"
36 #include <com/sun/star/form/FormComponentType.hpp>
37 #include <com/sun/star/container/XChild.hpp>
38 #include <com/sun/star/container/XIndexAccess.hpp>
39 #include <com/sun/star/sdbc/XResultSet.hpp>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/lang/XServiceInfo.hpp>
42 #include <comphelper/property.hxx>
43 #include <comphelper/types.hxx>
45 //............................................................................
48 //............................................................................
50 using namespace ::com::sun::star::uno
;
51 using namespace ::com::sun::star::container
;
52 using namespace ::com::sun::star::beans
;
53 using namespace ::com::sun::star::form
;
54 using namespace ::com::sun::star::sdbc
;
55 using namespace ::com::sun::star::lang
;
57 //========================================================================
59 //========================================================================
60 DBG_NAME(OSelectLabelDialog
)
61 //------------------------------------------------------------------------
62 OSelectLabelDialog::OSelectLabelDialog( Window
* pParent
, Reference
< XPropertySet
> _xControlModel
)
63 :ModalDialog(pParent
, PcrRes(RID_DLG_SELECTLABELCONTROL
))
64 ,m_aMainDesc(this, PcrRes(1))
65 ,m_aControlTree(this, PcrRes(1))
66 ,m_aNoAssignment(this, PcrRes(1))
67 ,m_aSeparator(this, PcrRes(1))
68 ,m_aOk(this, PcrRes(1))
69 ,m_aCancel(this, PcrRes(1))
70 ,m_aModelImages(PcrRes(RID_IL_FORMEXPLORER
))
71 ,m_xControlModel(_xControlModel
)
72 ,m_pInitialSelection(NULL
)
73 ,m_pLastSelected(NULL
)
74 ,m_bHaveAssignableControl(sal_False
)
76 DBG_CTOR(OSelectLabelDialog
,NULL
);
78 // initialize the TreeListBox
79 m_aControlTree
.SetSelectionMode( SINGLE_SELECTION
);
80 m_aControlTree
.SetDragDropMode( 0 );
81 m_aControlTree
.EnableInplaceEditing( sal_False
);
82 m_aControlTree
.SetStyle(m_aControlTree
.GetStyle() | WB_BORDER
| WB_HASLINES
| WB_HASLINESATROOT
| WB_HASBUTTONS
| WB_HASBUTTONSATROOT
| WB_HSCROLL
);
84 m_aControlTree
.SetNodeBitmaps( m_aModelImages
.GetImage( RID_SVXIMG_COLLAPSEDNODE
), m_aModelImages
.GetImage( RID_SVXIMG_EXPANDEDNODE
) );
85 m_aControlTree
.SetSelectHdl(LINK(this, OSelectLabelDialog
, OnEntrySelected
));
86 m_aControlTree
.SetDeselectHdl(LINK(this, OSelectLabelDialog
, OnEntrySelected
));
88 // fill the description
89 UniString sDescription
= m_aMainDesc
.GetText();
90 sal_Int16 nClassID
= FormComponentType::CONTROL
;
91 if (::comphelper::hasProperty(PROPERTY_CLASSID
, m_xControlModel
))
92 nClassID
= ::comphelper::getINT16(m_xControlModel
->getPropertyValue(PROPERTY_CLASSID
));
94 sDescription
.SearchAndReplace(String::CreateFromAscii("$control_class$"), GetUIHeadlineName(nClassID
, makeAny(m_xControlModel
)));
95 UniString sName
= ::comphelper::getString(m_xControlModel
->getPropertyValue(PROPERTY_NAME
)).getStr();
96 sDescription
.SearchAndReplace(String::CreateFromAscii("$control_name$"), sName
);
97 m_aMainDesc
.SetText(sDescription
);
99 // search for the root of the form hierarchy
100 Reference
< XChild
> xCont(m_xControlModel
, UNO_QUERY
);
101 Reference
< XInterface
> xSearch( xCont
.is() ? xCont
->getParent() : Reference
< XInterface
> ());
102 Reference
< XResultSet
> xParentAsResultSet(xSearch
, UNO_QUERY
);
103 while (xParentAsResultSet
.is())
105 xCont
= Reference
< XChild
> (xSearch
, UNO_QUERY
);
106 xSearch
= xCont
.is() ? xCont
->getParent() : Reference
< XInterface
> ();
107 xParentAsResultSet
= Reference
< XResultSet
> (xSearch
, UNO_QUERY
);
110 // and insert all entries below this root into the listbox
113 // check wich service the allowed components must suppport
114 sal_Int16 nClassId
= 0;
115 try { nClassId
= ::comphelper::getINT16(m_xControlModel
->getPropertyValue(PROPERTY_CLASSID
)); } catch(...) { }
116 m_sRequiredService
= (FormComponentType::RADIOBUTTON
== nClassId
) ? SERVICE_COMPONENT_GROUPBOX
: SERVICE_COMPONENT_FIXEDTEXT
;
117 m_aRequiredControlImage
= m_aModelImages
.GetImage((FormComponentType::RADIOBUTTON
== nClassId
) ? RID_SVXIMG_GROUPBOX
: RID_SVXIMG_FIXEDTEXT
);
119 // calc the currently set label control (so InsertEntries can calc m_pInitialSelection)
120 Any
aCurrentLabelControl( m_xControlModel
->getPropertyValue(PROPERTY_CONTROLLABEL
) );
121 DBG_ASSERT((aCurrentLabelControl
.getValueTypeClass() == TypeClass_INTERFACE
) || !aCurrentLabelControl
.hasValue(),
123 "OSelectLabelDialog::OSelectLabelDialog : invalid ControlLabel property !");
124 if (aCurrentLabelControl
.hasValue())
125 aCurrentLabelControl
>>= m_xInitialLabelControl
;
128 Image aRootImage
= m_aModelImages
.GetImage(RID_SVXIMG_FORMS
);
129 SvLBoxEntry
* pRoot
= m_aControlTree
.InsertEntry(PcrRes(RID_STR_FORMS
), aRootImage
, aRootImage
);
132 m_pInitialSelection
= NULL
;
133 m_bHaveAssignableControl
= sal_False
;
134 InsertEntries(xSearch
, pRoot
);
135 m_aControlTree
.Expand(pRoot
);
138 if (m_pInitialSelection
)
140 m_aControlTree
.MakeVisible(m_pInitialSelection
, sal_True
);
141 m_aControlTree
.Select(m_pInitialSelection
, sal_True
);
145 m_aControlTree
.MakeVisible(m_aControlTree
.First(), sal_True
);
146 if (m_aControlTree
.FirstSelected())
147 m_aControlTree
.Select(m_aControlTree
.FirstSelected(), sal_False
);
148 m_aNoAssignment
.Check(sal_True
);
151 if (!m_bHaveAssignableControl
)
152 { // no controls which can be assigned
153 m_aNoAssignment
.Check(sal_True
);
154 m_aNoAssignment
.Enable(sal_False
);
157 m_aNoAssignment
.SetClickHdl(LINK(this, OSelectLabelDialog
, OnNoAssignmentClicked
));
158 m_aNoAssignment
.GetClickHdl().Call(&m_aNoAssignment
);
163 //------------------------------------------------------------------------
164 OSelectLabelDialog::~OSelectLabelDialog()
166 // delete the entry datas of the listbox entries
167 SvLBoxEntry
* pLoop
= m_aControlTree
.First();
170 void* pData
= pLoop
->GetUserData();
172 delete (Reference
< XPropertySet
> *)pData
;
173 pLoop
= m_aControlTree
.Next(pLoop
);
176 DBG_DTOR(OSelectLabelDialog
,NULL
);
179 //------------------------------------------------------------------------
180 sal_Int32
OSelectLabelDialog::InsertEntries(const Reference
< XInterface
> & _xContainer
, SvLBoxEntry
* pContainerEntry
)
182 Reference
< XIndexAccess
> xContainer(_xContainer
, UNO_QUERY
);
183 if (!xContainer
.is())
186 sal_Int32 nChildren
= 0;
187 UniString sName
,sDisplayName
;
188 Reference
< XPropertySet
> xAsSet
;
189 for (sal_Int32 i
=0; i
<xContainer
->getCount(); ++i
)
191 xContainer
->getByIndex(i
) >>= xAsSet
;
194 DBG_WARNING("OSelectLabelDialog::InsertEntries : strange : a form component which isn't a property set !");
198 if (!::comphelper::hasProperty(PROPERTY_NAME
, xAsSet
))
199 // we need at least a name for displaying ...
201 sName
= ::comphelper::getString(xAsSet
->getPropertyValue(PROPERTY_NAME
)).getStr();
203 // we need to check if the control model supports the required service
204 Reference
< XServiceInfo
> xInfo(xAsSet
, UNO_QUERY
);
208 if (!xInfo
->supportsService(m_sRequiredService
))
209 { // perhaps it is a container
210 Reference
< XIndexAccess
> xCont(xAsSet
, UNO_QUERY
);
211 if (xCont
.is() && xCont
->getCount())
212 { // yes -> step down
213 Image aFormImage
= m_aModelImages
.GetImage( RID_SVXIMG_FORM
);
214 SvLBoxEntry
* pCont
= m_aControlTree
.InsertEntry(sName
, aFormImage
, aFormImage
, pContainerEntry
);
215 sal_Int32 nContChildren
= InsertEntries(xCont
, pCont
);
218 m_aControlTree
.Expand(pCont
);
222 { // oops, no valid childs -> remove the entry
223 m_aControlTree
.ModelIsRemoving(pCont
);
224 m_aControlTree
.GetModel()->Remove(pCont
);
225 m_aControlTree
.ModelHasRemoved(pCont
);
232 if (!::comphelper::hasProperty(PROPERTY_LABEL
, xAsSet
))
234 sDisplayName
= ::comphelper::getString(xAsSet
->getPropertyValue(PROPERTY_LABEL
)).getStr();
235 sDisplayName
+= String::CreateFromAscii(" (");
236 sDisplayName
+= sName
;
239 // all requirements met -> insert
240 SvLBoxEntry
* pCurrent
= m_aControlTree
.InsertEntry(sDisplayName
, m_aRequiredControlImage
, m_aRequiredControlImage
, pContainerEntry
);
241 pCurrent
->SetUserData(new Reference
< XPropertySet
> (xAsSet
));
244 if (m_xInitialLabelControl
== xAsSet
)
245 m_pInitialSelection
= pCurrent
;
247 m_bHaveAssignableControl
= sal_True
;
253 //------------------------------------------------------------------------
254 IMPL_LINK(OSelectLabelDialog
, OnEntrySelected
, SvTreeListBox
*, pLB
)
256 DBG_ASSERT(pLB
== &m_aControlTree
, "OSelectLabelDialog::OnEntrySelected : where did this come from ?");
258 SvLBoxEntry
* pSelected
= m_aControlTree
.FirstSelected();
259 void* pData
= pSelected
? pSelected
->GetUserData() : NULL
;
262 m_xSelectedControl
= Reference
< XPropertySet
> (*(Reference
< XPropertySet
> *)pData
);
264 m_aNoAssignment
.SetClickHdl(Link());
265 m_aNoAssignment
.Check(pData
== NULL
);
266 m_aNoAssignment
.SetClickHdl(LINK(this, OSelectLabelDialog
, OnNoAssignmentClicked
));
271 //------------------------------------------------------------------------
272 IMPL_LINK(OSelectLabelDialog
, OnNoAssignmentClicked
, Button
*, pButton
)
274 DBG_ASSERT(pButton
== &m_aNoAssignment
, "OSelectLabelDialog::OnNoAssignmentClicked : where did this come from ?");
277 if (m_aNoAssignment
.IsChecked())
278 m_pLastSelected
= m_aControlTree
.FirstSelected();
281 DBG_ASSERT(m_bHaveAssignableControl
, "OSelectLabelDialog::OnNoAssignmentClicked");
282 // search the first assignable entry
283 SvLBoxEntry
* pSearch
= m_aControlTree
.First();
286 if (pSearch
->GetUserData())
288 pSearch
= m_aControlTree
.Next(pSearch
);
293 m_aControlTree
.Select(pSearch
);
294 m_pLastSelected
= pSearch
;
300 m_aControlTree
.SetSelectHdl(Link());
301 m_aControlTree
.SetDeselectHdl(Link());
302 m_aControlTree
.Select(m_pLastSelected
, !m_aNoAssignment
.IsChecked());
303 m_aControlTree
.SetSelectHdl(LINK(this, OSelectLabelDialog
, OnEntrySelected
));
304 m_aControlTree
.SetDeselectHdl(LINK(this, OSelectLabelDialog
, OnEntrySelected
));
310 //............................................................................
312 //............................................................................