package apps.it;

import ncs.db.*;
import ncs.app.*;
import ncs.app.event.*;
import ncs.fields.*;
import ncs.fields.event.*;
import ncs.util.*; 
import apps.master.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;


// Module Maker 
public class ModuleMaker extends FormModule {
  // Internal variables           
  
  private Container pane; 

  // Global variables from markup
  private BoundDouble moduleField;
  private FixedComboBox typeField;
  private FixedComboBox appField;
  private FixedComboBox dbroleField;
  private CharField nameField;
  private CharField menuField;
  private CharField baseTableField;
  private CharCheckBox hasClearButtonField;
  private CharCheckBox hasQueryButtonField;
  private CharCheckBox hasNewButtonField;
  private CharCheckBox hasEditButtonField;
  private CharCheckBox hasDeleteButtonField;
  private CharCheckBox queryOnNextField;
  private FixedComboBox defaultModeField;
  private FixedComboBox afterNewField;
  

  // Field labels
  private JLabel moduleFieldLabel = new JLabel("");
  private JLabel typeFieldLabel = new JLabel("Type:");
  private JLabel appFieldLabel = new JLabel("App:");
  private JLabel dbroleFieldLabel = new JLabel("DB Role:");
  private JLabel nameFieldLabel = new JLabel("Name:");
  private JLabel menuFieldLabel = new JLabel("Menu:");
  private JLabel baseTableFieldLabel = new JLabel("Base Table:");
  private JLabel hasClearButtonFieldLabel = new JLabel(" ");
  private JLabel hasQueryButtonFieldLabel = new JLabel(" ");
  private JLabel hasNewButtonFieldLabel = new JLabel(" ");
  private JLabel hasEditButtonFieldLabel = new JLabel(" ");
  private JLabel hasDeleteButtonFieldLabel = new JLabel(" ");
  private JLabel queryOnNextFieldLabel = new JLabel(" ");
  private JLabel defaultModeFieldLabel = new JLabel("Startup Mode:");
  private JLabel afterNewFieldLabel = new JLabel("After New Mode:");
  

  // Constructor: (Create new ModuleMaker instance)
  public ModuleMaker (BasicApp app) throws SQLException {
    super("Module Maker", app, new Role("it"));    
    pane = frame.getContentPane(); 
    panel = this; 

    defaultQueryParameters = new QueryParameters (); 

    cache = new BindingCache(getDbInstance(), 
          "MODULES");
          
    initFields();
    initFrame();
    initialize();
    initStartup(); 
  }

  // Perform startup code
  private void initStartup() { 
    clearForm(); 
  }

