<?xml version="1.0"?> | |
<!-- | |
$RCSfile: Writer.AutomaticallyCreateAParagraphStyleNamedCode.snip,v $ | |
last change: $Revision: 1.1 $ $Author: tomsontom $ $Date: 2008/10/20 11:29:27 $ | |
(c)2003 by the copyright holders listed with the author-tags. | |
If no explicit copyright holder is mentioned with a certain author, | |
the author him-/herself is the copyright holder. All rights reserved. | |
Public Documentation License Notice: | |
The contents of this Documentation are subject to the | |
Public Documentation License Version 1.0 (the "License"); | |
you may only use this Documentation if you comply with | |
the terms of this License. A copy of the License is | |
available at http://www.openoffice.org/licenses/PDL.html | |
The Original Documentation can be found in the CVS archives | |
of openoffice.org at the place specified by RCSfile: in this header. | |
The Initial Writer(s) of the Original Documentation are listed | |
with the author-tags below. | |
The Contributor(s) are listed with the author-tags below | |
without the marker for being an initial author. | |
All Rights Reserved. | |
--> | |
<snippet language="ooRexx" application="Writer"> | |
<keywords> | |
<keyword>create</keyword> | |
<keyword>paragraph</keyword> | |
<keyword>style</keyword> | |
<keyword>code</keyword> | |
</keywords> | |
<authors> | |
<author id="1" initial="true" email="" copyright="Josef Frysak">Josef Frysak</author> | |
</authors> | |
<question heading="Automatically create a paragraph style named code">How to automatically create a paragraph style with name "code"? | |
</question> | |
<answer> | |
<p>First get a list of all paragraph styles of the current document.</p> | |
<p>("XStyleFamiliesSupplier"). Then search it for the name "code".</p> | |
<p>If no paragraph style named "code" exists, create a new one using the</p> | |
<p>documents "XMultiServiceFactory" interface. Next configurate the newly</p> | |
<p>created paragrah style object by changing its properties. Finally</p> | |
<p>add this object to the list of paragraph styles.</p> | |
<p>For further details see http://wi.wu-wien.ac.at/rgf/diplomarbeiten/BakkStuff/2008/200809_Frysak/200809_Frysak_Automating_OOo_ooRexx_Nutshells.pdf.</p> | |
<listing>-- try to get a script context, will be .nil, if script was not invoked by OOo | |
x_ScriptContext = uno.getScriptContext() | |
if (x_ScriptContext <> .nil) then | |
do | |
-- invoked by OOo as a macro | |
-- get context | |
x_ComponentContext = x_ScriptContext~getComponentContext | |
-- get desktop (an XDesktop) | |
x_Desktop = x_ScriptContext~getDesktop | |
-- get current document | |
x_Document = x_ScriptContext~getDocument | |
end | |
else | |
do | |
-- called from outside of OOo, create a connection | |
-- connect to Open Office and get component context | |
x_ComponentContext = UNO.connect() | |
-- create a desktop service and its interface | |
service = "{%see com.sun.star.frame.Desktop}" | |
s_Desktop = x_ComponentContext~getServiceManager~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}~createInstance(service) | |
x_Desktop = s_Desktop~{%see com.sun.star.frame.XDesktop%XDesktop} | |
-- get the last active document | |
x_Document = x_Desktop~getCurrentComponent() | |
end | |
-- first we search all paragraph styles if there is allready one | |
-- with name "code" | |
x_StyleFamiliesSupplier = x_Document~{%see com.sun.star.style.XStyleFamiliesSupplier%XStyleFamiliesSupplier} | |
x_StyleFamilies = x_StyleFamiliesSupplier~getStyleFamilies() | |
s_StyleFamily = x_StyleFamilies~getByName("ParagraphStyles") | |
x_NameAccess = s_StyleFamily~{%see com.sun.star.container.XNameAccess%XNameAccess} | |
if x_NameAccess~hasByName("code") then | |
do | |
-- if "code" allready exists show error message | |
.bsf.dialog~messageBox('PageStyle "code" allready exists!', "ERROR", "error") | |
end | |
else | |
do | |
-- if "code" does not exist: | |
-- create a factory to create a new paragraph style object | |
x_MultiServiceFactory = x_Document~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory} | |
s_ParagraphStyle = x_MultiServiceFactory~createInstance("{%see com.sun.star.style.ParagraphStyle}") | |
-- now set its properties | |
s_ParagraphProperties = s_ParagraphStyle~{%see com.sun.star.beans.XPropertySet%XPropertySet} | |
-- WARNING! Java.Integer = UNO_LONG !!!!!!! | |
-- its background color | |
paracolor = .bsf~new("java.lang.Integer", X2D("FFFFCC")) | |
s_ParagraphProperties~setPropertyValue("ParaBackColor", paracolor) | |
-- left and right margin | |
pramargin = .bsf~new("java.lang.Integer", 500) | |
s_ParagraphProperties~setPropertyValue("ParaLeftMargin", pramargin) | |
s_ParagraphProperties~setPropertyValue("ParaRightMargin", pramargin) | |
-- left, right, top and bottom border | |
o_Border = .bsf~new("{%see com.sun.star.table.BorderLine}") | |
o_Border~bsf.setFieldValue("Color", 0) | |
o_Border~bsf.setFieldValue("InnerLineWidth", 0) | |
o_Border~bsf.setFieldValue("OuterLineWidth", 1) | |
o_Border~bsf.setFieldValue("LineDistance", 0) | |
s_ParagraphProperties~setPropertyValue("LeftBorder", o_Border) | |
s_ParagraphProperties~setPropertyValue("RightBorder", o_Border) | |
s_ParagraphProperties~setPropertyValue("TopBorder", o_Border) | |
s_ParagraphProperties~setPropertyValue("BottomBorder", o_Border) | |
borderdist = .bsf~new("java.lang.Integer", 50) | |
s_ParagraphProperties~setPropertyValue("LeftBorderDistance", borderdist) | |
s_ParagraphProperties~setPropertyValue("RightBorderDistance", borderdist) | |
s_ParagraphProperties~setPropertyValue("TopBorderDistance", borderdist) | |
s_ParagraphProperties~setPropertyValue("BottomBorderDistance", borderdist) | |
-- and the fontname | |
s_ParagraphProperties~setPropertyValue("CharFontName", "Arial") | |
-- finally insert the new paragraph style to the list of | |
-- paragraph styles of this document | |
x_NameAccess~insertByName("code", s_ParagraphStyle) | |
end | |
::requires UNO.CLS</listing> | |
</answer> | |
<versions> | |
<version number="2.4.1" status="tested"/> | |
</versions> | |
<operating-systems> | |
<operating-system name="All"/> | |
</operating-systems> | |
<changelog> | |
<change author-id="1" date="2008-10-14">Initial version</change> | |
</changelog> | |
</snippet> |