//
// WizardPane.java
//

/*
VisBio application for visualization of multidimensional
biological image data. Copyright (C) 2002-2004 Curtis Rueden.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.visbio.util;

import com.jgoodies.forms.factories.ButtonBarFactory;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * WizardPane provides an extensible interface for creating
 * multi-page wizard dialogs with Cancel, Back, Next and Finish buttons.
 */
public class WizardPane extends DialogPane {

  // -- GUI components --

  /** Panels representing the wizard's pages. */
  protected JPanel[] pages;

  /** Wizard's next button. */
  protected JButton next;

  /** Wizard's back button. */
  protected JButton back;


  // -- Other fields --

  /** The current page. */
  protected int page;


  // -- Constructor --

  /** Creates a file series chooser dialog. */
  public WizardPane(String title) {
    super(title);
    page = -1;
  }


  // -- WizardPane API methods --

  /** Sets wizard's pages to match the given panels. */
  public void setPages(JPanel[] pages) {
    this.pages = pages;
    for (int i=0; i<pages.length; i++) {
      add(pages[i]);
      pages[i].setVisible(false);
    }
    setPage(0);
  }

  /** Sets the current page to the given value. */
  public void setPage(int page) {
    if (this.page == page) return;
    if (this.page >= 0) pages[this.page].setVisible(false);
    pages[page].setVisible(true);
    this.page = page;
    back.setEnabled(page > 0);
    int last = pages.length - 1;
    next.setEnabled(page < last);
    ok.setEnabled(page == last);
    if (dialog != null) {
      // resize and center dialog
      Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension w = dialog.getPreferredSize();
      dialog.setBounds(new Rectangle((ss.width - w.width) / 2,
        (ss.height - w.height) / 2, w.width, w.height));
      dialog.validate();
    }
  }


  // -- DialogPane API methods --

  /** Resets the wizard pane's components to their default states. */
  public void resetComponents() { setPage(0); }

  /** Internal method for creating dialog buttons. */
  protected void makeButtons(boolean doCancel) {
    super.makeButtons(doCancel);

    // back button
    back = new JButton("< Back");
    if (!LAFUtil.isMacLookAndFeel()) back.setMnemonic('b');
    back.setActionCommand("back");
    back.addActionListener(this);
    back.setEnabled(false);

    // next button
    next = new JButton("Next >");
    if (!LAFUtil.isMacLookAndFeel()) next.setMnemonic('n');
    next.setActionCommand("next");
    next.addActionListener(this);
    next.setEnabled(false);

    // finish button
    ok.setText("Finish");
    if (!LAFUtil.isMacLookAndFeel()) ok.setMnemonic('f');
  }

  /** Internal method for doing button layout. */
  protected JPanel doButtonLayout() {
    return ButtonBarFactory.buildWizardBar(back, next, ok, cancel);
  }


  // -- ActionListener API methods --

  /** Handles button press events. */
  public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    if (command.equals("back")) setPage(page - 1);
    else if (command.equals("next")) setPage(page + 1);
    else super.actionPerformed(e);
  }

}

@@ -2230,134 +2230,127 @@
     
-back.setEnabled(page %3E 0);%0A    int last = pages.length - 1;%0A    next.setEnabled(page %3C last);%0A    ok.setEnabled(page == last);
+enableButtons();%0A    repack();%0A  %7D%0A%0A  /** Resizes and centers dialog based on its contents. */%0A  public void repack() %7B
 %0A   
@@ -2365,17 +2365,17 @@
 log 
-!
+=
 = null) 
 %7B%0A  
@@ -2374,46 +2374,16 @@
 ll) 
-%7B%0A      // resize and center dialog%0A  
+return;%0A
     
@@ -2446,18 +2446,16 @@
 ();%0A    
-  
 Dimensio
@@ -2487,18 +2487,16 @@
 Size();%0A
-  
     dial
@@ -2554,18 +2554,16 @@
 ,%0A      
- 
- 
 (ss.heig
@@ -2611,34 +2611,444 @@
     
-  dialog.validate();%0A    %7D
+dialog.validate();%0A  %7D%0A%0A  /** Enables wizard's buttons. */%0A  public void enableButtons() %7B%0A    back.setEnabled(page %3E 0);%0A    int last = pages.length - 1;%0A    next.setEnabled(page %3C last);%0A    ok.setEnabled(page == last);%0A    cancel.setEnabled(true);%0A  %7D%0A%0A  /** Disables wizard's buttons. */%0A  public void disableButtons() %7B%0A    back.setEnabled(false);%0A    next.setEnabled(false);%0A    ok.setEnabled(false);%0A    cancel.setEnabled(false);
 %0A  %7D

Update WizardPane API to support DatasetPane's indeterminate progress bars.