OO-Snippets: Hiding Userfield

Commons

Keywordshide UserField
LanguageJava
ApplicationWriter
AuthorsTom Schindl (initial)
Fabricio Lemos (initial)
Stephan Wunderlich (initial)
Supported Versions1.1.x  2.0.x  
Supported OSAll  
QuestionHow can I hide a UserField

I'm trying to hide a UserField programmatically but could not find a property to set

Answer

You to set the IsVisible-property of the field to False

Code-Snippet-Listing (snippet-source)

import com.sun.star.beans.PropertyVetoException;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.NoSuchElementException;
import com.sun.star.container.XEnumeration;
import com.sun.star.container.XEnumerationAccess;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.lang.XComponent;
import com.sun.star.text.XDependentTextField;
import com.sun.star.text.XTextFieldsSupplier;
import com.sun.star.uno.UnoRuntime;

public class HideFieldSnippet {

	public void hideField(XComponent component, String fieldName ) throws NoSuchElementException, WrappedTargetException, UnknownPropertyException, PropertyVetoException, IllegalArgumentException {
		// Get Access to the TextFields in the document
		XTextFieldsSupplier xTextFieldsSupplier = (XTextFieldsSupplier)UnoRuntime.queryInterface(XTextFieldsSupplier.class, component);
		XEnumerationAccess xEnumeratedFields = xTextFieldsSupplier.getTextFields();
		XEnumeration enumeration = xEnumeratedFields.createEnumeration();

		boolean changed = false;
		// Loop through the TextFields and search for the right field
		while (enumeration.hasMoreElements() && !changed) {
		    Object field = enumeration.nextElement();
		    XDependentTextField dependentTextField = (XDependentTextField)UnoRuntime.queryInterface(XDependentTextField.class, field);
		    XPropertySet propertySet = dependentTextField.getTextFieldMaster();
		    String name = (String) propertySet.getPropertyValue("Name");
		    if (name.equals(fieldName)) {
		        XPropertySet fieldProperties = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field);
		        fieldProperties.setPropertyValue("IsVisible", Boolean.FALSE);
		        changed = true;
		    }
		}
	}

}

Changelog

DateUserModification
2005-09-13tomsonInitial version

and