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.tree.DefaultTreeCellEditor;
21 import javax.swing.tree.TreePath;
22 import java.awt.*;
23 import java.awt.event.MouseEvent;
24 import java.util.EventObject;
25
26 /***
27 * CategoryImmediateEditor
28 *
29 * @author Michael J. Sikorsky
30 * @author Robert Shaw
31 */
32
33 // Contributed by ThoughtWorks Inc.
34
35 public class CategoryImmediateEditor extends DefaultTreeCellEditor {
36 //--------------------------------------------------------------------------
37 // Constants:
38 //--------------------------------------------------------------------------
39
40 //--------------------------------------------------------------------------
41 // Protected Variables:
42 //--------------------------------------------------------------------------
43 private CategoryNodeRenderer renderer;
44 protected Icon editingIcon = null;
45
46 //--------------------------------------------------------------------------
47 // Private Variables:
48 //--------------------------------------------------------------------------
49
50 //--------------------------------------------------------------------------
51 // Constructors:
52 //--------------------------------------------------------------------------
53 public CategoryImmediateEditor(JTree tree,
54 CategoryNodeRenderer renderer,
55 CategoryNodeEditor editor) {
56 super(tree, renderer, editor);
57 this.renderer = renderer;
58 renderer.setIcon(null);
59 renderer.setLeafIcon(null);
60 renderer.setOpenIcon(null);
61 renderer.setClosedIcon(null);
62
63 super.editingIcon = null;
64 }
65
66 //--------------------------------------------------------------------------
67 // Public Methods:
68 //--------------------------------------------------------------------------
69 public boolean shouldSelectCell(EventObject e) {
70 boolean rv = false; // only mouse events
71
72 if (e instanceof MouseEvent) {
73 MouseEvent me = (MouseEvent) e;
74 TreePath path = tree.getPathForLocation(me.getX(),
75 me.getY());
76 CategoryNode node = (CategoryNode)
77 path.getLastPathComponent();
78
79 rv = node.isLeaf() /*|| !inCheckBoxHitRegion(me)*/;
80 }
81 return rv;
82 }
83
84 public boolean inCheckBoxHitRegion(MouseEvent e) {
85 TreePath path = tree.getPathForLocation(e.getX(),
86 e.getY());
87 if (path == null) {
88 return false;
89 }
90 CategoryNode node = (CategoryNode) path.getLastPathComponent();
91 boolean rv = false;
92
93 if (true) {
94 // offset and lastRow DefaultTreeCellEditor
95 // protected members
96
97 Rectangle bounds = tree.getRowBounds(lastRow);
98 Dimension checkBoxOffset =
99 renderer.getCheckBoxOffset();
100
101 bounds.translate(offset + checkBoxOffset.width,
102 checkBoxOffset.height);
103
104 rv = bounds.contains(e.getPoint());
105 }
106 return true;
107 }
108
109 //--------------------------------------------------------------------------
110 // Protected Methods:
111 //--------------------------------------------------------------------------
112
113 protected boolean canEditImmediately(EventObject e) {
114 boolean rv = false;
115
116 if (e instanceof MouseEvent) {
117 MouseEvent me = (MouseEvent) e;
118 rv = inCheckBoxHitRegion(me);
119 }
120
121 return rv;
122 }
123
124 protected void determineOffset(JTree tree, Object value,
125 boolean isSelected, boolean expanded,
126 boolean leaf, int row) {
127 // Very important: means that the tree won't jump around.
128 offset = 0;
129 }
130
131 //--------------------------------------------------------------------------
132 // Private Methods:
133 //--------------------------------------------------------------------------
134
135 //--------------------------------------------------------------------------
136 // Nested Top-Level Classes or Interfaces:
137 //--------------------------------------------------------------------------
138
139 }
140
141
142
143
144
145