1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
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"
33 #endif
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>
44 
45 //............................................................................
46 namespace pcr
47 {
48 //............................................................................
49 
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;
56 
57 	//========================================================================
58 	// OSelectLabelDialog
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)
75 	{
76 		DBG_CTOR(OSelectLabelDialog,NULL);
77 
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);
83 
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));
87 
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));
93 
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);
98 
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())
104 		{
105 			xCont = Reference< XChild > (xSearch, UNO_QUERY);
106 			xSearch = xCont.is() ? xCont->getParent() : Reference< XInterface > ();
107 			xParentAsResultSet = Reference< XResultSet > (xSearch, UNO_QUERY);
108 		}
109 
110 		// and insert all entries below this root into the listbox
111 		if (xSearch.is())
112 		{
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);
118 
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(),
122 
123 				"OSelectLabelDialog::OSelectLabelDialog : invalid ControlLabel property !");
124 			if (aCurrentLabelControl.hasValue())
125 				aCurrentLabelControl >>= m_xInitialLabelControl;
126 
127 			// insert the root
128 			Image aRootImage = m_aModelImages.GetImage(RID_SVXIMG_FORMS);
129 			SvLBoxEntry* pRoot = m_aControlTree.InsertEntry(PcrRes(RID_STR_FORMS), aRootImage, aRootImage);
130 
131 			// build the tree
132 			m_pInitialSelection = NULL;
133 			m_bHaveAssignableControl = sal_False;
134 			InsertEntries(xSearch, pRoot);
135 			m_aControlTree.Expand(pRoot);
136 		}
137 
138 		if (m_pInitialSelection)
139 		{
140 			m_aControlTree.MakeVisible(m_pInitialSelection, sal_True);
141 			m_aControlTree.Select(m_pInitialSelection, sal_True);
142 		}
143 		else
144 		{
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);
149 		}
150 
151 		if (!m_bHaveAssignableControl)
152 		{	// no controls which can be assigned
153 			m_aNoAssignment.Check(sal_True);
154 			m_aNoAssignment.Enable(sal_False);
155 		}
156 
157 		m_aNoAssignment.SetClickHdl(LINK(this, OSelectLabelDialog, OnNoAssignmentClicked));
158 		m_aNoAssignment.GetClickHdl().Call(&m_aNoAssignment);
159 
160 		FreeResource();
161 	}
162 
163 	//------------------------------------------------------------------------
164 	OSelectLabelDialog::~OSelectLabelDialog()
165 	{
166 		// delete the entry datas of the listbox entries
167 		SvLBoxEntry* pLoop = m_aControlTree.First();
168 		while (pLoop)
169 		{
170 			void* pData = pLoop->GetUserData();
171 			if (pData)
172 				delete (Reference< XPropertySet > *)pData;
173 			pLoop = m_aControlTree.Next(pLoop);
174 		}
175 
176 		DBG_DTOR(OSelectLabelDialog,NULL);
177 	}
178 
179 	//------------------------------------------------------------------------
180 	sal_Int32 OSelectLabelDialog::InsertEntries(const Reference< XInterface > & _xContainer, SvLBoxEntry* pContainerEntry)
181 	{
182 		Reference< XIndexAccess >  xContainer(_xContainer, UNO_QUERY);
183 		if (!xContainer.is())
184 			return 0;
185 
186 		sal_Int32 nChildren = 0;
187 		UniString sName,sDisplayName;
188 		Reference< XPropertySet >  xAsSet;
189 		for (sal_Int32 i=0; i<xContainer->getCount(); ++i)
190 		{
191 			xContainer->getByIndex(i) >>= xAsSet;
192 			if (!xAsSet.is())
193 			{
194 				DBG_WARNING("OSelectLabelDialog::InsertEntries : strange : a form component which isn't a property set !");
195 				continue;
196 			}
197 
198 			if (!::comphelper::hasProperty(PROPERTY_NAME, xAsSet))
199 				// we need at least a name for displaying ...
200 				continue;
201 			sName = ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_NAME)).getStr();
202 
203 			// we need to check if the control model supports the required service
204 			Reference< XServiceInfo >  xInfo(xAsSet, UNO_QUERY);
205 			if (!xInfo.is())
206 				continue;
207 
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);
216 					if (nContChildren)
217 					{
218 						m_aControlTree.Expand(pCont);
219 						++nChildren;
220 					}
221 					else
222 					{	// oops, no valid childs -> remove the entry
223 						m_aControlTree.ModelIsRemoving(pCont);
224 						m_aControlTree.GetModel()->Remove(pCont);
225 						m_aControlTree.ModelHasRemoved(pCont);
226 					}
227 				}
228 				continue;
229 			}
230 
231 			// get the label
232 			if (!::comphelper::hasProperty(PROPERTY_LABEL, xAsSet))
233 				continue;
234 			sDisplayName = ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_LABEL)).getStr();
235 			sDisplayName += String::CreateFromAscii(" (");
236 			sDisplayName += sName;
237 			sDisplayName += ')';
238 
239 			// all requirements met -> insert
240 			SvLBoxEntry* pCurrent = m_aControlTree.InsertEntry(sDisplayName, m_aRequiredControlImage, m_aRequiredControlImage, pContainerEntry);
241 			pCurrent->SetUserData(new Reference< XPropertySet > (xAsSet));
242 			++nChildren;
243 
244 			if (m_xInitialLabelControl == xAsSet)
245 				m_pInitialSelection = pCurrent;
246 
247 			m_bHaveAssignableControl = sal_True;
248 		}
249 
250 		return nChildren;
251 	}
252 
253 	//------------------------------------------------------------------------
254 	IMPL_LINK(OSelectLabelDialog, OnEntrySelected, SvTreeListBox*, pLB)
255 	{
256 		DBG_ASSERT(pLB == &m_aControlTree, "OSelectLabelDialog::OnEntrySelected : where did this come from ?");
257         (void)pLB;
258 		SvLBoxEntry* pSelected = m_aControlTree.FirstSelected();
259 		void* pData = pSelected ? pSelected->GetUserData() : NULL;
260 
261 		if (pData)
262 			m_xSelectedControl = Reference< XPropertySet > (*(Reference< XPropertySet > *)pData);
263 
264 		m_aNoAssignment.SetClickHdl(Link());
265 		m_aNoAssignment.Check(pData == NULL);
266 		m_aNoAssignment.SetClickHdl(LINK(this, OSelectLabelDialog, OnNoAssignmentClicked));
267 
268 		return 0L;
269 	}
270 
271 	//------------------------------------------------------------------------
272 	IMPL_LINK(OSelectLabelDialog, OnNoAssignmentClicked, Button*, pButton)
273 	{
274 		DBG_ASSERT(pButton == &m_aNoAssignment, "OSelectLabelDialog::OnNoAssignmentClicked : where did this come from ?");
275         (void)pButton;
276 
277 		if (m_aNoAssignment.IsChecked())
278 			m_pLastSelected = m_aControlTree.FirstSelected();
279 		else
280 		{
281 			DBG_ASSERT(m_bHaveAssignableControl, "OSelectLabelDialog::OnNoAssignmentClicked");
282 			// search the first assignable entry
283 			SvLBoxEntry* pSearch = m_aControlTree.First();
284 			while (pSearch)
285 			{
286 				if (pSearch->GetUserData())
287 					break;
288 				pSearch = m_aControlTree.Next(pSearch);
289 			}
290 			// and select it
291 			if (pSearch)
292 			{
293 				m_aControlTree.Select(pSearch);
294 				m_pLastSelected = pSearch;
295 			}
296 		}
297 
298 		if (m_pLastSelected)
299 		{
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));
305 		}
306 
307 		return 0L;
308 	}
309 
310 //............................................................................
311 }	// namespace pcr
312 //............................................................................
313 
314