KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > chainsaw > ControlPanel


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.chainsaw;
17
18 import java.awt.GridBagConstraints JavaDoc;
19 import java.awt.GridBagLayout JavaDoc;
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.awt.event.ActionListener JavaDoc;
22 import javax.swing.BorderFactory JavaDoc;
23 import javax.swing.JButton JavaDoc;
24 import javax.swing.JComboBox JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.JTextField JavaDoc;
28 import javax.swing.event.DocumentEvent JavaDoc;
29 import javax.swing.event.DocumentListener JavaDoc;
30 import org.apache.log4j.Logger;
31 import org.apache.log4j.Priority;
32
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 JavaDoc {
39     /** use the log messages **/
40     private static final Logger LOG =
41                                   Logger.getLogger(ControlPanel.class);
42
43     /**
44      * Creates a new <code>ControlPanel</code> instance.
45      *
46      * @param aModel the model to control
47      */

48     ControlPanel(final MyTableModel aModel) {
49         setBorder(BorderFactory.createTitledBorder("Controls: "));
50         final GridBagLayout JavaDoc gridbag = new GridBagLayout JavaDoc();
51         final GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
52         setLayout(gridbag);
53
54         // Pad everything
55
c.ipadx = 5;
56         c.ipady = 5;
57
58         // Add the 1st column of labels
59
c.gridx = 0;
60         c.anchor = GridBagConstraints.EAST;
61
62         c.gridy = 0;
63         JLabel JavaDoc label = new JLabel JavaDoc("Filter Level:");
64         gridbag.setConstraints(label, c);
65         add(label);
66
67         c.gridy++;
68         label = new JLabel JavaDoc("Filter Thread:");
69         gridbag.setConstraints(label, c);
70         add(label);
71
72         c.gridy++;
73         label = new JLabel JavaDoc("Filter Logger:");
74         gridbag.setConstraints(label, c);
75         add(label);
76
77         c.gridy++;
78         label = new JLabel JavaDoc("Filter NDC:");
79         gridbag.setConstraints(label, c);
80         add(label);
81
82         c.gridy++;
83         label = new JLabel JavaDoc("Filter Message:");
84         gridbag.setConstraints(label, c);
85         add(label);
86
87         // Add the 2nd column of filters
88
c.weightx = 1;
89         //c.weighty = 1;
90
c.gridx = 1;
91         c.anchor = GridBagConstraints.WEST;
92
93         c.gridy = 0;
94         final Priority[] allPriorities = Priority.getAllPossiblePriorities();
95         final JComboBox JavaDoc priorities = new JComboBox JavaDoc(allPriorities);
96         final Priority lowest = allPriorities[allPriorities.length - 1];
97         priorities.setSelectedItem(lowest);
98         aModel.setPriorityFilter(lowest);
99         gridbag.setConstraints(priorities, c);
100         add(priorities);
101         priorities.setEditable(false);
102         priorities.addActionListener(new ActionListener JavaDoc() {
103                 public void actionPerformed(ActionEvent JavaDoc aEvent) {
104                     aModel.setPriorityFilter(
105                         (Priority) priorities.getSelectedItem());
106                 }
107             });
108
109
110         c.fill = GridBagConstraints.HORIZONTAL;
111         c.gridy++;
112         final JTextField JavaDoc threadField = new JTextField JavaDoc("");
113         threadField.getDocument().addDocumentListener(new DocumentListener JavaDoc () {
114                 public void insertUpdate(DocumentEvent JavaDoc aEvent) {
115                     aModel.setThreadFilter(threadField.getText());
116                 }
117                 public void removeUpdate(DocumentEvent JavaDoc aEvente) {
118                     aModel.setThreadFilter(threadField.getText());
119                 }
120                 public void changedUpdate(DocumentEvent JavaDoc aEvent) {
121                     aModel.setThreadFilter(threadField.getText());
122                 }
123             });
124         gridbag.setConstraints(threadField, c);
125         add(threadField);
126
127         c.gridy++;
128         final JTextField JavaDoc catField = new JTextField JavaDoc("");
129         catField.getDocument().addDocumentListener(new DocumentListener JavaDoc () {
130                 public void insertUpdate(DocumentEvent JavaDoc aEvent) {
131                     aModel.setCategoryFilter(catField.getText());
132                 }
133                 public void removeUpdate(DocumentEvent JavaDoc aEvent) {
134                     aModel.setCategoryFilter(catField.getText());
135                 }
136                 public void changedUpdate(DocumentEvent JavaDoc aEvent) {
137                     aModel.setCategoryFilter(catField.getText());
138                 }
139             });
140         gridbag.setConstraints(catField, c);
141         add(catField);
142
143         c.gridy++;
144         final JTextField JavaDoc ndcField = new JTextField JavaDoc("");
145         ndcField.getDocument().addDocumentListener(new DocumentListener JavaDoc () {
146                 public void insertUpdate(DocumentEvent JavaDoc aEvent) {
147                     aModel.setNDCFilter(ndcField.getText());
148                 }
149                 public void removeUpdate(DocumentEvent JavaDoc aEvent) {
150                     aModel.setNDCFilter(ndcField.getText());
151                 }
152                 public void changedUpdate(DocumentEvent JavaDoc aEvent) {
153                     aModel.setNDCFilter(ndcField.getText());
154                 }
155             });
156         gridbag.setConstraints(ndcField, c);
157         add(ndcField);
158
159         c.gridy++;
160         final JTextField JavaDoc msgField = new JTextField JavaDoc("");
161         msgField.getDocument().addDocumentListener(new DocumentListener JavaDoc () {
162                 public void insertUpdate(DocumentEvent JavaDoc aEvent) {
163                     aModel.setMessageFilter(msgField.getText());
164                 }
165                 public void removeUpdate(DocumentEvent JavaDoc aEvent) {
166                     aModel.setMessageFilter(msgField.getText());
167                 }
168                 public void changedUpdate(DocumentEvent JavaDoc aEvent) {
169                     aModel.setMessageFilter(msgField.getText());
170                 }
171             });
172
173
174         gridbag.setConstraints(msgField, c);
175         add(msgField);
176
177         // Add the 3rd column of buttons
178
c.weightx = 0;
179         c.fill = GridBagConstraints.HORIZONTAL;
180         c.anchor = GridBagConstraints.EAST;
181         c.gridx = 2;
182
183         c.gridy = 0;
184         final JButton JavaDoc exitButton = new JButton JavaDoc("Exit");
185         exitButton.setMnemonic('x');
186         exitButton.addActionListener(ExitAction.INSTANCE);
187         gridbag.setConstraints(exitButton, c);
188         add(exitButton);
189
190         c.gridy++;
191         final JButton JavaDoc clearButton = new JButton JavaDoc("Clear");
192         clearButton.setMnemonic('c');
193         clearButton.addActionListener(new ActionListener JavaDoc() {
194                 public void actionPerformed(ActionEvent JavaDoc aEvent) {
195                     aModel.clear();
196                 }
197             });
198         gridbag.setConstraints(clearButton, c);
199         add(clearButton);
200
201         c.gridy++;
202         final JButton JavaDoc toggleButton = new JButton JavaDoc("Pause");
203         toggleButton.setMnemonic('p');
204         toggleButton.addActionListener(new ActionListener JavaDoc() {
205                 public void actionPerformed(ActionEvent JavaDoc aEvent) {
206                     aModel.toggle();
207                     toggleButton.setText(
208                         aModel.isPaused() ? "Resume" : "Pause");
209                 }
210             });
211         gridbag.setConstraints(toggleButton, c);
212         add(toggleButton);
213     }
214 }
215
Popular Tags