View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.util.wizard;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Dimension;
26  import java.awt.FlowLayout;
27  import java.awt.Frame;
28  import java.awt.event.ActionEvent;
29  import java.awt.event.ActionListener;
30  import java.awt.event.KeyEvent;
31  
32  import javax.swing.AbstractAction;
33  import javax.swing.ImageIcon;
34  import javax.swing.JButton;
35  import javax.swing.JComponent;
36  import javax.swing.JPanel;
37  import javax.swing.JSplitPane;
38  import javax.swing.KeyStroke;
39  import javax.swing.UIManager;
40  import javax.swing.border.CompoundBorder;
41  import javax.swing.border.MatteBorder;
42  import javax.swing.plaf.basic.BasicSplitPaneUI;
43  
44  import org.bounce.event.CardEvent;
45  import org.bounce.event.CardPanelAdapter;
46  import org.bounce.image.ImageLoader;
47  import org.bounce.wizard.Wizard;
48  import org.bounce.wizard.WizardPage;
49  import org.xmlhammer.gui.help.HelpPanel;
50  
51  public abstract class HelpEnabledWizard extends Wizard {
52      private static final long serialVersionUID = 7119005231853816073L;
53      
54      private static final ImageIcon HELP_ICON = ImageLoader.get().getImage("/org/xmlhammer/gui/icons/etool16/help.gif");
55  
56      private static final int WIZARD_WIDTH = 500;
57      private static final int MINIMUM_WIZARD_HEIGHT = 500;
58  
59      private HelpPanel helpPanel = null;
60      private JSplitPane helpSplit = null;
61  
62      // Initialised by parent.
63      private JButton helpButton;
64      private JPanel main;
65  
66  
67      public HelpEnabledWizard(Frame parent) {
68          super(parent);
69  
70          getRootPane().getActionMap().put("helpAction", new AbstractAction() {
71              private static final long serialVersionUID = -6931927193561857562L;
72  
73              public void actionPerformed(ActionEvent e) {
74                  showHelpPanel();
75              }
76          });
77          getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0, false), "helpAction");
78  
79          getCards().addCardPanelListener(new CardPanelAdapter<WizardPage>() {
80              public void cardChanged(CardEvent<WizardPage> event) {
81                  getHelpPanel().showID(((HelpEnabledWizardPage)event.getCard()).getHelpID());
82              }
83          });
84  
85          pack();
86          setSize(new Dimension(WIZARD_WIDTH, Math.max(getSize().height, MINIMUM_WIZARD_HEIGHT)));
87          setLocationRelativeTo(getParent());
88      }
89      
90      protected JPanel createCenterPanel() {
91          main = super.createCenterPanel();
92          
93          return main;
94      }
95      
96      @Override
97      protected JPanel createSouthPanel() {
98          JPanel helpButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
99          helpButtonPanel.add(getHelpButton());
100         
101         JPanel southPanel = super.createSouthPanel();
102         southPanel.add(helpButtonPanel, BorderLayout.WEST);
103         
104         return southPanel;
105     }
106     
107     private JButton getHelpButton() {
108         if (helpButton == null) {
109             helpButton = new JButton("Help", HELP_ICON);
110             helpButton.addActionListener(new ActionListener() {
111                 public void actionPerformed(ActionEvent arg0) {
112                     showHelpPanel();
113                 }
114             });
115         }
116         
117         return helpButton;
118     }
119     
120     /***
121      * Show the help panel (preferred-size) if not already shown.
122      */
123     public void showHelpPanel() {
124         HelpPanel helpPanel = getHelpPanel();
125         
126         if (helpSplit == null) {
127             this.setSize(new Dimension(getSize().width + helpPanel.getPreferredSize().width, getSize().height));
128             
129             helpSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, main, helpPanel);
130             helpSplit.setResizeWeight(1);
131             helpSplit.setOneTouchExpandable(true);
132             Object ui = helpSplit.getUI();
133             if (ui instanceof BasicSplitPaneUI) {
134                 ((BasicSplitPaneUI)ui).getDivider().setBorder(
135                         new CompoundBorder(
136                             new CompoundBorder(
137                                   new MatteBorder(0, 1, 0, 0, UIManager.getColor("controlDkShadow")),
138                                   new MatteBorder( 0, 0, 0, 1, UIManager.getColor("controlLtHighlight"))),
139                             new CompoundBorder(
140                                   new MatteBorder(0, 0, 0, 1, UIManager.getColor("controlDkShadow")),
141                                   new MatteBorder( 0, 1, 0, 0, UIManager.getColor("controlLtHighlight")))));
142             }
143             helpSplit.setBorder(null);
144             
145             JPanel contentPane = (JPanel)getContentPane();
146 //            contentPane.removeAll();
147             contentPane.add(helpSplit, BorderLayout.CENTER);
148             
149             helpSplit.revalidate();
150             helpSplit.repaint();
151             
152             contentPane.revalidate();
153             contentPane.repaint();
154 
155             validate();
156             repaint();
157         }
158         
159         if (!helpPanel.isVisible() || helpPanel.getSize().width < 10) { // let's use a number a little bigger than 0
160             helpSplit.setDividerLocation(helpSplit.getDividerLocation() - helpPanel.getPreferredSize().width);
161         }
162     
163         helpPanel.showContext();
164     }
165 
166     private void hideHelpPanel() {
167         JButton helpButton = getHelpButton();
168         
169         helpButton.requestFocusInWindow();
170 
171         helpSplit = null;
172         this.setSize(new Dimension(getSize().width - getHelpPanel().getPreferredSize().width, getSize().height));
173         
174         JPanel contentPane = (JPanel)getContentPane();
175         contentPane.removeAll();
176         contentPane.add(main, BorderLayout.CENTER);
177         contentPane.revalidate();
178         contentPane.repaint();
179 
180         validate();
181         repaint();
182 
183         helpButton.requestFocusInWindow();
184     }
185 
186     private HelpPanel getHelpPanel() {
187         if (helpPanel == null) {
188             helpPanel = new HelpPanel();
189             helpPanel.setBorder(null);
190             helpPanel.addCloseActionListener(new ActionListener() {
191                 public void actionPerformed(ActionEvent arg0) {
192                     hideHelpPanel();
193                 }
194             });
195         }
196         
197         return helpPanel;
198     }
199 }