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  import java.util.List;
    27
    28  import javax.swing.BorderFactory;
    29  import javax.swing.Box;
    30  import javax.swing.JButton;
    31  import javax.swing.JComponent;
    32  import javax.swing.JLabel;
    33  import javax.swing.JPanel;
    34  import javax.swing.JScrollPane;
    35  import javax.swing.JTree;
    36  import javax.swing.SwingUtilities;
    37  import javax.swing.event.TreeSelectionEvent;
    38  import javax.swing.event.TreeSelectionListener;
    39  import javax.swing.tree.DefaultMutableTreeNode;
    40  import javax.swing.tree.DefaultTreeCellRenderer;
    41  import javax.swing.tree.DefaultTreeSelectionModel;
    42  import javax.swing.tree.TreeModel;
    43  import javax.swing.tree.TreePath;
    44  import javax.swing.tree.TreeSelectionModel;
    45
    46  import org.apache.log4j.chainsaw.helper.SwingHelper;
    47  import org.apache.log4j.chainsaw.icons.ChainsawIcons;
    48  /**
    49   * Some basic plumbing for Preference related dialogs.
    50   *
    51   * Sub classes have the following responsibilities:
    52   *
    53   * <ul>
    54   * <li>Must call this this classes initComponents() method from
    55   * within the sub-classes constructor, this method laysout the
    56   * panel and calls the abstract createTreeModel method to initialise the
    57   * tree of Panels.
    58   * <li>Must override createTreeModel() method to return a TreeModel whose
    59   * TreeNodes getUserObject() method will return a JComponent that is displayed
    60   * as the selected editable Preference panel.  This JComponent's .toString() method is
    61   * used as the title of the preference panel
    62   * </ul>
    63   *
    64   * @author Paul Smith &lt;psmith@apache.org&gt;
    65   *
    66   */
    67  public abstract class AbstractPreferencePanel extends JPanel
    68  {
    69
    70    private final JLabel titleLabel = new JLabel("Selected Pref Panel");
    71    private final JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
    72    private final JPanel selectedPrefPanel = new JPanel(new BorderLayout(0, 3));
    73    private final JButton okButton = new JButton(" OK ");
    74    private final JButton cancelButton = new JButton(" Cancel ");
    75    private ActionListener okCancelListener;
    76    private Component currentlyDisplayedPanel = null;
    77    private final JTree prefTree = new JTree();
    78    /**
    79     * Setup and layout for the components
    80     */
    81    protected void initComponents()
    82    {
    83      //		setBorder(BorderFactory.createLineBorder(Color.red));
    84      setLayout(new BorderLayout(5, 5));
    85      setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
    86
    87      prefTree.setModel(createTreeModel());
    88
    89      prefTree.setRootVisible(false);
    90
    91      DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
    92      renderer.setLeafIcon(ChainsawIcons.ICON_PREFERENCES);
    93      prefTree.setCellRenderer(renderer);
    94
    95      final JScrollPane treeScroll = new JScrollPane(prefTree);
    96
    97      treeScroll.setPreferredSize(new Dimension(200, 240));
    98
    99      titleLabel.setFont(titleLabel.getFont().deriveFont(16.0f));
    100     titleLabel.setBorder(BorderFactory.createEtchedBorder());
    101     titleLabel.setBackground(Color.white);
    102     titleLabel.setOpaque(true);
    103
    104     selectedPrefPanel.add(titleLabel, BorderLayout.NORTH);
    105
    106     mainPanel.add(treeScroll, BorderLayout.WEST);
    107     mainPanel.add(selectedPrefPanel, BorderLayout.CENTER);
    108
    109     add(mainPanel, BorderLayout.CENTER);
    110
    111     Box buttonBox = Box.createHorizontalBox();
    112     List buttons = SwingHelper.orderOKCancelButtons(okButton, cancelButton);
    113     buttonBox.add(Box.createHorizontalGlue());
    114     buttonBox.add((JButton)buttons.get(0));
    115     buttonBox.add(Box.createHorizontalStrut(10));
    116     buttonBox.add((JButton)buttons.get(1));
    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 }