  // Initialize frame 
  private void initFrame () { 
    panel.setLayout ( 
       new javax.swing.BoxLayout (panel, javax.swing.BoxLayout.Y_AXIS)); 
       
    panel.setBorder (BorderFactory.createEmptyBorder (12,12,0,12)); 

    setNavigationPanel (new NavigationPanel (getFrame().getApp(), 
                                             NavigationPanel.FORM_STYLE, 
                                             true, true, 
                                             true, true, true));

    setButtonPanel (new ButtonPanel ());
    
    pane.add ("South",buttons); 
    pane.add ("North",navigation);
    pane.add ("Center",panel); 	
    
    // Create layout panels
    JPanel modulecol2Panel = new JPanel();
      JPanel modulecol2Panel0 = new JPanel();
      JPanel modulecol2Panel1 = new JPanel();
      JPanel modulecol2Panel2 = new JPanel();
    JPanel modulePanel = new JPanel();
      JPanel modulePanel0 = new JPanel();
      JPanel modulePanel1 = new JPanel();
      JPanel modulePanel2 = new JPanel();
      JPanel modulePanel3 = new JPanel();
      JPanel modulePanel4 = new JPanel();
      JPanel modulePanel5 = new JPanel();
    JPanel modulecol1Panel = new JPanel();
      JPanel modulecol1Panel0 = new JPanel();
      JPanel modulecol1Panel1 = new JPanel();
      JPanel modulecol1Panel2 = new JPanel();
    JPanel layoutPanel = new JPanel();
    JPanel mainPanel = new JPanel();
      JPanel mainPanel0 = new JPanel();
      JPanel mainPanel1 = new JPanel();
    JPanel fieldPanel = new JPanel();
    JTabbedPane tabsPane = new JTabbedPane(JTabbedPane.TOP);
    JPanel tabsPanel = new JPanel();
    
    // Setup Layout Managers
    modulecol2Panel.setLayout (new BoxLayout (modulecol2Panel, BoxLayout.Y_AXIS));
      modulecol2Panel0.setLayout (new BoxLayout (modulecol2Panel0, BoxLayout.X_AXIS));
      modulecol2Panel1.setLayout (new BoxLayout (modulecol2Panel1, BoxLayout.X_AXIS));
      modulecol2Panel2.setLayout (new BoxLayout (modulecol2Panel2, BoxLayout.X_AXIS));
    modulePanel.setLayout (new BoxLayout (modulePanel, BoxLayout.Y_AXIS));
      modulePanel0.setLayout (new BoxLayout (modulePanel0, BoxLayout.X_AXIS));
      modulePanel1.setLayout (new BoxLayout (modulePanel1, BoxLayout.X_AXIS));
      modulePanel2.setLayout (new BoxLayout (modulePanel2, BoxLayout.X_AXIS));
      modulePanel3.setLayout (new BoxLayout (modulePanel3, BoxLayout.X_AXIS));
      modulePanel4.setLayout (new BoxLayout (modulePanel4, BoxLayout.X_AXIS));
      modulePanel5.setLayout (new BoxLayout (modulePanel5, BoxLayout.X_AXIS));
    modulecol1Panel.setLayout (new BoxLayout (modulecol1Panel, BoxLayout.Y_AXIS));
      modulecol1Panel0.setLayout (new BoxLayout (modulecol1Panel0, BoxLayout.X_AXIS));
      modulecol1Panel1.setLayout (new BoxLayout (modulecol1Panel1, BoxLayout.X_AXIS));
      modulecol1Panel2.setLayout (new BoxLayout (modulecol1Panel2, BoxLayout.X_AXIS));
    layoutPanel.setLayout (new BoxLayout (layoutPanel, BoxLayout.Y_AXIS));
    mainPanel.setLayout (new BoxLayout (mainPanel, BoxLayout.Y_AXIS));
      mainPanel0.setLayout (new BoxLayout (mainPanel0, BoxLayout.X_AXIS));
      mainPanel1.setLayout (new BoxLayout (mainPanel1, BoxLayout.X_AXIS));
    fieldPanel.setLayout (new BoxLayout (fieldPanel, BoxLayout.Y_AXIS));
    tabsPanel.setLayout (new BoxLayout (tabsPanel, BoxLayout.Y_AXIS));
    
    ///////////////////////
    // Setup modulecol2 section
    
    modulecol2Panel0.add(hasNewButtonFieldLabel);
    modulecol2Panel0.add(Box.createRigidArea (new Dimension (9,0)) );
    modulecol2Panel0.add(hasNewButtonField);
    modulecol2Panel0.add(Box.createHorizontalGlue());
    
    modulecol2Panel1.add(hasEditButtonFieldLabel);
    modulecol2Panel1.add(Box.createRigidArea (new Dimension (9,0)) );
    modulecol2Panel1.add(hasEditButtonField);
    modulecol2Panel1.add(Box.createHorizontalGlue());
    
    modulecol2Panel2.add(hasDeleteButtonFieldLabel);
    modulecol2Panel2.add(Box.createRigidArea (new Dimension (9,0)) );
    modulecol2Panel2.add(hasDeleteButtonField);
    modulecol2Panel2.add(Box.createHorizontalGlue());
    
    modulecol2Panel.add(modulecol2Panel0);
    modulecol2Panel.add(Box.createRigidArea (new Dimension (0,4)) );
    modulecol2Panel.add(modulecol2Panel1);
    modulecol2Panel.add(Box.createRigidArea (new Dimension (0,4)) );
    modulecol2Panel.add(modulecol2Panel2);
    modulecol2Panel.add(Box.createRigidArea (new Dimension (0,4)) );
    
    
    ///////////////////////
    // Setup module section
    
    modulePanel0.add(typeFieldLabel);
    modulePanel0.add(Box.createRigidArea (new Dimension (9,0)) );
    modulePanel0.add(typeField);
    modulePanel0.add(Box.createHorizontalGlue());
    
    modulePanel1.add(appFieldLabel);
    modulePanel1.add(Box.createRigidArea (new Dimension (9,0)) );
    modulePanel1.add(appField);
    modulePanel1.add(Box.createRigidArea (new Dimension (9,0)) );
    modulePanel1.add(Box.createHorizontalGlue());
    modulePanel1.add(menuFieldLabel);
    modulePanel1.add(Box.createRigidArea (new Dimension (9,0)) );
    modulePanel1.add(menuField);
    modulePanel2.add(dbroleFieldLabel);
    modulePanel2.add(Box.createRigidArea (new Dimension (9,0)) );
    modulePanel2.add(dbroleField);
    modulePanel2.add(Box.createRigidArea (new Dimension (9,0)) );
    modulePanel2.add(Box.createHorizontalGlue());
    modulePanel2.add(baseTableFieldLabel);
    modulePanel2.add(Box.createRigidArea (new Dimension (9,0)) );
    modulePanel2.add(baseTableField);
    modulePanel3.add(Box.createRigidArea (new Dimension (9,9)) );
    modulePanel3.add(Box.createHorizontalGlue());
    
    modulePanel4.add(modulecol1Panel);
    modulePanel4.add(Box.createRigidArea (new Dimension (9,0)) );
    modulePanel4.add(modulecol2Panel);
    modulePanel4.add(Box.createHorizontalGlue());
    
    modulePanel5.add(afterNewFieldLabel);
    modulePanel5.add(Box.createRigidArea (new Dimension (9,0)) );
    modulePanel5.add(afterNewField);
    modulePanel5.add(Box.createHorizontalGlue());
    
    modulePanel.add(modulePanel0);
    modulePanel.add(Box.createRigidArea (new Dimension (0,4)) );
    modulePanel.add(modulePanel1);
    modulePanel.add(Box.createRigidArea (new Dimension (0,4)) );
    modulePanel.add(modulePanel2);
    modulePanel.add(Box.createRigidArea (new Dimension (0,4)) );
    modulePanel.add(modulePanel3);
    modulePanel.add(Box.createRigidArea (new Dimension (0,4)) );
    modulePanel.add(modulePanel4);
    modulePanel.add(Box.createRigidArea (new Dimension (0,4)) );
    modulePanel.add(modulePanel5);
    modulePanel.add(Box.createRigidArea (new Dimension (0,4)) );
    
    DisplayUtils.setGreatestWidth( new JComponent[]{ (JComponent)afterNewFieldLabel });
    DisplayUtils.setGreatestWidth( new JComponent[]{ (JComponent)menuFieldLabel,
                        (JComponent)baseTableFieldLabel });
    DisplayUtils.setGreatestWidth( new JComponent[]{ (JComponent)typeFieldLabel,
                        (JComponent)appFieldLabel,
                        (JComponent)dbroleFieldLabel });
    
    ///////////////////////
    // Setup modulecol1 section
    
    modulecol1Panel0.add(hasClearButtonFieldLabel);
    modulecol1Panel0.add(Box.createRigidArea (new Dimension (9,0)) );
    modulecol1Panel0.add(hasClearButtonField);
    modulecol1Panel0.add(Box.createHorizontalGlue());
    
    modulecol1Panel1.add(hasQueryButtonFieldLabel);
    modulecol1Panel1.add(Box.createRigidArea (new Dimension (9,0)) );
    modulecol1Panel1.add(hasQueryButtonField);
    modulecol1Panel1.add(Box.createHorizontalGlue());
    
    modulecol1Panel2.add(queryOnNextFieldLabel);
    modulecol1Panel2.add(Box.createRigidArea (new Dimension (9,0)) );
    modulecol1Panel2.add(queryOnNextField);
    modulecol1Panel2.add(Box.createHorizontalGlue());
    
    modulecol1Panel.add(modulecol1Panel0);
    modulecol1Panel.add(Box.createRigidArea (new Dimension (0,4)) );
    modulecol1Panel.add(modulecol1Panel1);
    modulecol1Panel.add(Box.createRigidArea (new Dimension (0,4)) );
    modulecol1Panel.add(modulecol1Panel2);
    modulecol1Panel.add(Box.createRigidArea (new Dimension (0,4)) );
    
    
    ///////////////////////
    // Setup layout section
    
    
    
    ///////////////////////
    // Setup main section
    
    mainPanel0.add(nameFieldLabel);
    mainPanel0.add(Box.createRigidArea (new Dimension (9,0)) );
    mainPanel0.add(nameField);
    mainPanel0.add(Box.createHorizontalGlue());
    
    mainPanel1.add(tabsPanel);
    mainPanel1.add(Box.createHorizontalGlue());
    
    mainPanel.add(mainPanel0);
    mainPanel.add(Box.createRigidArea (new Dimension (0,4)) );
    mainPanel.add(mainPanel1);
    mainPanel.add(Box.createRigidArea (new Dimension (0,4)) );
    
    
    ///////////////////////
    // Setup field section
    
    
    
    ///////////////////////
    // Setup tabs section
    tabsPanel.add (tabsPane);
    // Adding to tabbed panel
    tabsPane.addTab ("Module",modulePanel);
      modulePanel.setBorder (BorderFactory.createEmptyBorder (10,10,9,9));
    tabsPane.addTab ("Fields",fieldPanel);
      fieldPanel.setBorder (BorderFactory.createEmptyBorder (10,10,9,9));
    tabsPane.addTab ("Layout",layoutPanel);
      layoutPanel.setBorder (BorderFactory.createEmptyBorder (10,10,9,9));
    
    
    
    // Setup alignment
    hasNewButtonFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    hasNewButtonField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    hasEditButtonFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    hasEditButtonField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    hasDeleteButtonFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    hasDeleteButtonField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    typeFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    typeField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    appFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    appField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    menuFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    menuField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    dbroleFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    dbroleField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    baseTableFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    baseTableField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    modulecol1Panel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    modulecol2Panel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    afterNewFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    afterNewField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    hasClearButtonFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    hasClearButtonField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    hasQueryButtonFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    hasQueryButtonField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    queryOnNextFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    queryOnNextField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    nameFieldLabel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    nameField.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    tabsPanel.setAlignmentY ((int)JPanel.TOP_ALIGNMENT);
    
    // Add layout panel to frame
    panel.add (mainPanel);  
  }

