KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > filter > FilterPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.core.filter;
21
22 import java.awt.*;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import javax.swing.*;
31
32 import org.netbeans.modules.tasklist.core.TaskListView;
33 import org.netbeans.modules.tasklist.core.TaskNode;
34 import org.openide.awt.Mnemonics;
35 import org.openide.explorer.view.BeanTreeView;
36 import org.openide.nodes.FilterNode;
37 import org.openide.nodes.Node;
38 import org.openide.util.NbBundle;
39
40 /**
41  * Panel which is used to create a filter for the tasklist
42  * The GUI is based on the one in Mozilla's mail tool.
43  * Please read comment at the beginning of initA11y before editing
44  * this file using the form builder.
45  *
46  * @author Tor Norbye
47  */

48 public final class FilterPanel extends JPanel implements java.awt.event.ActionListener JavaDoc, java.beans.PropertyChangeListener JavaDoc, java.awt.event.FocusListener JavaDoc {
49
50     private static final long serialVersionUID = 1;
51
52     private FilteredTopComponent view;
53     private Filter filter;
54
55     // head of subpanels chain
56
private FilterSubpanel subpanel;
57
58     /**
59      * Creates new form FilterPanel.
60      *
61      * @param view view with the given filter
62      * @param filter filter to be edited. Can be null and in that case
63      * all fields are disabled.
64      */

65     public FilterPanel(FilteredTopComponent view, Filter filter) {
66         this.view = view;
67         this.filter = filter;
68
69         initComponents();
70         initA11y();
71
72         // it's not generated by form editor
73
JPanel topAlign = new JPanel();
74         topAlign.setLayout(new BorderLayout());
75         topAlign.add(conditionsPanel, BorderLayout.NORTH);
76         conditionsScrollPane.setViewportView(topAlign);
77         // compute 80x10 chars space in scroll pane
78
FontMetrics fm = getFontMetrics(getFont());
79         int width = fm.charWidth('n') * 80; // NOI18N
80
int height = fm.getHeight() * 10;
81         conditionsScrollPane.setPreferredSize(new java.awt.Dimension JavaDoc(width, height));
82
83         Color background = (Color)UIManager.get("Table.background"); //NOI18N
84
conditionsPanel.setBackground(background);
85         topAlign.setBackground(background);
86
87         moreButton.addActionListener(this);
88         fewerButton.addActionListener(this);
89         matchAllRadio.addActionListener(this);
90         matchAnyRadio.addActionListener(this);
91
92         showFilter(filter);
93         updateSensitivity();
94     }
95
96     /**
97      * Links to next filter customizer (or null).
98      */

99     public void initSubpanel(FilterSubpanel next) {
100         assert subpanel == null;
101         subpanel = next;
102     }
103
104     /**
105      * Show the given filter in the GUI
106      */

107     private void showFilter(Filter filter) {
108         if (filter != null) {
109             if (filter.matchAll()) {
110                 matchAllRadio.setSelected(true);
111             } else {
112                 matchAnyRadio.setSelected(true);
113             }
114             conditionsPanel.removeAll();
115             List JavaDoc conditions = filter.getConditions();
116             if (conditions.size() > 0) {
117                 Iterator JavaDoc it = conditions.iterator();
118                 while (it.hasNext()) {
119                     AppliedFilterCondition cond = (AppliedFilterCondition) it.next();
120                     addCondition(false, cond);
121                 }
122             } else {
123                 addCondition(false, null);
124             }
125             filterName.setText(filter.getName());
126         } else {
127             this.setEnabled(false);
128             namePanel.setEnabled(false);
129             filterName.setEnabled(false);
130             filterName.setText(NbBundle.getMessage(FilterPanel.class, "no-filter"));
131             matchAnyRadio.setEnabled(false);
132             matchAllRadio.setEnabled(false);
133             conditionsPanel.setEnabled(false);
134             moreButton.setEnabled(false);
135             fewerButton.setEnabled(false);
136         }
137     }
138
139     /**
140      * This method is called from within the constructor to
141      * initialize the form.
142      * WARNING: Do NOT modify this code. The content of this method is
143      * always regenerated by the Form Editor.
144      */

145     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
146
private void initComponents() {
147         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
148
149         setLayout(new java.awt.GridBagLayout JavaDoc());
150
151         addFocusListener(this);
152
153         namePanel.setLayout(new java.awt.GridBagLayout JavaDoc());
154
155         filterNameLabel.setLabelFor(filterName);
156         /*
157         org.openide.awt.Mnemonics.setLocalizedText(filterNameLabel, java.util.ResourceBundle.getBundle("org/netbeans/modules/tasklist/core/filter/Bundle").getString("LBL_Filter_Name"));
158         */

159         filterNameLabel.setToolTipText(java.util.ResourceBundle.getBundle("org/netbeans/modules/tasklist/core/filter/Bundle").getString("LBL_Filter_Name_Hint"));
160         filterNameLabel.setAlignmentY(0.0F);
161         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
162         gridBagConstraints.gridx = 0;
163         gridBagConstraints.gridy = 0;
164         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 5);
165         namePanel.add(filterNameLabel, gridBagConstraints);
166         filterNameLabel.getAccessibleContext().setAccessibleName("<Name>");
167
168         filterName.setText("jTextField1");
169         filterName.setToolTipText(java.util.ResourceBundle.getBundle("org/netbeans/modules/tasklist/core/filter/Bundle").getString("LBL_Filter_Name_Hint"));
170         filterName.setAlignmentX(0.0F);
171         filterName.setAlignmentY(0.0F);
172         filterName.setPreferredSize(new java.awt.Dimension JavaDoc(32767, 20));
173         filterName.addFocusListener(this);
174
175         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
176         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
177         gridBagConstraints.ipadx = 3;
178         gridBagConstraints.ipady = 5;
179         gridBagConstraints.weightx = 1.0;
180         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 5, 0);
181         namePanel.add(filterName, gridBagConstraints);
182
183         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
184         gridBagConstraints.gridx = 0;
185         gridBagConstraints.gridy = 0;
186         gridBagConstraints.gridwidth = 3;
187         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
188         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
189         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 10, 0);
190         add(namePanel, gridBagConstraints);
191
192         matchGroup.add(matchAllRadio);
193         /*
194         org.openide.awt.Mnemonics.setLocalizedText(matchAllRadio, NbBundle.getMessage(FilterPanel.class, "MatchAll")); // NOI18N();
195         */

