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