  //Initialize all fields
  private void initFields() throws SQLException { 

    // Create fields
    moduleField = new BoundDouble ();
    typeField = new FixedComboBox (new String[][]{{"M","Module"},{"O","Object"},{"T","Table"}});
    appField = new FixedComboBox (new String[][]{{"O","Operations"},{"A","Accounting"},{"I","IT"}});
    dbroleField = new FixedComboBox (new String[][]{{"O","Operations"},{"A","Accounting"},{"I","IT"}});
    nameField = new CharField (30,20,CharField.MIXED_CASE);
    menuField = new CharField (30,20,CharField.MIXED_CASE);
    baseTableField = new CharField (30,20,CharField.UPPER_CASE);
    hasClearButtonField = new CharCheckBox ("Y","N","Clear Button?");
    hasQueryButtonField = new CharCheckBox ("Y","N","Active Button?");
    hasNewButtonField = new CharCheckBox ("Y","N","New Button?");
    hasEditButtonField = new CharCheckBox ("Y","N","Edit Button?");
    hasDeleteButtonField = new CharCheckBox ("Y","N","Delete Button?");
    queryOnNextField = new CharCheckBox ("Y","N","Query on Next?");
    defaultModeField = new FixedComboBox (new String[][]{{"C","Clear"},{"Q","Query"},{"N","New"}});
    afterNewField = new FixedComboBox (new String[][]{{"C","Clear"},{"Q","Query"},{"N","New"}});
    

    // Bind fields
    cache.bind("MODULE",moduleField);
    cache.bind("TYPE",typeField);
    cache.bind("APP",appField);
    cache.bind("DBROLE",dbroleField);
    cache.bind("NAME",nameField);
    cache.bind("MENU",menuField);
    cache.bind("BASE_TABLE",baseTableField);
    cache.bind("HAS_CLEAR_BUTTON",hasClearButtonField);
    cache.bind("HAS_QUERY_BUTTON",hasQueryButtonField);
    cache.bind("HAS_NEW_BUTTON",hasNewButtonField);
    cache.bind("HAS_EDIT_BUTTON",hasEditButtonField);
    cache.bind("HAS_DELETE_BUTTON",hasDeleteButtonField);
    cache.bind("QUERY_ON_NEXT",queryOnNextField);
    cache.bind("DEFAULT_MODE",defaultModeField);
    cache.bind("AFTER_NEW",afterNewField);
    

    // Init fields
    moduleField.setReadOnly(true);
    typeField.setReadOnly(false);
    appField.setReadOnly(false);
    dbroleField.setReadOnly(false);
    nameField.setReadOnly(false);
    menuField.setReadOnly(false);
    baseTableField.setReadOnly(false);
    hasClearButtonField.setReadOnly(false);
    hasQueryButtonField.setReadOnly(false);
    hasNewButtonField.setReadOnly(false);
    hasEditButtonField.setReadOnly(false);
    hasDeleteButtonField.setReadOnly(false);
    queryOnNextField.setReadOnly(false);
    defaultModeField.setReadOnly(false);
    afterNewField.setReadOnly(false);
    

    // Associate field labels with fields 
    moduleFieldLabel.setLabelFor(moduleField);
    typeFieldLabel.setLabelFor(typeField);
    appFieldLabel.setLabelFor(appField);
    dbroleFieldLabel.setLabelFor(dbroleField);
    nameFieldLabel.setLabelFor(nameField);
    menuFieldLabel.setLabelFor(menuField);
    baseTableFieldLabel.setLabelFor(baseTableField);
    hasClearButtonFieldLabel.setLabelFor(hasClearButtonField);
    hasQueryButtonFieldLabel.setLabelFor(hasQueryButtonField);
    hasNewButtonFieldLabel.setLabelFor(hasNewButtonField);
    hasEditButtonFieldLabel.setLabelFor(hasEditButtonField);
    hasDeleteButtonFieldLabel.setLabelFor(hasDeleteButtonField);
    queryOnNextFieldLabel.setLabelFor(queryOnNextField);
    defaultModeFieldLabel.setLabelFor(defaultModeField);
    afterNewFieldLabel.setLabelFor(afterNewField);
    
    
    // Add form object listeners (to cascade triggers) 
    addFormWidget(moduleField);
    addFormWidget(typeField);
    addFormWidget(appField);
    addFormWidget(dbroleField);
    addFormWidget(nameField);
    addFormWidget(menuField);
    addFormWidget(baseTableField);
    addFormWidget(hasClearButtonField);
    addFormWidget(hasQueryButtonField);
    addFormWidget(hasNewButtonField);
    addFormWidget(hasEditButtonField);
    addFormWidget(hasDeleteButtonField);
    addFormWidget(queryOnNextField);
    addFormWidget(defaultModeField);
    addFormWidget(afterNewField);
    

    // Finalize binding 
    cache.bound(); 

  }




