Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "selectlabeldialog.hxx"
21 : #include "formresid.hrc"
22 : #include "formbrowsertools.hxx"
23 : #include "formstrings.hxx"
24 : #include <com/sun/star/form/FormComponentType.hpp>
25 : #include <com/sun/star/container/XChild.hpp>
26 : #include <com/sun/star/container/XIndexAccess.hpp>
27 : #include <com/sun/star/sdbc/XResultSet.hpp>
28 : #include <com/sun/star/beans/XPropertySet.hpp>
29 : #include <com/sun/star/lang/XServiceInfo.hpp>
30 : #include <comphelper/property.hxx>
31 : #include <comphelper/types.hxx>
32 : #include "svtools/treelistentry.hxx"
33 :
34 :
35 : namespace pcr
36 : {
37 :
38 :
39 : using namespace ::com::sun::star::uno;
40 : using namespace ::com::sun::star::container;
41 : using namespace ::com::sun::star::beans;
42 : using namespace ::com::sun::star::form;
43 : using namespace ::com::sun::star::sdbc;
44 : using namespace ::com::sun::star::lang;
45 :
46 :
47 : // OSelectLabelDialog
48 :
49 :
50 0 : OSelectLabelDialog::OSelectLabelDialog( Window* pParent, Reference< XPropertySet > _xControlModel )
51 : :ModalDialog(pParent, PcrRes(RID_DLG_SELECTLABELCONTROL))
52 : ,m_aMainDesc(this, PcrRes(1))
53 : ,m_aControlTree(this, PcrRes(1))
54 : ,m_aNoAssignment(this, PcrRes(1))
55 : ,m_aSeparator(this, PcrRes(1))
56 : ,m_aOk(this, PcrRes(1))
57 : ,m_aCancel(this, PcrRes(1))
58 : ,m_aModelImages(PcrRes(RID_IL_FORMEXPLORER))
59 : ,m_xControlModel(_xControlModel)
60 : ,m_pInitialSelection(NULL)
61 : ,m_pLastSelected(NULL)
62 0 : ,m_bHaveAssignableControl(sal_False)
63 : {
64 : // initialize the TreeListBox
65 0 : m_aControlTree.SetSelectionMode( SINGLE_SELECTION );
66 0 : m_aControlTree.SetDragDropMode( 0 );
67 0 : m_aControlTree.EnableInplaceEditing( false );
68 0 : m_aControlTree.SetStyle(m_aControlTree.GetStyle() | WB_BORDER | WB_HASLINES | WB_HASLINESATROOT | WB_HASBUTTONS | WB_HASBUTTONSATROOT | WB_HSCROLL);
69 :
70 0 : m_aControlTree.SetNodeBitmaps( m_aModelImages.GetImage( RID_SVXIMG_COLLAPSEDNODE ), m_aModelImages.GetImage( RID_SVXIMG_EXPANDEDNODE ) );
71 0 : m_aControlTree.SetSelectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
72 0 : m_aControlTree.SetDeselectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
73 :
74 : // fill the description
75 0 : OUString sDescription = m_aMainDesc.GetText();
76 0 : sal_Int16 nClassID = FormComponentType::CONTROL;
77 0 : if (::comphelper::hasProperty(PROPERTY_CLASSID, m_xControlModel))
78 0 : nClassID = ::comphelper::getINT16(m_xControlModel->getPropertyValue(PROPERTY_CLASSID));
79 :
80 0 : sDescription = sDescription.replaceAll(OUString("$control_class$"),
81 0 : GetUIHeadlineName(nClassID, makeAny(m_xControlModel)));
82 0 : OUString sName = ::comphelper::getString(m_xControlModel->getPropertyValue(PROPERTY_NAME));
83 0 : sDescription = sDescription.replaceAll(OUString("$control_name$"), sName);
84 0 : m_aMainDesc.SetText(sDescription);
85 :
86 : // search for the root of the form hierarchy
87 0 : Reference< XChild > xCont(m_xControlModel, UNO_QUERY);
88 0 : Reference< XInterface > xSearch( xCont.is() ? xCont->getParent() : Reference< XInterface > ());
89 0 : Reference< XResultSet > xParentAsResultSet(xSearch, UNO_QUERY);
90 0 : while (xParentAsResultSet.is())
91 : {
92 0 : xCont = Reference< XChild > (xSearch, UNO_QUERY);
93 0 : xSearch = xCont.is() ? xCont->getParent() : Reference< XInterface > ();
94 0 : xParentAsResultSet = Reference< XResultSet > (xSearch, UNO_QUERY);
95 : }
96 :
97 : // and insert all entries below this root into the listbox
98 0 : if (xSearch.is())
99 : {
100 : // check which service the allowed components must suppport
101 0 : sal_Int16 nClassId = 0;
102 0 : try { nClassId = ::comphelper::getINT16(m_xControlModel->getPropertyValue(PROPERTY_CLASSID)); } catch(...) { }
103 0 : m_sRequiredService = (FormComponentType::RADIOBUTTON == nClassId) ? OUString(SERVICE_COMPONENT_GROUPBOX) : OUString(SERVICE_COMPONENT_FIXEDTEXT);
104 0 : m_aRequiredControlImage = m_aModelImages.GetImage((FormComponentType::RADIOBUTTON == nClassId) ? RID_SVXIMG_GROUPBOX : RID_SVXIMG_FIXEDTEXT);
105 :
106 : // calc the currently set label control (so InsertEntries can calc m_pInitialSelection)
107 0 : Any aCurrentLabelControl( m_xControlModel->getPropertyValue(PROPERTY_CONTROLLABEL) );
108 : DBG_ASSERT((aCurrentLabelControl.getValueTypeClass() == TypeClass_INTERFACE) || !aCurrentLabelControl.hasValue(),
109 :
110 : "OSelectLabelDialog::OSelectLabelDialog : invalid ControlLabel property !");
111 0 : if (aCurrentLabelControl.hasValue())
112 0 : aCurrentLabelControl >>= m_xInitialLabelControl;
113 :
114 : // insert the root
115 0 : Image aRootImage = m_aModelImages.GetImage(RID_SVXIMG_FORMS);
116 0 : SvTreeListEntry* pRoot = m_aControlTree.InsertEntry(PcrRes(RID_STR_FORMS).toString(), aRootImage, aRootImage);
117 :
118 : // build the tree
119 0 : m_pInitialSelection = NULL;
120 0 : m_bHaveAssignableControl = sal_False;
121 0 : InsertEntries(xSearch, pRoot);
122 0 : m_aControlTree.Expand(pRoot);
123 : }
124 :
125 0 : if (m_pInitialSelection)
126 : {
127 0 : m_aControlTree.MakeVisible(m_pInitialSelection, true);
128 0 : m_aControlTree.Select(m_pInitialSelection, true);
129 : }
130 : else
131 : {
132 0 : m_aControlTree.MakeVisible(m_aControlTree.First(), true);
133 0 : if (m_aControlTree.FirstSelected())
134 0 : m_aControlTree.Select(m_aControlTree.FirstSelected(), false);
135 0 : m_aNoAssignment.Check(true);
136 : }
137 :
138 0 : if (!m_bHaveAssignableControl)
139 : { // no controls which can be assigned
140 0 : m_aNoAssignment.Check(true);
141 0 : m_aNoAssignment.Enable(false);
142 : }
143 :
144 0 : m_aNoAssignment.SetClickHdl(LINK(this, OSelectLabelDialog, OnNoAssignmentClicked));
145 0 : m_aNoAssignment.GetClickHdl().Call(&m_aNoAssignment);
146 :
147 0 : FreeResource();
148 0 : }
149 :
150 :
151 0 : OSelectLabelDialog::~OSelectLabelDialog()
152 : {
153 : // delete the entry datas of the listbox entries
154 0 : SvTreeListEntry* pLoop = m_aControlTree.First();
155 0 : while (pLoop)
156 : {
157 0 : void* pData = pLoop->GetUserData();
158 0 : if (pData)
159 0 : delete (Reference< XPropertySet > *)pData;
160 0 : pLoop = m_aControlTree.Next(pLoop);
161 : }
162 :
163 0 : }
164 :
165 :
166 0 : sal_Int32 OSelectLabelDialog::InsertEntries(const Reference< XInterface > & _xContainer, SvTreeListEntry* pContainerEntry)
167 : {
168 0 : Reference< XIndexAccess > xContainer(_xContainer, UNO_QUERY);
169 0 : if (!xContainer.is())
170 0 : return 0;
171 :
172 0 : sal_Int32 nChildren = 0;
173 0 : OUString sName;
174 0 : Reference< XPropertySet > xAsSet;
175 0 : for (sal_Int32 i=0; i<xContainer->getCount(); ++i)
176 : {
177 0 : xContainer->getByIndex(i) >>= xAsSet;
178 0 : if (!xAsSet.is())
179 : {
180 : DBG_WARNING("OSelectLabelDialog::InsertEntries : strange : a form component which isn't a property set !");
181 0 : continue;
182 : }
183 :
184 0 : if (!::comphelper::hasProperty(PROPERTY_NAME, xAsSet))
185 : // we need at least a name for displaying ...
186 0 : continue;
187 0 : sName = ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_NAME)).getStr();
188 :
189 : // we need to check if the control model supports the required service
190 0 : Reference< XServiceInfo > xInfo(xAsSet, UNO_QUERY);
191 0 : if (!xInfo.is())
192 0 : continue;
193 :
194 0 : if (!xInfo->supportsService(m_sRequiredService))
195 : { // perhaps it is a container
196 0 : Reference< XIndexAccess > xCont(xAsSet, UNO_QUERY);
197 0 : if (xCont.is() && xCont->getCount())
198 : { // yes -> step down
199 0 : Image aFormImage = m_aModelImages.GetImage( RID_SVXIMG_FORM );
200 0 : SvTreeListEntry* pCont = m_aControlTree.InsertEntry(sName, aFormImage, aFormImage, pContainerEntry);
201 0 : sal_Int32 nContChildren = InsertEntries(xCont, pCont);
202 0 : if (nContChildren)
203 : {
204 0 : m_aControlTree.Expand(pCont);
205 0 : ++nChildren;
206 : }
207 : else
208 : { // oops, no valid children -> remove the entry
209 0 : m_aControlTree.ModelIsRemoving(pCont);
210 0 : m_aControlTree.GetModel()->Remove(pCont);
211 0 : m_aControlTree.ModelHasRemoved(pCont);
212 0 : }
213 : }
214 0 : continue;
215 : }
216 :
217 : // get the label
218 0 : if (!::comphelper::hasProperty(PROPERTY_LABEL, xAsSet))
219 0 : continue;
220 :
221 : OUString sDisplayName = OUStringBuffer(
222 0 : ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_LABEL))).
223 0 : append(" (").append(sName).append(')').
224 0 : makeStringAndClear();
225 :
226 : // all requirements met -> insert
227 0 : SvTreeListEntry* pCurrent = m_aControlTree.InsertEntry(sDisplayName, m_aRequiredControlImage, m_aRequiredControlImage, pContainerEntry);
228 0 : pCurrent->SetUserData(new Reference< XPropertySet > (xAsSet));
229 0 : ++nChildren;
230 :
231 0 : if (m_xInitialLabelControl == xAsSet)
232 0 : m_pInitialSelection = pCurrent;
233 :
234 0 : m_bHaveAssignableControl = sal_True;
235 0 : }
236 :
237 0 : return nChildren;
238 : }
239 :
240 :
241 0 : IMPL_LINK(OSelectLabelDialog, OnEntrySelected, SvTreeListBox*, pLB)
242 : {
243 : DBG_ASSERT(pLB == &m_aControlTree, "OSelectLabelDialog::OnEntrySelected : where did this come from ?");
244 : (void)pLB;
245 0 : SvTreeListEntry* pSelected = m_aControlTree.FirstSelected();
246 0 : void* pData = pSelected ? pSelected->GetUserData() : NULL;
247 :
248 0 : if (pData)
249 0 : m_xSelectedControl = Reference< XPropertySet > (*(Reference< XPropertySet > *)pData);
250 :
251 0 : m_aNoAssignment.SetClickHdl(Link());
252 0 : m_aNoAssignment.Check(pData == NULL);
253 0 : m_aNoAssignment.SetClickHdl(LINK(this, OSelectLabelDialog, OnNoAssignmentClicked));
254 :
255 0 : return 0L;
256 : }
257 :
258 :
259 0 : IMPL_LINK(OSelectLabelDialog, OnNoAssignmentClicked, Button*, pButton)
260 : {
261 : DBG_ASSERT(pButton == &m_aNoAssignment, "OSelectLabelDialog::OnNoAssignmentClicked : where did this come from ?");
262 : (void)pButton;
263 :
264 0 : if (m_aNoAssignment.IsChecked())
265 0 : m_pLastSelected = m_aControlTree.FirstSelected();
266 : else
267 : {
268 : DBG_ASSERT(m_bHaveAssignableControl, "OSelectLabelDialog::OnNoAssignmentClicked");
269 : // search the first assignable entry
270 0 : SvTreeListEntry* pSearch = m_aControlTree.First();
271 0 : while (pSearch)
272 : {
273 0 : if (pSearch->GetUserData())
274 0 : break;
275 0 : pSearch = m_aControlTree.Next(pSearch);
276 : }
277 : // and select it
278 0 : if (pSearch)
279 : {
280 0 : m_aControlTree.Select(pSearch);
281 0 : m_pLastSelected = pSearch;
282 : }
283 : }
284 :
285 0 : if (m_pLastSelected)
286 : {
287 0 : m_aControlTree.SetSelectHdl(Link());
288 0 : m_aControlTree.SetDeselectHdl(Link());
289 0 : m_aControlTree.Select(m_pLastSelected, !m_aNoAssignment.IsChecked());
290 0 : m_aControlTree.SetSelectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
291 0 : m_aControlTree.SetDeselectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
292 : }
293 :
294 0 : return 0L;
295 : }
296 :
297 :
298 : } // namespace pcr
299 :
300 :
301 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|