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 java.awt.Dimension;
    20  import java.awt.Rectangle;
    21  import java.awt.event.MouseEvent;
    22  import java.util.EventObject;
    23
    24  import javax.swing.Icon;
    25  import javax.swing.JTree;
    26  import javax.swing.tree.DefaultTreeCellEditor;
    27  import javax.swing.tree.TreePath;
    28
    29  /**
    30   * CategoryImmediateEditor
    31   *
    32   * @author Michael J. Sikorsky
    33   * @author Robert Shaw
    34   */
    35
    36  // Contributed by ThoughtWorks Inc.
    37
    38  public class CategoryImmediateEditor extends DefaultTreeCellEditor {
    39    //--------------------------------------------------------------------------
    40    //   Constants:
    41    //--------------------------------------------------------------------------
    42
    43    //--------------------------------------------------------------------------
    44    //   Protected Variables:
    45    //--------------------------------------------------------------------------
    46    private CategoryNodeRenderer renderer;
    47    protected Icon editingIcon = null;
    48
    49    //--------------------------------------------------------------------------
    50    //   Private Variables:
    51    //--------------------------------------------------------------------------
    52
    53    //--------------------------------------------------------------------------
    54    //   Constructors:
    55    //--------------------------------------------------------------------------
    56    public CategoryImmediateEditor(JTree tree,
    57        CategoryNodeRenderer renderer,
    58        CategoryNodeEditor editor) {
    59      super(tree, renderer, editor);
    60      this.renderer = renderer;
    61      renderer.setIcon(null);
    62      renderer.setLeafIcon(null);
    63      renderer.setOpenIcon(null);
    64      renderer.setClosedIcon(null);
    65
    66      super.editingIcon = null;
    67    }
    68
    69    //--------------------------------------------------------------------------
    70    //   Public Methods:
    71    //--------------------------------------------------------------------------
    72    public boolean shouldSelectCell(EventObject e) {
    73      boolean rv = false;  // only mouse events
    74
    75      if (e instanceof MouseEvent) {
    76        MouseEvent me = (MouseEvent) e;
    77        TreePath path = tree.getPathForLocation(me.getX(),
    78            me.getY());
    79        CategoryNode node = (CategoryNode)
    80            path.getLastPathComponent();
    81
    82        rv = node.isLeaf() /*|| !inCheckBoxHitRegion(me)*/;
    83      }
    84      return rv;
    85    }
    86
    87    public boolean inCheckBoxHitRegion(MouseEvent e) {
    88      TreePath path = tree.getPathForLocation(e.getX(),
    89          e.getY());
    90      if (path == null) {
    91        return false;
    92      }
    93      CategoryNode node = (CategoryNode) path.getLastPathComponent();
    94      boolean rv = false;
    95
    96      if (true) {
    97        // offset and lastRow DefaultTreeCellEditor
    98        // protected members
    99
    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