  /////////////////////////////
  // Enable/Disable navigation
  /////////////////////////////
  public void disableNavigation () { 
      navigation.setPrevEnabled (false);
      navigation.setNextEnabled (false);
      navigation.setClearEnabled (false);
      navigation.setNewEnabled (false);
      navigation.setEditEnabled (false);
      navigation.setDeleteEnabled (false);
  }
    
  ////////////////////////////
  // Set current mode
  ////////////////////////////
  public void setMode (int mode) { 
    if (getMode() != mode) { 
      super.setMode(mode); 
      switch (mode) { 
        case Triggerable.EMPTY:
          moduleField.setEnabled(false);
          typeField.setEnabled(false);
          appField.setEnabled(false);
          dbroleField.setEnabled(false);
          nameField.setEnabled(false);
          menuField.setEnabled(false);
          baseTableField.setEnabled(false);
          hasClearButtonField.setEnabled(false);
          hasQueryButtonField.setEnabled(false);
          hasNewButtonField.setEnabled(false);
          hasEditButtonField.setEnabled(false);
          hasDeleteButtonField.setEnabled(false);
          queryOnNextField.setEnabled(false);
          defaultModeField.setEnabled(false);
          afterNewField.setEnabled(false);
          
          break;
        case Triggerable.INSERTING:
          moduleField.setEnabled(false);
          typeField.setEnabled(true);
          appField.setEnabled(true);
          dbroleField.setEnabled(true);
          nameField.setEnabled(true);
          menuField.setEnabled(true);
          baseTableField.setEnabled(true);
          hasClearButtonField.setEnabled(true);
          hasQueryButtonField.setEnabled(true);
          hasNewButtonField.setEnabled(true);
          hasEditButtonField.setEnabled(true);
          hasDeleteButtonField.setEnabled(true);
          queryOnNextField.setEnabled(true);
          defaultModeField.setEnabled(true);
          afterNewField.setEnabled(true);
          
          break; 
        case Triggerable.UPDATING:
          moduleField.setEnabled(false);
          typeField.setEnabled(true);
          appField.setEnabled(true);
          dbroleField.setEnabled(true);
          nameField.setEnabled(true);
          menuField.setEnabled(true);
          baseTableField.setEnabled(true);
          hasClearButtonField.setEnabled(true);
          hasQueryButtonField.setEnabled(true);
          hasNewButtonField.setEnabled(true);
          hasEditButtonField.setEnabled(true);
          hasDeleteButtonField.setEnabled(true);
          queryOnNextField.setEnabled(true);
          defaultModeField.setEnabled(true);
          afterNewField.setEnabled(true);
          
          break;
        case Triggerable.QUERYING:
          moduleField.setEnabled(false);
          typeField.setEnabled(false);
          appField.setEnabled(false);
          dbroleField.setEnabled(false);
          nameField.setEnabled(false);
          menuField.setEnabled(false);
          baseTableField.setEnabled(false);
          hasClearButtonField.setEnabled(false);
          hasQueryButtonField.setEnabled(false);
          hasNewButtonField.setEnabled(false);
          hasEditButtonField.setEnabled(false);
          hasDeleteButtonField.setEnabled(false);
          queryOnNextField.setEnabled(false);
          defaultModeField.setEnabled(false);
          afterNewField.setEnabled(false);
          
          break; 
        case Triggerable.DELETING:
          break; 
      }
    }
  }
  
