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.preferences;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Component;
26  import java.awt.Dimension;
27  import java.awt.FlowLayout;
28  import java.awt.Frame;
29  import java.awt.Point;
30  import java.awt.event.ActionEvent;
31  import java.awt.event.ActionListener;
32  import java.awt.event.KeyEvent;
33  
34  import javax.swing.AbstractAction;
35  import javax.swing.ImageIcon;
36  import javax.swing.JButton;
37  import javax.swing.JComponent;
38  import javax.swing.JOptionPane;
39  import javax.swing.JPanel;
40  import javax.swing.JSplitPane;
41  import javax.swing.JTable;
42  import javax.swing.KeyStroke;
43  import javax.swing.UIManager;
44  import javax.swing.border.CompoundBorder;
45  import javax.swing.border.MatteBorder;
46  import javax.swing.plaf.basic.BasicSplitPaneUI;
47  
48  import org.bounce.event.CardEvent;
49  import org.bounce.event.CardPanelAdapter;
50  import org.bounce.image.ImageLoader;
51  import org.bounce.preferences.PreferencesDialog;
52  import org.bounce.preferences.PreferencesPage;
53  import org.xmlhammer.gui.help.HelpPanel;
54  
55  /***
56   * JAXP Properties ...
57   * 
58   * @version $Revision$, $Date$
59   * @author Edwin Dankert <edankert@gmail.com>
60   */
61  
62  public class HelpEnabledPreferencesDialog extends PreferencesDialog {
63  	private static final long serialVersionUID = -7403432397234000652L;
64  
65      public static final int OK_OPTION = JOptionPane.OK_OPTION;
66      public static final int CANCEL_OPTION = JOptionPane.CANCEL_OPTION;
67  
68      private static final ImageIcon HELP_ICON = ImageLoader.get().getImage("/org/xmlhammer/gui/icons/etool16/help.gif");
69  
70      private static Dimension size = null;
71      private static Point position = null;
72  
73      private JSplitPane helpSplit = null;
74      private HelpPanel helpPanel = null;
75  
76      // initialised
77      private JButton helpButton;
78      private JPanel main;
79  
80  	/***
81  	 * @param parent
82  	 * @throws java.awt.HeadlessException
83  	 */
84  	public HelpEnabledPreferencesDialog(Frame parent, String title) {
85  		super(parent, title);
86  		
87          getCards().addCardPanelListener(new CardPanelAdapter<PreferencesPage>() {
88              public void cardChanged(CardEvent<PreferencesPage> event) {
89                  getHelpPanel().showID(((HelpEnabledPreferencesPage)event.getCard()).getHelpID());
90              }
91          });
92  
93          getRootPane().getActionMap().put("helpAction", new AbstractAction() {
94              private static final long serialVersionUID = -6931927193561857562L;
95  
96              public void actionPerformed(ActionEvent e) {
97                  showHelpPanel();
98              }
99          });
100         getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0, false), "helpAction");
101 
102         size = new Dimension( 674, 534);
103 	}
104     
105     private JButton getHelpButton() {
106         if (helpButton == null) {
107             helpButton = new JButton("Help", HELP_ICON);
108             helpButton.addActionListener(new ActionListener() {
109                 public void actionPerformed(ActionEvent arg0) {
110                     showHelpPanel();
111                 }
112             });
113         }
114         
115         return helpButton;
116     }
117     
118     @Override
119     protected JPanel createSouthPanel() {
120         JPanel helpButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
121         helpButtonPanel.add(getHelpButton());
122         
123         JPanel southPanel = super.createSouthPanel();
124         southPanel.add(helpButtonPanel, BorderLayout.WEST);
125         
126         return southPanel;
127     }
128 
129     protected JPanel createCenterPanel() {
130         main = super.createCenterPanel();
131         
132         return main;
133     }
134 
135     /***
136      * Show the help panel (preferred-size) if not already shown.
137      */
138     public void showHelpPanel() {
139         HelpPanel helpPanel = getHelpPanel();
140         
141         if (helpSplit == null) {
142             this.setSize(new Dimension(getSize().width + helpPanel.getPreferredSize().width, getSize().height));
143             
144             helpSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, main, helpPanel);
145             helpSplit.setResizeWeight(1);
146             helpSplit.setOneTouchExpandable(true);
147             Object ui = helpSplit.getUI();
148             if (ui instanceof BasicSplitPaneUI) {
149                 ((BasicSplitPaneUI)ui).getDivider().setBorder(
150                         new CompoundBorder(
151                             new CompoundBorder(
152                                   new MatteBorder(0, 1, 0, 0, UIManager.getColor("controlDkShadow")),
153                                   new MatteBorder( 0, 0, 0, 1, UIManager.getColor("controlLtHighlight"))),
154                             new CompoundBorder(
155                                   new MatteBorder(0, 0, 0, 1, UIManager.getColor("controlDkShadow")),
156                                   new MatteBorder( 0, 1, 0, 0, UIManager.getColor("controlLtHighlight")))));
157             }
158             helpSplit.setBorder(null);
159             
160             JPanel contentPane = (JPanel)getContentPane();
161             contentPane.add(helpSplit, BorderLayout.CENTER);
162             
163             helpSplit.revalidate();
164             helpSplit.repaint();
165             
166             contentPane.revalidate();
167             contentPane.repaint();
168 
169             validate();
170             repaint();
171         }
172         
173         if (!helpPanel.isVisible() || helpPanel.getSize().width < 10) { // let's use a number a little bigger than 0
174             helpSplit.setDividerLocation(helpSplit.getDividerLocation() - helpPanel.getPreferredSize().width);
175         }
176     
177         helpPanel.showContext();
178     }
179 
180     public void hideHelpPanel() {
181         helpSplit = null;
182         this.setSize(new Dimension(getSize().width - getHelpPanel().getPreferredSize().width, getSize().height));
183         
184         JPanel contentPane = (JPanel)getContentPane();
185         contentPane.removeAll();
186         contentPane.add(main, BorderLayout.CENTER);
187         contentPane.revalidate();
188         contentPane.repaint();
189 
190         validate();
191         repaint();
192         
193         helpButton.requestFocusInWindow();
194     }
195 
196     private HelpPanel getHelpPanel() {
197         if (helpPanel == null) {
198             helpPanel = new HelpPanel();
199             helpPanel.setBorder(null);
200             helpPanel.addCloseActionListener(new ActionListener() {
201                 public void actionPerformed(ActionEvent arg0) {
202                     hideHelpPanel();
203                 }
204             });
205         }
206         
207         return helpPanel;
208     }
209 
210     public void close() {
211         size = getSize();
212         position = getLocation();
213         
214         super.close();
215     }
216     
217     public void cancel() {
218         // Just to make sure the table is no longer edited. 
219         // Causing bug 1552429
220         Component comp = getFocusOwner();
221         
222         if (comp instanceof JTable && ((JTable)comp).getCellEditor() != null) {
223             ((JTable)comp).getCellEditor().stopCellEditing();
224         }
225         
226         size = getSize();
227         position = getLocation();
228         
229         super.cancel();
230     }
231     
232     public int getSelectedOption() {
233         if (isCancelled()) {
234             return JOptionPane.CANCEL_OPTION;
235         }
236         
237         return JOptionPane.OK_OPTION;
238     }
239 
240     /***
241      * @return whether the properties should be stored.
242      */
243     public int open() { 
244         restore();
245         setVisible(true);
246         
247         return getSelectedOption();
248     }
249 
250     /***
251      * Restores to previous position and size.
252      */
253     public void restore() { 
254         this.setSize(size);
255 
256         if (position == null) {
257             setLocationRelativeTo(getParent());
258         } else {
259             setLocation(position);
260         }
261     }
262 }