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.Dimension; 020import java.awt.Rectangle; 021import java.awt.event.MouseEvent; 022import java.util.EventObject; 023 024import javax.swing.Icon; 025import javax.swing.JTree; 026import javax.swing.tree.DefaultTreeCellEditor; 027import javax.swing.tree.TreePath; 028 029/** 030 * CategoryImmediateEditor 031 * 032 * @author Michael J. Sikorsky 033 * @author Robert Shaw 034 */ 035 036// Contributed by ThoughtWorks Inc. 037 038public class CategoryImmediateEditor extends DefaultTreeCellEditor { 039 //-------------------------------------------------------------------------- 040 // Constants: 041 //-------------------------------------------------------------------------- 042 043 //-------------------------------------------------------------------------- 044 // Protected Variables: 045 //-------------------------------------------------------------------------- 046 private CategoryNodeRenderer renderer; 047 protected Icon editingIcon = null; 048 049 //-------------------------------------------------------------------------- 050 // Private Variables: 051 //-------------------------------------------------------------------------- 052 053 //-------------------------------------------------------------------------- 054 // Constructors: 055 //-------------------------------------------------------------------------- 056 public CategoryImmediateEditor(JTree tree, 057 CategoryNodeRenderer renderer, 058 CategoryNodeEditor editor) { 059 super(tree, renderer, editor); 060 this.renderer = renderer; 061 renderer.setIcon(null); 062 renderer.setLeafIcon(null); 063 renderer.setOpenIcon(null); 064 renderer.setClosedIcon(null); 065 066 super.editingIcon = null; 067 } 068 069 //-------------------------------------------------------------------------- 070 // Public Methods: 071 //-------------------------------------------------------------------------- 072 public boolean shouldSelectCell(EventObject e) { 073 boolean rv = false; // only mouse events 074 075 if (e instanceof MouseEvent) { 076 MouseEvent me = (MouseEvent) e; 077 TreePath path = tree.getPathForLocation(me.getX(), 078 me.getY()); 079 CategoryNode node = (CategoryNode) 080 path.getLastPathComponent(); 081 082 rv = node.isLeaf() /*|| !inCheckBoxHitRegion(me)*/; 083 } 084 return rv; 085 } 086 087 public boolean inCheckBoxHitRegion(MouseEvent e) { 088 TreePath path = tree.getPathForLocation(e.getX(), 089 e.getY()); 090 if (path == null) { 091 return false; 092 } 093 CategoryNode node = (CategoryNode) path.getLastPathComponent(); 094 boolean rv = false; 095 096 if (true) { 097 // offset and lastRow DefaultTreeCellEditor 098 // protected members 099 100 Rectangle bounds = tree.getRowBounds(lastRow); 101 Dimension checkBoxOffset = 102 renderer.getCheckBoxOffset(); 103 104 bounds.translate(offset + checkBoxOffset.width, 105 checkBoxOffset.height); 106 107 rv = bounds.contains(e.getPoint()); 108 } 109 return true; 110 } 111 112 //-------------------------------------------------------------------------- 113 // Protected Methods: 114 //-------------------------------------------------------------------------- 115 116 protected boolean canEditImmediately(EventObject e) { 117 boolean rv = false; 118 119 if (e instanceof MouseEvent) { 120 MouseEvent me = (MouseEvent) e; 121 rv = inCheckBoxHitRegion(me); 122 } 123 124 return rv; 125 } 126 127 protected void determineOffset(JTree tree, Object value, 128 boolean isSelected, boolean expanded, 129 boolean leaf, int row) { 130 // Very important: means that the tree won't jump around. 131 offset = 0; 132 } 133 134 //-------------------------------------------------------------------------- 135 // Private Methods: 136 //-------------------------------------------------------------------------- 137 138 //-------------------------------------------------------------------------- 139 // Nested Top-Level Classes or Interfaces: 140 //-------------------------------------------------------------------------- 141 142} 143 144 145 146 147 148