  //////////////////////////
  // Record navigation 
  //////////////////////////

  private void setQueryModeButtons () { 
    try {
      buttons.setDisabled(); 
      navigation.setQueryEnabled (true); 
      navigation.setPrevEnabled (cache.hasPrevious());
      navigation.setNextEnabled (cache.hasNext());
      navigation.setClearEnabled (true); 
      navigation.setNewEnabled (true);
      navigation.setEditEnabled (true);
      navigation.setDeleteEnabled (true);
    } catch (SQLException ex) {
      getDbManager().handleSQLError (ex, this);    
    }
  }  

  public void next () { 
      // Generate a result set if one does not exist
      if ( !cache.hasResultSet() )
        executeQuery(); 
      else 
        super.next(); 
      setQueryModeButtons(); 
  } 
 
  public void previous () { 
      super.previous(); 
      buttons.setDisabled(); 
      setQueryModeButtons(); 
  }

  public void clearForm () { 
//    try {
      super.clearForm(); 
      buttons.setDisabled(); 
      navigation.setQueryEnabled (true); 
      navigation.setPrevEnabled (false);
      navigation.setNextEnabled (true);

      navigation.setNewEnabled (true);
      navigation.setEditEnabled (false);
      navigation.setDeleteEnabled (false);
//    } catch (SQLException ex) {
//      getDbManager().handleSQLError (ex, this);       
//    }
  }

