KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ic2d > gui > dialog > FilteredClassesPanel


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ic2d.gui.dialog;
32
33 import org.objectweb.proactive.ic2d.util.ActiveObjectFilter;
34
35 public class FilteredClassesPanel extends javax.swing.JPanel JavaDoc {
36
37   private javax.swing.JList JavaDoc list;
38   private javax.swing.DefaultListModel JavaDoc listModel;
39
40   public FilteredClassesPanel(ActiveObjectFilter filter) {
41     super(new java.awt.GridLayout JavaDoc(1,1));
42     listModel = new javax.swing.DefaultListModel JavaDoc();
43     java.util.Iterator JavaDoc iterator = filter.iterator();
44     if (! iterator.hasNext()) {
45       listModel.addElement(new javax.swing.JCheckBox JavaDoc("There is no filtered classes."));
46     } else {
47       listModel.addElement(new javax.swing.JCheckBox JavaDoc("Uncheck the classes you don't want to filter anymore"));
48     }
49     while (iterator.hasNext()) {
50       javax.swing.JCheckBox JavaDoc cb = new javax.swing.JCheckBox JavaDoc((String JavaDoc) iterator.next());
51       cb.setSelected(true);
52       listModel.addElement(cb);
53     }
54
55     //Create the list and put it in a scroll pane
56
list = new javax.swing.JList JavaDoc(listModel);
57     list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
58     list.setSelectedIndex(0);
59     list.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
60       public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc e) {
61         if (e.getValueIsAdjusting())
62           return;
63         if (list.getSelectedIndex() != -1) {
64           javax.swing.JCheckBox JavaDoc cb = (javax.swing.JCheckBox JavaDoc) listModel.get(list.getSelectedIndex());
65           cb.setSelected(!cb.isSelected());
66         }
67       }
68     });
69     list.setCellRenderer(new MyCellRenderer());
70     javax.swing.JScrollPane JavaDoc listScrollPane = new javax.swing.JScrollPane JavaDoc(list);
71     add(listScrollPane);
72   }
73
74   public boolean updateFilter(ActiveObjectFilter filter) {
75     boolean updated = false;
76     for (int i = 1; i < listModel.size(); i++) {
77       javax.swing.JCheckBox JavaDoc cb = (javax.swing.JCheckBox JavaDoc) listModel.get(i);
78       if (!cb.isSelected()) {
79         updated = filter.removeClass(cb.getText()) || updated;
80       }
81     }
82     return updated;
83   }
84
85   private class MyCellRenderer implements javax.swing.ListCellRenderer JavaDoc {
86     public java.awt.Component JavaDoc getListCellRendererComponent(javax.swing.JList JavaDoc list, Object JavaDoc value, // value to display
87
int index, // cell index
88
boolean isSelected, // is the cell selected
89
boolean cellHasFocus) // the list and the cell have the focus
90
{
91       javax.swing.JCheckBox JavaDoc cb = (javax.swing.JCheckBox JavaDoc) value;
92       if (isSelected) {
93         cb.setBackground(list.getSelectionBackground());
94         cb.setForeground(list.getSelectionForeground());
95       } else {
96         cb.setBackground(list.getBackground());
97         cb.setForeground(list.getForeground());
98       }
99       cb.setEnabled(list.isEnabled());
100       cb.setFont(list.getFont());
101       return cb;
102     }
103   }
104 }
105
Popular Tags