Overzicht van enkele centrale interfaces

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

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")

Benoemde toegang tot onderliggende objecten

De interfaces XNameAccess en XNameContainer worden gebruikt in objecten die onderliggende objecten bevatten, welke kunnen worden benaderd door een naam in een natuurlijke taal.

Terwijl XNameAccess toegang verleent tot de individuele objecten, neemt XNameContainer de invoeging, wijziging en verwijdering van elementen op zich.

Interface com.sun.star.container.XNameAccess

Een voorbeeld van het gebruik van XNameAccess wordt verschaft door het object sheets van een werkbladdocument. Het combineert alle bladen binnen het werkbladdocument. De individuele bladen worden benaderd vanuit het object Bladen met behulp van de methode getByName vanuit XNameAccess:

Dim Bladen As Object
Dim Blad As Object
 
Bladen = Spreadsheet.Sheets
Blad = Bladen.getByName("Blad1")

De methode getElementNames verschaft een overzicht van de namen van alle elementen. Als resultaat geeft het een gegevensveld weer waarin de namen staan. Het volgende voorbeeld toont hoe alle namen van elementen van een werkbladdocument daardoor kunnen worden bepaald en weergegeven in een lus:

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

De methode hasByName van de interface XNameAccess onthult of een onderliggend object met een bepaalde naam bestaat binnen het basis-object. Het volgende voorbeeld geeft daarom een bericht weer dat de gebruiker informeert of het object Spreadsheet een blad bevat met de naam Blad1.

Dim Bladen As Object
 
Bladen = Spreadsheet.Sheets
If Bladen.HasByName("Blad1") Then
  MsgBox "Blad1 beschikbaar"
Else
  MsgBox "Blad1 niet beschikbaar"
End If

Interface com.sun.star.container.XNameContainer

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