  public void createRecord () { 
      super.createRecord();
      
      buttons.setSaveEnabled (true); 
      buttons.setCancelEnabled (true); 
      buttons.setSaveLabel ("Create"); 
      navigation.setQueryEnabled (false); 
      navigation.setPrevEnabled (false);
      navigation.setNextEnabled (false);
      navigation.setNewEnabled (false);
      navigation.setEditEnabled (false);
      navigation.setDeleteEnabled (false);
  }

  public void editRecord () { 
    super.editRecord(); 
      buttons.setSaveEnabled (true); 
      buttons.setCancelEnabled (true); 
      buttons.setSaveLabel ("Update"); 
      navigation.setQueryEnabled (true); 
      navigation.setPrevEnabled (false);
      navigation.setNextEnabled (false);
      navigation.setNewEnabled (false);
      navigation.setEditEnabled (false);
  } 

  protected void reinstateQuery () { 
    setQueryModeButtons();     
  }

 
  /********************************************
   *  See if any required fields are missing  *
   *  Overloads FormBasis.getMissingFields()  *
   ********************************************/
  
  protected Bindable[] getMissingFields () { 
    ArrayList missingFields = new ArrayList(); 
    System.out.println ("Checking required fields"); 
    
    if (!typeField.hasValue()) { 
      missingFields.add (typeField); 
      System.out.println ("typeField has not been entered!"); 
    }
    if (!appField.hasValue()) { 
      missingFields.add (appField); 
      System.out.println ("appField has not been entered!"); 
    }
    if (!dbroleField.hasValue()) { 
      missingFields.add (dbroleField); 
      System.out.println ("dbroleField has not been entered!"); 
    }
    if (!hasClearButtonField.hasValue()) { 
      missingFields.add (hasClearButtonField); 
      System.out.println ("hasClearButtonField has not been entered!"); 
    }
    if (!hasQueryButtonField.hasValue()) { 
      missingFields.add (hasQueryButtonField); 
      System.out.println ("hasQueryButtonField has not been entered!"); 
    }
    if (!hasNewButtonField.hasValue()) { 
      missingFields.add (hasNewButtonField); 
      System.out.println ("hasNewButtonField has not been entered!"); 
    }
    if (!hasEditButtonField.hasValue()) { 
      missingFields.add (hasEditButtonField); 
      System.out.println ("hasEditButtonField has not been entered!"); 
    }
    if (!hasDeleteButtonField.hasValue()) { 
      missingFields.add (hasDeleteButtonField); 
      System.out.println ("hasDeleteButtonField has not been entered!"); 
    }
    if (!queryOnNextField.hasValue()) { 
      missingFields.add (queryOnNextField); 
      System.out.println ("queryOnNextField has not been entered!"); 
    }
    if (!defaultModeField.hasValue()) { 
      missingFields.add (defaultModeField); 
      System.out.println ("defaultModeField has not been entered!"); 
    }
    if (!afterNewField.hasValue()) { 
      missingFields.add (afterNewField); 
      System.out.println ("afterNewField has not been entered!"); 
    }
    
    
    return (Bindable[])missingFields.toArray (new Bindable[]{}); 
  }


  protected void initQueryFields () {
     queryFields = new Object[][]{
           {new JLabel("Name:"),"NAME",new CharField (30,20,CharField.MIXED_CASE)},
                      {null,null,null}};   
  }
  
