| View previous topic :: View next topic |
| Author |
Message |
Frank Zalabard Guest
|
Posted: Thu Dec 04, 2003 5:14 am Post subject: Need help with checkboxes and java |
|
|
I have a writer document that has fields, bookmarks and checkboxes in it.
I need to set the values for fields, insert some text at bookmarks and check/uncheck checkboxes, depending on data read from a DB.
All this is done in JAVA: I have successfully updated fields and inserted text at bookmark position, but I do not understand how to get hold on checkboxes to change the State:
have you any JAVA sample code that can access the checkbox ByName, starting from the XComponent writer document object that I have opened?
Thanks in advance, Frank |
|
| Back to top |
|
 |
fsarzana Newbie


Joined: 04 Dec 2003 Posts: 2
|
Posted: Fri Dec 05, 2003 5:01 am Post subject: NO ONE THERE?!?! |
|
|
Nobody can answer this SIMPLE question  |
|
| Back to top |
|
 |
hol.sten Super User


Joined: 14 Nov 2004 Posts: 3532 Location: Hamburg, Germany
|
Posted: Sat Dec 18, 2004 2:53 am Post subject: Re: NO ONE THERE?!?! |
|
|
| fsarzana wrote: | Nobody can answer this SIMPLE question  |
Although it's a year to late, now I can:
| Code: | import com.sun.star.beans.Property;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.XPropertySetInfo;
import com.sun.star.container.XNameAccess;
import com.sun.star.container.XNameContainer;
import com.sun.star.container.XNamed;
import com.sun.star.drawing.XDrawPage;
import com.sun.star.drawing.XDrawPageSupplier;
import com.sun.star.drawing.XDrawPages;
import com.sun.star.drawing.XDrawPagesSupplier;
import com.sun.star.form.XFormsSupplier;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.uno.UnoRuntime;
public class CheckBox
{
/**
* Get draw page of document to get the FormComponents of that document
*
* @param document A document
* @return The draw page
* @throws Exception The document contains no draw page
*/
private XDrawPage getDocumentDrawPage(XComponent document) throws Exception {
XDrawPage xDrawPage = null;
// Ask the XDrawPageSupplier to get the draw page of a text document
XDrawPageSupplier xSuppPage = (XDrawPageSupplier) UnoRuntime.queryInterface(XDrawPageSupplier.class, document);
xDrawPage = xSuppPage.getDrawPage();
if (xDrawPage == null)
{
// The document is not a text document
// Perhaps it's an Impress oder Calc document
XDrawPagesSupplier xSuppPages = (XDrawPagesSupplier) UnoRuntime.queryInterface(XDrawPagesSupplier.class, document);
if (xSuppPages != null && xSuppPages.getDrawPages() != null)
{
XDrawPages xPages = xSuppPages.getDrawPages();
xDrawPage = (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class, xPages.getByIndex(0));
}
}
return xDrawPage;
}
/**
* Get the FormComponents of a document.
*
* Through the FormComponents you can get access to checkboxes and a lot more
*
* @param document A document
* @return The names of the FormComponents
* @throws Exception The document contains no FormComponents
*/
private XNameContainer getFormComponentTreeRoot(XComponent document) throws Exception
{
XFormsSupplier xSuppForms = (XFormsSupplier)UnoRuntime.queryInterface(XFormsSupplier.class, getDocumentDrawPage(document));
XNameContainer xFormsCollection = null;
if (null != xSuppForms)
{
xFormsCollection = xSuppForms.getForms();
}
return xFormsCollection;
}
/**
* Get the checkbox of a document with a given name
*
* @param checkBoxName The name of the checkbox
* @param document The document with the checkbox
* @return The checkbox
* @throws Exception The document doesn't contains the given checkbox
*/
public Object getCheckBox(String checkBoxName, XComponent document) throws Exception
{
Object checkBox = null;
XFormsSupplier xSuppForms = (XFormsSupplier)UnoRuntime.queryInterface(XFormsSupplier.class, getDocumentDrawPage(document));
if (xSuppForms != null && xSuppForms.getForms() != null)
{
checkBox = getCheckBox(checkBoxName, xSuppForms.getForms());
}
return checkBox;
}
/**
* Get the checkbox of a FormComponent container with a given name
*
* @param checkBoxName The name of the checkbox
* @param formComponentContainer The FormComponent container
* @return The checkbox
* @throws Exception The document doesn't contains the given checkbox
*/
private Object getCheckBox(String checkBoxName, XNameAccess formComponentContainer) throws Exception
{
Object checkBox = null;
String formComponentNames[] = formComponentContainer.getElementNames();
int i = 0;
while (checkBox == null && i < formComponentNames.length)
{
// Compare names
if (formComponentNames[i].equals(checkBoxName))
{
// TODO: Check if it's really a checkbox
checkBox = formComponentContainer.getByName(checkBoxName);
}
if (checkBox == null)
{
// We haven't found the checkbox, so we continue our search
// Check if the component contains another FormComponent
XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class,formComponentContainer.getByName(formComponentNames[i]));
if (xServiceInfo.supportsService("com.sun.star.form.FormComponents"))
{
// The component contains another FormComponent, so we have to descend
XNameAccess xChildContainer = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, xServiceInfo);
checkBox = getCheckBox(checkBoxName, xChildContainer);
}
}
i++;
}
return checkBox;
}
/**
* List all FormComponents.
*
* @param formComponentContainer The FormComponent container
* @throws Exception The container contains no FormComponents
*/
private void enumFormComponents(XNameAccess formComponentContainer) throws java.lang.Exception
{
XNamed xNameAcc = (XNamed) UnoRuntime.queryInterface(XNamed.class, formComponentContainer);
if (xNameAcc != null)
{
String sObjectName = xNameAcc.getName();
System.out.println( new String("List container \"") + sObjectName + new String("\"\n"));
System.out.println(sObjectName);
enumFormComponents(formComponentContainer, " ");
}
}
/**
* List all FormComponents.
*
* @param formComponentContainer The FormComponent container
* @param prefix The prefix for indentations
* @throws Exception The container contains no FormComponents
*/
private void enumFormComponents(XNameAccess formComponentContainer, String prefix) throws Exception
{
String aNames[] = formComponentContainer.getElementNames();
for (int i=0; i<aNames.length; ++i)
{
System.out.println(prefix + aNames[i]);
XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class,formComponentContainer.getByName(aNames[i]));
if (xServiceInfo.supportsService("com.sun.star.form.FormComponents"))
{
XNameAccess xChildContainer = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, xServiceInfo);
enumFormComponents(xChildContainer, new String(" ") + prefix);
}
else
{
XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, formComponentContainer.getByName(aNames[i]));
if (xProps != null)
{
System.out.println(prefix + " Has properties");
XPropertySetInfo xPropSetInfo = xProps.getPropertySetInfo();
Property[] properties = xPropSetInfo.getProperties();
for (int j = 0; j < properties.length; j++)
{
Property property = properties[j];
if (property != null)
{
System.out.println(prefix + " "+property.Name+"("+property.Type+"): "+property.toString());
}
}
}
}
}
}
/**
* List all FormComponents of a given document.
*
* @param document A document
* @throws Exception The document contains no FormComponents
*/
public void printFormControls(XComponent document) throws Exception
{
XNameContainer formComponentTreeRoot = getFormComponentTreeRoot(document);
String[] formNames = formComponentTreeRoot.getElementNames();
System.out.println("FormComponents-Anzahl: "+formNames.length);
for (int i = 0; i < formNames.length; i++)
{
System.out.println(formNames[i]);
XNameAccess xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class,formComponentTreeRoot.getByName(formNames[i]));
enumFormComponents(xNameAccess);
}
}
/**
* Set checkbox of a given document.
*
* @param document A document
* @param checkBoxName The name of the checkbox
* @param checkBoxState The state of the checkbox (true = checked, false = unchecked)
* @throws Exception The document doesn't contains the given checkbox
*/
public void setCheckBox(XComponent document, String checkBoxName, boolean checkBoxState) throws Exception
{
Object checkBox = getCheckBox(checkBoxName, document);
if (checkBox != null)
{
// Get properties of checkbox
XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, checkBox);
if (xProps != null)
{
Short value = (Short) xProps.getPropertyValue("State");
if (value != null)
{
short newValue = new Integer(checkBoxState ? 1 : 0).shortValue();
xProps.setPropertyValue("State", new Short(newValue));
}
}
}
else
{
throw new Exception("checkbox "+checkBoxName+" unknown");
}
}
}
|
Hope that helps anybody.
To answer further questions read http://api.openoffice.org/docs/DevelopersGuide/Forms/Forms.htm. It's all in there.
With kind regards
hol.sten |
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
fsarzana Newbie


Joined: 04 Dec 2003 Posts: 2
|
Posted: Sat Dec 18, 2004 8:23 am Post subject: |
|
|
Thank you very much, in any case. I solved the problem in another way, in the mean time, but your neat solution is very much appreciated.
Frank |
|
| Back to top |
|
 |
hol.sten Super User


Joined: 14 Nov 2004 Posts: 3532 Location: Hamburg, Germany
|
Posted: Sat Dec 18, 2004 8:43 am Post subject: |
|
|
| SergeM wrote: | | Of course it will help somebody. |
Good to read that
| SergeM wrote: | | When you have working code like that, please put it in the Code Snippets Forum. |
I posted some code snippets, where I found unanswered questions in this forum. I hope that other people with the same problems that I had now will find the answer here.
| SergeM wrote: | I will read more deeply your code when i will takle this subject in C++.
|
As I already stated, most of the code is from the Developers Guide.
With kind regards
hol.sten |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|