View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17   
18  package org.apache.log4j.chainsaw;
19  
20  import java.awt.BorderLayout;
21  import java.awt.Color;
22  import java.awt.Component;
23  import java.awt.Dimension;
24  import java.awt.event.ActionListener;
25  import java.util.Enumeration;
26  
27  import javax.swing.BorderFactory;
28  import javax.swing.Box;
29  import javax.swing.JButton;
30  import javax.swing.JComponent;
31  import javax.swing.JLabel;
32  import javax.swing.JPanel;
33  import javax.swing.JScrollPane;
34  import javax.swing.JTree;
35  import javax.swing.SwingUtilities;
36  import javax.swing.event.TreeSelectionEvent;
37  import javax.swing.event.TreeSelectionListener;
38  import javax.swing.tree.DefaultMutableTreeNode;
39  import javax.swing.tree.DefaultTreeCellRenderer;
40  import javax.swing.tree.DefaultTreeSelectionModel;
41  import javax.swing.tree.TreeModel;
42  import javax.swing.tree.TreePath;
43  import javax.swing.tree.TreeSelectionModel;
44  
45  import org.apache.log4j.chainsaw.icons.ChainsawIcons;
46  /***
47   * Some basic plumbing for Preference related dialogs.
48   * 
49   * Sub classes have the following responsibilities:
50   * 
51   * <ul>
52   * <li>Must call this this classes initComponents() method from
53   * within the sub-classes constructor, this method laysout the
54   * panel and calls the abstract createTreeModel method to initialise the
55   * tree of Panels.
56   * <li>Must override createTreeModel() method to return a TreeModel whose
57   * TreeNodes getUserObject() method will return a JComponent that is displayed
58   * as the selected editable Preference panel.  This JComponent's .toString() method is
59   * used as the title of the preference panel
60   * </ul>
61   * 
62   * @author Paul Smith <psmith@apache.org>
63   *
64   */
65  public abstract class AbstractPreferencePanel extends JPanel
66  {
67  
68    private final JLabel titleLabel = new JLabel("Selected Pref Panel");
69    private final JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
70    private final JPanel selectedPrefPanel = new JPanel(new BorderLayout(0, 3));
71    private final JButton okButton = new JButton("OK");
72    private final JButton cancelButton = new JButton("Cancel");
73    private ActionListener okCancelListener;
74    private Component currentlyDisplayedPanel = null;
75    private final JTree prefTree = new JTree();
76    /***
77     * Setup and layout for the components
78     */
79    protected void initComponents()
80    {
81      //		setBorder(BorderFactory.createLineBorder(Color.red));
82      setLayout(new BorderLayout(5, 5));
83      setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
84    
85      prefTree.setModel(createTreeModel());
86      
87      prefTree.setRootVisible(false);
88    
89      DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
90      renderer.setLeafIcon(ChainsawIcons.ICON_PREFERENCES);
91      prefTree.setCellRenderer(renderer);
92    
93      final JScrollPane treeScroll = new JScrollPane(prefTree);
94    
95      treeScroll.setPreferredSize(new Dimension(200, 240));
96    
97      titleLabel.setFont(titleLabel.getFont().deriveFont(16.0f));
98      titleLabel.setBorder(BorderFactory.createEtchedBorder());
99      titleLabel.setBackground(Color.white);
100     titleLabel.setOpaque(true);
101   
102     selectedPrefPanel.add(titleLabel, BorderLayout.NORTH);
103   
104     mainPanel.add(treeScroll, BorderLayout.WEST);
105     mainPanel.add(selectedPrefPanel, BorderLayout.CENTER);
106   
107     add(mainPanel, BorderLayout.CENTER);
108   
109   
110 
111   
112     Box buttonBox = Box.createHorizontalBox();
113     buttonBox.add(Box.createHorizontalGlue());
114     buttonBox.add(okButton);
115     buttonBox.add(Box.createHorizontalStrut(5));
116     buttonBox.add(cancelButton);
117   
118     add(buttonBox, BorderLayout.SOUTH);
119   
120     DefaultTreeSelectionModel treeSelectionModel =
121       new DefaultTreeSelectionModel();
122     treeSelectionModel.setSelectionMode(
123       TreeSelectionModel.SINGLE_TREE_SELECTION);
124     prefTree.setSelectionModel(treeSelectionModel);
125     prefTree.addTreeSelectionListener(
126       new TreeSelectionListener() {
127         public void valueChanged(TreeSelectionEvent e) {
128           TreePath path = e.getNewLeadSelectionPath();
129           DefaultMutableTreeNode node =
130             (DefaultMutableTreeNode) path.getLastPathComponent();
131           setDisplayedPrefPanel((JComponent) node.getUserObject());
132         }
133       });
134   
135     // ensure the first pref panel is selected and displayed
136     DefaultMutableTreeNode root =
137       (DefaultMutableTreeNode) prefTree.getModel().getRoot();
138     DefaultMutableTreeNode firstNode =
139       (DefaultMutableTreeNode) root.getFirstChild();
140     prefTree.setSelectionPath(new TreePath(firstNode.getPath()));
141   }
142 
143   /***
144    * @return tree model
145    */
146   protected abstract TreeModel createTreeModel();
147 
148   /***
149    * Ensures a specific panel is displayed in the spot where
150    * preferences can be selected.
151    *
152   * @param panel
153   */
154   protected void setDisplayedPrefPanel(JComponent panel)
155   {
156     if (currentlyDisplayedPanel != null) {
157       selectedPrefPanel.remove(currentlyDisplayedPanel);
158     }
159   
160     selectedPrefPanel.add(panel, BorderLayout.CENTER);
161     currentlyDisplayedPanel = panel;
162     String title = panel.toString();
163     titleLabel.setText(title);
164     selectedPrefPanel.revalidate();
165     selectedPrefPanel.repaint();
166   }
167 
168   public void notifyOfLookAndFeelChange() {
169     SwingUtilities.updateComponentTreeUI(this);
170     
171     Enumeration enumeration = ((DefaultMutableTreeNode)prefTree.getModel().getRoot()).breadthFirstEnumeration();
172     while (enumeration.hasMoreElements()) {
173       DefaultMutableTreeNode node = (DefaultMutableTreeNode) enumeration.nextElement();
174       if (node.getUserObject() instanceof Component) {
175         Component c = (Component) node.getUserObject();
176         SwingUtilities.updateComponentTreeUI(c);
177       }
178     }
179   }
180                                           
181   public void setOkCancelActionListener(ActionListener l)
182   {
183     this.okCancelListener = l;
184   }
185 
186   public void hidePanel()
187   {
188     if (okCancelListener != null) {
189       okCancelListener.actionPerformed(null);
190     }
191   }
192 
193   /***
194    * @return Returns the cancelButton.
195    */
196   protected final JButton getCancelButton()
197   {
198     return cancelButton;
199   }
200   /***
201    * @return Returns the okButton.
202    */
203   protected final JButton getOkButton()
204   {
205     return okButton;
206   }
207 }