  ////////////////////////////
  //Application level triggers
  ////////////////////////////
  public boolean validateModuleField() {
    return (true);
  }
  
  public boolean focusGainedModuleField() {
    return (true);
  }
  
  public boolean focusLostModuleField() {
    return (true);
  }
  
  public boolean validateTypeField() {
    return (true);
  }
  
  public boolean focusGainedTypeField() {
    return (true);
  }
  
  public boolean focusLostTypeField() {
    return (true);
  }
  
  public boolean validateAppField() {
    return (true);
  }
  
  public boolean focusGainedAppField() {
    return (true);
  }
  
  public boolean focusLostAppField() {
    return (true);
  }
  
  public boolean validateDbroleField() {
    return (true);
  }
  
  public boolean focusGainedDbroleField() {
    return (true);
  }
  
  public boolean focusLostDbroleField() {
    return (true);
  }
  
  public boolean validateNameField() {
    return (true);
  }
  
  public boolean focusGainedNameField() {
    return (true);
  }
  
  public boolean focusLostNameField() {
    return (true);
  }
  
  public boolean validateMenuField() {
    return (true);
  }
  
  public boolean focusGainedMenuField() {
    return (true);
  }
  
  public boolean focusLostMenuField() {
    return (true);
  }
  
  public boolean validateBaseTableField() {
    return (true);
  }
  
  public boolean focusGainedBaseTableField() {
    return (true);
  }
  
  public boolean focusLostBaseTableField() {
    return (true);
  }
  
  public boolean validateHasClearButtonField() {
    return (true);
  }
  
  public boolean focusGainedHasClearButtonField() {
    return (true);
  }
  
  public boolean focusLostHasClearButtonField() {
    return (true);
  }
  
  public boolean validateHasQueryButtonField() {
    return (true);
  }
  
  public boolean focusGainedHasQueryButtonField() {
    return (true);
  }
  
  public boolean focusLostHasQueryButtonField() {
    return (true);
  }
  
  public boolean validateHasNewButtonField() {
    return (true);
  }
  
  public boolean focusGainedHasNewButtonField() {
    return (true);
  }
  
  public boolean focusLostHasNewButtonField() {
    return (true);
  }
  
  public boolean validateHasEditButtonField() {
    return (true);
  }
  
  public boolean focusGainedHasEditButtonField() {
    return (true);
  }
  
  public boolean focusLostHasEditButtonField() {
    return (true);
  }
  
  public boolean validateHasDeleteButtonField() {
    return (true);
  }
  
  public boolean focusGainedHasDeleteButtonField() {
    return (true);
  }
  
  public boolean focusLostHasDeleteButtonField() {
    return (true);
  }
  
  public boolean validateQueryOnNextField() {
    return (true);
  }
  
  public boolean focusGainedQueryOnNextField() {
    return (true);
  }
  
  public boolean focusLostQueryOnNextField() {
    return (true);
  }
  
  public boolean validateDefaultModeField() {
    return (true);
  }
  
  public boolean focusGainedDefaultModeField() {
    return (true);
  }
  
  public boolean focusLostDefaultModeField() {
    return (true);
  }
  
  public boolean validateAfterNewField() {
    return (true);
  }
  
  public boolean focusGainedAfterNewField() {
    return (true);
  }
  
  public boolean focusLostAfterNewField() {
    return (true);
  }
  
  public boolean initialize() {
    return true; 
  }
  
  public boolean whenNewRecord() {
    if (!super.whenNewRecord())
      return (false);
  
   
    if ( isInserting() ) {
      try { 
        moduleField.setValue ( getSequence ("module_seq") );
        System.out.println ("Module # " + String.valueOf (moduleField.getValue())); 
      } catch (SQLException ex) {
        getDbManager().handleSQLError (ex, this);    
        return false;
      }
    }
   
    return true; 
  }
  
  

  ////////////////////////////
  // User-Defined Methods
  ////////////////////////////
   
  

