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
    18  package org.apache.log4j.chainsaw;
    19
    20  import java.util.Arrays;
    21  import java.util.Collections;
    22  import java.util.Comparator;
    23  import java.util.Hashtable;
    24  import java.util.Iterator;
    25  import java.util.List;
    26
    27  import javax.swing.DefaultBoundedRangeModel;
    28  import javax.swing.JLabel;
    29  import javax.swing.JSlider;
    30  import javax.swing.SwingConstants;
    31
    32  import org.apache.log4j.Level;
    33
    34
    35  /**
    36   * A Slider implementation that allows a user to
    37   * choose a particular Threshold
    38   * .
    39   * @author Paul Smith &lt;psmith@apache.org&gt;
    40   *
    41   */
    42  final class ThresholdSlider extends JSlider {
    43    final List priorityList;
    44
    45    ThresholdSlider() {
    46      Level[] levels =
    47        new Level[] {
    48          Level.OFF, Level.FATAL, Level.ERROR, Level.WARN, Level.INFO,
    49          Level.DEBUG, Level.TRACE, Level.ALL
    50        };
    51
    52      priorityList = Arrays.asList(levels);
    53
    54      Collections.sort(
    55        priorityList,
    56        new Comparator() {
    57          public int compare(Object o1, Object o2) {
    58            Level p1 = (Level) o1;
    59            Level p2 = (Level) o2;
    60
    61            if (p1.toInt() == p2.toInt()) {
    62              return 0;
    63            } else if (p1.toInt() < p2.toInt()) {
    64              return -1;
    65            }
    66
    67            return 1;
    68          }
    69        });
    70
    71      setModel(
    72        new DefaultBoundedRangeModel(
    73          priorityList.indexOf(Level.TRACE), 0, 0, priorityList.size() - 1));
    74
    75      Hashtable labelMap = new Hashtable();
    76
    77      for (Iterator iter = priorityList.iterator(); iter.hasNext();) {
    78        Level item = (Level) iter.next();
    79        labelMap.put(
    80          new Integer(priorityList.indexOf(item)), new JLabel(item.toString()));
    81
    82        //      System.out.println("creating levels for :: " + item.toInt() + "," + item.toString());
    83      }
    84
    85      setOrientation(SwingConstants.VERTICAL);
    86      setInverted(true);
    87      setLabelTable(labelMap);
    88
    89      setPaintLabels(true);
    90
    91      //    setPaintTicks(true);
    92      setSnapToTicks(true);
    93
    94      //    setMajorTickSpacing(10000);
    95      //    setPaintTrack(true);
    96    }
    97
    98    void setChosenLevel(Level level) {
    99      setValue(priorityList.indexOf(level));
    100   }
    101
    102   /**
    103    * Returns the Log4j Level that is currently selected in this slider
    104    * @return
    105    */
    106   Level getSelectedLevel() {
    107     Level level = (Level) priorityList.get(getValue());
    108
    109     if (level == null) {
    110       level = Level.TRACE;
    111     }
    112
    113     return level;
    114   }
    115 }