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( 0 );
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 : // delete the entry datas of the listbox entries
149 0 : SvTreeListEntry* pLoop = m_pControlTree->First();
150 0 : while (pLoop)
151 : {
152 0 : void* pData = pLoop->GetUserData();
153 0 : if (pData)
154 0 : delete (Reference< XPropertySet > *)pData;
155 0 : pLoop = m_pControlTree->Next(pLoop);
156 : }
157 :
158 0 : }
159 :
160 0 : sal_Int32 OSelectLabelDialog::InsertEntries(const Reference< XInterface > & _xContainer, SvTreeListEntry* pContainerEntry)
161 : {
162 0 : Reference< XIndexAccess > xContainer(_xContainer, UNO_QUERY);
163 0 : if (!xContainer.is())
164 0 : return 0;
165 :
166 0 : sal_Int32 nChildren = 0;
167 0 : OUString sName;
168 0 : Reference< XPropertySet > xAsSet;
169 0 : for (sal_Int32 i=0; i<xContainer->getCount(); ++i)
170 : {
171 0 : xContainer->getByIndex(i) >>= xAsSet;
172 0 : if (!xAsSet.is())
173 : {
174 : DBG_WARNING("OSelectLabelDialog::InsertEntries : strange : a form component which isn't a property set !");
175 0 : continue;
176 : }
177 :
178 0 : if (!::comphelper::hasProperty(PROPERTY_NAME, xAsSet))
179 : // we need at least a name for displaying ...
180 0 : continue;
181 0 : sName = ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_NAME)).getStr();
182 :
183 : // we need to check if the control model supports the required service
184 0 : Reference< XServiceInfo > xInfo(xAsSet, UNO_QUERY);
185 0 : if (!xInfo.is())
186 0 : continue;
187 :
188 0 : if (!xInfo->supportsService(m_sRequiredService))
189 : { // perhaps it is a container
190 0 : Reference< XIndexAccess > xCont(xAsSet, UNO_QUERY);
191 0 : if (xCont.is() && xCont->getCount())
192 : { // yes -> step down
193 0 : Image aFormImage = m_aModelImages.GetImage( RID_SVXIMG_FORM );
194 0 : SvTreeListEntry* pCont = m_pControlTree->InsertEntry(sName, aFormImage, aFormImage, pContainerEntry);
195 0 : sal_Int32 nContChildren = InsertEntries(xCont, pCont);
196 0 : if (nContChildren)
197 : {
198 0 : m_pControlTree->Expand(pCont);
199 0 : ++nChildren;
200 : }
201 : else
202 : { // oops, no valid children -> remove the entry
203 0 : m_pControlTree->ModelIsRemoving(pCont);
204 0 : m_pControlTree->GetModel()->Remove(pCont);
205 0 : m_pControlTree->ModelHasRemoved(pCont);
206 0 : }
207 : }
208 0 : continue;
209 : }
210 :
211 : // get the label
212 0 : if (!::comphelper::hasProperty(PROPERTY_LABEL, xAsSet))
213 0 : continue;
214 :
215 : OUString sDisplayName = OUStringBuffer(
216 0 : ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_LABEL))).
217 0 : append(" (").append(sName).append(')').
218 0 : makeStringAndClear();
219 :
220 : // all requirements met -> insert
221 0 : SvTreeListEntry* pCurrent = m_pControlTree->InsertEntry(sDisplayName, m_aRequiredControlImage, m_aRequiredControlImage, pContainerEntry);
222 0 : pCurrent->SetUserData(new Reference< XPropertySet > (xAsSet));
223 0 : ++nChildren;
224 :
225 0 : if (m_xInitialLabelControl == xAsSet)
226 0 : m_pInitialSelection = pCurrent;
227 :
228 0 : m_bHaveAssignableControl = true;
229 0 : }
230 :
231 0 : return nChildren;
232 : }
233 :
234 :
235 0 : IMPL_LINK(OSelectLabelDialog, OnEntrySelected, SvTreeListBox*, pLB)
236 : {
237 : DBG_ASSERT(pLB == m_pControlTree, "OSelectLabelDialog::OnEntrySelected : where did this come from ?");
238 : (void)pLB;
239 0 : SvTreeListEntry* pSelected = m_pControlTree->FirstSelected();
240 0 : void* pData = pSelected ? pSelected->GetUserData() : NULL;
241 :
242 0 : if (pData)
243 0 : m_xSelectedControl = Reference< XPropertySet > (*(Reference< XPropertySet > *)pData);
244 :
245 0 : m_pNoAssignment->SetClickHdl(Link());
246 0 : m_pNoAssignment->Check(pData == NULL);
247 0 : m_pNoAssignment->SetClickHdl(LINK(this, OSelectLabelDialog, OnNoAssignmentClicked));
248 :
249 0 : return 0L;
250 : }
251 :
252 :
253 0 : IMPL_LINK(OSelectLabelDialog, OnNoAssignmentClicked, Button*, pButton)
254 : {
255 : DBG_ASSERT(pButton == m_pNoAssignment, "OSelectLabelDialog::OnNoAssignmentClicked : where did this come from ?");
256 : (void)pButton;
257 :
258 0 : if (m_pNoAssignment->IsChecked())
259 0 : m_pLastSelected = m_pControlTree->FirstSelected();
260 : else
261 : {
262 : DBG_ASSERT(m_bHaveAssignableControl, "OSelectLabelDialog::OnNoAssignmentClicked");
263 : // search the first assignable entry
264 0 : SvTreeListEntry* pSearch = m_pControlTree->First();
265 0 : while (pSearch)
266 : {
267 0 : if (pSearch->GetUserData())
268 0 : break;
269 0 : pSearch = m_pControlTree->Next(pSearch);
270 : }
271 : // and select it
272 0 : if (pSearch)
273 : {
274 0 : m_pControlTree->Select(pSearch);
275 0 : m_pLastSelected = pSearch;
276 : }
277 : }
278 :
279 0 : if (m_pLastSelected)
280 : {
281 0 : m_pControlTree->SetSelectHdl(Link());
282 0 : m_pControlTree->SetDeselectHdl(Link());
283 0 : m_pControlTree->Select(m_pLastSelected, !m_pNoAssignment->IsChecked());
284 0 : m_pControlTree->SetSelectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
285 0 : m_pControlTree->SetDeselectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
286 : }
287 :
288 0 : return 0L;
289 : }
290 :
291 :
292 12 : } // namespace pcr
293 :
294 :
295 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|