Connecting the Forms to the Data-Layer (part 6)
Now Let's look at The Galaxy View Code. I'll take it from the Top Down. the the next post will be it in its entirety.
Beginning at the top we Have:
Define quitfrmGalaxy
Declare GalaxyCustom()
Declare GalaxySetScreen()
Declare GalaxyCreate()
Declare GalaxyModify(GalaxyID.i)
Declare GalaxyDelete(GalaxyID.i)
Declare GalaxySetData()
Declare GalaxySave()
Declare GalaxyClose()
These are nothing more than the declaration of the aforementioned Procedures.
First Procedure is the "SetScreen", as I always put the Form Custom at the bottom. You will remember this Procedure's job is to Display the Form, and load information from the DataLayer to the Screen. After this the procedure contains the Control-Loop for the form.
Procedure GalaxySetScreen()
Protected String$, Index, ID
Shared WorkGalaxy
Protected EventID, MenuID, GadgetID, WindowID, Index
Shared quitfrmGalaxy
quitfrmGalaxy = #False
If Window_frmList()
DisableWindow(#Window_frmMain, #True)
SetActiveWindow(#Window_frmGalaxy)
GalaxyCustom()
SetGadgetText(#Gadget_frmGalaxy_txtName, WorkGalaxy\Name)
SetActiveGadget(#Gadget_frmGalaxy_txtName)
;Set Screen Data
GALAXY::ListIconSolarSystemCollection(#Gadget_frmGalaxy_liSolarsystems)
If ListEx::CountItems(#Gadget_frmGalaxy_liSolarsystems) > 0
ListEx::SetState(#Gadget_frmGalaxy_liSolarsystems, 0)
EndIf
Repeat
EventID =WaitWindowEvent()
MenuID =EventMenu()
GadgetID =EventGadget()
WindowID =EventWindow()
Select EventID
Case #PB_Event_CloseWindow
Select WindowID
Case #Window_frmGalaxy
GalaxyClose()
EndSelect
Case #PB_Event_Menu
Select WindowID
EndSelect
Case #PB_Event_Gadget
Select GadgetID
;*************************************
;**** Start frmGalaxy
;*************************************
Case #Gadget_frmGalaxy_lbName
Case #Gadget_frmGalaxy_txtName
Select EventType()
Case #PB_EventType_Focus
Default
EndSelect
Case #Gadget_frmGalaxy_lbSolarsystems
Case #Gadget_frmGalaxy_liSolarsystems
Select EventType()
Case #PB_EventType_LeftDoubleClick
PostEvent(#PB_Event_Gadget, #Window_frmGalaxy, #Gadget_frmGalaxy_btModify, #PB_EventType_LeftClick)
EndSelect
Case #Gadget_frmGalaxy_btCreate
Select EventType()
Case #PB_EventType_LeftClick
SolarSystemCreate()
EndSelect
Case #Gadget_frmGalaxy_btModify
Select EventType()
Case #PB_EventType_LeftClick
Index=ListEx::GetState(#Gadget_frmGalaxy_liSolarsystems)
If Index>-1
SolarSystemModify(ListEx::GetItemData(#Gadget_frmGalaxy_liSolarsystems, ListEx::GetState(#Gadget_frmGalaxy_liSolarsystems)))
Else
MessageRequester("Blog", "Please Select a Record To MODIFY!")
EndIf
EndSelect
Case #Gadget_frmGalaxy_btDelete
Select EventType()
Case #PB_EventType_LeftClick
Index=ListEx::GetState(#Gadget_frmGalaxy_liSolarsystems)
If Index>-1
SolarSystemDelete(ListEx::GetItemData(#Gadget_frmGalaxy_liSolarsystems, ListEx::GetState(#Gadget_frmGalaxy_liSolarsystems)))
Else
MessageRequester("Blog", "Please Select a Record To DELETE!")
EndIf
EndSelect
Case #Gadget_frmGalaxy_btSave
Select EventType()
Case #PB_EventType_LeftClick
GalaxySave()
EndSelect
Case #Gadget_frmGalaxy_btCancel
Select EventType()
Case #PB_EventType_LeftClick
GalaxyClose()
EndSelect
EndSelect
EndSelect
Until quitfrmGalaxy = #True
CloseWindow(#Window_frmGalaxy)
EndIf
EndProcedure
This is a big chunk of code. I'll take piece by piece. Initially we define some helper variables that this procedure will use. Then we move on to loading the Form Gadgets with Values from our Previously Defined Global Variables. But WAIT! you might be saying, We have not yet Loaded tat Variable from the Database! You are Correct we have Not! But that is NOT this Procedures Job; We will get to this, hold on.
After the definitions and Shared code lines I Call the Create/Display Form, then Call the Form Customization Procedure, then with these lines of Code I load the Gadgets on our Form, List The SolarSystems from our Collection (not from the Database), and load the Galaxy Name into the String Gadget.
quitfrmGalaxy = #False
If Window_frmGalaxy()
SetActiveWindow(#Window_frmGalaxy)
GalaxyCustom()
SetGadgetText(#Gadget_frmGalaxy_txtName, WorkGalaxy\Name)
SetActiveGadget(#Gadget_frmGalaxy_txtName)
;Set Screen Data
GALAXY::ListIconSolarSystemCollection(#Gadget_frmGalaxy_liSolarsystems)
If ListEx::CountItems(#Gadget_frmGalaxy_liSolarsystems) > 0
ListEx::SetState(#Gadget_frmGalaxy_liSolarsystems, 0)
EndIf
Repeat
EventID =WaitWindowEvent()
MenuID =EventMenu()
Next, we have the Form's Control-Loop.
The Control Loop, is Very alike to the Previous One I have shown you. On the Double-Left-Click call the SolarSystem Modify, The Create Button calls the SolarSystem Create, Modify Button and Delete Button calling their respective Code Blocks on the SolarSystem View Code. As you can see:
Case #Gadget_frmGalaxy_liSolarsystems
Select EventType()
Case #PB_EventType_LeftDoubleClick
PostEvent(#PB_Event_Gadget, #Window_frmGalaxy, #Gadget_frmGalaxy_btModify, #PB_EventType_LeftClick)
EndSelect
Case #Gadget_frmGalaxy_btCreate
Select EventType()
Case #PB_EventType_LeftClick
SolarSystemCreate()
EndSelect
Case #Gadget_frmGalaxy_btModify
Select EventType()
Case #PB_EventType_LeftClick
Index=ListEx::GetState(#Gadget_frmGalaxy_liSolarsystems)
If Index>-1
SolarSystemModify(ListEx::GetItemData(#Gadget_frmGalaxy_liSolarsystems, ListEx::GetState(#Gadget_frmGalaxy_liSolarsystems)))
Else
MessageRequester("Blog", "Please Select a Record To MODIFY!")
EndIf
EndSelect
Case #Gadget_frmGalaxy_btDelete
Select EventType()
Case #PB_EventType_LeftClick
Index=ListEx::GetState(#Gadget_frmGalaxy_liSolarsystems)
If Index>-1
SolarSystemDelete(ListEx::GetItemData(#Gadget_frmGalaxy_liSolarsystems, ListEx::GetState(#Gadget_frmGalaxy_liSolarsystems)))
Else
MessageRequester("Blog", "Please Select a Record To DELETE!")
EndIf
EndSelect
We have the Addition of the Save and Cancel Buttons. These relate to the Galaxy, not SolarSystem, and therefore, the call the GalaxySave and GalaxyClose Procedure later on in THIS Code Sheet. Shown Below
Case #Gadget_frmGalaxy_btSave
Select EventType()
Case #PB_EventType_LeftClick
GalaxySave()
EndSelect
Case #Gadget_frmGalaxy_btCancel
Select EventType()
Case #PB_EventType_LeftClick
GalaxyClose()
EndSelect
EndSelect
Now we will move on to the GalaxyCreate() Procedure:
Procedure GalaxyCreate()
Shared WorkGalaxy
GALAXY::Init(@WorkGalaxy)
GalaxySetScreen()
EndProcedure
Here is our FIRST Entry point into this Code Sheet from our Application Desktop.
Here we Initialize, the Global WorkGalaxy Variable, and Call the GalaxySetScreen Procedure.
Next, we'll look at the GalaxyModify Procedure:
Procedure GalaxyModify(GalaxyID.i)
Shared WorkGalaxy
GALAXY::Find(GalaxyID , @WorkGalaxy)
GalaxySetScreen()
EndProcedure
The GalaxyModify, receives the ID of the Galaxy Record to be modified, Here instead of Initializing the Object we call the Galaxy Module's Find procedure and load it in from the database.
And the GalaxyDelete Procedure, The Delete Galaxy necer call the SetScreen Procedure, Instead I pops up a message window forcing the User to Confirm their desires to delete the Record.
Procedure GalaxyDelete(GalaxyID.i)
Protected Index, Result, itemCount
Index = ListEx::GetState(#Gadget_frmMain_liList)
DisableWindow(#Window_frmMain, #True)
Shared WorkGalaxy
GALAXY::Find(GalaxyID , @WorkGalaxy)
MessageRequester("Delete Confirmation", "Are you SURE you want To DELETE this Record ?", #PB_MessageRequester_YesNo )
If Result = #PB_MessageRequester_Yes
If GALAXY::Delete(@WorkGalaxy) = #True
ListEx::RemoveItem(#Gadget_frmMain_liList, Index)
itemCount.i = ListEx::CountItems(#Gadget_frmMain_liList)-1
If itemCount>0
ListEx::SetState(#Gadget_frmMain_liList, 0)
EndIf
EndIf
EndIf
DisableWindow(#Window_frmMain, #False)
EndProcedure
Upon his Confirmation, this procedure Deletes the Record from the Database, AND eliminates the record from the List. As a final steb before returning to the main Screen he selects the first record in list, assuming the IS a first Record.
The GalaxySetData Procedure Takes the user Input on the Form and Loads it back into the Data-Layer WorkGalaxy Variable in _Preparation for saving the Record back to the Database.
Procedure GalaxySetData()
Protected Index
Shared WorkGalaxy
WorkGalaxy\Name = GetGadgetText(#Gadget_frmGalaxy_txtName)
EndProcedure
This code I feel is pretty straight forward.
Then we have the GalaxySave Procedure:
Procedure GalaxySave()
Shared WorkGalaxy
Protected State = WorkGalaxy\State
GalaxySetData()
;Save to Database
If Galaxy::Save(@WorkGalaxy) = #True
If State = DB::#New ;insert record
ListEx::AddItem(#Gadget_frmMain_liList,-1, WorkGalaxy\Name, "", 0)
ListEx::SetItemData(#Gadget_frmMain_liList, ListEx::CountItems(#Gadget_frmMain_liList)-1, WorkGalaxy\ID)
ListEx::SetState(#Gadget_frmMain_liList , ListEx::CountItems(#Gadget_frmMain_liList)-1)
Else ;update selected record
ListEx::SetItemText(#Gadget_frmMain_liList, ListEx::GetState(#Gadget_frmMain_liList), WorkGalaxy\Name,0)
EndIf
GalaxyClose()
EndIf
EndProcedure
The Save, FIRST calls the SetData, to get the screen changes, the calls the Galaxy Module Save Procedure. If you remember the First thing the Save Procedure does is Validate the Data. If the save Return False, we know the data is bad, and this Procedure does Nothing. Recall that the Validate Procedure displays any and All Errors to th4e User. If the Record is Saves or Updated, True is Returned. Now This procedure needs to Inset or Update the List of Galaxies on our Main Screen. But the Object or variable's Status AFTER Save is DB::#Clean. That us why BEFORE Saving we capture the preSave State, so now we can Evaluate this variable to know if we need to insert or update. When we are done we can call the Galaxy Close Procedure.
The GalaxyClose Procedure is called so as to funnel ALL closing of this Form through a single Procedure. In this manner any Housekeeping needed can be performed in ONE place here, before the Form Closes.
Procedure GalaxyClose()
Shared quitfrmGalaxy
If IsWindow(#Window_frmMain)
SetActiveWindow(#Window_frmMain)
DisableWindow(#Window_frmMain, #False)
EndIf
quitfrmGalaxy = #True
EndProcedure
If we had had Images working Variables or any memory references created in this code Scheet here is where we will insure, they are freed to prevent memory leaks.
This leaves our last Procedure, the Form Customization Procedure. As you have seen, and I have Explained, Here the Form's Theming is preformed, along with the Replacement of Standard to Custom Gadgets.
Procedure GalaxyCustom()
Define X, Y, W, H, Flags
;Convert the LidtIcinGadget to ListEX Custom Gadget
X = GadgetX(#Gadget_frmGalaxy_liSolarsystems) : Y = GadgetY(#Gadget_frmGalaxy_liSolarsystems)
W = GadgetWidth(#Gadget_frmGalaxy_liSolarsystems) : H = GadgetHeight(#Gadget_frmGalaxy_liSolarsystems)
FreeGadget(#Gadget_frmGalaxy_liSolarsystems)
;Now to create oue ListEx
Flags = ListEx::#AdjustColumns|ListEx::#ResizeColumn|ListEx::#GridLines;|ListEx::#AutoResize
ListEx::Gadget(#Gadget_frmGalaxy_liSolarsystems, X, Y, W, H, "", 25, "", Flags, #Window_frmMain)
EndProcedure
That concludes the Galaxy View Code detailed Explanation. Next we will take a look at the SolarSystem View Code.
Comments
Post a Comment