MongoDB Tutorial 1: Basic Window App
MongoDB Tutorial 1 : Introduction | Alternative control | Basic Window App | Buttons 1 | Buttons 2 | Mongo-Start
|
|
MongoDB Standalone UniServer 6-Carbo. |
MongoDB
Introduction
Our main application is a Windows interface for running MongoDB. Interestingly the real work for this application has already been covered. We have a set of functions that perform all the functionality we need.
A windows application is nothing more than a pretty user interface. One redeeming feature is providing user feedback. If it was not for this I would suggest stick with batch files and scripts.
Is creating a Windows application difficult and complex? Yes if you want to do it the hard way by compiling and using winapi directly. Alternatively WinBinder makes it a snip; all you need to know is a small amount of PHP.
Never reinvent the wheel use one of the many examples as a template. Check out these links for additional information:
- PHP WinBinder: Introduction
- PHP WinBinder 2: Introduction
- PHP WinBinder 3: Introduction
- PHP WinBinder 4: Introduction
This tutorial is provided with a working template see below.
Window template
Template shown below
<?php chdir(dirname(__FILE__)); // Change wd to this files location include "winbinder/winbinder.php"; // Location of WinBinder library //=== Application name define("APPNAME", "MongoDB Control" ); //=== Button Text define("START_DB_BTXT", "Start MongoDB"); define("STOP_DB_BTXT", "Stop MongoDB"); define("START_CLIENT_BTXT", "Start Mongo Client"); define("OPEN_CMD_BTEXT", "Open cmd Window"); define("HELP_INFO_BTXT", "Help and Information"); //-- Constants ---------------------------------------------------------------- define('ID_DB_BUTTON', 1010); define('ID_CLIENT_BUTTON', 1020); define('ID_OPEN_CMD_BUTTON', 1030); define('ID_HELP_BUTTON', 1040); // Common variables $win_mongo; // Window $statusbar_mongo; // Status bar //=== 1) Create main window --------------------------------------------------- // create main window define("CAPTION", APPNAME); // Set caption to match application if(wb_get_instance(CAPTION, TRUE)) // Is there an existing instance? die; // Yes: bring it to the front and quit $win_mongo = wb_create_window(NULL, AppWindow, APPNAME, WBC_CENTER, WBC_CENTER, 207,190, 0); //=== 2) Create controls for the main window ---------------------------------- wb_create_control($win_mongo, Frame, "Mongo", 5, 5, 190, 147, 0, 0, 0, 0); wb_create_control($win_mongo, PushButton, START_DB_BTXT, 15, 25, 170, 25, ID_DB_BUTTON, 0, 0, 0); wb_create_control($win_mongo, PushButton, START_CLIENT_BTXT, 15, 55, 170, 25, ID_CLIENT_BUTTON, 0, 0, 0); wb_create_control($win_mongo, PushButton, OPEN_CMD_BTEXT, 15, 85, 170, 25, ID_OPEN_CMD_BUTTON, 0, 0, 0); wb_create_control($win_mongo, PushButton, HELP_INFO_BTXT, 15, 115, 170, 25, ID_HELP_BUTTON, 0, 0, 0); //=== 3) Assign handler function to the main window -------------------------- wb_set_handler($win_mongo, "process_mongo"); wb_set_image($win_mongo, getcwd()."/images/utray.ico"); // Add logo //=== 5) Enter application loop ----------------------------------------------- wb_main_loop(); //=== 4) Handler Function ----------------------------------------------------- function process_mongo($window, $id){ global $win_mongo; global $statusbar_mongo; switch($id) { case 1010: case 1020: wb_message_box($window, "Button #$id was pressed."); break; case IDCLOSE: // The constant IDCLOSE is predefined wb_destroy_window($window); // Destroy the window break; } } //------------------------------------------------------------------ END OF FILE ?>
Overview
Collectively it looks complex however taking small chunks it really is simple:
|
Template produces the following: |
Initialisation
When our application is run we have an opportunity to nag a user. We know the server must be shutdown before turning PC off; hence inform a new user of this fact. Allow users to disable nagging, this can be implemented by creating a file.
Add the following line to mongo_db_inc.php
define("NO_NAG_TXT", "$mongo_base_f/mongodb_1/control/no_nag.txt"); // Dummy file
A user may have intentionally closed the application with server running. On resting the application check for server running and toggle button text accordingly.
Add the following function to end of mongo_tutorial.phpw
//=== Initialise Main ========================================================= function mongo_init(){ global $win_mongo; //=== Nag User //== Checkuser disabled nagging if(!file_exists(NO_NAG_TXT)){ // File not found $str = "Before turning your PC off \n"; $str .= "Ensure you have closed MongoDB \n\n"; $str .= "Failure to do will result in data loss\n"; $str .= "Data from RAM needs to be written to disk.\n\n"; $str .= "Display this alert next time you start MongoDB?"; if(!wb_message_box($win_mongo, $str, "IMPORTANT", WBC_YESNO)){ // User wants to disable nagging hence create file //== Create or update a new redirect page $no_nag[] = "To enable nagging\n"; $no_nag[] = "Delete this file\n"; $str_no_nag = implode($no_nag); // Convert array to string file_put_contents(NO_NAG_TXT,$str_no_nag); // Save string to file } } //=== Check mongo runnung if(mongodb_running()){ // MongoDB running wb_set_text(wb_get_control($win_mongo, ID_DB_BUTTON),STOP_DB_BTXT); // set button text } else{ // MongoDB not running wb_set_text(wb_get_control($win_mongo, ID_DB_BUTTON),START_DB_BTXT); // set button text wb_set_enabled(wb_get_control($win_mongo, ID_CLIENT_BUTTON), FALSE); // Disable Client button } }//=================================================== END Initialise Main ===
We need to call this function add the following line to end of section 3
mongo_init();
Currently our script has no access to the functions at top of page add the following line
include_once "mongo_db_inc.php";
Test
|
Summary
With a working window we need to add functionality to each button.
Net page covers lower two buttons.