001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.log4j.lf5.viewer.categoryexplorer; 018 019import java.awt.event.MouseEvent; 020 021import javax.swing.JTree; 022import javax.swing.event.TreeModelEvent; 023import javax.swing.tree.TreePath; 024 025/** 026 * CategoryExplorerTree 027 * 028 * @author Michael J. Sikorsky 029 * @author Robert Shaw 030 * @author Brent Sprecher 031 * @author Brad Marlborough 032 */ 033 034// Contributed by ThoughtWorks Inc. 035 036public class CategoryExplorerTree extends JTree { 037 private static final long serialVersionUID = 8066257446951323576L; 038 //-------------------------------------------------------------------------- 039 // Constants: 040 //-------------------------------------------------------------------------- 041 042 //-------------------------------------------------------------------------- 043 // Protected Variables: 044 //-------------------------------------------------------------------------- 045 protected CategoryExplorerModel _model; 046 protected boolean _rootAlreadyExpanded = false; 047 048 //-------------------------------------------------------------------------- 049 // Private Variables: 050 //-------------------------------------------------------------------------- 051 052 //-------------------------------------------------------------------------- 053 // Constructors: 054 //-------------------------------------------------------------------------- 055 056 /** 057 * Construct a CategoryExplorerTree with a specific model. 058 */ 059 public CategoryExplorerTree(CategoryExplorerModel model) { 060 super(model); 061 062 _model = model; 063 init(); 064 } 065 066 /** 067 * Construct a CategoryExplorerTree and create a default CategoryExplorerModel. 068 */ 069 public CategoryExplorerTree() { 070 super(); 071 072 CategoryNode rootNode = new CategoryNode("Categories"); 073 074 _model = new CategoryExplorerModel(rootNode); 075 076 setModel(_model); 077 078 init(); 079 } 080 081 //-------------------------------------------------------------------------- 082 // Public Methods: 083 //-------------------------------------------------------------------------- 084 085 public CategoryExplorerModel getExplorerModel() { 086 return (_model); 087 } 088 089 public String getToolTipText(MouseEvent e) { 090 091 try { 092 return super.getToolTipText(e); 093 } catch (Exception ex) { 094 return ""; 095 } 096 097 } 098 099 //-------------------------------------------------------------------------- 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