196         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
197         gridBagConstraints.gridx = 0;
198         gridBagConstraints.gridy = 1;
199         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
200         add(matchAllRadio, gridBagConstraints);
201
202         matchGroup.add(matchAnyRadio);
203         matchAnyRadio.setSelected(true);
204         /*
205         org.openide.awt.Mnemonics.setLocalizedText(matchAnyRadio, NbBundle.getMessage(FilterPanel.class, "MatchAny")); // NOI18N();
206         */

207         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
208         gridBagConstraints.gridx = 1;
209         gridBagConstraints.gridy = 1;
210         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
211         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
212         add(matchAnyRadio, gridBagConstraints);
213
214         conditionsScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
215         conditionsScrollPane.setAlignmentX(0.0F);
216         conditionsScrollPane.setAlignmentY(0.0F);
217         conditionsScrollPane.setPreferredSize(new java.awt.Dimension JavaDoc(32767, 32767));
218         conditionsPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
219
220         conditionsScrollPane.setViewportView(conditionsPanel);
221
222         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
223         gridBagConstraints.gridx = 0;
224         gridBagConstraints.gridy = 2;
225         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
226         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
227         gridBagConstraints.weightx = 1.0;
228         gridBagConstraints.weighty = 1.0;
229         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 12, 0);
230         add(conditionsScrollPane, gridBagConstraints);
231         conditionsScrollPane.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(FilterPanel.class).getString("LBL_Conditions"));
232         conditionsScrollPane.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(FilterPanel.class).getString("HNT_Conditions"));
233
234         buttonsPanel.setLayout(new java.awt.FlowLayout JavaDoc(java.awt.FlowLayout.LEFT));
235
236         /*
237         org.openide.awt.Mnemonics.setLocalizedText(moreButton, NbBundle.getMessage(FilterPanel.class, "More")); // NOI18N();
238         */

239         buttonsPanel.add(moreButton);
240
241         /*
242         org.openide.awt.Mnemonics.setLocalizedText(fewerButton, NbBundle.getMessage(FilterPanel.class, "Fewer")); // NOI18N();
243         */

244         buttonsPanel.add(fewerButton);
245
246         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
247         gridBagConstraints.gridx = 0;
248         gridBagConstraints.gridy = 3;
249         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
250         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
251         add(buttonsPanel, gridBagConstraints);
252
253     }
254
255     // Code for dispatching events from components to event handlers.
256

