home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 May / Gamestar_62_2004-05_dvd.iso / Programy / openoffice / f_0181 / FormWizard.xba < prev    next >
Extensible Markup Language  |  2003-06-06  |  13KB  |  395 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. Public DocumentName as String
  6. Public FormPath as String
  7. Public WizardPath as String
  8. Public WebWizardPath as String
  9. Public WorkPath as String
  10. Public TexturePath as String
  11. Public sQueryName as String
  12. Public oDBConnection as Object
  13. Public bWithBackGraphic as Boolean
  14. Public bNeedFieldRefresh as Boolean
  15. Public oDBForm as Object
  16. Public oColumns() as Object
  17. Public sDatabaseList() as String
  18. Public TableNames() as String
  19. Public QueryNames() as String
  20. Public FieldNames() as String
  21. Public ImgFieldNames() as String
  22. Public oDBContext as Object
  23. Public oUcb as Object
  24. Public oDocInfo as Object
  25. Public WidthList(15,3)
  26. Public ImgWidthList(3,3)
  27. Public sDBName as String
  28. Public Tablename as String
  29. Public Const SBSIZETEXT = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog."
  30. Public bDisposeDoc as Boolean
  31. Public bDebug as Boolean
  32. 'Public bStartUp as Boolean
  33. Public bConnectionIsovergiven as Boolean
  34.  
  35. ' The macro can be called in 4 possible scenarios:
  36. ' Scenario 1. No parameters at given
  37. ' Scenario 2: Only Datasourcename is given, but no connection and no Content
  38. ' Scenario 3: a data source and a connection are given
  39. ' Scenario 4: all parameters (data source name, connection, object type and object) are given
  40.  
  41. Sub Main()
  42. Dim oLocDBContext as Object
  43. Dim oLocConnection as Object
  44.  
  45. ' Scenario 1. No parameters at given
  46.     MainWithDefault()
  47.  
  48. ' Scenario 2: Only Datasourcename is given, but no connection and no Content
  49. '    MainWithDefault("Bibliography")
  50.  
  51. ' Scenario 3: a data source and a connection are given
  52. '    oLocDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  53. '    oLocConnection = oLocDBContext.GetByName("Bibliography").GetConnection("","")
  54. '    MainWithDefault("Bibliography", oLocConnection)
  55.  
  56. ' Scenario 4: all parameters (data source name, connection, object type and object) are given
  57. '    oLocDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  58. '    oLocConnection = oLocDBContext.GetByName("Bibliography").GetConnection("","")
  59. '    MainWithDefault("Bibliography", oLocConnection, com.sun.star.sdb.CommandType.TABLE, "biblio")
  60. End Sub
  61.  
  62.  
  63. Sub MainWithDefault(Optional DatasourceName as String, Optional oConnection as Object, Optional CommandType as Integer, Optional sContent as String)
  64. Dim i as Integer
  65. Dim SelCount as Integer
  66. Dim RetValue as Integer
  67. Dim SelList(0) as Integer
  68. Dim LocList() as String
  69.     SelList(0) = 0
  70.     BasicLibraries.LoadLibrary("Tools")
  71.     BasicLibraries.LoadLibrary("WebWizard")
  72.     bDebug = False
  73.     If Not bDebug Then
  74.         On Local Error GoTo WIZARDERROR
  75.     End If
  76.     OpenFormDocument()
  77.     CurArrangement = 0
  78.     bControlsareCreated = False
  79.     bEnableBinaryOptionGroup = False
  80.     bDisposeDoc = True
  81.     MaxIndex = -1
  82.     If Not InitResources("Formwizard","dbw") Then
  83.         Exit Sub
  84.     End If
  85.     oDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  86.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  87.     If GetFormWizardPaths() = False Then
  88.         Exit Sub
  89.     End If
  90.     oDocument.GetCurrentController().Frame.ComponentWindow.Enable = False
  91.     oProgressBar.Value = 10
  92.     LoadLanguage()
  93.     oProgressBar.Value = 20
  94.     InitializeWidthList()
  95.     oProgressBar.Value = 30
  96.     Styles() = getListBoxArrays(oUcb, "/stl")
  97.     CurIndex = GetCurIndex(DialogModel, Styles(), 2)
  98.     oProgressBar.Value = 40
  99.     ConfigurePageStyle()
  100.     oProgressBar.Value = 50
  101.     InitializeLabelValues()
  102.     bNeedFieldRefresh = True
  103.     SetDialogLanguage()
  104. '    bStartUp = true
  105.     With DialogModel
  106.         .cmdBack.Enabled = False
  107.         .cmdGoOn.Enabled = False
  108.         .lblTables.Enabled = False
  109.         .lstSelFields.Tag = False
  110.         .Step = 1
  111.         .lstDatabases.StringItemList()= GetDatabaseNames(IsMissing(DataSourceName))
  112.         .lstDatabases.SelectedItems() = Array(0)
  113.     End With
  114.     oProgressBar.Value = 60
  115.     bConnectionIsovergiven = Not IsMissing(oConnection)
  116.     If Not IsMissing(DataSourceName) Then
  117.         sDBName = DataSourceName
  118.         DlgFormDB.GetControl("lstDatabases").SelectItem(DataSourceName, True)
  119.         If Not IsMissing(oConnection) Then
  120.             ' Scenario 3: a data source and a connection are given
  121.             Set oDBConnection = oConnection
  122.             oDataSource = oDBContext.GetByName(DataSourceName)
  123.             DialogModel.lstTables.Enabled = True
  124.             DialogModel.lblTables.Enabled = True
  125.             If GetDBMetaData() Then
  126.                 LocList() = AddListToList(TableNames(), QueryNames())
  127.                 iCommandTypes = CreateCommandTypeList()
  128.                 If Not IsMissing(sContent) Then
  129.                     ' Scenario 4: all parameters (data source name, connection, object type and object) are given
  130.                     DialogModel.lstTables.StringItemList() = LocList()
  131.                     iCommandTypes() = CreateCommandTypeList()
  132.                     SelCount = CountItemsInArray(DialogModel.lstTables.StringItemList(), sContent)
  133.                     If SelCount = 1 Then
  134.                         DlgFormDB.GetControl("lstTables").SelectItem(sContent, True)
  135.                     Else
  136.                         If CommandType = com.sun.star.sdb.CommandType.QUERY Then
  137.                             SelIndex = IndexInArray(sContent, QueryNames()
  138.                             DlgFormDB.GetControl("lstTables").SelectItemPos(SelIndex, True)
  139.                         ElseIf CommandType = com.sun.star.sdb.CommandType.TABLE Then
  140.                             SelIndex = IndexInArray(sContent, TableNames()
  141.                             DlgFormDB.GetControl("lstTables").SelectItemPos(Ubound(QueryNames()+1 + SelIndex, True)
  142.                         End If
  143.                     End If
  144.                     CurCommandType = CommandType
  145.                     FillUpFieldsListbox(False)
  146.                 Else
  147.                     LocList() = AddListToList(Array(sSelectDBTable), LocList())
  148.                     DialogModel.lstTables.StringItemList() = LocList()
  149. '                    bSelectContent = True
  150.                     DialogModel.lstTables.SelectedItems() = Array(0)
  151.  
  152.                 End If
  153.             End If
  154.         Else
  155.             ' Scenario 2: Only Datasourcename is given, but no connection and no Content
  156.             GetSelectedDBMetaData()
  157.         End If
  158.     Else
  159.         ' Scenario 1: No parameters are given
  160.         ToggleListboxControls(DialogModel, False)
  161.     End If
  162.     oProgressBar.Value = 80
  163.     bWithBackGraphic = LoadNewStyles(oDocument, DialogModel, CurIndex, Styles(CurIndex, 8), Styles(), TexturePath)
  164.     DlgFormDB.Title = WizardTitle(1)
  165.     DialogModel.lstStyles.StringItemList() = ArrayfromMultiArray(Styles, 1)
  166.     DialogModel.lstStyles.SelectedItems() = SelList()
  167.     ControlCaptionsToStandardLayout()
  168.     oDocument.GetCurrentController().Frame.ComponentWindow.Enable = True
  169.     oProgressBar.Value = 90
  170.     DialogModel.imgTheme.ImageURL = FormPath & "FormWizard_1.bmp"
  171.     DialogModel.imgTheme.BackGroundColor = RGB(0,60,126)
  172.     ToggleDatabasePage(True)
  173.     oProgressBar.Value = 100
  174.     DlgFormDB.GetControl("lstDatabases").SetFocus()
  175.     oProgressbar.End
  176.     RetValue = DlgFormDB.Execute()
  177.     DlgFormDB.Dispose()
  178.     If ((Not IsNull(oDBConnection)) And (Not bConnectionIsovergiven)) Then
  179.         oDBConnection.Dispose()
  180.     End If
  181.     If bDisposeDoc Then
  182.         DisposeDocument(oDocument)
  183.     ElseIf RetValue = 0 Then
  184.         RemoveNirwanaShapes()
  185.     End If
  186. WIZARDERROR:
  187.     If Err <> 0 Then
  188.         Msgbox(sMsgErrMsg, 16, GetProductName())
  189.         Resume LOCERROR
  190.         LOCERROR:
  191.     End If
  192. End Sub
  193.  
  194.  
  195. Sub FormGetFields()
  196. Dim i as Integer
  197. '    If bSelectContent Then
  198. '        bSelectContent = False
  199. '        Exit Sub
  200. '    End If
  201.     DeleteFirstListBoxEntry("lstTables", sSelectDBTable)
  202.     ToggleDatabasePage(False)
  203.     FillUpFieldsListbox(True)
  204.     ToggleDatabasePage(True)
  205. End Sub
  206.  
  207.  
  208. Sub FillUpFieldsListbox(bGetCommandType as Boolean)
  209. Dim SelIndex as Integer
  210. Dim QueryIndex as Integer
  211.     If Not bDebug Then
  212.         On Local Error GoTo NOFIELDS
  213.     End If
  214.     SelIndex = DlgFormDB.GetControl("lstTables").getSelectedItemPos() '.SelectedItems())
  215.     If SelIndex > -1 Then
  216.         If bGetCommandType Then
  217.             CurCommandType = iCommandTypes(SelIndex)
  218.         End If
  219.         If CurCommandType = com.sun.star.sdb.CommandType.QUERY Then
  220.             QueryIndex = SelIndex  - Ubound(Tablenames()) - 1
  221.             Tablename = QueryNames(QueryIndex)
  222.             oColumns = oDBConnection.Queries.GetByName(TableName).Columns
  223.         Else
  224.             Tablename = Tablenames(SelIndex)
  225.             oColumns = oDBConnection.Tables.GetByName(Tablename).Columns
  226.         End If
  227.         If GetSpecificFieldNames() <> -1 Then
  228.             ToggleListboxControls(DialogModel, True)
  229.             Exit Sub
  230.         End If
  231.     End If
  232.     EmptyFieldsListboxes()
  233. NOFIELDS:
  234.     If Err <> 0 Then
  235.         MsgBox sMsgErrCouldNotOpenObject, 16, sMsgWizardName
  236.     End If
  237. End Sub
  238.  
  239.  
  240. Sub PreviousStep()
  241.     If Not bDebug Then
  242.         On Local Error GoTo WIZARDERROR
  243.     End If
  244.     With DialogModel
  245.         .Step = 1
  246.         .cmdBack.Enabled = False
  247.         .cmdGoOn.Enabled = True
  248.         .lstSelFields.Tag = Not bControlsareCreated
  249.         .cmdGoOn.Label = sGoOn
  250.         .imgTheme.ImageUrl = FormPath & "FormWizard_1.bmp"
  251.     End With
  252.     FormSetMoveRights()
  253. WIZARDERROR:
  254.     If Err <> 0 Then
  255.         Msgbox(sMsgErrMsg, 16, GetProductName())
  256.         Resume LOCERROR
  257.         LOCERROR:
  258.     End If
  259. End Sub
  260.  
  261.  
  262. Sub NextStep()
  263.     If Not bDebug Then
  264.         On Local Error GoTo WIZARDERROR
  265.     End If
  266.     Select Case DialogModel.Step
  267.         Case 1
  268.             bControlsAreCreated = Not (cBool(DialogModel.lstSelFields.Tag))
  269.             If Not bControlsAreCreated Then
  270.                 GetTableMetaData()
  271.                 CreateDBForm()
  272.                 RemoveShapes()
  273.                 InitializeLayoutSettings()
  274.                 oDBForm.Load
  275.             End If
  276.             DialogModel.cmdGoOn.Label = sReady
  277.             DialogModel.cmdBack.Enabled = True
  278.             DialogModel.Step = 2
  279.             bDisposeDoc = False
  280.         Case 2
  281.             StoreForm()
  282.     End Select
  283.     DialogModel.imgTheme.ImageUrl = FormPath & "FormWizard_" & DialogModel.Step & ".bmp"
  284.     DlgFormDB.Title = WizardTitle(DialogModel.Step)
  285. WIZARDERROR:
  286.     If Err <> 0 Then
  287.         Msgbox(sMsgErrMsg, 16, GetProductName())
  288.         Resume LOCERROR
  289.         LOCERROR:
  290.     End If
  291. End Sub
  292.  
  293.  
  294. Sub InitializeLayoutSettings()
  295.     SwitchArrangementButtons(cTabled)
  296.     SwitchAlignMode(SBALIGNLEFT)
  297.     SwitchBorderMode(SB3DBORDER)
  298.     ToggleBorderGroup(bControlsAreCreated)
  299.     ToggleAlignGroup(bControlsAreCreated)
  300.     ArrangeControls()
  301.     If OldAlignMode <> 0 Then
  302.         DlgFormDB.GetControl("optAlign2").Model.State = 0
  303.     End If
  304. End Sub
  305.  
  306.  
  307. Sub ToggleDatabasePage(bDoEnable as Boolean)
  308.     With DialogModel
  309.         .cmdBack.Enabled = False
  310.         .cmdHelp.Enabled = bDoEnable
  311.         .cmdGoOn.Enabled = Ubound(DialogModel.lstSelFields.StringItemList()) <> -1
  312.         .hlnBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  313.         .optIgnoreBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  314.         .optBinariesasGraphics.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  315.     End With
  316. End Sub
  317.  
  318.  
  319. ' This Sub is called from the Procedure "StoreDocument" in the "Tools" Library
  320. Sub CommitLastDocumentChanges(sTargetPath as String)
  321. Dim i as Integer
  322. Dim sBookmarkName as String
  323. Dim oDBBookmarks as Object
  324. Dim bLinkExists as Boolean
  325. Dim sBaseBookmarkName as String
  326.     sBookmarkName = GetFileNamewithoutExtension(FileNameoutofPath(sTargetPath))
  327.     sBaseBookmarkName = sBookmarkName
  328.     oDBBookmarks = oDataSource.GetBookmarks()
  329.     i = 1
  330.     Do
  331.         bLinkExists = oDBBookmarks.HasbyName(sBookmarkName)
  332.         If bLinkExists Then
  333.             i = i + 1
  334.             sBookmarkName = sBaseBookmarkName & "_" & i
  335.         Else
  336.             oDBBookmarks.insertByName(sBookmarkName, sTargetPath)
  337.         End If
  338.     Loop Until Not bLinkExists
  339.     bDisposeDoc = False
  340.     GroupShapesTogether()
  341.     ToggleDesignMode(oDocument)
  342.     oDBForm.Reload()
  343. End Sub
  344.  
  345.  
  346. Sub StoreForm()
  347. Dim sTargetPath as String
  348. Dim TypeNames(0,2) as String
  349. Dim oMasterKey as Object
  350. Dim oTypes() as Object
  351.     oMasterKey = GetRegistryKeyContent("org.openoffice.Office.TypeDetection/")
  352.     oTypes() = oMasterKey.Filters
  353.     TypeNames(0,0) = GetFilterName("StarOffice XML (Writer)")
  354.     TypeNames(0,1) = "*.sxw"
  355.     TypeNames(0,2) = ""
  356.     sTargetPath = StoreDocument(oDocument, TypeNames(), "Form_" & sDBName & "_" & TableName & ".sxw", WorkPath, 1)
  357.     If sTargetPath <> "" Then
  358.         DlgFormDB.EndExecute()
  359.     End If
  360. End Sub
  361.  
  362.  
  363. Sub EmptyFieldsListboxes()
  364. Dim NullList() as String
  365.     ToggleListboxControls(DialogModel, False)
  366.     DialogModel.lstFields.StringItemList() = NullList()
  367.     DialogModel.lstSelFields.StringItemList() = NullList()
  368.     bEnableBinaryOptionGroup = False
  369. End Sub
  370.  
  371.  
  372. Sub DeleteFirstDBListBoxEntry()
  373.     DeleteFirstListBoxEntry("lstDatabases", sSelectDatasource)    
  374. End Sub
  375.  
  376.  
  377. Sub DeleteFirstTableListBoxEntry()
  378.     DeleteFirstListBoxEntry("lstTables", sSelectDBTable)
  379. End Sub
  380.  
  381. Sub DeleteFirstListboxEntry(ListBoxName as String, DelEntryName as String)
  382. Dim oListbox as Object
  383. Dim sFirstItem as String
  384. dim iSelPos as Integer
  385.     oListBox = DlgFormDB.getControl(ListBoxName)
  386.     sFirstItem = oListBox.getItem(0)
  387.     If sFirstItem = DelEntryName Then
  388.         iSelPos = oListBox.getSelectedItemPos()
  389.         oListBox.removeItems(0, 1)
  390.         If iSelPos > 0 Then
  391.             oListBox.selectItemPos(iSelPos-1, True)
  392.         End If
  393.     End If
  394. End Sub
  395. </script:module>