Package com.wix.gui.utils

Source Code of com.wix.gui.utils.JAdvancedDialog

/*
* JAdvancedDialog.java
*
* Created on 18 de junio de 2006, 14:44
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/

package com.wix.gui.utils;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import com.jix.JixException;
import com.jix.gui.ErrorDetailsDialog;

/**
*
* @author Ismael
*/
public class JAdvancedDialog extends JDialog implements ValidationListener
{
    private final JFrame  mainWindow;
  private final Translator translator;
    private final JAdvancedPanel panels[];
   
    private final JTextArea titleTextArea    = new JTextArea();
    private final JTextArea subtitleTextArea = new JTextArea();

    private final JButton cancelButton;
    private final JButton prevButton;
    private final JButton nextButton;

    private final JPanel container = new JPanel(new BorderLayout());

    private int panelsIndex = 0;
  private boolean wasCompleted;

    /** Creates a new instance of JAdvancedDialog
     * @throws JixException
     * @throws HeadlessException */
    public JAdvancedDialog(JFrame mainWindow, Dimension size, final Translator translator, JAdvancedPanel[] panels) throws JixException
    {
        super(mainWindow, translator.getString("frame.title"), true);
       
        setSize(size);
       
    this.mainWindow = mainWindow;
    this.translator = translator;
    this.panels = panels;
       
        for (int i=0; i<panels.length; i++)
          panels[i].addValidationListener(this);
       
        if (panels.length == 0)
          throw new JixException(new Exception("No panels defined"));
       
        this.cancelButton = new JButton(translator.getString("frame.button.cancel"));
        this.prevButton   = new JButton(translator.getString("frame.button.prev"));
        this.nextButton   = new JButton(translator.getString("frame.button.next"));

        cancelButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        try
        {
          cancelPanel();
        }
        catch(JixException ex)
        {
          ErrorDetailsDialog errorDetailsDialog = new ErrorDetailsDialog(ex);
          errorDetailsDialog.openDialog();
          errorDetailsDialog.dispose();
        }
      }
        });
       
        prevButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        try
        {
          prevPanel();
        }
        catch(JixException ex)
        {
          ErrorDetailsDialog errorDetailsDialog = new ErrorDetailsDialog(ex);
          errorDetailsDialog.openDialog();
          errorDetailsDialog.dispose();
        }
      }
        });
       
        nextButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        try
        {
          nextPanel();
        }
        catch(JixException ex)
        {
          ErrorDetailsDialog errorDetailsDialog = new ErrorDetailsDialog(ex);
          errorDetailsDialog.openDialog();
          errorDetailsDialog.dispose();
        }
      }
        });

    setDefaultCloseOperation(JAdvancedDialog.DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e)
      {
        try
        {
          cancelPanel();
        }
        catch(JixException ex)
        {
          ErrorDetailsDialog errorDetailsDialog = new ErrorDetailsDialog(ex);
          errorDetailsDialog.openDialog();
          errorDetailsDialog.dispose();
        }
      }
    });
   
        JPanel bottomPanel = new JPanel(new GridLayout(1, 4));
        bottomPanel.add(new JPanel());
        bottomPanel.add(cancelButton);
        bottomPanel.add(prevButton);
        bottomPanel.add(nextButton);
       
        titleTextArea.setEditable(false);
        titleTextArea.setFont(titleTextArea.getFont().deriveFont(Font.BOLD));
        titleTextArea.setLineWrap(true);
        titleTextArea.setWrapStyleWord(true);
       
        subtitleTextArea.setEditable(false);
        subtitleTextArea.setLineWrap(true);
        subtitleTextArea.setWrapStyleWord(true);
       
        container.setBorder(new EmptyBorder(15, 10, 5, 10));
        bottomPanel.setBorder(new EmptyBorder(5, 10, 5, 10));
       
        JPanel helpPanel = JAdvancedPanel.newJRowPanel(new JComponent[] { titleTextArea, subtitleTextArea });
        helpPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        helpPanel.setBackground(Color.white);
       
        JPanel retPanel = new JPanel(new BorderLayout());
        retPanel.add(helpPanel, BorderLayout.NORTH);
        retPanel.add(container);
        retPanel.add(bottomPanel, BorderLayout.SOUTH);
       
        getContentPane().add(retPanel);
    }
   
  private void refreshContainer() throws JixException
    {
    container.removeAll();
        container.add(panels[panelsIndex], BorderLayout.CENTER);
        container.updateUI();
       
      panels[panelsIndex].openedPanel();

      titleTextArea   .setText(panels[panelsIndex].getTitle());
      subtitleTextArea.setText(panels[panelsIndex].getSubtitle());
     
      nextButton.setText(panelsIndex < panels.length - 1 ? translator.getString("frame.button.next") : translator.getString("frame.button.finish"));

      nextButton.setEnabled(panels[panelsIndex].isValidated());
      prevButton.setEnabled(panelsIndex != 0 && panels[panelsIndex].canGoBack());
     
      nextButton.requestFocus();
    }
   

  public void validationChecked(ValidationEvent event)
  {
    if (panels[panelsIndex] == event.getPanel())
      nextButton.setEnabled(event.isValidated());
   
    prevButton  .setEnabled(panelsIndex != 0 && event.canGoBack());
    cancelButton.setEnabled(event.requestConfirmationOnExit());
  }
 
    private void prevPanel() throws JixException
    {
      panels[panelsIndex].cancelledPanel();
     
      panelsIndex --;
     
      refreshContainer();
    }
   
    private void nextPanel() throws JixException
    {
      panels[panelsIndex].confirmedPanel();
     
      panelsIndex ++;
     
      if (panelsIndex == panels.length)
      {
          this.wasCompleted = true;
         
          setVisible(false);
      }
      else
      {
          refreshContainer();
      }
    }
   
    private void cancelPanel() throws JixException
    {
      if (!panels[panelsIndex].letUserExit())
        return;
     
    if (!panels[panelsIndex].requestConfirmationOnExit() ||
      JOptionPane.showConfirmDialog(JAdvancedDialog.this,
        translator.getString("frame.quit.message"),
        translator.getString("frame.quit.title"),
        JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION)
    {
      panels[panelsIndex].cancelledPanel();
     
        this.wasCompleted = false;
       
        setVisible(false);
    }
    }
   
    public boolean openDialog() throws JixException
    {
    Point     mainFrameLocation;
    Dimension mainFrameSize;

    if (mainWindow != null)
      {
        mainFrameLocation = mainWindow.getLocation();
        mainFrameSize     = mainWindow.getSize();
      }
    else
    {
      mainFrameLocation = new Point();
      mainFrameSize     = Toolkit.getDefaultToolkit().getScreenSize();
    }
   
        Dimension dialogSize = getSize();
       
        int x = mainFrameLocation.x + (mainFrameSize.width  - dialogSize.width )/2;
        int y = mainFrameLocation.y + (mainFrameSize.height - dialogSize.height)/2;
   
        setLocation(x, y);
       
        this.panelsIndex = 0;
        this.wasCompleted = false;
       
        refreshContainer();
       
        setVisible(true);
       
        return wasCompleted;
    }
}
TOP

Related Classes of com.wix.gui.utils.JAdvancedDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.