Hi Juan,
Juan Emilio Gabito escribió:
Hi all.
I have some user textfileds that I added to a document using something like this
XDependentTextField xUserField = (XDependentTextField)UnoRuntime.queryInterface(
XDependentTextField.class,
mxDocFactory.createInstance("com.sun.star.text.TextField.User"));
XPropertySet xMasterPropSet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,
mxDocFactory.createInstance("com.sun.star.text.FieldMaster.User"));
xMasterPropSet.setPropertyValue ( "Name", "SGT_Com_" + compoNom);
xUserField.attachTextFieldMaster ( xMasterPropSet );
mxDocText.insertTextContent(cursorDocumento, xUserField, false);
Then I removed that user field from the visible document, and if I press ctrl+F2 I can see the field is there.
Up to here it's everything ok
The thing is that later in time I want to add that field again to the same document, but I can't use the lines above, cause they gave me an IllegalArgumentException, because that field already exists.
So, I would like to know how can I add the field again to the document.
Thanks
BEFORE creating a field master, you should use
XTextFieldsSupplier::getTextFieldMasters(); it returns an XNameAccess,
so if the collection of TextFieldMaster instances defined in the
document has *already* a text field master with that name
(== XNameAccess::hasByName(TextFieldMaster_NAME) returns true), then you
GET it by name; if *not*, you can instantiate it, that is create a new one.
Remember that you must use the fully qualified name, for example:
"com.sun.star.text.FieldMaster.SetExpression.OOoMathFórmula" is the
fully qualified name for a field master, type SetExpression, name
"OOoMathFórmula" (with the accent included!!!!).
Bellow you can find a complete example, it supposes that the current
component in the desktop is a text document of course (and that you can
bootstrap a running office!). The first time you run it, it will create
the field master; the next time you run it on the same doc., it will get
it by name.
Hope it helps!
Bye,
Ariel
//********************************************************************************
package ar.com.arielconstenlahaile.test.embed;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.frame.XDesktop;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XText;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextRange;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextEmbeddedObjectsSupplier;
import com.sun.star.text.ControlCharacter;
import com.sun.star.text.SetVariableType;
import com.sun.star.text.TextContentAnchorType;
import com.sun.star.text.XDependentTextField;
import com.sun.star.text.XTextFieldsSupplier;
import com.sun.star.style.NumberingType;
import com.sun.star.document.XEmbeddedObjectSupplier;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XNamed;
import com.sun.star.container.XNameAccess;
import com.sun.star.container.XEnumerationAccess;
import com.sun.star.util.XRefreshable;
/**
*
* @author ArielConstenlaHaile
*/
public class TextFieldMasterTest {
private XComponentContext xContext;
private XTextDocument xTextDocument;
public TextFieldMasterTest(XTextDocument xTextDocument)
throws com.sun.star.uno.Exception {
this.xContext = xContext;
this.xTextDocument = xTextDocument;
XText xText = xTextDocument.getText();
XTextCursor xTextCursor =
xText.createTextCursorByRange(xText.getEnd());
xText.insertControlCharacter(
xTextCursor, ControlCharacter.PARAGRAPH_BREAK,
Boolean.FALSE);
xText.insertString(xTextCursor, "F\u00F3rmula N\u00B0 ",
Boolean.FALSE);
String fieldName = "OOoMathF\u00F3rmula";
XPropertySet xFieldMaster = createMasterSetExpression(
xTextDocument, fieldName, (byte) -1, "",
SetVariableType.SEQUENCE);
XDependentTextField xSetExpressionField = createSetExpression(
xTextDocument, xFieldMaster, fieldName + "+1",
SetVariableType.SEQUENCE, 4, NumberingType.ARABIC);
xText.insertTextContent(xTextCursor, xSetExpressionField,
Boolean.FALSE);
xText.insertString( xTextCursor, " ", Boolean.FALSE );
XTextEmbeddedObjectsSupplier xTextEmbeddedObjectsSupplier =
(XTextEmbeddedObjectsSupplier)
UnoRuntime.queryInterface(
XTextEmbeddedObjectsSupplier.class, xTextDocument);
XNameAccess xTextEmbeddedObjects =
xTextEmbeddedObjectsSupplier.getEmbeddedObjects();
insertMathFormula(
xTextDocument, xTextCursor.getEnd(),
"{ { 6 over 7 times 8 } + 5 } over 10",
createUniqueNameForContainerElement(xTextEmbeddedObjects,"OOoMathFormula"));
XTextFieldsSupplier xTextFieldsSupplier = (XTextFieldsSupplier)
UnoRuntime.queryInterface(XTextFieldsSupplier.class,
xTextDocument);
XEnumerationAccess xFieldEnumAccess =
xTextFieldsSupplier.getTextFields();
XRefreshable xRefreshable =
(XRefreshable)UnoRuntime.queryInterface(
XRefreshable.class, xFieldEnumAccess);
xRefreshable.refresh();
}
private XPropertySet createMasterSetExpression(
XTextDocument
xTextDocument,
String fieldName,
byte chapNumLevel,
String numSeparator,
short subType)
{
XPropertySet xFieldMaster = null;
String serviceName = "com.sun.star.text.FieldMaster.SetExpression";
String fieldMasterName = serviceName + "." + fieldName;
try {
XTextFieldsSupplier xTextFieldsSupplier = (XTextFieldsSupplier)
UnoRuntime.queryInterface(
XTextFieldsSupplier.class, xTextDocument);
XNameAccess xFieldMasterAccess =
xTextFieldsSupplier.getTextFieldMasters();
if ( xFieldMasterAccess.hasByName( fieldMasterName ) ){
xFieldMaster = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class,
xFieldMasterAccess.getByName(fieldMasterName));
} else {
XMultiServiceFactory xDocFactory = (XMultiServiceFactory)
UnoRuntime.queryInterface(
XMultiServiceFactory.class, xTextDocument);
xFieldMaster = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class,
xDocFactory.createInstance( serviceName));
if ( xFieldMaster != null ){
xFieldMaster.setPropertyValue("Name", fieldName);
xFieldMaster.setPropertyValue("ChapterNumberingLevel", chapNumLevel);
xFieldMaster.setPropertyValue("NumberingSeparator",
numSeparator);
xFieldMaster.setPropertyValue("SubType", subType);
}
}
} catch (Exception ex){
ex.printStackTrace();
} finally {
return xFieldMaster;
}
}
private XDependentTextField createSetExpression(
XTextDocument xTextDocument,
XPropertySet xFieldMaster,
String content,
short subType,
int numberFormat,
short numberingType)
{
Object textfield = null;
XDependentTextField xDependentTextField = null;
try {
XMultiServiceFactory xDocFactory = (XMultiServiceFactory)
UnoRuntime.queryInterface(
XMultiServiceFactory.class, xTextDocument);
textfield = xDocFactory.createInstance(
"com.sun.star.text.TextField.SetExpression");
if ( textfield != null ){
XPropertySet xSetExpression = (XPropertySet)
UnoRuntime.queryInterface(
XPropertySet.class, textfield);
xSetExpression.setPropertyValue("Content", content);
xSetExpression.setPropertyValue("SubType", subType);
xSetExpression.setPropertyValue("NumberFormat",
numberFormat);
xSetExpression.setPropertyValue("NumberingType",
numberingType);
xDependentTextField = (XDependentTextField)
UnoRuntime.queryInterface(
XDependentTextField.class, textfield);
xDependentTextField.attachTextFieldMaster(xFieldMaster);
}
} catch (com.sun.star.uno.Exception ex) {
ex.printStackTrace();
} finally {
return xDependentTextField;
}
}
private String createUniqueNameForContainerElement(
XNameAccess xElementContainer, String
elementName) {
boolean bHasByName = true;
int i = 1;
String uniqueName = elementName;
while (bHasByName) {
bHasByName = xElementContainer.hasByName(elementName);
if (bHasByName) {
elementName = uniqueName + Integer.toString(i++);
}
}
return elementName;
}
private void insertMathFormula(
XTextDocument xTextDocument,
XTextRange xTextRange,
String formula, String formulaName
) throws com.sun.star.uno.Exception
{
XMultiServiceFactory xDocFactory = (XMultiServiceFactory)
UnoRuntime.queryInterface(XMultiServiceFactory.class,
xTextDocument);
XTextContent xTextContent = (XTextContent)
UnoRuntime.queryInterface(
XTextContent.class,
xDocFactory.createInstance(
"com.sun.star.text.TextEmbeddedObject" ));
XPropertySet xPropertySet = (XPropertySet)
UnoRuntime.queryInterface(
XPropertySet.class, xTextContent);
xPropertySet.setPropertyValue(
"CLSID",
"078B7ABA-54FC-457F-8551-6147e776a997" );
xPropertySet.setPropertyValue("AnchorType",
TextContentAnchorType.AS_CHARACTER_value);
XNamed xnamed = (XNamed) UnoRuntime.queryInterface(
XNamed.class, xTextContent);
xnamed.setName(formulaName);
xTextDocument.getText().insertTextContent( xTextRange,
xTextContent, false );
XEmbeddedObjectSupplier xEmbeddedObjectSupplier =
(XEmbeddedObjectSupplier) UnoRuntime.queryInterface(
XEmbeddedObjectSupplier.class, xTextContent);
XComponent xEmbeddedObjectModel =
xEmbeddedObjectSupplier.getEmbeddedObject();
XPropertySet xFormulaProperties = (XPropertySet)
UnoRuntime.queryInterface(
XPropertySet.class, xEmbeddedObjectModel);
xFormulaProperties.setPropertyValue( "Formula", formula );
}
public static void main(String[] args) {
try {
XComponentContext xCtxt = Bootstrap.bootstrap();
XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
XDesktop.class,
xCtxt.getServiceManager().createInstanceWithContext(
"com.sun.star.frame.Desktop", xCtxt));
XComponent xCurrentComponent = xDesktop.getCurrentComponent();
if (xCurrentComponent != null){
XServiceInfo xServiceInfo = (XServiceInfo)
UnoRuntime.queryInterface(
XServiceInfo.class, xCurrentComponent);
if (xServiceInfo != null ) {
if (xServiceInfo.supportsService(
"com.sun.star.text.TextDocument")){
TextFieldMasterTest test = new TextFieldMasterTest(
(XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class,
xCurrentComponent));
}
}
}
} catch (java.lang.Exception e){
e.printStackTrace();
} finally {
System.exit( 0 );
}
}
}
//********************************************************************************
--
Ariel Constenla-Haile
La Plata, Argentina
[EMAIL PROTECTED]
[EMAIL PROTECTED]
http://www.arielconstenlahaile.com.ar/ooo/
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]