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