KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.tasklist.core.filter;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.GridBagConstraints JavaDoc;
23
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.beans.PropertyChangeEvent JavaDoc;
28 import javax.swing.*;
29
30 import org.openide.util.NbBundle;
31 import javax.swing.text.JTextComponent JavaDoc;
32
33 /**
34  * GUI component for a single condition within the Filter Panel
35  *
36  * @author Tor Norbye
37  * @author tl
38  */

39 public final class ConditionPanel extends JPanel implements ActionListener JavaDoc, PropertyChangeListener JavaDoc {
40
41     private static final long serialVersionUID = 1;
42
43     private Filter filter;
44     private JComponent valueField;
45    
46     /**
47      * Creates new form ConditionPanel
48      *
49      * @param filter filter to be used
50      * @param cond condition to be shown or null
51      */

52     public ConditionPanel(Filter filter, AppliedFilterCondition cond) {
53         this.filter = filter;
54
55         initComponents();
56         initA11y();
57         
58         valueField = emptyPanel;
59         
60         // fill ComboBox with properties
61
SuggestionProperty [] props = filter.getProperties();
62     int selectedIndex = -1;
63
64         DefaultComboBoxModel m = new DefaultComboBoxModel();
65         for (int i = 0; i < props.length; i++) {
66             AppliedFilterCondition[] c = filter.createConditions(props[i]);
67             if (c.length != 0) {
68                 m.addElement(props[i]);
69                 if (cond == null)
70                     cond = c[0];
71             }
72         if (props[i].getName().equals(cond.getProperty().getName())) selectedIndex = i;
73         }
74         propertyCombo.setModel(m);
75         propertyCombo.addActionListener(this);
76         propertyCombo.setSelectedIndex(selectedIndex);
77     propertyCombo.setRenderer(new ConditionPanel.PropertyCellRenderer());
78
79
80     // construct the rest of items for the current condition
81
AppliedFilterCondition[] conditions = filter.createConditions(cond.getProperty());
82         m = new DefaultComboBoxModel();
83         for (int i = 0; i < conditions.length; i++) {
84             if (conditions[i].sameType(cond)) {
85                 m.addElement(cond);
86             } else {
87                 m.addElement(conditions[i]);
88             }
89         }
90         relationCombo.setModel(m);
91         relationCombo.addActionListener(this);
92         relationCombo.setSelectedItem(cond);
93         relationCombo.setRenderer(new ConditionPanel.ConditionCellRenderer());
94         
95         setValueComponent(cond.getCondition().createConstantComponent());
96     }
97     
98     public void actionPerformed(ActionEvent JavaDoc evt) {
99         if (evt.getSource() == propertyCombo) {
100             AppliedFilterCondition[] c =
101           filter.createConditions((SuggestionProperty)propertyCombo.getSelectedItem());
102
103             relationCombo.setModel(new DefaultComboBoxModel(c));
104             if (c.length != 0) {
105                 relationCombo.setSelectedIndex(0);
106                 AppliedFilterCondition cond = (AppliedFilterCondition)relationCombo.getSelectedItem();
107                 setValueComponent(cond.getCondition().createConstantComponent());
108             } else {
109                 setValueComponent(null);
110             }
111         } else if (evt.getSource() == relationCombo) {
112             AppliedFilterCondition cond =
113                 (AppliedFilterCondition) relationCombo.getSelectedItem();
114             setValueComponent(cond.getCondition().createConstantComponent());
115         }
116     }
117     
118     /**
119      * Return a filter condition corresponding to what is in the GUI
120      *
121      * @return choosed filter condition
122      */

123     public AppliedFilterCondition getCondition() {
124         AppliedFilterCondition cond = (AppliedFilterCondition)relationCombo.getSelectedItem();
125         if (valueField != emptyPanel)
126             cond.getCondition().getConstantFrom(valueField);
127         return cond;
128     }
129
130     private void initA11y() {
131         propertyCombo.getAccessibleContext().setAccessibleName(
132                 NbBundle.getMessage(ConditionPanel.class,
133                                     "ACSN_Property")); // NOI18N
134
propertyCombo.getAccessibleContext().setAccessibleDescription(
135                 NbBundle.getMessage(ConditionPanel.class,
136                                     "ACSD_Property")); // NOI18N
137
relationCombo.getAccessibleContext().setAccessibleName(
138                 NbBundle.getMessage(ConditionPanel.class,
139                                     "ACSN_Relation")); // NOI18N
140
relationCombo.getAccessibleContext().setAccessibleDescription(
141                 NbBundle.getMessage(ConditionPanel.class,
142                                     "ACSD_Relation")); // NOI18N
143
}
144
145     /**
146      * Changes component with a constant.
147      *
148      * @param cmp new component or null if it should be removed
149      */

150     private void setValueComponent(JComponent cmp) {
151         if (valueField == emptyPanel && cmp == null || valueField == cmp)
152             return;
153         
154         remove(valueField);
155         
156         if (cmp == null)
157             valueField = emptyPanel;
158         else {
159             if ((cmp instanceof JTextComponent JavaDoc) && (valueField instanceof JTextComponent JavaDoc)
160                 && ((JTextComponent JavaDoc)cmp).getText().length()==0)
161                     ((JTextComponent JavaDoc)cmp).setText(((JTextComponent JavaDoc)valueField).getText());
162             valueField = cmp;
163         
164             // supply with default values if no specific one defined
165
if (cmp.getAccessibleContext().getAccessibleName() == null) {
166                 cmp.getAccessibleContext().setAccessibleName(
167                         NbBundle.getMessage(ConditionPanel.class,
168                                             "ACSN_Value")); // NOI18N
169
}
170
171             if (cmp.getAccessibleContext().getAccessibleDescription() == null) {
172                 cmp.getAccessibleContext().setAccessibleDescription(
173                         NbBundle.getMessage(ConditionPanel.class,
174                                             "ACSD_Value")); // NOI18N
175
}
176
177             cmp.addPropertyChangeListener(FilterCondition.PROP_VALUE_VALID, this);
178         }
179         
180         GridBagConstraints JavaDoc gridBagConstraints =
181             new java.awt.GridBagConstraints JavaDoc();
182         gridBagConstraints.gridx = 2;
183         gridBagConstraints.gridy = 0;
184         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
185         gridBagConstraints.weightx = 1.0;
186         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 11);
187         add(valueField, gridBagConstraints);
188         revalidate();
189         repaint();
190     }
191     
192     public void focusPropertyCombo() {
193         propertyCombo.requestFocusInWindow();
194     }
195     
196     /**
197      * Cell renderer for FilterCondition
198      */

