Coverage Report - org.xmlhammer.gui.util.wizard.HelpEnabledWizard
 
Classes in this File Line Coverage Branch Coverage Complexity
HelpEnabledWizard
45% 
40% 
0
 
 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  2430
 public abstract class HelpEnabledWizard extends Wizard {
 52  
     private static final long serialVersionUID = 7119005231853816073L;
 53  
     
 54  22
     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  220
     private HelpPanel helpPanel = null;
 60  220
     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  220
         super(parent);
 69  
 
 70  220
         getRootPane().getActionMap().put("helpAction", new AbstractAction() {
 71  
             private static final long serialVersionUID = -6931927193561857562L;
 72  
 
 73  220
             public void actionPerformed(ActionEvent e) {
 74  0
                 showHelpPanel();
 75  0
             }
 76  
         });
 77  220
         getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0, false), "helpAction");
 78  
 
 79  220
         getCards().addCardPanelListener(new CardPanelAdapter<WizardPage>() {
 80  220
             public void cardChanged(CardEvent<WizardPage> event) {
 81  2430
                 getHelpPanel().showID(((HelpEnabledWizardPage)event.getCard()).getHelpID());
 82  2430
             }
 83  
         });
 84  
 
 85  220
         pack();
 86  220
         setSize(new Dimension(WIZARD_WIDTH, Math.max(getSize().height, MINIMUM_WIZARD_HEIGHT)));
 87  220
         setLocationRelativeTo(getParent());
 88  220
     }
 89  
     
 90  
     protected JPanel createCenterPanel() {
 91  440
         main = super.createCenterPanel();
 92  
         
 93  440
         return main;
 94  
     }
 95  
     
 96  
     @Override
 97  
     protected JPanel createSouthPanel() {
 98  220
         JPanel helpButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
 99  220
         helpButtonPanel.add(getHelpButton());
 100  
         
 101  220
         JPanel southPanel = super.createSouthPanel();
 102  220
         southPanel.add(helpButtonPanel, BorderLayout.WEST);
 103  
         
 104  220
         return southPanel;
 105  
     }
 106  
     
 107  
     private JButton getHelpButton() {
 108  220
         if (helpButton == null) {
 109  220
             helpButton = new JButton("Help", HELP_ICON);
 110  220
             helpButton.addActionListener(new ActionListener() {
 111  220
                 public void actionPerformed(ActionEvent arg0) {
 112  0
                     showHelpPanel();
 113  0
                 }
 114  
             });
 115  
         }
 116  
         
 117  220
         return helpButton;
 118  
     }
 119  
     
 120  
     /**
 121  
      * Show the help panel (preferred-size) if not already shown.
 122  
      */
 123  
     public void showHelpPanel() {
 124  0
         HelpPanel helpPanel = getHelpPanel();
 125  
         
 126  0
         if (helpSplit == null) {
 127  0
             this.setSize(new Dimension(getSize().width + helpPanel.getPreferredSize().width, getSize().height));
 128  
             
 129  0
             helpSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, main, helpPanel);
 130  0
             helpSplit.setResizeWeight(1);
 131  0
             helpSplit.setOneTouchExpandable(true);
 132  0
             Object ui = helpSplit.getUI();
 133  0
             if (ui instanceof BasicSplitPaneUI) {
 134  0
                 ((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  0
             helpSplit.setBorder(null);
 144  
             
 145  0
             JPanel contentPane = (JPanel)getContentPane();
 146  
 //            contentPane.removeAll();
 147  0
             contentPane.add(helpSplit, BorderLayout.CENTER);
 148  
             
 149  0
             helpSplit.revalidate();
 150  0
             helpSplit.repaint();
 151  
             
 152  0
             contentPane.revalidate();
 153  0
             contentPane.repaint();
 154  
 
 155  0
             validate();
 156  0
             repaint();
 157  
         }
 158  
         
 159  0
         if (!helpPanel.isVisible() || helpPanel.getSize().width < 10) { // let's use a number a little bigger than 0
 160  0
             helpSplit.setDividerLocation(helpSplit.getDividerLocation() - helpPanel.getPreferredSize().width);
 161  
         }
 162  
     
 163  0
         helpPanel.showContext();
 164  0
     }
 165  
 
 166  
     private void hideHelpPanel() {
 167  0
         JButton helpButton = getHelpButton();
 168  
         
 169  0
         helpButton.requestFocusInWindow();
 170  
 
 171  0
         helpSplit = null;
 172  0
         this.setSize(new Dimension(getSize().width - getHelpPanel().getPreferredSize().width, getSize().height));
 173  
         
 174  0
         JPanel contentPane = (JPanel)getContentPane();
 175  0
         contentPane.removeAll();
 176  0
         contentPane.add(main, BorderLayout.CENTER);
 177  0
         contentPane.revalidate();
 178  0
         contentPane.repaint();
 179  
 
 180  0
         validate();
 181  0
         repaint();
 182  
 
 183  0
         helpButton.requestFocusInWindow();
 184  0
     }
 185  
 
 186  
     private HelpPanel getHelpPanel() {
 187  2430
         if (helpPanel == null) {
 188  220
             helpPanel = new HelpPanel();
 189  220
             helpPanel.setBorder(null);
 190  220
             helpPanel.addCloseActionListener(new ActionListener() {
 191  220
                 public void actionPerformed(ActionEvent arg0) {
 192  0
                     hideHelpPanel();
 193  0
                 }
 194  
             });
 195  
         }
 196  
         
 197  2430
         return helpPanel;
 198  
     }
 199  
 }