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  package org.xmlhammer.gui.output;
22  
23  import java.awt.BorderLayout;
24  import java.awt.Dimension;
25  import java.awt.FlowLayout;
26  import java.awt.SystemColor;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.awt.event.KeyEvent;
30  
31  import javax.swing.AbstractAction;
32  import javax.swing.ImageIcon;
33  import javax.swing.JButton;
34  import javax.swing.JComponent;
35  import javax.swing.JFrame;
36  import javax.swing.JPanel;
37  import javax.swing.JSplitPane;
38  import javax.swing.JTabbedPane;
39  import javax.swing.KeyStroke;
40  import javax.swing.UIManager;
41  import javax.swing.border.CompoundBorder;
42  import javax.swing.border.EmptyBorder;
43  import javax.swing.border.MatteBorder;
44  import javax.swing.event.ChangeEvent;
45  import javax.swing.event.ChangeListener;
46  import javax.swing.plaf.basic.BasicSplitPaneUI;
47  
48  import org.bounce.FormConstraints;
49  import org.bounce.QDialog;
50  import org.bounce.image.ImageLoader;
51  import org.xmlhammer.gui.help.HelpPanel;
52  
53  abstract class DetailsDialog extends QDialog {
54      private static final long serialVersionUID = 7985467156365925248L;
55  
56      protected static final FormConstraints LABEL_CONSTRAINTS = new FormConstraints(FormConstraints.LEFT, FormConstraints.RIGHT, FormConstraints.TOP);
57      private static final ImageIcon HELP_ICON = ImageLoader.get().getImage("/org/xmlhammer/gui/icons/etool16/help.gif");
58  
59      private JButton helpButton = null;
60      private JSplitPane helpSplit = null;
61      private HelpPanel helpPanel = null;
62  
63      private JPanel contentPane = null;
64      private JPanel main = null;
65  
66      private JTabbedPane tabs = null;
67      private JPanel leftButtonPanel = null;
68      private JPanel rightButtonPanel = null;
69  
70      DetailsDialog(JFrame parent, String title) {
71          super(parent, title, true);
72          
73          main = new JPanel( new BorderLayout());
74  
75          tabs = new JTabbedPane();
76          tabs.addChangeListener(new ChangeListener() {
77              public void stateChanged(ChangeEvent event) {
78                  if (tabs.getSelectedComponent() != null) {
79                      getHelpPanel().showID(getBaseHelpID()+"."+((DetailsPanel)tabs.getSelectedComponent()).getHelpID());
80                  }
81              }
82          });
83  
84          JPanel tabContainer = new JPanel(new BorderLayout());
85          tabContainer.setBorder( new CompoundBorder( 
86                                      new CompoundBorder(
87                                          new MatteBorder( 1, 1, 0, 0, SystemColor.controlDkShadow),
88                                          new MatteBorder(0, 0, 1, 1, SystemColor.controlLtHighlight)),
89                                      new EmptyBorder( 1, 1, 1, 1)));
90          tabContainer.add(tabs, BorderLayout.CENTER);
91          main.add(tabContainer, BorderLayout.CENTER);
92          
93          leftButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
94          rightButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
95  
96          JPanel buttonPanel = new JPanel(new BorderLayout());
97          buttonPanel.add(leftButtonPanel, BorderLayout.WEST);
98          buttonPanel.add(rightButtonPanel, BorderLayout.EAST);
99  
100         JButton closeButton = new JButton("Close");
101         closeButton.addActionListener(new ActionListener() { 
102             public void actionPerformed(ActionEvent e) {
103                 close();
104             }
105         });
106         getRootPane().setDefaultButton(closeButton);
107         
108         addRightButton(closeButton);
109         buttonPanel.setBorder(new EmptyBorder(5, 0, 5, 0));
110         main.add(buttonPanel, BorderLayout.SOUTH);
111 
112         helpButton = new JButton("Help", HELP_ICON);
113         helpButton.addActionListener(new ActionListener() {
114             public void actionPerformed(ActionEvent arg0) {
115                 showHelpPanel();
116             }
117         });
118         addLeftButton(helpButton);
119         
120         getRootPane().getActionMap().put("helpAction", new AbstractAction() {
121             private static final long serialVersionUID = -6931927193561857562L;
122 
123             public void actionPerformed(ActionEvent e) {
124                 showHelpPanel();
125             }
126         });
127         getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0, false), "helpAction");
128 
129         contentPane = new JPanel(new BorderLayout());
130         contentPane.add(main, BorderLayout.CENTER);
131         setContentPane(contentPane);
132     }
133     
134     protected void addTab(String title, DetailsPanel panel) {
135         if (tabs.getTabCount() == 0) {
136             getHelpPanel().showID(getBaseHelpID()+"."+panel.getHelpID());
137         }
138         
139         tabs.addTab(title, panel);
140     }
141     
142     protected abstract String getBaseHelpID();
143     
144     protected void addLeftButton(JButton button) {
145         leftButtonPanel.add(button);
146     }
147 
148     protected void addRightButton(JButton button) {
149         rightButtonPanel.add(button);
150     }
151 
152     /***
153      * Show the help panel (preferred-size) if not already shown.
154      */
155     public void showHelpPanel() {
156         HelpPanel helpPanel = getHelpPanel();
157         
158         if (helpSplit == null) {
159             this.setSize(new Dimension(getSize().width + helpPanel.getPreferredSize().width, getSize().height));
160             
161             helpSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, main, helpPanel);
162             helpSplit.setResizeWeight(1);
163             helpSplit.setOneTouchExpandable(true);
164             Object ui = helpSplit.getUI();
165             if (ui instanceof BasicSplitPaneUI) {
166                 ((BasicSplitPaneUI)ui).getDivider().setBorder(
167                         new CompoundBorder(
168                             new CompoundBorder(
169                                   new MatteBorder(0, 1, 0, 0, UIManager.getColor("controlDkShadow")),
170                                   new MatteBorder( 0, 0, 0, 1, UIManager.getColor("controlLtHighlight"))),
171                             new CompoundBorder(
172                                   new MatteBorder(0, 0, 0, 1, UIManager.getColor("controlDkShadow")),
173                                   new MatteBorder( 0, 1, 0, 0, UIManager.getColor("controlLtHighlight")))));
174             }
175             helpSplit.setBorder(null);
176             
177             contentPane.add(helpSplit, BorderLayout.CENTER);
178             
179             helpSplit.revalidate();
180             helpSplit.repaint();
181             
182             contentPane.revalidate();
183             contentPane.repaint();
184 
185             validate();
186             repaint();
187         }
188         
189         if (!helpPanel.isVisible() || helpPanel.getSize().width < 10) { // let's use a number a little bigger than 0
190             helpSplit.setDividerLocation(main.getSize().width - helpPanel.getPreferredSize().width);
191         }
192     
193         helpPanel.showContext();
194     }
195 
196     public void hideHelpPanel() {
197 //        updateHelpSplitPreferences();
198         helpSplit = null;
199         this.setSize(new Dimension(getSize().width - getHelpPanel().getPreferredSize().width, getSize().height));
200         
201         contentPane.removeAll();
202         contentPane.add(main, BorderLayout.CENTER);
203         contentPane.revalidate();
204         contentPane.repaint();
205 
206         validate();
207         repaint();
208         
209         helpButton.requestFocusInWindow();
210     }
211     
212     private HelpPanel getHelpPanel() {
213         if (helpPanel == null) {
214             helpPanel = new HelpPanel();
215             helpPanel.setBorder(null);
216             helpPanel.addCloseActionListener(new ActionListener() {
217                 public void actionPerformed(ActionEvent arg0) {
218                     hideHelpPanel();
219                 }
220             });
221         }
222         
223         return helpPanel;
224     }
225 }