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.chainsaw;
    18
    19  import java.awt.GridBagConstraints;
    20  import java.awt.GridBagLayout;
    21  import java.awt.event.ActionEvent;
    22  import java.awt.event.ActionListener;
    23  import javax.swing.BorderFactory;
    24  import javax.swing.JButton;
    25  import javax.swing.JComboBox;
    26  import javax.swing.JLabel;
    27  import javax.swing.JPanel;
    28  import javax.swing.JTextField;
    29  import javax.swing.event.DocumentEvent;
    30  import javax.swing.event.DocumentListener;
    31  import org.apache.log4j.Logger;
    32  import org.apache.log4j.Priority;
    33  import org.apache.log4j.Level;
    34
    35  /**
    36   * Represents the controls for filtering, pausing, exiting, etc.
    37   *
    38   * @author <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
    39   */
    40  class ControlPanel extends JPanel {
    41      /** use the log messages **/
    42      private static final Logger LOG =
    43                                    Logger.getLogger(ControlPanel.class);
    44
    45      /**
    46       * Creates a new <code>ControlPanel</code> instance.
    47       *
    48       * @param aModel the model to control
    49       */
    50      ControlPanel(final MyTableModel aModel) {
    51          setBorder(BorderFactory.createTitledBorder("Controls: "));
    52          final GridBagLayout gridbag = new GridBagLayout();
    53          final GridBagConstraints c = new GridBagConstraints();
    54          setLayout(gridbag);
    55
    56          // Pad everything
    57          c.ipadx = 5;
    58          c.ipady = 5;
    59
    60          // Add the 1st column of labels
    61          c.gridx = 0;
    62          c.anchor = GridBagConstraints.EAST;
    63
    64          c.gridy = 0;
    65          JLabel label = new JLabel("Filter Level:");
    66          gridbag.setConstraints(label, c);
    67          add(label);
    68
    69          c.gridy++;
    70          label = new JLabel("Filter Thread:");
    71          gridbag.setConstraints(label, c);
    72          add(label);
    73
    74          c.gridy++;
    75          label = new JLabel("Filter Logger:");
    76          gridbag.setConstraints(label, c);
    77          add(label);
    78
    79          c.gridy++;
    80          label = new JLabel("Filter NDC:");
    81          gridbag.setConstraints(label, c);
    82          add(label);
    83
    84          c.gridy++;
    85          label = new JLabel("Filter Message:");
    86          gridbag.setConstraints(label, c);
    87          add(label);
    88
    89          // Add the 2nd column of filters
    90          c.weightx = 1;
    91          //c.weighty = 1;
    92          c.gridx = 1;
    93          c.anchor = GridBagConstraints.WEST;
    94
    95          c.gridy = 0;
    96          final Level[] allPriorities = new Level[] {Level.FATAL,
    97                 Level.ERROR,
    98                 Level.WARN,
    99  			   Level.INFO,
    100 			   Level.DEBUG,
    101 			   Level.TRACE };
    102
    103         final JComboBox priorities = new JComboBox(allPriorities);
    104         final Level lowest = allPriorities[allPriorities.length - 1];
    105         priorities.setSelectedItem(lowest);
    106         aModel.setPriorityFilter(lowest);
    107         gridbag.setConstraints(priorities, c);
    108         add(priorities);
    109         priorities.setEditable(false);
    110         priorities.addActionListener(new ActionListener() {
    111                 public void actionPerformed(ActionEvent aEvent) {
    112                     aModel.setPriorityFilter(
    113                         (Priority) priorities.getSelectedItem());
    114                 }
    115             });
    116
    117
    118         c.fill = GridBagConstraints.HORIZONTAL;
    119         c.gridy++;
    120         final JTextField threadField = new JTextField("");
    121         threadField.getDocument().addDocumentListener(new DocumentListener () {
    122                 public void insertUpdate(DocumentEvent aEvent) {
    123                     aModel.setThreadFilter(threadField.getText());
    124                 }
    125                 public void removeUpdate(DocumentEvent aEvente) {
    126                     aModel.setThreadFilter(threadField.getText());
    127                 }
    128                 public void changedUpdate(DocumentEvent aEvent) {
    129                     aModel.setThreadFilter(threadField.getText());
    130                 }
    131             });
    132         gridbag.setConstraints(threadField, c);
    133         add(threadField);
    134
    135         c.gridy++;
    136         final JTextField catField = new JTextField("");
    137         catField.getDocument().addDocumentListener(new DocumentListener () {
    138                 public void insertUpdate(DocumentEvent aEvent) {
    139                     aModel.setCategoryFilter(catField.getText());
    140                 }
    141                 public void removeUpdate(DocumentEvent aEvent) {
    142                     aModel.setCategoryFilter(catField.getText());
    143                 }
    144                 public void changedUpdate(DocumentEvent aEvent) {
    145                     aModel.setCategoryFilter(catField.getText());
    146                 }
    147             });
    148         gridbag.setConstraints(catField, c);
    149         add(catField);
    150
    151         c.gridy++;
    152         final JTextField ndcField = new JTextField("");
    153         ndcField.getDocument().addDocumentListener(new DocumentListener () {
    154                 public void insertUpdate(DocumentEvent aEvent) {
    155                     aModel.setNDCFilter(ndcField.getText());
    156                 }
    157                 public void removeUpdate(DocumentEvent aEvent) {
    158                     aModel.setNDCFilter(ndcField.getText());
    159                 }
    160                 public void changedUpdate(DocumentEvent aEvent) {
    161                     aModel.setNDCFilter(ndcField.getText());
    162                 }
    163             });
    164         gridbag.setConstraints(ndcField, c);
    165         add(ndcField);
    166
    167         c.gridy++;
    168         final JTextField msgField = new JTextField("");
    169         msgField.getDocument().addDocumentListener(new DocumentListener () {
    170                 public void insertUpdate(DocumentEvent aEvent) {
    171                     aModel.setMessageFilter(msgField.getText());
    172                 }
    173                 public void removeUpdate(DocumentEvent aEvent) {
    174                     aModel.setMessageFilter(msgField.getText());
    175                 }
    176                 public void changedUpdate(DocumentEvent aEvent) {
    177                     aModel.setMessageFilter(msgField.getText());
    178                 }
    179             });
    180
    181
    182         gridbag.setConstraints(msgField, c);
    183         add(msgField);
    184
    185         // Add the 3rd column of buttons
    186         c.weightx = 0;
    187         c.fill = GridBagConstraints.HORIZONTAL;
    188         c.anchor = GridBagConstraints.EAST;
    189         c.gridx = 2;
    190
    191         c.gridy = 0;
    192         final JButton exitButton = new JButton("Exit");
    193         exitButton.setMnemonic('x');
    194         exitButton.addActionListener(ExitAction.INSTANCE);
    195         gridbag.setConstraints(exitButton, c);
    196         add(exitButton);
    197
    198         c.gridy++;
    199         final JButton clearButton = new JButton("Clear");
    200         clearButton.setMnemonic('c');
    201         clearButton.addActionListener(new ActionListener() {
    202                 public void actionPerformed(ActionEvent aEvent) {
    203                     aModel.clear();
    204                 }
    205             });
    206         gridbag.setConstraints(clearButton, c);
    207         add(clearButton);
    208
    209         c.gridy++;
    210         final JButton toggleButton = new JButton("Pause");
    211         toggleButton.setMnemonic('p');
    212         toggleButton.addActionListener(new ActionListener() {
    213                 public void actionPerformed(ActionEvent aEvent) {
    214                     aModel.toggle();
    215                     toggleButton.setText(
    216                         aModel.isPaused() ? "Resume" : "Pause");
    217                 }
    218             });
    219         gridbag.setConstraints(toggleButton, c);
    220         add(toggleButton);
    221     }
    222 }