  ////////////////////////////
  public boolean executeTrigger (EventObject e, String trigger) {
    
    // form triggers
    if (trigger.equals ("whenSaving")) 
      return (whenSaving());

    else if (trigger.equals ("whenCanceling")) 
      return (whenCanceling());
    
    else if (trigger.equals ("whenNewRecord")) 
      return (whenNewRecord());

    else if (trigger.equals ("whenExiting")) 
      return (whenExiting());
    
    else if (trigger.equals ("whenInsertRecord")) 
      return (whenInsertRecord());

    else if (trigger.equals ("whenDeleteRecord")) 
      return (whenDeleteRecord());

    // field triggers
    else if (trigger.equals("validateModuleField"))
      return (validateModuleField());
     
    else if (trigger.equals("focusGainedModuleField"))
      return (focusGainedModuleField());
     
    else if (trigger.equals("focusLostModuleField"))
      return (focusLostModuleField());
     
    else if (trigger.equals("validateTypeField"))
      return (validateTypeField());
     
    else if (trigger.equals("focusGainedTypeField"))
      return (focusGainedTypeField());
     
    else if (trigger.equals("focusLostTypeField"))
      return (focusLostTypeField());
     
    else if (trigger.equals("validateAppField"))
      return (validateAppField());
     
    else if (trigger.equals("focusGainedAppField"))
      return (focusGainedAppField());
     
    else if (trigger.equals("focusLostAppField"))
      return (focusLostAppField());
     
    else if (trigger.equals("validateDbroleField"))
      return (validateDbroleField());
     
    else if (trigger.equals("focusGainedDbroleField"))
      return (focusGainedDbroleField());
     
    else if (trigger.equals("focusLostDbroleField"))
      return (focusLostDbroleField());
     
    else if (trigger.equals("validateNameField"))
      return (validateNameField());
     
    else if (trigger.equals("focusGainedNameField"))
      return (focusGainedNameField());
     
    else if (trigger.equals("focusLostNameField"))
      return (focusLostNameField());
     
    else if (trigger.equals("validateMenuField"))
      return (validateMenuField());
     
    else if (trigger.equals("focusGainedMenuField"))
      return (focusGainedMenuField());
     
    else if (trigger.equals("focusLostMenuField"))
      return (focusLostMenuField());
     
    else if (trigger.equals("validateBaseTableField"))
      return (validateBaseTableField());
     
    else if (trigger.equals("focusGainedBaseTableField"))
      return (focusGainedBaseTableField());
     
    else if (trigger.equals("focusLostBaseTableField"))
      return (focusLostBaseTableField());
     
    else if (trigger.equals("validateHasClearButtonField"))
      return (validateHasClearButtonField());
     
    else if (trigger.equals("focusGainedHasClearButtonField"))
      return (focusGainedHasClearButtonField());
     
    else if (trigger.equals("focusLostHasClearButtonField"))
      return (focusLostHasClearButtonField());
     
    else if (trigger.equals("validateHasQueryButtonField"))
      return (validateHasQueryButtonField());
     
    else if (trigger.equals("focusGainedHasQueryButtonField"))
      return (focusGainedHasQueryButtonField());
     
    else if (trigger.equals("focusLostHasQueryButtonField"))
      return (focusLostHasQueryButtonField());
     
    else if (trigger.equals("validateHasNewButtonField"))
      return (validateHasNewButtonField());
     
    else if (trigger.equals("focusGainedHasNewButtonField"))
      return (focusGainedHasNewButtonField());
     
    else if (trigger.equals("focusLostHasNewButtonField"))
      return (focusLostHasNewButtonField());
     
    else if (trigger.equals("validateHasEditButtonField"))
      return (validateHasEditButtonField());
     
    else if (trigger.equals("focusGainedHasEditButtonField"))
      return (focusGainedHasEditButtonField());
     
    else if (trigger.equals("focusLostHasEditButtonField"))
      return (focusLostHasEditButtonField());
     
    else if (trigger.equals("validateHasDeleteButtonField"))
      return (validateHasDeleteButtonField());
     
    else if (trigger.equals("focusGainedHasDeleteButtonField"))
      return (focusGainedHasDeleteButtonField());
     
    else if (trigger.equals("focusLostHasDeleteButtonField"))
      return (focusLostHasDeleteButtonField());
     
    else if (trigger.equals("validateQueryOnNextField"))
      return (validateQueryOnNextField());
     
    else if (trigger.equals("focusGainedQueryOnNextField"))
      return (focusGainedQueryOnNextField());
     
    else if (trigger.equals("focusLostQueryOnNextField"))
      return (focusLostQueryOnNextField());
     
    else if (trigger.equals("validateDefaultModeField"))
      return (validateDefaultModeField());
     
    else if (trigger.equals("focusGainedDefaultModeField"))
      return (focusGainedDefaultModeField());
     
    else if (trigger.equals("focusLostDefaultModeField"))
      return (focusLostDefaultModeField());
     
    else if (trigger.equals("validateAfterNewField"))
      return (validateAfterNewField());
     
    else if (trigger.equals("focusGainedAfterNewField"))
      return (focusGainedAfterNewField());
     
    else if (trigger.equals("focusLostAfterNewField"))
      return (focusLostAfterNewField());
     
      
    // no matching trigger?
    else 
      return false; 
  }
}


