home *** CD-ROM | disk | FTP | other *** search
/ Kompuutteri Kaikille K-CD 2002 #3 / K-CD_2002-03.iso / OpenOffice / f_0018 / FormWizard.xba < prev    next >
Extensible Markup Language  |  2001-10-12  |  12KB  |  318 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="FormWizard" script:language="StarBasic">Option Explicit
  4.  
  5. ' Todo: Gruppieren der DBControls mit den Textfeldern einbinden
  6. ' Evtl k├╢nnte dies auch am Ende geschehen
  7. Public DocumentName as String
  8. Public FormPath$, FormDBName$, FormReturnValue$
  9. Public TemplatePath$
  10. Public WizardPath as String
  11. Public WebWizardPath as String
  12. Public WorkPath as String
  13. Public TexturePath as String
  14. Public sQueryName as String
  15. Public NumberofStyles as Integer
  16. Public oDBConnection as Object
  17. Public bWithBackGraphic as Boolean
  18.  
  19.  
  20. Public bNeedFieldRefresh as Boolean
  21. Public oDBForm as Object
  22. Public oColumns() as Object
  23. Public sDatabaseList()
  24. Public TableNames() as String
  25. Public QueryNames() as String
  26. Public FieldNames() as String
  27. Public ImgFieldNames() as String
  28. Public oDBContext as Object
  29. Public oUcb as Object
  30. Public oDocInfo as Object
  31. Public WidthList(15,3)
  32. Public ImgWidthList(3,3)
  33. Public sDBName as String
  34. Public Tablename as String
  35. Public Const SBSIZETEXT = "The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog."
  36. Public bDisposeDoc as Boolean
  37. ' Todo: Mit FS abschnacken, dass als CommandType Nur Queries und Tables zugelassen sind. Dabei m├╝sste noch abgkl├ñrt werden
  38. ' wann ein Content ein 'Command' ist.
  39.  
  40.  
  41. ' The macro can be called in 4 possible scenarios:
  42. ' Scenario 1. No parameters at given
  43. ' Scenario 2: Only Datasourcename is given, but no connection and no Content
  44. ' Scenario 3: a data source and a connection are given
  45. ' Scenario 4: all parameters (data source name, connection, object type and object) are given
  46.  
  47. Sub Main()
  48. Dim oLocDBContext as Object
  49. Dim oLocConnection as Object
  50.  
  51. ' Scenario 1. No parameters at given
  52.     MainWithDefault()
  53.  
  54. ' Scenario 2: Only Datasourcename is given, but no connection and no Content
  55. '    MainWithDefault("Bibliography")
  56.  
  57. ' Scenario 3: a data source and a connection are given
  58. '    oLocDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  59. '    oLocConnection = oLocDBContext.GetByName("Bibliography").GetConnection("","")
  60. '    MainWithDefault("Bibliography", oLocConnection)
  61.  
  62. ' Scenario 4: all parameters (data source name, connection, object type and object) are given
  63. '    oLocDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  64. '    oLocConnection = oLocDBContext.GetByName("Bibliography").GetConnection("","")
  65. '    MainWithDefault("Bibliography", oLocConnection, com.sun.star.sdb.CommandType.TABLE, "biblio")
  66. End Sub
  67.  
  68.  
  69. Sub MainWithDefault(Optional DatasourceName as String, Optional oConnection as Object, Optional CommandType as Integer, Optional sContent as String)
  70. Dim i as Integer
  71. Dim SelCount as Integer
  72. 'On Local Error Goto GlobalError
  73.     BasicLibraries.LoadLibrary("Tools")
  74.     BasicLibraries.LoadLibrary("WebWizard")
  75. ' Todo: Variable is initialized due to Bug#88329
  76.     CurArrangement = 0
  77.     bControlsareCreated = False
  78.     bDisposeDoc = True
  79.     MaxIndex = -1
  80.     If Not InitResources("Formwizard","dbw") Then
  81.         Exit Sub
  82.     End If
  83.     oDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  84.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  85.     FormPath = GetOfficeSubPath("Template","wizard/bitmap")
  86.     WebWizardPath = GetOfficeSubPath("Template","wizard/web")
  87.     WizardPath = GetOfficeSubPath("Template","wizard/")
  88.     TexturePath = GetOfficeSubPath("Gallery", "www-back/")
  89.     WorkPath = GetPathSettings("Work")
  90.     OpenFormDocument()
  91.     GetDatabaseNames()
  92.     InitializeWidthList()
  93.     LoadLanguage
  94.     bNeedFieldRefresh = True
  95.  
  96.     With oDialogModel
  97. '        .optIgnoreBinaries.State = True
  98.         .cmdBack.Enabled = False
  99.         .cmdGoOn.Enabled = False
  100.         .lblTables.Enabled = False
  101.         .lstSelFields.Tag = False
  102.         .Step = 1
  103.         .lstDatabases.StringItemList()= sDatabaseList()' = AddItem(sDatabaseList(i)
  104.     End With
  105.     If Not IsMissing(DataSourceName) Then
  106.         sDBName = DataSourceName
  107.         DlgFormDB.GetControl("lstDatabases").SelectItem(DataSourceName, True)
  108.         If Not IsMissing(oConnection) Then
  109.             ' Scenario 3: a data source and a connection are given
  110.             Set oDBConnection = oConnection
  111.             oDataSource = oDBContext.GetByName(DataSourceName)
  112.             oDialogModel.lstTables.Enabled = True
  113.             oDialogModel.lblTables.Enabled = True
  114.             If GetDBMetaData() Then
  115.                 oDialogModel.lstTables.StringItemList() = AddListToList(TableNames(), QueryNames())
  116.                 iCommandTypes = CreateCommandTypeList()
  117.                 If Not IsMissing(sContent) Then
  118.                     ' Scenario 4: all parameters (data source name, connection, object type and object) are given
  119.                     iCommandTypes() = CreateCommandTypeList()
  120.                     SelCount = CountItemsInArray(oDialogModel.lstTables.StringItemList(), sContent)
  121.                     If SelCount = 1 Then
  122.                         DlgFormDB.GetControl("lstTables").SelectItem(sContent, True)
  123.                     Else         ' Note: Here is no Error Handling in case that no valid content is transferred
  124.                         If CommandType = com.sun.star.sdb.CommandType.QUERY Then
  125.                             SelIndex = IndexInArray(sContent, QueryNames()
  126.                             DlgFormDB.GetControl("lstTables").SelectItemPos(SelIndex, True)
  127.                         ElseIf CommandType = com.sun.star.sdb.CommandType.TABLE Then
  128.                             SelIndex = IndexInArray(sContent, TableNames()
  129.                             DlgFormDB.GetControl("lstTables").SelectItemPos(Ubound(QueryNames()+1 + SelIndex, True)            
  130.                         End If
  131.                     End If
  132.                     CurCommandType = CommandType
  133.                     FillUpFieldsListbox(False)
  134.                 End If
  135.             End If
  136.         Else
  137.             ' Scenario 2: Only Datasourcename is given, but no connection and no Content
  138.             GetSelectedDBMetaData()
  139.         End If
  140.     Else
  141.         ' Scenario 1: No parameters are given
  142.         ToggleListboxControls(oDialogModel, False)
  143.     End If
  144.  
  145.     DlgFormDB.Title = WizardTitle(1)
  146.     NumberofStyles = FillupWebListbox(oUcb, "/stl", DlgFormDB, "lstStyles", Styles())
  147.     ImportStyles()
  148.     ToggleWindow(True)
  149.     oDialogModel.imgTheme.ImageURL = FormPath & "FormWizard_1.bmp"
  150.     oDialogModel.cmdBack.Enabled = False
  151.     oDialogModel.cmdGoOn.Enabled = False
  152.  
  153.     DlgFormDB.Execute()
  154.     DlgFormDB.Dispose
  155.     If Not IsNull(oDBConnection) Then
  156.         oDBConnection.Dispose()
  157.     End If
  158. '    If bDisposeDoc Then
  159. '        oDocument.Dispose
  160. '    End If
  161.     Exit Sub
  162. GlobalError:
  163.     MsgBox(sMsgErrMsg , 16, sMsgWizardName)
  164.     ToggleWindow(True)
  165.     DlgFormDB.EndExecute()
  166. End Sub
  167.  
  168.  
  169. Sub FormGetFields()
  170. Dim i as Integer
  171.     ToggleDatabasePage(False)
  172.     FillUpFieldsListbox(True)
  173.     ToggleDatabasePage(True)
  174. End Sub
  175.  
  176.  
  177. Sub FillUpFieldsListbox(bGetCommandType as Boolean)
  178. Dim n as Integer
  179. Dim SelIndex as Integer
  180. Dim QueryIndex as Integer
  181.     n = Ubound(oDialogModel.lstTables.SelectedItems())
  182.     If n > -1 Then
  183.         SelIndex = oDialogModel.lstTables.SelectedItems(0)
  184.         If bGetCommandType Then
  185.             CurCommandType = iCommandTypes(SelIndex)
  186.         End If
  187.         If CurCommandType = com.sun.star.sdb.CommandType.QUERY Then
  188.             QueryIndex = SelIndex - Ubound(Tablenames()) - 1
  189.             Tablename = QueryNames(QueryIndex)
  190.             oColumns = oDBConnection.Queries.GetByName(TableName).Columns
  191.         Else
  192.             Tablename = Tablenames(SelIndex)
  193.             oColumns = oDBConnection.Tables.GetByName(Tablename).Columns
  194.         End If
  195.         GetSpecificFieldNames()
  196.         ToggleListboxControls(oDialogModel, True)
  197.     End If
  198.     Exit Sub
  199. 'TODO: Diese Fehlermarke sinnvoll einbinden    
  200. NOFIELDS:
  201.     MsgBox sMsgErrCouldNotOpenObject, 16, sMsgWizardName
  202. End Sub
  203.  
  204.  
  205. Sub CancelFormWizard()
  206.     DlgFormDB.EndExecute()
  207. End Sub
  208.  
  209.  
  210. Sub PreviousStep()
  211.     With oDialogModel
  212.         .Step = 1
  213.         .cmdBack.Enabled = False
  214.         .cmdGoOn.Enabled = True
  215.         .lstSelFields.Tag = Not bControlsareCreated
  216.         .cmdGoOn.Label = sGoOn
  217.         .imgTheme.ImageUrl = FormPath & "FormWizard_1.bmp"    
  218.     End With
  219.     DlgFormDB.Title = WizardTitle(1)
  220. End Sub
  221.  
  222.  
  223. Sub NextStep()
  224. Dim bOldVisible as Boolean
  225.     ' Note: Unfortunately it is not possible to query the visibility of the imagecontrol directly
  226.     bOldVisible = oDialogModel.Height > 40
  227.     Select Case oDialogModel.Step
  228.         Case 1
  229.             bControlsAreCreated = Not (CBool(oDialogModel.lstSelFields.Tag))
  230.             If Not bControlsAreCreated Then
  231.                 GetTableMetaData()
  232.                 CreateDBForm()
  233.                 RemoveShapes()
  234.                 InitializeLayoutSettings()
  235.                 oDBForm.Load
  236.             End If
  237.             oDialogModel.cmdGoOn.Label = sReady
  238.             oDialogModel.cmdBack.Enabled = True
  239.             oDialogModel.Step = 2
  240.         Case 2
  241.             StoreForm()
  242.     End Select
  243.     DlgFormDB.GetControl("imgTheme").Visible = bOldVisible
  244.     oDialogModel.imgTheme.ImageUrl = FormPath & "FormWizard_" & oDialogModel.Step & ".bmp"
  245.     DlgFormDB.Title = WizardTitle(oDialogModel.Step)
  246. End Sub
  247.  
  248.  
  249. Sub InitializeLayoutSettings()
  250.     If oPageStyle.BackGraphicLocation = com.sun.star.style.GraphicLocation.TILED Then
  251.         oDialogModel.optTiled.State = 1
  252.     Else
  253.         oDialogModel.optArea.State = 1
  254.     End If
  255.     SwitchArrangementButtons(cTabled)
  256.     SwitchAlignMode(SBALIGNLEFT)
  257.     SwitchBorderMode(SB3DBORDER)
  258.     ToggleBorderGroup(bControlsAreCreated)
  259.     ToggleAlignGroup(bControlsAreCreated)
  260.     ArrangeControls()
  261.     If OldAlignMode <> 0 Then
  262.         DlgFormDB.GetControl("optAlign2").Model.State = 0
  263.     End If
  264. End Sub
  265.  
  266.  
  267. Sub ToggleDatabasePage(bDoEnable as Boolean)
  268.     With oDialogModel
  269.         .cmdHelp.Enabled = bDoEnable
  270.         .cmdGoOn.Enabled = Ubound(oDialogModel.lstSelFields.StringItemList()) <> -1
  271.         .hlnBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  272.         .optIgnoreBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  273.         .optBinariesasGraphics.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  274.     End With
  275. End Sub
  276.  
  277.  
  278. Sub StoreForm()
  279. Dim bDocIsStored as Boolean
  280. Dim FilterNames(1,2) as String
  281. Dim oMasterKey as Object
  282. Dim oFilters() as Object
  283. Dim sTargetPath as String
  284. Dim sBookmarkName as String
  285. Dim oDBBookmarks as Object        
  286. Dim bLinkExists as Boolean
  287. Dim i as Integer    
  288. Dim sBaseBookmarkName as String
  289.     oMasterKey = GetRegistryKeyContent("org.openoffice.Office.TypeDetection/")
  290.     oFilters() = oMasterKey.Types
  291.     FilterNames(0,0) = oFilters.GetByName("writer_StarOffice_XML_Writer").UIName
  292.     FilterNames(0,1) = "*.sxw"
  293.     FilterNames(0,2) = ""
  294.     FilterNames(1,0) = oFilters.GetByName("writer_StarOffice_XML_Writer_Template").UIName
  295.     FilterNames(1,1) = "*.stw"
  296.     FilterNames(1,2) = "swriter: writer_StarOffice_XML_Writer_Template"
  297.     sTargetPath = StoreDocument(oDocument, FilterNames(), "Form_" & sDBName & "." & TableName, WorkPath)
  298.     If sTargetPath <> "" Then
  299.         sBookmarkName = GetFileNamewithoutExtension(FileNameoutofPath(sTargetPath))
  300.         sBaseBookmarkName = sBookmarkName
  301.         oDBBookmarks = oDataSource.GetBookmarks()
  302.         i = 1
  303.         Do
  304.             bLinkExists = oDBBookmarks.HasbyName(sBookmarkName)
  305.             If bLinkExists Then
  306.                 i = i + 1
  307.                 sBookmarkName = sBaseBookmarkName & "_" & i
  308.             Else
  309.                 oDBBookmarks.insertByName(sBookmarkName, sTargetPath)
  310.             End If
  311.         Loop Until Not bLinkExists
  312.         bDisposeDoc = False
  313.         DlgFormDB.EndExecute()
  314.         ToggleDesignMode(oDocument)
  315.         ' Todo: evtl unsinnige Standardwerte f├╝r Controls rauswerfen    
  316.         oDBForm.Reload()
  317.     End If
  318. End Sub</script:module>