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( vcl::Window* pParent, Reference< XPropertySet > _xControlModel )
51 : :ModalDialog(pParent, "LabelSelectionDialog", "modules/spropctrlr/ui/labelselectiondialog.ui")
52 : ,m_aModelImages(PcrRes(RID_IL_FORMEXPLORER))
53 : ,m_xControlModel(_xControlModel)
54 : ,m_pInitialSelection(NULL)
55 : ,m_pLastSelected(NULL)
56 0 : ,m_bHaveAssignableControl(false)
57 : {
58 0 : get(m_pMainDesc, "label");
59 0 : get(m_pControlTree, "control");
60 0 : get(m_pNoAssignment, "noassignment");
61 :
62 : // initialize the TreeListBox
63 0 : m_pControlTree->SetSelectionMode( SINGLE_SELECTION );
64 0 : m_pControlTree->SetDragDropMode( DragDropMode::NONE );
65 0 : m_pControlTree->EnableInplaceEditing( false );
66 0 : m_pControlTree->SetStyle(m_pControlTree->GetStyle() | WB_BORDER | WB_HASLINES | WB_HASLINESATROOT | WB_HASBUTTONS | WB_HASBUTTONSATROOT | WB_HSCROLL);
67 :
68 0 : m_pControlTree->SetNodeBitmaps( m_aModelImages.GetImage( RID_SVXIMG_COLLAPSEDNODE ), m_aModelImages.GetImage( RID_SVXIMG_EXPANDEDNODE ) );
69 0 : m_pControlTree->SetSelectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
70 0 : m_pControlTree->SetDeselectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
71 :
72 : // fill the description
73 0 : OUString sDescription = m_pMainDesc->GetText();
74 0 : sal_Int16 nClassID = FormComponentType::CONTROL;
75 0 : if (::comphelper::hasProperty(PROPERTY_CLASSID, m_xControlModel))
76 0 : nClassID = ::comphelper::getINT16(m_xControlModel->getPropertyValue(PROPERTY_CLASSID));
77 :
78 0 : sDescription = sDescription.replaceAll(OUString("$controlclass$"),
79 0 : GetUIHeadlineName(nClassID, makeAny(m_xControlModel)));
80 0 : OUString sName = ::comphelper::getString(m_xControlModel->getPropertyValue(PROPERTY_NAME));
81 0 : sDescription = sDescription.replaceAll(OUString("$controlname$"), sName);
82 0 : m_pMainDesc->SetText(sDescription);
83 :
84 : // search for the root of the form hierarchy
85 0 : Reference< XChild > xCont(m_xControlModel, UNO_QUERY);
86 0 : Reference< XInterface > xSearch( xCont.is() ? xCont->getParent() : Reference< XInterface > ());
87 0 : Reference< XResultSet > xParentAsResultSet(xSearch, UNO_QUERY);
88 0 : while (xParentAsResultSet.is())
89 : {
90 0 : xCont = Reference< XChild > (xSearch, UNO_QUERY);
91 0 : xSearch = xCont.is() ? xCont->getParent() : Reference< XInterface > ();
92 0 : xParentAsResultSet = Reference< XResultSet > (xSearch, UNO_QUERY);
93 : }
94 :
95 : // and insert all entries below this root into the listbox
96 0 : if (xSearch.is())
97 : {
98 : // check which service the allowed components must suppport
99 0 : sal_Int16 nClassId = 0;
100 0 : try { nClassId = ::comphelper::getINT16(m_xControlModel->getPropertyValue(PROPERTY_CLASSID)); } catch(...) { }
101 0 : m_sRequiredService = (FormComponentType::RADIOBUTTON == nClassId) ? OUString(SERVICE_COMPONENT_GROUPBOX) : OUString(SERVICE_COMPONENT_FIXEDTEXT);
102 0 : m_aRequiredControlImage = m_aModelImages.GetImage((FormComponentType::RADIOBUTTON == nClassId) ? RID_SVXIMG_GROUPBOX : RID_SVXIMG_FIXEDTEXT);
103 :
104 : // calc the currently set label control (so InsertEntries can calc m_pInitialSelection)
105 0 : Any aCurrentLabelControl( m_xControlModel->getPropertyValue(PROPERTY_CONTROLLABEL) );
106 : DBG_ASSERT((aCurrentLabelControl.getValueTypeClass() == TypeClass_INTERFACE) || !aCurrentLabelControl.hasValue(),
107 :
108 : "OSelectLabelDialog::OSelectLabelDialog : invalid ControlLabel property !");
109 0 : if (aCurrentLabelControl.hasValue())
110 0 : aCurrentLabelControl >>= m_xInitialLabelControl;
111 :
112 : // insert the root
113 0 : Image aRootImage = m_aModelImages.GetImage(RID_SVXIMG_FORMS);
114 0 : SvTreeListEntry* pRoot = m_pControlTree->InsertEntry(PcrRes(RID_STR_FORMS).toString(), aRootImage, aRootImage);
115 :
116 : // build the tree
117 0 : m_pInitialSelection = NULL;
118 0 : m_bHaveAssignableControl = false;
119 0 : InsertEntries(xSearch, pRoot);
120 0 : m_pControlTree->Expand(pRoot);
121 : }
122 :
123 0 : if (m_pInitialSelection)
124 : {
125 0 : m_pControlTree->MakeVisible(m_pInitialSelection, true);
126 0 : m_pControlTree->Select(m_pInitialSelection, true);
127 : }
128 : else
129 : {
130 0 : m_pControlTree->MakeVisible(m_pControlTree->First(), true);
131 0 : if (m_pControlTree->FirstSelected())
132 0 : m_pControlTree->Select(m_pControlTree->FirstSelected(), false);
133 0 : m_pNoAssignment->Check(true);
134 : }
135 :
136 0 : if (!m_bHaveAssignableControl)
137 : { // no controls which can be assigned
138 0 : m_pNoAssignment->Check(true);
139 0 : m_pNoAssignment->Enable(false);
140 : }
141 :
142 0 : m_pNoAssignment->SetClickHdl(LINK(this, OSelectLabelDialog, OnNoAssignmentClicked));
143 0 : m_pNoAssignment->GetClickHdl().Call(m_pNoAssignment);
144 0 : }
145 :
146 0 : OSelectLabelDialog::~OSelectLabelDialog()
147 : {
148 0 : disposeOnce();
149 0 : }
150 :
151 0 : void OSelectLabelDialog::dispose()
152 : {
153 : // delete the entry datas of the listbox entries
154 0 : SvTreeListEntry* pLoop = m_pControlTree->First();
155 0 : while (pLoop)
156 : {
157 0 : void* pData = pLoop->GetUserData();
158 0 : if (pData)
159 0 : delete static_cast<Reference< XPropertySet > *>(pData);
160 0 : pLoop = m_pControlTree->Next(pLoop);
161 : }
162 0 : m_pMainDesc.clear();
163 0 : m_pControlTree.clear();
164 0 : m_pNoAssignment.clear();
165 0 : ModalDialog::dispose();
166 0 : }
167 :
168 0 : sal_Int32 OSelectLabelDialog::InsertEntries(const Reference< XInterface > & _xContainer, SvTreeListEntry* pContainerEntry)
169 : {
170 0 : Reference< XIndexAccess > xContainer(_xContainer, UNO_QUERY);
171 0 : if (!xContainer.is())
172 0 : return 0;
173 :
174 0 : sal_Int32 nChildren = 0;
175 0 : OUString sName;
176 0 : Reference< XPropertySet > xAsSet;
177 0 : for (sal_Int32 i=0; i<xContainer->getCount(); ++i)
178 : {
179 0 : xContainer->getByIndex(i) >>= xAsSet;
180 0 : if (!xAsSet.is())
181 : {
182 : DBG_WARNING("OSelectLabelDialog::InsertEntries : strange : a form component which isn't a property set !");
183 0 : continue;
184 : }
185 :
186 0 : if (!::comphelper::hasProperty(PROPERTY_NAME, xAsSet))
187 : // we need at least a name for displaying ...
188 0 : continue;
189 0 : sName = ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_NAME)).getStr();
190 :
191 : // we need to check if the control model supports the required service
192 0 : Reference< XServiceInfo > xInfo(xAsSet, UNO_QUERY);
193 0 : if (!xInfo.is())
194 0 : continue;
195 :
196 0 : if (!xInfo->supportsService(m_sRequiredService))
197 : { // perhaps it is a container
198 0 : Reference< XIndexAccess > xCont(xAsSet, UNO_QUERY);
199 0 : if (xCont.is() && xCont->getCount())
200 : { // yes -> step down
201 0 : Image aFormImage = m_aModelImages.GetImage( RID_SVXIMG_FORM );
202 0 : SvTreeListEntry* pCont = m_pControlTree->InsertEntry(sName, aFormImage, aFormImage, pContainerEntry);
203 0 : sal_Int32 nContChildren = InsertEntries(xCont, pCont);
204 0 : if (nContChildren)
205 : {
206 0 : m_pControlTree->Expand(pCont);
207 0 : ++nChildren;
208 : }
209 : else
210 : { // oops, no valid children -> remove the entry
211 0 : m_pControlTree->ModelIsRemoving(pCont);
212 0 : m_pControlTree->GetModel()->Remove(pCont);
213 0 : m_pControlTree->ModelHasRemoved(pCont);
214 0 : }
215 : }
216 0 : continue;
217 : }
218 :
219 : // get the label
220 0 : if (!::comphelper::hasProperty(PROPERTY_LABEL, xAsSet))
221 0 : continue;
222 :
223 : OUString sDisplayName = OUStringBuffer(
224 0 : ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_LABEL))).
225 0 : append(" (").append(sName).append(')').
226 0 : makeStringAndClear();
227 :
228 : // all requirements met -> insert
229 0 : SvTreeListEntry* pCurrent = m_pControlTree->InsertEntry(sDisplayName, m_aRequiredControlImage, m_aRequiredControlImage, pContainerEntry);
230 0 : pCurrent->SetUserData(new Reference< XPropertySet > (xAsSet));
231 0 : ++nChildren;
232 :
233 0 : if (m_xInitialLabelControl == xAsSet)
234 0 : m_pInitialSelection = pCurrent;
235 :
236 0 : m_bHaveAssignableControl = true;
237 0 : }
238 :
239 0 : return nChildren;
240 : }
241 :
242 :
243 0 : IMPL_LINK(OSelectLabelDialog, OnEntrySelected, SvTreeListBox*, pLB)
244 : {
245 : DBG_ASSERT(pLB == m_pControlTree, "OSelectLabelDialog::OnEntrySelected : where did this come from ?");
246 : (void)pLB;
247 0 : SvTreeListEntry* pSelected = m_pControlTree->FirstSelected();
248 0 : void* pData = pSelected ? pSelected->GetUserData() : NULL;
249 :
250 0 : if (pData)
251 0 : m_xSelectedControl = Reference< XPropertySet > (*static_cast<Reference< XPropertySet > *>(pData));
252 :
253 0 : m_pNoAssignment->SetClickHdl(Link<>());
254 0 : m_pNoAssignment->Check(pData == NULL);
255 0 : m_pNoAssignment->SetClickHdl(LINK(this, OSelectLabelDialog, OnNoAssignmentClicked));
256 :
257 0 : return 0L;
258 : }
259 :
260 :
261 0 : IMPL_LINK(OSelectLabelDialog, OnNoAssignmentClicked, Button*, pButton)
262 : {
263 : DBG_ASSERT(pButton == m_pNoAssignment, "OSelectLabelDialog::OnNoAssignmentClicked : where did this come from ?");
264 : (void)pButton;
265 :
266 0 : if (m_pNoAssignment->IsChecked())
267 0 : m_pLastSelected = m_pControlTree->FirstSelected();
268 : else
269 : {
270 : DBG_ASSERT(m_bHaveAssignableControl, "OSelectLabelDialog::OnNoAssignmentClicked");
271 : // search the first assignable entry
272 0 : SvTreeListEntry* pSearch = m_pControlTree->First();
273 0 : while (pSearch)
274 : {
275 0 : if (pSearch->GetUserData())
276 0 : break;
277 0 : pSearch = m_pControlTree->Next(pSearch);
278 : }
279 : // and select it
280 0 : if (pSearch)
281 : {
282 0 : m_pControlTree->Select(pSearch);
283 0 : m_pLastSelected = pSearch;
284 : }
285 : }
286 :
287 0 : if (m_pLastSelected)
288 : {
289 0 : m_pControlTree->SetSelectHdl(Link<>());
290 0 : m_pControlTree->SetDeselectHdl(Link<>());
291 0 : m_pControlTree->Select(m_pLastSelected, !m_pNoAssignment->IsChecked());
292 0 : m_pControlTree->SetSelectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
293 0 : m_pControlTree->SetDeselectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
294 : }
295 :
296 0 : return 0L;
297 : }
298 :
299 :
300 6 : } // namespace pcr
301 :
302 :
303 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|