Overzicht van enkele centrale interfaces

From Apache OpenOffice Wiki
< NL‎ | Documentation‎ | BASIC Guide
Revision as of 13:53, 28 January 2013 by DiGro (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Book.png


Sommige interfaces van Apache OpenOffice kunnen worden gevonden in vele delen van de Apache OpenOffice API. Zij definiëren reeksen van methoden voor abstracte taken welke kunnen worden toegepast voor verschillende problemen. Hier vindt u een overzicht van de meest voorkomende van deze interfaces.

De herkomst van de objecten wordt op een later punt in deze gids verklaard. Op dit punt worden slechts enkele van de abstracte aspecten van objecten, waarvoor de Apache OpenOffice API sommige centrale interfaces verschaft, besproken.

Context-afhankelijke objecten maken

De Apache OpenOffice API verschaft twee opties voor het creëren van objecten. Eén kan worden gevonden in de functie createUnoService, vermeld aan het begin van dit hoofdstuk. createUnoService maakt een object dat universeel kan worden gebruikt. Zulke objecten en services zijn ook bekend als context-onafhankelijke services.

In aanvulling op de context-onafhankelijke services, zijn er ook context-afhankelijke services waarvan de objecten alleen handig zijn in samenhang met een ander object. Een tekenobject voor een werkbladdocument kan daarom bijvoorbeeld alleen bestaan in samenhang met dat ene document.

Interface com.sun.star.lang.XMultiServiceFactory

Context-afhankelijke objecten worden normaal gesproken gemaakt door middel van een objectmethode, waar het object van afhankelijk is. De methode createInstance, welke wordt gedefinieerd in de interface XMultiServiceFactory, wordt speciaal gebruikt in de objecten document.

Het tekenobject kan bijvoorbeeld als volgt worden gemaakt door gebruik van een object werkbladdocument:

Dim RechthoekVorm As Object
RechthoekVorm = _
  Spreadsheet.createInstance("com.sun.star.drawing.RectangleShape")

Een sjabloon voor een alinea in een tekstdocument wordt op dezelfde manier gemaakt:

Dim Opmaakprofiel as Object
Opmaakprofiel = Textdocument.createInstance("com.sun.star.style.ParagraphStyle")

Named Access to Subordinate Objects

The XNameAccess and XNameContainer interfaces are used in objects that contain subordinate objects, which can be addressed using a natural language name.

While XNameAccess permits access to the individual objects, XNameContainer takes on the insertion, modification and deletion of elements.

com.sun.star.container.XNameAccess Interface

An example of the use of XNameAccess is provided by the sheets object of a spreadsheet. It combines all the pages within the spreadsheet. The individual pages are accessed from the sheets object, by using the getByName method from XNameAccess:

Dim Sheets As Object
Dim Sheet As Object
 
Sheets = Spreadsheet.Sheets
Sheet = Sheets.getByName("Sheet1")

The getElementNames method provides an overview of the names of all elements. As a result, it returns a data field containing the names. The following example shows how all element names of a spreadsheet can thereby be determined and displayed in a loop:

Dim Sheets As Object
Dim SheetNames
Dim I As Integer
 
Sheets = Spreadsheet.Sheets
SheetNames = Sheets.getElementNames
 
For I=LBound(SheetNames) To UBound(SheetNames)
  MsgBox SheetNames(I)
Next I

The hasByName method of the XNameAccess interface reveals whether a subordinate object with a particular name exists within the basic object. The following example therefore displays a message that informs the user whether the Spreadsheet object contains a page of the name Sheet1.

Dim Sheets As Object
 
Sheets = Spreadsheet.Sheets
If Sheets.HasByName("Sheet1") Then
  MsgBox " Sheet1 available"
Else
  MsgBox "Sheet1 not available"
End If

com.sun.star.container.XNameContainer Interface

The XNameContainer interface takes on the insertion, deletion and modification of subordinate elements in a basic object. The functions responsible are insertByName, removeByName and replaceByName.

The following is a practical example of this. It calls a text document, which contains a StyleFamilies object and uses this to in turn make the paragraph templates (ParagraphStyles) of the document available.

Dim StyleFamilies As Object
Dim ParagraphStyles As Object
Dim NewStyle As Object   
 
StyleFamilies = Textdoc.StyleFamilies
ParagraphStyles = StyleFamilies.getByName("ParagraphStyles")
ParagraphStyles.insertByName("NewStyle", NewStyle)      
ParagraphStyles.replaceByName("ChangingStyle", NewStyle)   
ParagraphStyles.removeByName("OldStyle")

The insertByName line inserts the NewStyle style under the name of the same name in the ParagraphStyles object. The replaceByName line changes the object behind ChangingStyle into NewStyle. Finally, the removeByName call removes the object behind OldStyle from ParagraphStyles.

Index-Based Access to Subordinate Objects

The XIndexAccess and XIndexContainer interfaces are used in objects which contain subordinate objects and which can be addressed using an index.

XIndexAccess provides the methods for accessing individual objects. XIndexContainer provides methods for inserting and removing elements.

com.sun.star.container.XIndexAccess Interface

XIndexAccess provides the getByIndex and getCount methods for calling the subordinate objects. getByIndex provides an object with a particular index. getCount returns how many objects are available.

Dim Sheets As Object
Dim Sheet As Object
Dim I As Integer
 
Sheets = Spreadsheet.Sheets
 
For I = 0 to Sheets.getCount() - 1
  Sheet = Sheets.getByIndex(I)
  ' Editing sheet
Next I

The example shows a loop that runs through all sheet elements one after another and saves a reference to each in the Sheet object variable. When working with the indexes, note that getCount returns the number of elements. The elements in getByIndex however are numbered beginning with 0. The counting variable of the loop therefore runs from 0 to getCount()-1.

com.sun.star.container.XIndexContainer Interface

The XIndexContainer interface provides the insertByIndex and removeByIndex functions. The parameters are structured in the same way as the corresponding functions in XNameContainer.

Iterative Access to Subordinate Objects

In some instances, an object may contain a list of subordinate objects that cannot be addressed by either a name or an index. In these situations, the XEnumeration and XenumerationAccess interfaces are appropriate. They provide a mechanism through which all subordinate elements of an objects can be passed, step by step, without having to use direct addressing.

com.sun.star.container.XEnumeration and XenumerationAccess Interfaces

The basic object must provide the XEnumerationAccess interface, which contains only a createEnumeration method. This returns an auxiliary object, which in turn provides the XEnumeration interface with the hasMoreElements and nextElement methods. Through these, you then have access to the subordinate objects.

The following example steps through all the paragraphs of a text:

Dim ParagraphEnumeration As Object
Dim Paragraph As Object
 
ParagraphEnumeration = Textdoc.Text.createEnumeration
 
While ParagraphEnumeration.hasMoreElements()
  Paragraph = ParagraphEnumeration.nextElement()
Wend

The example first creates a ParagraphEnumeration auxiliary object. This gradually returns the individual paragraphs of the text in a loop. The loop is terminated as soon as the hasMoreElements method returns the False value, signaling that the end of the text has been reached.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools