PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/apache-log4j-1.2.17/src/main/java/org/apache/log4j/chainsaw/ControlPanel.java

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