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  package org.apache.log4j.lf5.viewer.categoryexplorer;
    18
    19  import javax.swing.*;
    20  import javax.swing.event.TreeModelEvent;
    21  import javax.swing.tree.TreePath;
    22  import java.awt.event.MouseEvent;
    23
    24  /***
    25   * CategoryExplorerTree
    26   *
    27   * @author Michael J. Sikorsky
    28   * @author Robert Shaw
    29   * @author Brent Sprecher
    30   * @author Brad Marlborough
    31   */
    32
    33  // Contributed by ThoughtWorks Inc.
    34
    35  public class CategoryExplorerTree extends JTree {
    36    private static final long serialVersionUID = 8066257446951323576L;
    37    //--------------------------------------------------------------------------
    38    //   Constants:
    39    //--------------------------------------------------------------------------
    40
    41    //--------------------------------------------------------------------------
    42    //   Protected Variables:
    43    //--------------------------------------------------------------------------
    44    protected CategoryExplorerModel _model;
    45    protected boolean _rootAlreadyExpanded = false;
    46
    47    //--------------------------------------------------------------------------
    48    //   Private Variables:
    49    //--------------------------------------------------------------------------
    50
    51    //--------------------------------------------------------------------------
    52    //   Constructors:
    53    //--------------------------------------------------------------------------
    54
    55    /***
    56     * Construct a CategoryExplorerTree with a specific model.
    57     */
    58    public CategoryExplorerTree(CategoryExplorerModel model) {
    59      super(model);
    60
    61      _model = model;
    62      init();
    63    }
    64
    65    /***
    66     * Construct a CategoryExplorerTree and create a default CategoryExplorerModel.
    67     */
    68    public CategoryExplorerTree() {
    69      super();
    70
    71      CategoryNode rootNode = new CategoryNode("Categories");
    72
    73      _model = new CategoryExplorerModel(rootNode);
    74
    75      setModel(_model);
    76
    77      init();
    78    }
    79
    80    //--------------------------------------------------------------------------
    81    //   Public Methods:
    82    //--------------------------------------------------------------------------
    83
    84    public CategoryExplorerModel getExplorerModel() {
    85      return (_model);
    86    }
    87
    88    public String getToolTipText(MouseEvent e) {
    89
    90      try {
    91        return super.getToolTipText(e);
    92      } catch (Exception ex) {
    93        return "";
    94      }
    95
    96    }
    97
    98    //--------------------------------------------------------------------------
    99    //   Protected Methods:
    100   //--------------------------------------------------------------------------
    101
    102   protected void init() {
    103     // Put visible lines on the JTree.
    104     putClientProperty("JTree.lineStyle", "Angled");
    105
    106     // Configure the Tree with the appropriate Renderers and Editors.
    107
    108     CategoryNodeRenderer renderer = new CategoryNodeRenderer();
    109     setEditable(true);
    110     setCellRenderer(renderer);
    111
    112     CategoryNodeEditor editor = new CategoryNodeEditor(_model);
    113
    114     setCellEditor(new CategoryImmediateEditor(this,
    115         new CategoryNodeRenderer(),
    116         editor));
    117     setShowsRootHandles(true);
    118
    119     setToolTipText("");
    120
    121     ensureRootExpansion();
    122
    123   }
    124
    125   protected void expandRootNode() {
    126     if (_rootAlreadyExpanded) {
    127       return;
    128     }
    129     _rootAlreadyExpanded = true;
    130     TreePath path = new TreePath(_model.getRootCategoryNode().getPath());
    131     expandPath(path);
    132   }
    133
    134   protected void ensureRootExpansion() {
    135     _model.addTreeModelListener(new TreeModelAdapter() {
    136       public void treeNodesInserted(TreeModelEvent e) {
    137         expandRootNode();
    138       }
    139     });
    140   }
    141
    142   //--------------------------------------------------------------------------
    143   //   Private Methods:
    144   //--------------------------------------------------------------------------
    145
    146   //--------------------------------------------------------------------------
    147   //   Nested Top-Level Classes or Interfaces:
    148   //--------------------------------------------------------------------------
    149
    150 }
    151
    152
    153
    154
    155
    156