1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd"> 3<!--*********************************************************** 4 * 5 * Licensed to the Apache Software Foundation (ASF) under one 6 * or more contributor license agreements. See the NOTICE file 7 * distributed with this work for additional information 8 * regarding copyright ownership. The ASF licenses this file 9 * to you under the Apache License, Version 2.0 (the 10 * "License"); you may not use this file except in compliance 11 * with the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, 16 * software distributed under the License is distributed on an 17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 18 * KIND, either express or implied. See the License for the 19 * specific language governing permissions and limitations 20 * under the License. 21 * 22 ***********************************************************--> 23<script:module xmlns:script="http://openoffice.org/2000/script" script:name="FormWizard" script:language="StarBasic">Option Explicit 24 25Public DocumentName as String 26Public FormPath as String 27Public WizardPath as String 28Public WebWizardPath as String 29Public WorkPath as String 30Public TempPath as String 31Public TexturePath as String 32Public sQueryName as String 33Public oDBConnection as Object 34Public bWithBackGraphic as Boolean 35Public bNeedFieldRefresh as Boolean 36Public oDBForm as Object 37Public oColumns() as Object 38Public sDatabaseList() as String 39Public TableNames() as String 40Public QueryNames() as String 41Public FieldNames() as String 42Public ImgFieldNames() as String 43Public oDBContext as Object 44Public oUcb as Object 45Public oDocInfo as Object 46Public WidthList(15,3) 47Public ImgWidthList(3,3) 48Public sDBName as String 49Public Tablename as String 50Public Const SBSIZETEXT = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog." 51Public bDisposeDoc as Boolean 52Public bDebug as Boolean 53'Public bStartUp as Boolean 54Public bConnectionIsovergiven as Boolean 55Public FormName As String 56Public sFormUrl as String 57Public oFormDocuments 58 59 60' The macro can be called in 4 possible scenarios: 61' Scenario 1. No parameters at given 62' Scenario 2: Only Datasourcename is given, but no connection and no Content 63' Scenario 3: a data source and a connection are given 64' Scenario 4: all parameters (data source name, connection, object type and object) are given 65 66Sub Main() 67Dim oLocDBContext as Object 68Dim oLocConnection as Object 69 70' Scenario 1. No parameters at given 71 MainWithDefault() 72 73' Scenario 2: Only Datasourcename is given, but no connection and no Content 74' MainWithDefault("Bibliography") 75 76' Scenario 3: a data source and a connection are given 77' oLocDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext") 78' oLocConnection = oLocDBContext.GetByName("Bibliography").GetConnection("","") 79' MainWithDefault("Bibliography", oLocConnection) 80 81' Scenario 4: all parameters (data source name, connection, object type and object) are given 82' oLocDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext") 83' oLocConnection = oLocDBContext.GetByName("Bibliography").GetConnection("","") 84' MainWithDefault("Bibliography", oLocConnection, com.sun.star.sdb.CommandType.TABLE, "biblio") 85End Sub 86 87 88Sub MainWithDefault(Optional DatasourceName as String, Optional oConnection as Object, Optional CommandType as Integer, Optional sContent as String) 89Dim i as Integer 90Dim SelCount as Integer 91Dim RetValue as Integer 92Dim SelList(0) as Integer 93Dim LocList() as String 94 SelList(0) = 0 95 BasicLibraries.LoadLibrary("Tools") 96 BasicLibraries.LoadLibrary("WebWizard") 97 bDebug = False 98 If Not bDebug Then 99 On Local Error GoTo WIZARDERROR 100 End If 101 OpenFormDocument() 102 CurArrangement = 0 103 bControlsareCreated = False 104 bEnableBinaryOptionGroup = False 105 bDisposeDoc = True 106 MaxIndex = -1 107 If Not InitResources("Formwizard","dbw") Then 108 Exit Sub 109 End If 110 oDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext") 111 oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess") 112 If GetFormWizardPaths() = False Then 113 Exit Sub 114 End If 115 oDocument.GetCurrentController().Frame.ComponentWindow.Enable = False 116 oProgressBar.Value = 10 117 LoadLanguage() 118 oProgressBar.Value = 20 119 InitializeWidthList() 120 oProgressBar.Value = 30 121 Styles() = getListBoxArrays(oUcb, "/stl") 122 CurIndex = GetCurIndex(DialogModel, Styles(), 2) 123 oProgressBar.Value = 40 124 ConfigurePageStyle() 125 oProgressBar.Value = 50 126 InitializeLabelValues() 127 bNeedFieldRefresh = True 128 SetDialogLanguage() 129' bStartUp = true 130 With DialogModel 131 .cmdBack.Enabled = False 132 .cmdGoOn.Enabled = False 133 .lblTables.Enabled = False 134 .lstSelFields.Tag = False 135 .Step = 1 136 End With 137 oProgressBar.Value = 60 138 bConnectionIsovergiven = Not IsMissing(oConnection) 139 If Not IsMissing(DataSourceName) Then 140 sDBName = DataSourceName 141 If Not IsMissing(oConnection) Then 142 ' Scenario 3: a data source and a connection are given 143 Set oDBConnection = oConnection 144 oDataSource = oDBContext.GetByName(DataSourceName) 145 DialogModel.lstTables.Enabled = True 146 DialogModel.lblTables.Enabled = True 147 If GetDBMetaData() Then 148 LocList() = AddListToList(TableNames(), QueryNames()) 149 iCommandTypes = CreateCommandTypeList() 150 If Not IsMissing(sContent) Then 151 ' Scenario 4: all parameters (data source name, connection, object type and object) are given 152 DialogModel.lstTables.StringItemList() = LocList() 153 iCommandTypes() = CreateCommandTypeList() 154 SelCount = CountItemsInArray(DialogModel.lstTables.StringItemList(), sContent) 155 If SelCount = 1 Then 156 DlgFormDB.GetControl("lstTables").SelectItem(sContent, True) 157 Else 158 If CommandType = com.sun.star.sdb.CommandType.QUERY Then 159 SelIndex = IndexInArray(sContent, QueryNames() 160 DlgFormDB.GetControl("lstTables").SelectItemPos(SelIndex, True) 161 ElseIf CommandType = com.sun.star.sdb.CommandType.TABLE Then 162 SelIndex = IndexInArray(sContent, TableNames() 163 DlgFormDB.GetControl("lstTables").SelectItemPos(Ubound(QueryNames()+1 + SelIndex, True) 164 End If 165 End If 166 CurCommandType = CommandType 167 FillUpFieldsListbox(False) 168 Else 169 LocList() = AddListToList(Array(sSelectDBTable), LocList()) 170 DialogModel.lstTables.StringItemList() = LocList() 171' bSelectContent = True 172 DialogModel.lstTables.SelectedItems() = Array(0) 173 174 End If 175 End If 176 Else 177 ' Scenario 2: Only Datasourcename is given, but no connection and no Content 178 GetSelectedDBMetaData(sDBName) 179 End If 180 Else 181 ' Scenario 1: No parameters are given 182 ToggleListboxControls(DialogModel, False) 183 End If 184 oProgressBar.Value = 80 185 bWithBackGraphic = LoadNewStyles(oDocument, DialogModel, CurIndex, Styles(CurIndex, 8), Styles(), TexturePath) 186 DlgFormDB.Title = WizardTitle(1) 187 DialogModel.lstStyles.StringItemList() = ArrayfromMultiArray(Styles, 1) 188 DialogModel.lstStyles.SelectedItems() = SelList() 189 ControlCaptionsToStandardLayout() 190 oDocument.GetCurrentController().Frame.ComponentWindow.Enable = True 191 oProgressBar.Value = 90 192 DialogModel.imgTheme.ImageURL = FormPath & "FormWizard_1.png" 193 DialogModel.imgTheme.BackGroundColor = RGB(0,60,126) 194 ToggleDatabasePage(True) 195 oProgressBar.Value = 100 196 DlgFormDB.GetControl("lstTables").SetFocus() 197 oProgressbar.End 198 RetValue = DlgFormDB.Execute() 199 DlgFormDB.Dispose() 200 If bDisposeDoc Then 201 Dim aPropertyValues(2) as new com.sun.star.beans.PropertyValue 202 oFormDocuments = oDataSource.getFormDocuments() 203 DlgFormDB.Dispose() 204 oDocument.dispose() 205 Dim bLinkExists as Boolean 206 i = 1 207 Dim FormBaseName as String 208 FormBaseName = FormName 209 Do 210 bLinkExists = oFormDocuments.HasbyHierarchicalName(FormName) 211 If bLinkExists Then 212 i = i + 1 213 FormName = FormBaseName & "_" & i 214 End If 215 Loop Until Not bLinkExists 216 aPropertyValues(0).Name = "Name" 217 aPropertyValues(0).Value = FormName 218 aPropertyValues(1).Name = "Parent" 219 aPropertyValues(1).Value = oFormDocuments() 220 aPropertyValues(2).Name = "URL" 221 aPropertyValues(2).Value = sFormUrl 222 Dim oDBDocument 223 oDBDocument = oFormDocuments.createInstanceWithArguments("com.sun.star.sdb.DocumentDefinition", aPropertyValues()) 224 oFormDocuments.insertbyName(FormName, oDBDocument) 225 ElseIf RetValue = 0 Then 226 RemoveNirwanaShapes() 227 End If 228 If ((Not IsNull(oDBConnection)) And (Not bConnectionIsovergiven)) Then 229 oDBConnection.Dispose() 230 End If 231WIZARDERROR: 232 If Err <> 0 Then 233 Msgbox(sMsgErrMsg, 16, GetProductName()) 234 Resume LOCERROR 235 LOCERROR: 236 End If 237End Sub 238 239 240Sub FormGetFields() 241Dim i as Integer 242' If bSelectContent Then 243' bSelectContent = False 244' Exit Sub 245' End If 246 DeleteFirstListBoxEntry("lstTables", sSelectDBTable) 247 ToggleDatabasePage(False) 248 FillUpFieldsListbox(True) 249 ToggleDatabasePage(True) 250End Sub 251 252 253Sub FillUpFieldsListbox(bGetCommandType as Boolean) 254Dim SelIndex as Integer 255Dim QueryIndex as Integer 256 If Not bDebug Then 257 On Local Error GoTo NOFIELDS 258 End If 259 SelIndex = DlgFormDB.GetControl("lstTables").getSelectedItemPos() '.SelectedItems()) 260 If SelIndex > -1 Then 261 If bGetCommandType Then 262 CurCommandType = iCommandTypes(SelIndex) 263 End If 264 If CurCommandType = com.sun.star.sdb.CommandType.QUERY Then 265 QueryIndex = SelIndex - Ubound(Tablenames()) - 1 266 Tablename = QueryNames(QueryIndex) 267 oColumns = oDBConnection.Queries.GetByName(TableName).Columns 268 Else 269 Tablename = Tablenames(SelIndex) 270 oColumns = oDBConnection.Tables.GetByName(Tablename).Columns 271 End If 272 If GetSpecificFieldNames() <> -1 Then 273 ToggleListboxControls(DialogModel, True) 274 Exit Sub 275 End If 276 End If 277 EmptyFieldsListboxes() 278NOFIELDS: 279 If Err <> 0 Then 280 MsgBox sMsgErrCouldNotOpenObject, 16, sMsgWizardName 281 End If 282End Sub 283 284 285Sub PreviousStep() 286 If Not bDebug Then 287 On Local Error GoTo WIZARDERROR 288 End If 289 With DialogModel 290 .Step = 1 291 .cmdBack.Enabled = False 292 .cmdGoOn.Enabled = True 293 .lstSelFields.Tag = Not bControlsareCreated 294 .cmdGoOn.Label = sGoOn 295 .imgTheme.ImageUrl = FormPath & "FormWizard_1.png" 296 End With 297 FormSetMoveRights() 298WIZARDERROR: 299 If Err <> 0 Then 300 Msgbox(sMsgErrMsg, 16, GetProductName()) 301 Resume LOCERROR 302 LOCERROR: 303 End If 304End Sub 305 306 307Sub NextStep() 308 If Not bDebug Then 309 On Local Error GoTo WIZARDERROR 310 End If 311 Select Case DialogModel.Step 312 Case 1 313 bControlsAreCreated = Not (cBool(DialogModel.lstSelFields.Tag)) 314 If Not bControlsAreCreated Then 315 GetTableMetaData() 316 CreateDBForm() 317 RemoveShapes() 318 InitializeLayoutSettings() 319 oDBForm.Load 320 End If 321 DialogModel.cmdGoOn.Label = sReady 322 DialogModel.cmdBack.Enabled = True 323 DialogModel.Step = 2 324 bDisposeDoc = False 325 Case 2 326 StoreForm() 327 DlgFormDB.EndExecute() 328 exit Sub 329 End Select 330 DialogModel.imgTheme.ImageUrl = FormPath & "FormWizard_" & DialogModel.Step & ".png" 331 DlgFormDB.Title = WizardTitle(DialogModel.Step) 332WIZARDERROR: 333 If Err <> 0 Then 334 Msgbox(sMsgErrMsg, 16, GetProductName()) 335 Resume LOCERROR 336 LOCERROR: 337 End If 338End Sub 339 340 341Sub InitializeLayoutSettings() 342 SwitchArrangementButtons(cTabled) 343 SwitchAlignMode(SBALIGNLEFT) 344 SwitchBorderMode(SB3DBORDER) 345 ToggleBorderGroup(bControlsAreCreated) 346 ToggleAlignGroup(bControlsAreCreated) 347 ArrangeControls() 348 If OldAlignMode <> 0 Then 349 DlgFormDB.GetControl("optAlign2").Model.State = 0 350 End If 351End Sub 352 353 354Sub ToggleDatabasePage(bDoEnable as Boolean) 355 With DialogModel 356 .cmdBack.Enabled = False 357 .cmdHelp.Enabled = bDoEnable 358 .cmdGoOn.Enabled = Ubound(DialogModel.lstSelFields.StringItemList()) <> -1 359 .hlnBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True)) 360 .optIgnoreBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True)) 361 .optBinariesasGraphics.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True)) 362 End With 363End Sub 364 365 366' This Sub is called from the Procedure "StoreDocument" in the "Tools" Library 367Sub CommitLastDocumentChanges(sTargetPath as String) 368Dim i as Integer 369Dim sBookmarkName as String 370Dim oDBBookmarks as Object 371Dim bLinkExists as Boolean 372Dim sBaseBookmarkName as String 373 sBookmarkName = GetFileNamewithoutExtension(FileNameoutofPath(sTargetPath)) 374 sBaseBookmarkName = sBookmarkName 375 oDBBookmarks = oDataSource.GetBookmarks() 376 i = 1 377 Do 378 bLinkExists = oDBBookmarks.HasbyName(sBookmarkName) 379 If bLinkExists Then 380 i = i + 1 381 sBookmarkName = sBaseBookmarkName & "_" & i 382 Else 383 oDBBookmarks.insertByName(sBookmarkName, sTargetPath) 384 End If 385 Loop Until Not bLinkExists 386 bDisposeDoc = False 387 GroupShapesTogether() 388 ToggleDesignMode(oDocument) 389 oDBForm.Reload() 390End Sub 391 392 393Sub StoreFormInDatabase() 394 Dim NoArgs() as new com.sun.star.beans.PropertyValue 395 FormName = "Form_" & sDBName & "_" & TableName & ".sxw" 396 sFormUrl = TempPath & "/" & FormName 397 oDocument.StoreAsUrl(sFormUrl, NoArgs()) 398 bdisposeDoc = true 399 DlgFormDB.Endexecute() 400End Sub 401 402 403Sub StoreForm() 404Dim sTargetPath as String 405Dim TypeNames(0,2) as String 406Dim oMasterKey as Object 407Dim oTypes() as Object 408 oMasterKey = GetRegistryKeyContent("org.openoffice.TypeDetection.Types/") 409 oTypes() = oMasterKey.Types 410 TypeNames(0,0) = GetFilterName("StarOffice XML (Writer)") 411 TypeNames(0,1) = "*.sxw" 412 TypeNames(0,2) = "" 413 StoreFormInDatabase() 414' sTargetPath = StoreDocument(oDocument, TypeNames(), "Form_" & sDBName & "_" & TableName & ".sxw", WorkPath, 1) 415End Sub 416 417 418Sub EmptyFieldsListboxes() 419Dim NullList() as String 420 ToggleListboxControls(DialogModel, False) 421 DialogModel.lstFields.StringItemList() = NullList() 422 DialogModel.lstSelFields.StringItemList() = NullList() 423 bEnableBinaryOptionGroup = False 424End Sub 425 426 427Sub DeleteFirstTableListBoxEntry() 428 DeleteFirstListBoxEntry("lstTables", sSelectDBTable) 429End Sub 430 431Sub DeleteFirstListboxEntry(ListBoxName as String, DelEntryName as String) 432Dim oListbox as Object 433Dim sFirstItem as String 434dim iSelPos as Integer 435 oListBox = DlgFormDB.getControl(ListBoxName) 436 sFirstItem = oListBox.getItem(0) 437 If sFirstItem = DelEntryName Then 438 iSelPos = oListBox.getSelectedItemPos() 439 oListBox.removeItems(0, 1) 440 If iSelPos > 0 Then 441 oListBox.selectItemPos(iSelPos-1, True) 442 End If 443 End If 444End Sub 445</script:module> 446