/** *######################################################################### * * A component of the Gatherer application, part of the Greenstone digital * library suite from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * *

* * Author: John Thompson, Greenstone Digital Library, University of Waikato * *

* * Copyright (C) 1999 New Zealand Digital Library Project * *

* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * *

* * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * *

* * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ package org.greenstone.gatherer.checklist; /************************************************************************************** * Title: Gatherer * Description: The Gatherer: a tool for gathering and enriching a digital collection. * Company: The University of Waikato * Written: 14/08/02 * Revised: **************************************************************************************/ import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; import org.greenstone.gatherer.checklist.Entry; /** This class provides a visual component that has the form of a list, as provided by JList but uses JCheckBox for data selection. Thus several elements can be 'ticked' in the list, and this selection returned using the method getSelected().
Parts of this code modified from Trevor Harmon's posting on www.dejanews.com. * @author John Thompson * @version 2.3 */ // #################################################################################### // Optimization Saving // #################################################################################### // Vector -> ArrayList + Processor // #################################################################################### public class CheckList extends JList { /** The border used when a list row is not in focus. */ protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); private boolean show_selected_row = true; /** Constructor. */ public CheckList(boolean show_selected_row) { super(); this.show_selected_row = show_selected_row; setCellRenderer(new CellRenderer()); setModel(new DefaultListModel()); setSelectionMode(ListSelectionModel.SINGLE_SELECTION); addMouseListener(new CheckListListener()); } /** Constructor. * @param list_data An ArrayList of entries for this checklist. * @see org.greenstone.gatherer.checklist.CheckList.Entry */ public CheckList(ArrayList list_data, boolean show_selected_row) { super(); this.show_selected_row = show_selected_row; // Create a new model. DefaultListModel model = new DefaultListModel(); for(int i = 0; i < list_data.size(); i++) { Entry entry = null; Object temp = list_data.get(i); if(temp instanceof Entry) { entry = (Entry) temp; } else { entry = new Entry(list_data.get(i)); } temp = null; String name = entry.toString(); int index = 0; boolean found = false; while(index < model.size() && !found) { Object sibling = model.getElementAt(index); if(name.compareTo(sibling.toString()) <= 0) { model.add(index, entry); found = true; } index++; } if(!found) { model.addElement(entry); } } setModel(model); setCellRenderer(new CellRenderer()); addMouseListener(new CheckListListener()); setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } /** Constructor. */ public CheckList(ListModel data_model, boolean show_selected_row) { super(data_model); this.show_selected_row = show_selected_row; setCellRenderer(new CellRenderer()); addMouseListener(new CheckListListener()); setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } public void addEntry(Entry entry) { DefaultListModel model = (DefaultListModel) getModel(); String name = entry.toString(); int index = 0; boolean found = false; while(index < model.size() && !found) { Object sibling = model.getElementAt(index); if(name.compareTo(sibling.toString()) <= 0) { model.add(index, entry); found = true; } index++; sibling = null; } if(!found) { model.addElement(entry); } name = null; model = null; } public Entry get(int index) { DefaultListModel model = (DefaultListModel) getModel(); return (Entry) model.get(index); } /** Retrieve the currently ticked entries from this list. * @return An ArrayList containing only those entries from the initial list that are checked. * @see org.greenstone.gatherer.checklist.CheckList.Entry */ public ArrayList getSelected() { ArrayList result = new ArrayList(); DefaultListModel model = (DefaultListModel) getModel(); int size = model.size(); for(int i = 0; i < size; i++) { Entry entry = (Entry) model.get(i); if(entry.isSelected()) { result.add(entry.getObject()); } } return result; } public boolean isSelected(int index) { DefaultListModel model = (DefaultListModel) getModel(); Entry entry = (Entry) model.get(index); return entry.isSelected(); } public boolean isSelectionEmpty() { DefaultListModel model = (DefaultListModel) getModel(); int size = model.size(); for(int i = 0; i < size; i++) { Entry entry = (Entry) model.get(i); if(entry.isSelected()) { entry = null; model = null; return false; } entry = null; } model = null; return true; } public int getEntryCount() { return getModel().getSize(); } public void setListData(Object[] list_data) { // Create a new model. DefaultListModel model = new DefaultListModel(); for(int i = 0; i < list_data.length; i++) { Entry entry = null; Object temp = list_data[i]; if(temp instanceof Entry) { entry = (Entry) temp; } else { entry = new Entry(temp); } temp = null; String name = entry.toString(); int index = 0; boolean found = false; while(index < model.size() && !found) { Object sibling = model.getElementAt(index); if(name.compareTo(sibling.toString()) <= 0) { model.add(index, entry); found = true; } index++; } if(!found) { model.addElement(entry); } } setModel(model); } /** Checks the entries in the list whose name appear in the given array. * @param names The name of entries to be checked as a String[]. * @see org.greenstone.gatherer.checklist.CheckList.Entry */ public void setSelected(String names[]) { DefaultListModel model = (DefaultListModel) getModel(); int size = model.size(); for(int i = 0; i < size; i++) { Entry entry = (Entry) model.get(i); for(int j = 0; names != null && j < names.length; j++) { if(entry.toString().equals(names[j])) { entry.setSelected(true); } } } } private void selectionChanged(int index) { fireSelectionValueChanged(index, index, false); } /** A custom list cell renderer for producing rows which contain clickable check boxes. */ protected class CellRenderer implements ListCellRenderer { /** Return a component that has been configured to display the specified value. That component's paint method is then called to "render" the cell. If it is necessary to compute the dimensions of a list because the list cells do not have a fixed size, this method is called to generate a component on which getPreferredSize can be invoked. * @param list The JList we're painting. * @param value The value returned by list.getModel().getElementAt(index), as an Object. * @param index The cells index as an int. * @param is_selected true if the specified cell was selected, false otherwise. * @param cell_has_focus true if and only if the specified cell has the focus. * @return A Component whose paint() method will render the specified value. */ public Component getListCellRendererComponent(JList list, Object value, int index, boolean is_selected, boolean cell_has_focus) { JCheckBox checkbox = (JCheckBox) value; if(show_selected_row) { checkbox.setBackground(is_selected ? list.getSelectionBackground() : list.getBackground()); checkbox.setForeground(is_selected ? list.getSelectionForeground() : list.getForeground()); checkbox.setBorderPainted(true); } else { checkbox.setBackground(list.getBackground()); checkbox.setForeground(list.getForeground()); checkbox.setBorderPainted(false); } checkbox.setEnabled(list.isEnabled()); checkbox.setFont(list.getFont()); checkbox.setFocusPainted(false); checkbox.setBorder((is_selected) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder); return checkbox; } } /** Listens for clicks apon the checks within the list, and updates as necessary. */ private class CheckListListener extends MouseAdapter { //private Entry previous_checkbox = null; /** Called whenever the mouse is clicked over our list. We find the nearest checkbox and change its state. * @param e A MouseEvent containing everything you ever wanted to know about the mouse event but were afraid to ask. */ public void mousePressed(MouseEvent e) { JList list = (JList) e.getSource(); int index = list.locationToIndex(e.getPoint()); Entry checkbox = (Entry)list.getModel().getElementAt(index); if(!checkbox.isFixed()) { checkbox.setSelected(!checkbox.isSelected()); } checkbox.grabFocus(); // Fire a selection changed event selectionChanged(index); } } }