Help: GUI Connection 2


Help is available for each task, or you can go straight to the solution source code.

Task 1

Determine the functionality of the application.

Now that you have built the GUI, you need to connect it to the kernel for your application.  The kernel is the part of your application that does all the real work.   Whenever you design an application, try to get as much separation between the GUI and kernel as possible.  Think of the kernel as something that could possible be run standalone, taking arguments from the command line.  You won't always be able to get that much separation, but it's a good goal and will make your design cleaner.

For this Magercise, you will connect the GUI you created in GUI Conversion 2 to a stub kernel that is provided.  If you were able to finish constructing the GUI, continue working in package magercises.gui2.  If you were not able to finish, there is a GUI pre-built in package magercises.gui2a.

This magercise will not go into as much help detail as GUI Connection 1.  Make sure you have worked through that Magercise first.

The functionality to implement will be as follows:

  • Selected->Open OR the Open button
    • call the kernel method openFile(String editor, String name) with the name of the selected file in the list
  • Selected->Command
    • bring up a dialog to get the name of the command to execute
    • call the kernel's execute(command, file) method
  • Edit->Clear List
    • calls the List's removeAll() method
  • Search button
    • calls the kernel's search(List, file, text) method
  • Help items
    • All help items should bring up a dialog that says "Abandon all help ye who enter here..."
  • All Drives, if checked, should disable the drive letter boxes.

The rest of the menu options and Buttons should just bring up a dialog that says "option not yet available".

This means that we need the following extra elements for our GUI:

  • An "option not yet available dialog"
  • An "Abandon all help ye who enter here..." dialog
  • A dialog that gets a string from the user.
  • A kernel object

No additional help for this task.

Task 2

Add the additional GUI elements that were mentioned in Task 1.
  1. Create the "option not available" dialog as you did for GUI Connection 1.  Don't forget to make the "Ok" button dispose() the dialog, and set the modal property of the dialog to true!
  2. Create the "Abandon all help..." dialog by copying the "option not available" dialog.  (Remember, copying a component is done by holding the control key and dragging the component.).  Again, make sure it's modal (it should be.)

Create another dialog to get a string from the user.  Do this the same way you created the number entry dialog for the GUI Connect 1 Magercise.

Finally, we add a kernel object -- it's magercises.gui2a.solution this time.

Task 3

Connect the GUI based on the stated behavior.

Lets take care of the easy ones first.  We have several items whose only action at this point is to bring up one of the message dialogs we created.

First, set up all the Help menu items and the Help button to bring up the "Abandon all help" dialog.

For each of the following menu items and buttons, connect them to the "Option not yet avaiable" dialog in the same manner.

  • File->Save
  • File->Save as
  • Selected->Process
  • Edit->Copy
  • all items on the Options menu
  • Stop button

Now would be an opportune time to hide all connections and save the bean...

All Drives Checkbox

This checkbox should have the effect of disabling all of the drive checkboxes.   You do this by calling setEnabled(!allDrivesState) on each drive checkbox.

The trick here is dealing with the "!" (not) part of the parameter.  You need to provide a filter for the parameter to modify its value.

We have provided a BoolOps class in the magercises.gui2a.solution package. It has a static method "not" that will return the boolean "not" of  a boolean parameter passed in.

Add a BoolOps variable to the design area.  Because the method we need is static, the variable will provide us a convenient way to acces it; we don't need an actual object created for it.  (Remember, Options->Add bean...")

Connect the itemStateChanged event of the "All Drives" checkbox to the enabled property of the "A" checkbox.  You'l need a parameter for this connection.

Connect the value parameter of this connection to the not(b) method of the BoolOps variable you created.  You'll need a parameter for this connection as well.

Connect the b parameter of the not() connection to the state property of the "All drives" checkbox.

Repeat this process for each of the drive letter checkboxes.

This is a nice way to make it very clear that if all drives is checked, the individual drives don't apply.

This is all we'll do with the checkboxes for this Magercise.

Edit->Clear List

This one's simple.  Just connect the actionPerformed method of this menu item to the removeAll() method of the list.

Selected->Open / Open Button

To perform this action, we need to call the kernel's openFile method passing the selected item in the list and the name of the editor to use.

  1. Connect the actionPerformed event for the Open button to the kernel's openFile(editor, selected) method.
  2. Connect the editor parameter to the Editor Filespec TextField's text property.
  3. Connect the selected parameter to the List's selectedItem property.

Repeat the above steps for the Selected->Open menu item.

Selected->Command

To perform this action, we need to call the kernel's execute method passing the selected item in the list and the name of a command to execute.

  1. Connect the actionPerformed event of the Selected->Command menu item to the show() method of the TextField dialog.  This brings up the dialog -- remember -- the dialog is modal, so it now has complete control.
  2. Connect the WindowOpened event of the dialog to the requestFocus() method of the TextField.  This is just a nice thing to do for the user, so he doesn't have to click the mouse to start entering text.
  3. In this magercise, we'll handle the dialog properly.  Because the dialog has control, we want to set it up so that when the user presses Enter,  the dialog is disposed.  (This just "hides" the dialog; it's data is still available.)   So connect the actionPerformed event of the TextField to the dipose() method of the dialog.  When the dispose is triggered, control will return to the main application.
  4. Connect the actionPerformed method of the Selected->Command menu item to the kernel's execute(command, file) method.
  5. Set the command parameter to the text propery of the TextField.
  6. Set the file parameter to the selectedItem propery of the List.

Search Button

This command will call the kernel's search(list, file, text) method.  (In real life this would probably also take state information about the drives that were checked, but we'll avoid that for this Magercise.)

The search method takes a reference to the List so it can fill in the data.  This is one of those places where sometimes you need to have abit more cohesion between the GUI and kernel.  In general, this is not a good idea, and if we really worked at it, we could create a better separation by writing a method in the GUI that takes an array of Strings and adds them to the List, then changing search() to return just an array of Strings.  ("Interested reader" time..)

For now, we'll do it the direct way.

  1. Connect the actionPerformed event of the Search button to the kernel's search(list, file, text) method.
  2. Connect the list parameter to the "this" property of the List.
  3. Connect the file parameter to the text property of the File to search for TextField.
  4. Connect the text parameter to the text property of the Text to search for TextField.

Dialog Locations

Once again, the dialogs are coming up al over the place.  Set their x,y constraint properties to 0,0.


Copyright © 1996-1997 MageLang Institute. All Rights Reserved.