Interface AgnosticDialog

  • All Known Subinterfaces:
    QueriedAgnosticDialog
    All Known Implementing Classes:
    AbbreviationEditor, CatalogDialog, ColourDialog, CoreUpdateDialog, CustomPaperDialog, DictionaryEditor, ImageResourceBrowser, InsertCharsDialog, InsertImageDialog, LocaleSelectionDialog, NetworkProxy, NewDocumentDialog, NewProjectDialog, ParagraphDialog, Preferences, ProjectFolderDialog, RootEditor, StyleEditor, UILibraryDialogTemplate

    public interface AgnosticDialog
    This interface is used by PlatformSupport to handle OK and Cancel button ActionEvents for swappable dialogs. An AgnosticDialog can have the content and functionality of its "OK" and "Cancel" buttons switched so that it looks correct on platforms that order them differently. The term "dialog" is meant in a general sense. No particular implementing class is assumed.

    This interface allows the designer to write the appropriate code for handling "OK" (commit) and "Cancel" actions independently of the physical button they are attached to. PlatformSupport.makeAgnosticDialog(ca.cgjennings.platform.AgnosticDialog, javax.swing.JButton, javax.swing.JButton) will add listeners so that presses on these buttons are routed to the appropriate method below regardless of button order.

    For a greater degree of control, you can leave the implementation of these methods empty and use the return result of makeAgnosticDialog to perform whatever customization is required. In this case, implementing the interface simply serves as a marker that a given class has been modified to be agnostic. For control over how the contents of the two buttons are swapped, implement QueriedAgnosticDialog instead.

    Almost all existing dialogs can be converted to be agnostic with a minimum of effort. The following steps are usually all that is required:

    1. Add an implements AgnosticDialog to the root container class for the dialog (usually a JDialog subclass).
    2. Add empty implementations for handleOKAction and handleCancelAction.
    3. Move the existing ActionEvent listener code for the OK and Cancel buttons to the above methods.
    4. Delete the original listeners and listener registration code (if using a GUI designer, delete the event bindings in the designer).
    5. Add a call to PlatformSupport.makeAgnosticDialog( this, okBtn, cancelBtn ) (or one of its variants) after the components are initialized but before they are shown. This will usually be in the constructor.

    A typical use would look like this:

     public class MyDialog extends JDialog implements AgnosticDialog {
         public MyDialog( ... ) {
             super( ... );
             initComponents();
             // if buttons are in OK, Cancel order
             PlatformSupport.makeAgnosticDialog( this, okBtn, cancelBtn );
             // if buttons are in Cancel, OK order
             // PlatformSupport.makeAgnosticDialog( this, false, okBtn, cancelBtn );
             ...
         }
         private void initComponents() {
             ...
             okBtn = new JButton( "Do Something" );
             cancelBtn = new JButton( "Cancel" );
             ...
         }
         public void handleOKAction( ActionEvent e ) {
             // do something
             dispose();
         }
         public void handleCancelAction( ActionEvent e ) {
             dispose();
         }
     }
     
    Author:
    Chris Jennings
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void handleCancelAction​(java.awt.event.ActionEvent e)
      Performs whatever action is required when the Cancel button is pressed.
      void handleOKAction​(java.awt.event.ActionEvent e)
      Performs whatever action is required when the OK button is pressed.
    • Method Detail

      • handleOKAction

        void handleOKAction​(java.awt.event.ActionEvent e)
        Performs whatever action is required when the OK button is pressed. Called when the (possibly swapped) OK button generates an ActionEvent.
        Parameters:
        e - the ActionEvent generated by the button
      • handleCancelAction

        void handleCancelAction​(java.awt.event.ActionEvent e)
        Performs whatever action is required when the Cancel button is pressed. Called when the (possibly swapped) OK button generates an ActionEvent.
        Parameters:
        e - the ActionEvent generated by the button