257     public void focusGained(java.awt.event.FocusEvent JavaDoc evt) {
258         if (evt.getSource() == FilterPanel.this) {
259             FilterPanel.this.formFocusGained(evt);
260         }
261         else if (evt.getSource() == filterName) {
262             FilterPanel.this.filterNameFocusGained(evt);
263         }
264     }
265
266     public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
267     }// </editor-fold>//GEN-END:initComponents
268

269     private void filterNameFocusGained(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_filterNameFocusGained
270
filterName.selectAll();
271     }//GEN-LAST:event_filterNameFocusGained
272

273     private void formFocusGained(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_formFocusGained
274
// TODO add your handling code here:
275
filterName.requestFocus();
276     }//GEN-LAST:event_formFocusGained
277

278     private void initA11y() {
279         /*
280           I couldn't figure out how to use Mnemonics.setLocalizedText
281           to set labels and checkboxes with a mnemonic using the
282           form builder, so the closest I got was to use "/*" and "* /
283           as code pre-init/post-init blocks, such that I don't actually
284           execute the bundle lookup code - and then call it explicitly
285           below. (I wanted to keep the text on the components so that
286           I can see them when visually editing the GUI.
287         */

288
289         Mnemonics.setLocalizedText(matchAllRadio, NbBundle.getMessage(
290                 FilterPanel.class, "MatchAll")); // NOI18N
291
matchAllRadio.setToolTipText (NbBundle.getMessage(
292                 FilterPanel.class, "HINT_MatchAll")); // NOI18N
293
Mnemonics.setLocalizedText(matchAnyRadio, NbBundle.getMessage(
294                 FilterPanel.class, "MatchAny")); // NOI18N
295
matchAnyRadio.setToolTipText (NbBundle.getMessage(
296                 FilterPanel.class, "HINT_MatchAny")); // NOI18N
297
Mnemonics.setLocalizedText(moreButton, NbBundle.getMessage(
298                 FilterPanel.class, "More")); // NOI18N
299
moreButton.setToolTipText (NbBundle.getMessage(
300                 FilterPanel.class, "HINT_More")); // NOI18N
301
Mnemonics.setLocalizedText(fewerButton, NbBundle.getMessage(
302                 FilterPanel.class, "Fewer")); // NOI18N
303
fewerButton.setToolTipText (NbBundle.getMessage(
304                 FilterPanel.class, "HINT_Fewer")); // NOI18N
305

306         Mnemonics.setLocalizedText(filterNameLabel, NbBundle.getMessage(
307                 FilterPanel.class, "LBL_Filter_Name"));
308         filterNameLabel.setToolTipText(NbBundle.getMessage(
309                 FilterPanel.class, "LBL_Filter_Name_Hint"));
310         
311         // accessible
312
this.getAccessibleContext().setAccessibleDescription(
313                 NbBundle.getMessage(FilterPanel.class,
314                                     "ACSD_Filter")); // NOI18N
315
conditionsPanel.getAccessibleContext().setAccessibleName(
316                 NbBundle.getMessage(FilterPanel.class,
317                                     "ACSN_Conditions")); // NOI18N
318
conditionsPanel.getAccessibleContext().setAccessibleDescription(
319                 NbBundle.getMessage(FilterPanel.class,
320                                     "ACSD_Conditions")); // NOI18N
321
}
322
323     /**
324      * Adds a condition to the filter.
325      *
326      * @param revalidate true = invalidate() will be called
327      * @param condition a condition or null
328      */

