/*
 * NewOrOpenWizard.java
 *
 * Copyright (c) 2003, Oracle Corporation. All rights reserved. Oracle grants
 * you ("Licensee") a non-exclusive, royalty-free, worldwide license to use,
 * modify and redistribute this software in source and binary code form, provided
 * that (i) this copyright notice and license appear on all copies of the
 * software; (ii) Licensee does not utilize the software in a manner disparaging
 * to Oracle; and (iii) Licensee does not utilize the software in a manner that
 * violates the terms of any Licensee agreement with Oracle.
 */
package samples.javaclient.analyzer;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;

import javax.swing.JTextArea;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JToggleButton;

import oracle.bali.ewt.graphics.ImageUtils;
import oracle.bali.ewt.text.MultiLineLabel;
import oracle.bali.ewt.text.WordWrapper;
import oracle.bali.ewt.wizard.ImageWizardPage;
import oracle.bali.ewt.wizard.Wizard;
import oracle.bali.ewt.wizard.WizardDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Wizard page that asks the user whether they want to open an existing worksheet or
 * to create a new one.  It is based on the Bali component WizardDialog.  The BI Beans
 * QueryBuilder also uses this component.
 */
public class NewOrOpenWizard extends WizardDialog implements ActionListener  {  
  // Track the action the user selected 
  /**
   * Action that the user selected. Default is "NEW".
   */
  private String m_selectedAction = "NEW";  

  // Strings used by the wizard
  /**
   * Selected action: "NEW".
   */
  public static final String NEW = "NEW";
  /**
   * Selected action: "OPEN".
   */
  public static final String OPEN = "OPEN";
  /**
   * Title for this dialog: "Create/Open Worksheet".
   */
  private static final String TITLE = "Create/Open Worksheet";
  /**
   * Instructional text for this dialog:
   * "This wizard helps you open an existing worksheet or create a new one 
   *  so that you can quickly retrieve information from the database."
   */
  private static final String DESCRIPTION = 
                    "This wizard helps you open an existing worksheet or create a new one " +
                    "so that you can quickly retrieve information from the database.";  
  /**
   * Default text for choice: "Create a new worksheet".
   */
  private static final String WHAT_TO_DO = "Create a new worksheet";                    
  /**
   * Text for opening choice: "Open an existing worksheet." 
   */
  private static final String OPEN_WORKSHEET = "Open an existing worksheet";
  /**
   * Text for creating choice: "Create a new worksheet".
   */
  private static final String CREATE_WORKSHEET = "Create a new worksheet";

  /**
   * Sets up the Wizard dialog
   */
  public NewOrOpenWizard() {
    // See the Bali (JEWT) help for details on setting up a wizard
    JPanel panel = new JPanel();

    StartingPage startPage = new StartingPage(this);
    Wizard wizard = new Wizard();
    wizard.addPage(startPage);
    setWizard(wizard);
    setWizardTitle(TITLE);    
  }

  /**
   * Gets the action that the user selected. 
   * @return A constant that indicates the user's choice:
   *        {@linkg #NEW} if the user wants to create a new worksheet, or
   *        {@link #OPEN} if the user wants to open a worksheet.
   */
  public String getSelectedAction() {
    return m_selectedAction;
  }

  /**
   * This inner class creates the starting page for the wizard.  It asks the
   * user what to do: Create or Open a Worksheet.  This class extends the Bali 
   * (JEWT) ImageWizardPage.
   */
  public class StartingPage extends ImageWizardPage {
    // Dialog fields
    /**
     * Button for New choice.
     */
    private JToggleButton btnNew = new JToggleButton ();
    /**
     * Button for Open choice.
     */
    private JToggleButton btnOpen = new JToggleButton ();
    /**
     * The wizard that this page is part of.
     */
    private NewOrOpenWizard m_wizardDialog = null;

    /**
     * Constructor.
     * Constructs the UI and adds the listeners for the wizard page.  This method is
     * called from the NewOrOpenWizard constructor.
     * 
     * @param wizardDialog A Bali Wizard Dialog instance.  This dialog provides
     * a default layout and buttons.
     */
    public StartingPage(NewOrOpenWizard wizardDialog) {
      // Cache the reference to the dialog
      setWizardDialog(wizardDialog);

      /*
       * Construct the UI
       */
      setImage(ImageUtils.getImageResource(getClass(), "images/launchWizard.gif"));
      JPanel contentArea = new JPanel(new BorderLayout());
      MultiLineLabel  ml = new MultiLineLabel(WordWrapper.getTextWrapper(), DESCRIPTION);
      contentArea.add(ml, BorderLayout.NORTH);      
      
      JPanel interactive = new JPanel();
      GridBagLayout gridBagLayout1 = new GridBagLayout();
      interactive.setLayout(gridBagLayout1);

      // Create the Buttons
      btnNew.setIcon(new ImageIcon(ImageUtils.getImageResource (getClass(), "images/new.gif")));
      btnNew.setSelected(true);
      btnNew.setActionCommand(NEW);
      btnOpen.setIcon(new ImageIcon(ImageUtils.getImageResource (getClass(), "images/open.gif")));      
      btnOpen.setSelected(false);
      btnOpen.setActionCommand(OPEN);
      
      ButtonGroup btnGroup = new ButtonGroup();
      btnGroup.add(btnNew);
      btnGroup.add(btnOpen);

      /*
       * Add the components to the page
       */
      int left = 10;
                      
      interactive.add(new JLabel(WHAT_TO_DO), 
               new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, left, 0, 0), 0, 0));
      interactive.add(new JSeparator(JSeparator.HORIZONTAL), new GridBagConstraints(0, 1, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, left, 5, 0), 0, 0));

      interactive.add(btnNew, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(8, left, 2, 0), 0, 0));

      interactive.add(new JLabel(CREATE_WORKSHEET), 
               new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(8, 4, 5, 0), 0, 0));

      interactive.add(btnOpen, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, left, 2, 0), 0, 0));
      interactive.add(new JLabel(OPEN_WORKSHEET), 
               new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 4, 5, 0), 0, 0));    

      contentArea.add(interactive, BorderLayout.CENTER);
      this.setInteractiveArea(contentArea);

      // Add the listeners to the toggle buttons
      btnNew.addActionListener(m_wizardDialog);
      btnOpen.addActionListener(m_wizardDialog);
    }

    /**
     * Stores the reference to the WizardDialog.
     * @param wizardDialog The wizard that this page belongs to.
     */
    public void setWizardDialog(NewOrOpenWizard wizardDialog) {
      m_wizardDialog = wizardDialog;
    }
  }

  /**
   * Responds to user clicks.
   * This method tracks the action that the user selected.
   * 
   * @param e Information about the user selection.
   */
  public void actionPerformed(ActionEvent e) {
    JToggleButton button = (JToggleButton) e.getSource();
    m_selectedAction = button.getActionCommand();
  }

}