199     private static class ConditionCellRenderer extends DefaultListCellRenderer {
200
201         private static final long serialVersionUID = 1;
202         
203         public ConditionCellRenderer () {}
204
205         public Component JavaDoc getListCellRendererComponent(JList list,
206             Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
207             super.getListCellRendererComponent(
208                 list, value, index, isSelected, cellHasFocus);
209             AppliedFilterCondition c = (AppliedFilterCondition) value;
210             if (c != null) setText(c.getCondition().getName());
211             return this;
212         }
213     }
214
215
216     /**
217      * Cell renderer for Properties
218      */

219     private static class PropertyCellRenderer extends DefaultListCellRenderer {
220
221         private static final long serialVersionUID = 1;
222
223         public PropertyCellRenderer () {}
224         
225         public Component JavaDoc getListCellRendererComponent(JList list,
226             Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
227             super.getListCellRendererComponent(
228                 list, value, index, isSelected, cellHasFocus);
229             SuggestionProperty prop = (SuggestionProperty)value;
230             if (prop != null) setText(prop.getName());
231             return this;
232         }
233     }
234
235
236     // forward PROP_VALUE_VALID event
237
public void propertyChange(PropertyChangeEvent JavaDoc evt) {
238         putClientProperty(FilterCondition.PROP_VALUE_VALID, Boolean.valueOf(isValueValid()));
239     }
240
241     public boolean isValueValid() {
242         AppliedFilterCondition cond = (AppliedFilterCondition) relationCombo.getSelectedItem();
243         return valueField != emptyPanel && cond.getCondition().isValueValid(valueField);
244     }
245
246     /**
247      * This method is called from within the constructor to
248      * initialize the form.
249      * WARNING: Do NOT modify this code. The content of this method is
250      * always regenerated by the Form Editor.
251      */

252     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
253
private void initComponents() {
254         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
255
256         setLayout(new java.awt.GridBagLayout JavaDoc());
257
258         setFocusable(false);
259         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
260         gridBagConstraints.gridx = 0;
261         gridBagConstraints.gridy = 0;
262         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
263         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
264         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 0);
265         add(propertyCombo, gridBagConstraints);
266
267         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
268         gridBagConstraints.gridx = 1;
269         gridBagConstraints.gridy = 0;
270         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
271         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
272         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 12);
273         add(relationCombo, gridBagConstraints);
274
275         emptyPanel.setOpaque(false);
276         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
277         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
278         gridBagConstraints.weightx = 1.0;
279         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
280         add(emptyPanel, gridBagConstraints);
281
282     }// </editor-fold>//GEN-END:initComponents
283

284     // Variables declaration - do not modify//GEN-BEGIN:variables
285
final javax.swing.JPanel JavaDoc emptyPanel = new javax.swing.JPanel JavaDoc();
286     final javax.swing.JComboBox JavaDoc propertyCombo = new javax.swing.JComboBox JavaDoc();
287     final javax.swing.JComboBox JavaDoc relationCombo = new javax.swing.JComboBox JavaDoc();
288     // End of variables declaration//GEN-END:variables
289
}
290
Popular Tags