329     private ConditionPanel addCondition(boolean revalidate, AppliedFilterCondition condition) {
330         GridBagConstraints gridBagConstraints = new GridBagConstraints();
331         gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
332         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
333         gridBagConstraints.anchor = GridBagConstraints.NORTH;
334         gridBagConstraints.insets = new Insets(6, 0, 0, 0);
335         gridBagConstraints.weightx = 1.0;
336         ConditionPanel panel = new ConditionPanel(filter, condition);
337         panel.addPropertyChangeListener(FilterCondition.PROP_VALUE_VALID, this);
338         panel.setBackground((Color)UIManager.get("Table.background")); //NOI18N
339
conditionsPanel.add(panel, gridBagConstraints);
340         if (revalidate) {
341             invalidate();
342             getParent().validate();
343             repaint();
344         }
345         putClientProperty(FilterCondition.PROP_VALUE_VALID, Boolean.valueOf(isValueValid()));
346         return panel;
347     }
348
349     /** @return aggregated PROP_VALUE_VALID value */
350     public final boolean isValueValid() {
351         Component[] cps = conditionsPanel.getComponents();
352         for (int i = 0; i < cps.length; i++) {
353             ConditionPanel condition = (ConditionPanel) cps[i];
354             if (condition != null) {
355                 if (condition.isValueValid() == false) {
356                     return false;
357                 }
358             }
359         }
360         return true;
361     }
362
363     // forward aggregated PROP_VALUE_VALID value
364
public void propertyChange(PropertyChangeEvent JavaDoc evt) {
365         putClientProperty(FilterCondition.PROP_VALUE_VALID, Boolean.valueOf(isValueValid()));
366     }
367
368     /**
369      * Invoked when an action occurs.
370      */

371     public void actionPerformed(ActionEvent JavaDoc evt) {
372         Object JavaDoc source = evt.getSource();
373         if (source == moreButton) {
374             ConditionPanel comp = addCondition(true, null);
375             JScrollBar vsb = conditionsScrollPane.getVerticalScrollBar();
376             vsb.setValue(vsb.getMaximum());
377             comp.focusPropertyCombo();
378         } else if (source == fewerButton) {
379             conditionsPanel.remove(conditionsPanel.getComponentCount() - 1);
380             invalidate();
381             getParent().validate();
382             repaint();
383         }
384         updateSensitivity();
385     }
386
387     /**
388      * Updates enabled/disabled state of the "fewer" button
389      */

390     private void updateSensitivity() {
391         int n = conditionsPanel.getComponentCount();
392         fewerButton.setEnabled(n > 0);
393     }
394
395     /**
396      * Return a filter corresponding to what is in the GUI
397      *
398      * @return filter or null
399      */

400     public Filter getFilter() {
401       if (filter != null) {
402         // Get conditions
403
Component[] cps = conditionsPanel.getComponents();
404         ArrayList JavaDoc conditions = new ArrayList JavaDoc(cps.length);
405         for (int i = 0; i < cps.length; i++) {
406             AppliedFilterCondition condition = ((ConditionPanel)cps[i]).getCondition();
407             if (condition != null) {
408                 conditions.add(condition);
409             }
410         }
411         filter.setName(filterName.getText());
412         filter.setConditions(conditions);
413         filter.setMatchAll(matchAllRadio.isSelected());
414         filter.setFlattened(true);
415         if (subpanel != null) {
416             subpanel.updateFilter(filter);
417         }
418       }
419       return filter;
420
421     }
422     
423     public JTextField getFilterNameField() {
424         return filterName;
425     }
426
427     // Variables declaration - do not modify//GEN-BEGIN:variables
428
final javax.swing.JPanel JavaDoc buttonsPanel = new javax.swing.JPanel JavaDoc();
429     final javax.swing.JPanel JavaDoc conditionsPanel = new javax.swing.JPanel JavaDoc();
430     final javax.swing.JScrollPane JavaDoc conditionsScrollPane = new javax.swing.JScrollPane JavaDoc();
431     final javax.swing.JButton JavaDoc fewerButton = new javax.swing.JButton JavaDoc();
432     final javax.swing.JTextField JavaDoc filterName = new javax.swing.JTextField JavaDoc();
433     final javax.swing.JLabel JavaDoc filterNameLabel = new javax.swing.JLabel JavaDoc();
434     final javax.swing.JRadioButton JavaDoc matchAllRadio = new javax.swing.JRadioButton JavaDoc();
435     final javax.swing.JRadioButton JavaDoc matchAnyRadio = new javax.swing.JRadioButton JavaDoc();
436     final javax.swing.ButtonGroup JavaDoc matchGroup = new javax.swing.ButtonGroup JavaDoc();
437     final javax.swing.JButton JavaDoc moreButton = new javax.swing.JButton JavaDoc();
438     final javax.swing.JPanel JavaDoc namePanel = new javax.swing.JPanel JavaDoc();
439     final javax.swing.ButtonGroup JavaDoc subtaskGroup = new javax.swing.ButtonGroup JavaDoc();
440     // End of variables declaration//GEN-END:variables
441

442 }
443
Popular Tags