/* * $Id: WizardPaneDemo.java,v 1.14 2005/10/31 04:46:40 zfq Exp $ * * Copyright (C) 2001-2003 Extreme Component, Inc. All rights reserved. * Use is subject to license terms. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; import java.net.URL; import java.text.MessageFormat; import java.io.IOException; import com.zfqjava.swing.JWizardPane; import com.zfqjava.swing.event.*; import com.zfqjava.swing.ActionVetoException; import com.zfqjava.swing.JBean; import com.zfqjava.swing.JCommonPane; /** * JWizardPane Demo * * @author $Author: zfq $ * @version $Revision: 1.14 $ $Date: 2005/10/31 04:46:40 $ */ public class WizardPaneDemo extends JPanel { private static final String WELCOME_TEXT = "Welcome to the {0} Setup Wizard
" + "
" + "This will install {1} on your computer.
" + "
" + "It is recommended that you close all other applications before continuing.
"+ "
"+ "Click Next to continue, or Cancel to exit Setup."; private static final String FINISH_TEXT = "Thank you for install {0} on your computer
" + "
" + "{1} will make you work better."; private JWizardPane wizardPane; private Image sideImage; private Image bannerImage; private Icon logoIcon; private String logoText; private static String ISV_TITLE = "Extreme Component"; private static String INSTALL_TITLE = "Install Wizard"; private static String APP_TITLE = "MyProgram"; private static String TITLE = APP_TITLE + " - " + INSTALL_TITLE; private static URL APP_LICENSE_FILE; public WizardPaneDemo() { init(); wizardPane = createWizardPane(); setLayout(new BorderLayout()); add(wizardPane, BorderLayout.CENTER); add(createEastPanel(), BorderLayout.EAST); } private void init() { sideImage = loadImage("resources/install-side.gif"); bannerImage = loadImage("resources/install-banner.gif"); logoIcon = loadIcon("resources/install-icon.gif"); logoText = ISV_TITLE; APP_LICENSE_FILE = getResource("resources/license.txt"); } private void setInstallSideImage() { Image image = loadImage("resources/install-side.gif"); this.sideImage = image; if(wizardPane != null) { wizardPane.setSideImage(image); } } private void setDeploySideImage() { Image image = loadImage("resources/deploy-side.gif"); this.sideImage = image; if(wizardPane != null) { wizardPane.setSideImage(image); } } private void setInstallLogoIcon() { Icon icon = loadIcon("resources/install-icon.gif"); this.logoIcon = icon; if(wizardPane != null) { wizardPane.setLogoIcon(icon); } } private void setDeployLogoIcon() { Icon icon = loadIcon("resources/deploy-icon.gif"); this.logoIcon = icon; if(wizardPane != null) { wizardPane.setLogoIcon(icon); } } private void setLogoText(boolean used) { if(used) { logoText = ISV_TITLE; } else { logoText = null; } if(wizardPane != null) { wizardPane.setLogoText(logoText); } } private void setBannerImage(boolean used) { Image bannerImage = null; if(used) { bannerImage = loadImage("resources/install-banner.gif"); } this.bannerImage = bannerImage; if(wizardPane != null) { wizardPane.setBannerImage(bannerImage); } } private Icon loadIcon(String name) { return new ImageIcon(getResource(name)); } private Image loadImage(String name) { URL url = getResource(name); if(url != null) { return Toolkit.getDefaultToolkit().getImage(url); } else { return null; } } private URL getResource(String name) { URL url = getClass().getResource(name); return url; } private JWizardPane createWizardPane() { JWizardPane wizardPane = new JWizardPane(); wizardPane.setSideImage(sideImage); wizardPane.setBannerImage(bannerImage); wizardPane.setLogoIcon(logoIcon); wizardPane.setLogoText(logoText); wizardPane.getControlButton(JWizardPane.FINISH_CONTROL).setVisible(false); wizardPane.getControlButton(JWizardPane.HELP_CONTROL).setVisible(false); AbstractWelcomePanel welcomePanel = new DefaultWelcomePanel(); welcomePanel.setAppName(APP_TITLE); AbstractLicensePanel licensePanel = new DefaultLicensePanel(wizardPane); licensePanel.setLicenseDocument(APP_LICENSE_FILE); DefaultWelcomePanel finishPanel = new DefaultWelcomePanel(); finishPanel.setWelcomeText(FINISH_TEXT); finishPanel.setAppName(APP_TITLE); // Welcome Page wizardPane.addPage(welcomePanel); // License Page wizardPane.addPage("License Agreement", "Please read the license agreement before continuing.", licensePanel); // wizardPane.addPage("Choose Directory", "Please choose install directory", new JFileChooser(System.getProperty("user.dir"))); // lazy to loading the JFileChooser wizardPane.addPage("Choose Directory", "Please choose install directory", new JPanel()); // Finish Page wizardPane.addPage(finishPanel); wizardPane.addWizardListener(new WizardAdapter() { public void beforeCancel(WizardEvent e) throws ActionVetoException { int option = JOptionPane.showConfirmDialog(null, "Do you want to exit?", "Warning", JOptionPane.YES_NO_OPTION); if(option != JOptionPane.YES_OPTION) { // veto cancel action throw new ActionVetoException(); } } public void beforeNext(WizardEvent e) throws ActionVetoException { JWizardPane pane = (JWizardPane)e.getSource(); int index = pane.getSelectedIndex(); if(index == 1) { if(!(pane.getComponentAt(2) instanceof JFileChooser)) { // lazy to add a JFileChooser at page 3 pane.getControlButton(JWizardPane.NEXT_CONTROL).setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir")); pane.setComponentAt(2, fileChooser); } } } public void afterNext(WizardEvent e) { JWizardPane pane = (JWizardPane)e.getSource(); pane.getControlButton(JWizardPane.NEXT_CONTROL).setCursor(null); } }); // aims to conform to windows install wizard specification wizardPane.addChangeListener(new ChangeListener() { int aMnemonic; int bMnemonic; String aText; String bText; private void changeTextAndMnemonic(JButton a, JButton b) { // store every button property aMnemonic = a.getMnemonic(); bMnemonic = b.getMnemonic(); aText = a.getText(); bText = b.getText(); a.setText(bText); b.setText(aText); a.setMnemonic(bMnemonic); b.setMnemonic(aMnemonic); } private void resetTextAndMnemonic(JButton a, JButton b) { a.setText(aText); b.setText(bText); a.setMnemonic(aMnemonic); b.setMnemonic(bMnemonic); } public void stateChanged(ChangeEvent e) { JWizardPane pane = (JWizardPane)e.getSource(); JButton nextButton = pane.getControlButton(JWizardPane.NEXT_CONTROL); JButton finishButton = pane.getControlButton(JWizardPane.FINISH_CONTROL); if(pane.getSelectedIndex() == (pane.getPageCount() -1)) { changeTextAndMnemonic(nextButton, finishButton); } else if (aText != null && bText != null) { resetTextAndMnemonic(nextButton, finishButton); } } }); return wizardPane; } private static abstract class AbstractWelcomePanel extends JPanel { public abstract void setAppName(String name); } private static abstract class AbstractLicensePanel extends JPanel { public abstract void setLicenseDocument(URL url); } private class DefaultWelcomePanel extends AbstractWelcomePanel { private JLabel textLabel; // should get from resource file private String welcomeText = WELCOME_TEXT; void setWelcomeText(String text) { this.welcomeText = text; } String getWelcomeText() { return welcomeText; } public DefaultWelcomePanel() { // start welcome page setBackground(Color.white); setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); setLayout(new BorderLayout()); textLabel = new JLabel(); add(textLabel, BorderLayout.NORTH); } public void setAppName(String name) { if(name == null) name = ""; Object[] args = { name, name }; String formatStr = MessageFormat.format(welcomeText, args); textLabel.setText(formatStr); } } private class DefaultLicensePanel extends AbstractLicensePanel { private JEditorPane editorPane; public DefaultLicensePanel(final JWizardPane wizardPane) { // start license page setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); setLayout(new BorderLayout()); editorPane = new JEditorPane(); editorPane.setEditable(false); add(new JScrollPane(editorPane), BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); ButtonGroup group = new ButtonGroup(); final JRadioButton acceptButton = new JRadioButton("I accept the agreement"); acceptButton.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { // wizardPane.getControlButton(JWizardPane.NEXT_CONTROL).setEnabled(true); } else if(e.getStateChange() == ItemEvent.DESELECTED) { // wizardPane.getControlButton(JWizardPane.NEXT_CONTROL).setEnabled(false); } } }); JRadioButton noButton = new JRadioButton("I do not accept the agreement"); noButton.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED) { // wizardPane.getControlButton(JWizardPane.NEXT_CONTROL).setEnabled(false); } else if(e.getStateChange() == ItemEvent.DESELECTED) { // wizardPane.getControlButton(JWizardPane.NEXT_CONTROL).setEnabled(true); } } }); group.add(acceptButton); group.add(noButton); noButton.setSelected(true); buttonPanel.add(acceptButton); buttonPanel.add(noButton); add(buttonPanel, BorderLayout.SOUTH); wizardPane.addWizardListener(new WizardAdapter() { private void configureButton(WizardEvent e) { JWizardPane pane = (JWizardPane)e.getSource(); int index = pane.getSelectedIndex(); if(index == 1) { pane.getControlButton(JWizardPane.NEXT_CONTROL).setEnabled(acceptButton.isSelected()); } else { pane.getControlButton(JWizardPane.NEXT_CONTROL).setEnabled(true); } } public void afterBack(WizardEvent e) { configureButton(e); } public void afterNext(WizardEvent e) { configureButton(e); } }); } public void setLicenseDocument(URL url) { try { editorPane.setPage(url); } catch (IOException e) { e.printStackTrace(); } } } private JPanel createEastPanel() { JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(createPropertyPanel(), BorderLayout.NORTH); panel.add(createDialogPanel(), BorderLayout.CENTER); return panel; } private JPanel createPropertyPanel() { JPanel panel = new JPanel(); Border titledBorder = BorderFactory.createTitledBorder("Change Property"); Border emptyBorder = BorderFactory.createEmptyBorder(12, 12, 12, 12); panel.setBorder(BorderFactory.createCompoundBorder(titledBorder, emptyBorder)); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); panel.setLayout(gridbag); c.weightx = 0.5; c.weighty = 0.5; c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.WEST; c.insets = new Insets(0, 0, 5, 0); JLabel label = new JLabel("Side Image: "); addPanel(panel, c, label); Object[] sideImages = { "Install", "Deploy" }; final JComboBox sideImagesCB = new JComboBox(sideImages); sideImagesCB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // side images String img = (String)sideImagesCB.getSelectedItem(); if(img.equals("Install")) { setInstallSideImage(); } else if(img.equals("Deploy")) { setDeploySideImage(); } else { System.out.println("error in side image: " + img); } } }); label.setLabelFor(sideImagesCB); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, sideImagesCB); c.gridwidth = GridBagConstraints.RELATIVE; label = new JLabel("Logo Icon: "); addPanel(panel, c, label); Object[] logoIcons = { "Install", "Deploy" }; final JComboBox logoIconsCB = new JComboBox(logoIcons); logoIconsCB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // logo icons String icon = (String)logoIconsCB.getSelectedItem(); if(icon.equals("Install")) { setInstallLogoIcon(); } else if(icon.equals("Deploy")) { setDeployLogoIcon(); } else { System.out.println("error in logo icon: " + icon); } } }); label.setLabelFor(logoIconsCB); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, logoIconsCB); c.gridwidth = GridBagConstraints.RELATIVE; label = new JLabel("Logo Text: "); addPanel(panel, c, label); Object[] logoText = { "Us", "None" }; final JComboBox logoTextCB = new JComboBox(logoText); logoTextCB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // logo text String text = (String)logoTextCB.getSelectedItem(); if(text.equals("Us")) { setLogoText(true); } else if(text.equals("None")) { setLogoText(false); } else { System.out.println("error in logo text: " + text); } } }); label.setLabelFor(logoTextCB); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, logoTextCB); c.gridwidth = GridBagConstraints.RELATIVE; label = new JLabel("Banner Image: "); addPanel(panel, c, label); Object[] bannerImages = { "Sky", "None" }; final JComboBox bannerImagesCB = new JComboBox(bannerImages); bannerImagesCB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // banner images String banner = (String)bannerImagesCB.getSelectedItem(); if(banner.equals("Sky")) { setBannerImage(true); } else if (banner.equals("None")) { setBannerImage(false); } else { System.out.println("error in banner image: " + banner); } } }); label.setLabelFor(bannerImagesCB); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, bannerImagesCB); c.gridwidth = GridBagConstraints.RELATIVE; label = new JLabel("Interior Page Style: "); addPanel(panel, c, label); Object[] pageStyle = { "BannerTip", "StepTip" }; final JComboBox pageStyleCB = new JComboBox(pageStyle); pageStyleCB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String pageStyle = (String)pageStyleCB.getSelectedItem(); wizardPane.putClientProperty("JWizardPane.interiorPageStyle", pageStyle); } }); label.setLabelFor(pageStyleCB); c.gridwidth = GridBagConstraints.REMAINDER; addPanel(panel, c, pageStyleCB); return panel; } private static void addPanel(Container container, GridBagConstraints c, Component component) { GridBagLayout gridbag = (GridBagLayout)container.getLayout(); gridbag.setConstraints(component, c); container.add(component); } private JPanel createDialogPanel() { JPanel panel = new JPanel(); Border titledBorder = BorderFactory.createTitledBorder("Dialog and Frame"); Border emptyBorder = BorderFactory.createEmptyBorder(12, 12, 12, 12); panel.setBorder(BorderFactory.createCompoundBorder(titledBorder, emptyBorder)); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); panel.setLayout(gridbag); // c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.CENTER; c.insets = new Insets(0, 0, 5, 0); c.gridwidth = GridBagConstraints.REMAINDER; JButton b = new JButton("Show Dialog"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JWizardPane pane = createWizardPane(); pane.removeAllPage(); pane.getSelectionModel().clearSelection(); //JWizardPane pane = new JWizardPane(); AbstractWelcomePanel welcomePanel = new DefaultWelcomePanel(); welcomePanel.setAppName(APP_TITLE); pane.addPage(welcomePanel); pane.putClientProperty("JWizardPane.interiorPageStyle", wizardPane.getClientProperty("JWizardPane.interiorPageStyle")); pane.showDialog(null, TITLE); } }); addPanel(panel, c, b); c.gridwidth = GridBagConstraints.RELATIVE; b = new JButton("Show Frame"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JWizardPane pane = createWizardPane(); pane.putClientProperty("JWizardPane.interiorPageStyle", wizardPane.getClientProperty("JWizardPane.interiorPageStyle")); pane.showFrame(TITLE); } }); addPanel(panel, c, b); return panel; } public static void main(String[] args) { DemoPanel demoPanel = new DemoPanel(); demoPanel.addDemo(new WizardPaneDemo()); demoPanel.setTitle("JWizardPane Demo"); demoPanel.setDefaultCloseOperation(JBean.EXIT_ON_CLOSE); demoPanel.showFrame(); } }