1 /*
   2  * Copyright 2000-2008 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 
  26 package com.sun.java.swing.ui;
  27 
  28 import com.sun.java.swing.action.*;
  29 import java.awt.*;
  30 import java.awt.event.ActionEvent;
  31 import java.awt.event.ActionListener;
  32 import java.util.Vector;
  33 import javax.swing.*;
  34 
  35 // Referenced classes of package com.sun.java.swing.ui:
  36 //            CommonUI
  37 
  38 public class WizardDlg extends JDialog
  39 {
  40     private class CancelListener
  41         implements ActionListener
  42     {
  43 
  44         public void actionPerformed(ActionEvent evt)
  45         {
  46             if(cancelListener != null)
  47                 cancelListener.actionPerformed(evt);
  48             setVisible(false);
  49         }
  50 
  51         private CancelListener()
  52         {
  53         }
  54 
  55     }
  56 
  57     private class FinishListener
  58         implements ActionListener
  59     {
  60 
  61         public void actionPerformed(ActionEvent evt)
  62         {
  63             if(finishListener != null)
  64                 finishListener.actionPerformed(evt);
  65             setVisible(false);
  66         }
  67 
  68         private FinishListener()
  69         {
  70         }
  71 
  72     }
  73 
  74     private class NextListener
  75         implements ActionListener
  76     {
  77 
  78         public void actionPerformed(ActionEvent evt)
  79         {
  80             cardShowing++;
  81             if(cardShowing > numCards)
  82                 cardShowing = numCards;
  83             else
  84                 panesLayout.next(panesPanel);
  85             if(nextListener != null)
  86                 nextListener.actionPerformed(evt);
  87             enableBackNextButtons();
  88         }
  89 
  90         private NextListener()
  91         {
  92         }
  93 
  94     }
  95 
  96     private class BackListener
  97         implements ActionListener
  98     {
  99 
 100         public void actionPerformed(ActionEvent evt)
 101         {
 102             cardShowing--;
 103             if(cardShowing < 1)
 104                 cardShowing = 1;
 105             else
 106                 panesLayout.previous(panesPanel);
 107             if(backListener != null)
 108                 backListener.actionPerformed(evt);
 109             enableBackNextButtons();
 110         }
 111 
 112         private BackListener()
 113         {
 114         }
 115 
 116     }
 117 
 118 
 119     public WizardDlg(JFrame frame, String title, Vector panels, Vector images)
 120     {
 121         super(frame, title, true);
 122         this.title = title;
 123         this.images = images;
 124         Container pane = getContentPane();
 125         pane.setLayout(new BorderLayout());
 126         panesLayout = new CardLayout();
 127         panesPanel = new JPanel(panesLayout);
 128         pane.add(panesPanel, "Center");
 129         pane.add(createButtonPanel(), "South");
 130         setPanels(panels);
 131         pack();
 132         CommonUI.centerComponent(this);
 133     }
 134 
 135     public WizardDlg(JFrame frame, String title, Vector panels)
 136     {
 137         this(frame, title, panels, null);
 138     }
 139 
 140     public WizardDlg(String title, Vector panels)
 141     {
 142         this(new JFrame(), title, panels, null);
 143     }
 144 
 145     public void setPanels(Vector panels)
 146     {
 147         numCards = panels.size();
 148         cardShowing = 1;
 149         this.panels = panels;
 150         panesPanel.removeAll();
 151         for(int i = 0; i < numCards; i++)
 152             panesPanel.add((JPanel)panels.elementAt(i), (new Integer(i)).toString());
 153 
 154         validate();
 155         enableBackNextButtons();
 156     }
 157 
 158     public void reset()
 159     {
 160         cardShowing = 1;
 161         panesLayout.first(panesPanel);
 162         enableBackNextButtons();
 163     }
 164 
 165     public void setWestPanel(JPanel panel)
 166     {
 167         Container pane = getContentPane();
 168         pane.add(panel, "West");
 169     }
 170 
 171     public static void main(String args[])
 172     {
 173         JPanel p1 = new JPanel();
 174         p1.add(new JButton("One"));
 175         JPanel p2 = new JPanel();
 176         p2.add(new JButton("Two"));
 177         JPanel p3 = new JPanel();
 178         p3.add(new JButton("Three"));
 179         JPanel p4 = new JPanel();
 180         p4.add(new JButton("Four"));
 181         Vector panels = new Vector();
 182         panels.addElement(p1);
 183         panels.addElement(p2);
 184         panels.addElement(p3);
 185         panels.addElement(p4);
 186         wizardDlg = new WizardDlg("Test Dialog", panels);
 187         wizardDlg.addFinishListener(new ActionListener() {
 188 
 189             public void actionPerformed(ActionEvent evt)
 190             {
 191                 System.exit(0);
 192             }
 193 
 194         }
 195 );
 196         wizardDlg.addCancelListener(new ActionListener() {
 197 
 198             public void actionPerformed(ActionEvent evt)
 199             {
 200                 System.exit(0);
 201             }
 202 
 203         }
 204 );
 205         wizardDlg.setVisible(true);
 206     }
 207 
 208     private JPanel createButtonPanel()
 209     {
 210         JPanel panel = new JPanel();
 211         backAction = new BackAction();
 212         nextAction = new NextAction();
 213         finishAction = new FinishAction();
 214         cancelAction = new CancelAction();
 215         backAction.setEnabled(false);
 216         finishAction.setEnabled(false);
 217         backAction.addActionListener(new BackListener());
 218         nextAction.addActionListener(new NextListener());
 219         finishAction.addActionListener(new FinishListener());
 220         cancelAction.addActionListener(new CancelListener());
 221         panel.add(CommonUI.createButton(backAction));
 222         panel.add(CommonUI.createButton(nextAction));
 223         panel.add(CommonUI.createButton(finishAction));
 224         panel.add(CommonUI.createButton(cancelAction));
 225         JPanel p2 = new JPanel(new BorderLayout());
 226         p2.add(panel, "Center");
 227         p2.add(new JSeparator(), "North");
 228         return p2;
 229     }
 230 
 231     private void enableBackNextButtons()
 232     {
 233         if(cardShowing == 1)
 234         {
 235             backAction.setEnabled(false);
 236             finishAction.setEnabled(false);
 237             if(numCards > 1)
 238             {
 239                 nextAction.setEnabled(true);
 240             } else
 241             {
 242                 finishAction.setEnabled(true);
 243                 nextAction.setEnabled(false);
 244             }
 245         } else
 246         if(cardShowing == numCards)
 247         {
 248             nextAction.setEnabled(false);
 249             finishAction.setEnabled(true);
 250             if(numCards > 1)
 251                 backAction.setEnabled(true);
 252             else
 253                 backAction.setEnabled(false);
 254         } else
 255         {
 256             backAction.setEnabled(true);
 257             nextAction.setEnabled(true);
 258             finishAction.setEnabled(false);
 259         }
 260         setTitle();
 261     }
 262 
 263     private void setTitle()
 264     {
 265         JPanel panel = (JPanel)panels.elementAt(cardShowing - 1);
 266         String newTitle = title;
 267         String panelTitle = panel.getName();
 268         if(panelTitle != null && panelTitle.equals(""))
 269         {
 270             newTitle = newTitle + " - ";
 271             newTitle = newTitle + panelTitle;
 272         }
 273         super.setTitle(newTitle);
 274     }
 275 
 276     public synchronized void addFinishListener(ActionListener l)
 277     {
 278         finishListener = AWTEventMulticaster.add(finishListener, l);
 279     }
 280 
 281     public synchronized void removeFinishListener(ActionListener l)
 282     {
 283         finishListener = AWTEventMulticaster.remove(finishListener, l);
 284     }
 285 
 286     public synchronized void addCancelListener(ActionListener l)
 287     {
 288         cancelListener = AWTEventMulticaster.add(cancelListener, l);
 289     }
 290 
 291     public synchronized void removeCancelListener(ActionListener l)
 292     {
 293         cancelListener = AWTEventMulticaster.remove(cancelListener, l);
 294     }
 295 
 296     public synchronized void addNextListener(ActionListener l)
 297     {
 298         nextListener = AWTEventMulticaster.add(nextListener, l);
 299     }
 300 
 301     public synchronized void removeNextListener(ActionListener l)
 302     {
 303         nextListener = AWTEventMulticaster.remove(nextListener, l);
 304     }
 305 
 306     public synchronized void addBackListener(ActionListener l)
 307     {
 308         backListener = AWTEventMulticaster.add(backListener, l);
 309     }
 310 
 311     public synchronized void removeBackListener(ActionListener l)
 312     {
 313         backListener = AWTEventMulticaster.remove(backListener, l);
 314     }
 315 
 316     private CardLayout panesLayout;
 317     private JPanel panesPanel;
 318     private DelegateAction backAction;
 319     private DelegateAction nextAction;
 320     private DelegateAction finishAction;
 321     private DelegateAction cancelAction;
 322     private ActionListener finishListener;
 323     private ActionListener cancelListener;
 324     private ActionListener nextListener;
 325     private ActionListener backListener;
 326     private int numCards;
 327     private int cardShowing;
 328     private String title;
 329     private Vector panels;
 330     private Vector images;
 331     private static WizardDlg wizardDlg;
 332 
 333 
 334 
 335 
 336 }