001    package org.springframework.richclient.wizard;
002    
003    import org.springframework.richclient.application.config.ApplicationObjectConfigurer;
004    import org.springframework.richclient.application.ApplicationServicesLocator;
005    import org.springframework.richclient.util.GuiStandardUtils;
006    
007    import javax.swing.*;
008    import java.awt.*;
009    import java.util.Map;
010    import java.util.HashMap;
011    
012    import com.jgoodies.forms.layout.*;
013    import com.jgoodies.forms.factories.FormFactory;
014    import com.jgoodies.forms.builder.DefaultFormBuilder;
015    
016    /**
017     * Wizard dialog that additionally adds a panel to the dialog
018     * showing the page path and the current page.
019     *
020     * @author Schaubroeck N.V.
021     */
022    public class ExtendedWizardDialog extends
023            org.springframework.richclient.wizard.WizardDialog
024    {
025        private Map<String, Component> indexComponents = new HashMap<String, Component>();
026        private Map<String, Component> indexNumbers = new HashMap<String, Component>();
027        private JLabel stepNofMax = new JLabel();
028        private String id = null;
029    
030        public ExtendedWizardDialog()
031        {
032            super();
033        }
034    
035        public ExtendedWizardDialog(Wizard wizard)
036        {
037            this(wizard, null);
038        }
039    
040        public ExtendedWizardDialog(Wizard wizard, String id)
041        {
042            super(wizard);
043            this.id = id;
044            if (this.id != null)
045                ((ApplicationObjectConfigurer) ApplicationServicesLocator.services().getService(ApplicationObjectConfigurer.class)).configure(this, this.id);
046        }
047    
048        public void setWizard(Wizard wizard)
049        {
050            super.setWizard(wizard);
051            this.wizard = wizard;
052        }
053    
054        protected JComponent createDialogContentPane()
055        {
056            JPanel wizardPanel = new JPanel(new FormLayout(
057                    new ColumnSpec[]{
058                            new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.NO_GROW),
059                            ColumnSpec.decode("fill:pref"),
060                            new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW)
061                    },
062                    new RowSpec[]{
063                            new RowSpec(RowSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
064                            RowSpec.decode("fill:pref")
065                    }));
066            CellConstraints cc = new CellConstraints();
067            wizardPanel.add(new JSeparator(SwingConstants.VERTICAL), cc.xy(2, 1));
068            wizardPanel.add(super.createDialogContentPane(), cc.xy(3, 1));
069            wizardPanel.add(createWizardIndex(), cc.xy(1, 1)); // do this after super.createDialogPane() because only then, pages are added
070            wizardPanel.add(new JSeparator(), cc.xyw(1, 2, 3));
071            return wizardPanel;
072        }
073    
074        private Component createWizardIndex()
075        {
076            JPanel indexPanel = new JPanel(new FormLayout(
077                    new ColumnSpec[]{
078                            new ColumnSpec(ColumnSpec.CENTER, Sizes.DEFAULT, FormSpec.NO_GROW)
079                    },
080                    new RowSpec[]{
081                            new RowSpec(RowSpec.FILL, Sizes.DEFAULT, FormSpec.NO_GROW),
082                            FormFactory.UNRELATED_GAP_ROWSPEC,
083                            new RowSpec(RowSpec.CENTER, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
084                            FormFactory.UNRELATED_GAP_ROWSPEC,
085                            new RowSpec(RowSpec.FILL, Sizes.DEFAULT, FormSpec.NO_GROW),
086                    }));
087            CellConstraints cc = new CellConstraints();
088            GuiStandardUtils.attachBorder(indexPanel, BorderFactory.createEmptyBorder(5, 5, 5, 5));
089            indexPanel.add(createWizardTitle(), cc.xy(1, 1));
090            WizardPage[] pages = wizard.getPages();
091            DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout(
092                    "right:pref, 3dlu, left:pref", ""));
093            JLabel indexNumber;
094            JLabel indexTitle;
095            for (int i = 0; i < pages.length; ++i)
096            {
097                indexNumber = new JLabel(Integer.toString(i + 1) + ".");
098                indexNumber.setName(Integer.toString(i + 1));
099                indexTitle = new JLabel(pages[i].getTitle());
100                indexNumbers.put(pages[i].getTitle(), indexNumber);
101                indexComponents.put(pages[i].getTitle(), indexTitle);
102                builder.append(indexNumber);
103                builder.append(indexTitle);
104                if (i < pages.length - 1)
105                    builder.nextLine();
106            }
107            indexPanel.add(builder.getPanel(), cc.xy(1, 3));
108            indexPanel.add(createStepNofMPanel(pages.length), cc.xy(1, 5));
109            return indexPanel;
110        }
111    
112        /** @return  */
113        private JLabel createWizardTitle()
114        {
115            return new JLabel(getTitle());
116        }
117    
118        private Component createStepNofMPanel(int m)
119        {
120            JPanel panel = new JPanel(new FormLayout("fill:pref, fill:pref:grow, fill:pref", "center:pref:none"));
121            CellConstraints cc = new CellConstraints();
122            panel.add(new JLabel("Stap "), cc.xy(1, 1));
123            panel.add(this.stepNofMax, cc.xy(2, 1));
124            panel.add(new JLabel(" van " + Integer.toString(m)), cc.xy(3, 1));
125            return panel;
126        }
127    
128        public void showPage(WizardPage page)
129        {
130            JComponent component;
131            String pageTitle;
132            if (getCurrentPage() != null)
133            {
134                pageTitle = getCurrentPage().getTitle();
135                component = (JComponent) indexComponents.get(pageTitle);
136                component.setFont(component.getFont().deriveFont(Font.PLAIN));
137                component = (JComponent) indexNumbers.get(pageTitle);
138                component.setFont(component.getFont().deriveFont(Font.PLAIN));
139            }
140            super.showPage(page);
141            pageTitle = page.getTitle();
142            component = (JComponent) indexComponents.get(pageTitle);
143            component.setFont(component.getFont().deriveFont(Font.BOLD));
144            component = (JComponent) indexNumbers.get(pageTitle);
145            component.setFont(component.getFont().deriveFont(Font.BOLD));
146            this.stepNofMax.setText(component.getName());
147        }
148    }