[Home] DMelt member access. Java source code: com.jstatcom.component.CheckBoxList
/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Markus Kraetzig
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.component;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.FontUIResource;
import org.apache.log4j.Logger;
import com.jstatcom.model.JSCConstants;
import com.jstatcom.model.JSCNArray;
import com.jstatcom.model.JSCSArray;
import com.jstatcom.model.JSCTypeDef;
import com.jstatcom.model.JSCTypes;
import com.jstatcom.model.Scope;
import com.jstatcom.model.Symbol;
import com.jstatcom.model.SymbolEvent;
import com.jstatcom.model.SymbolListener;
import com.jstatcom.model.SymbolTable;
import com.jstatcom.util.UMatrix;
/**
* This class provides a special JList that renders its items as
* selectable JCheckBox components. Ctrl-A selects all items,
* Ctrl-X deselects all.
*
* It can be used to provide the user with a selection mechanism for a set of
* keys. The objects to select can be set via a JSCSArray object
* or a general Object array. If new data is set that contains
* elements that have been set before, the current selection is preserved. This
* behaviour can be turned off by invoking clear before setting
* new data. It is possible to provide a titled border for this list via
* setBorderTitle.
*
* The contents of this list can be specified by a symbol. In the same way, the
* selection state can be stored to another symbol. However, it is not possible
* to change the selection state by changing the symbol contents.
*
* Whenever there is a change in the selection status by either clicking on the
* list checkboxes or by invoking clearSelectionStatus, a
* "selectionChanged" property change event is fired. This does not happen if
* the selection status changed due to changes in the data model.
*
* @author Markus Kraetzig
*/
public final class CheckBoxList extends JList implements SymbolListener {
private static final Logger log = Logger.getLogger(CheckBoxList.class);
private JSCTypeDef symbolDef = null;
private Symbol symbol = null;
private String symbolNameSelected = null;
private CheckBoxListItem[] currentData = new CheckBoxListItem[0];
private Scope symbolScope = Scope.GLOBAL;
/**
* The class representing items and its selection state.
*/
private static final class CheckBoxListItem {
private final String name;
private final Object value;
private boolean selected = false;
public CheckBoxListItem(String name, Object value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public Object getValue() {
return value;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean newSelected) {
selected = newSelected;
}
}
/**
* Updates list if symbol changed.
*/
public void valueChanged(SymbolEvent evt) {
symbol = evt.getSource();
setJSCSArray(symbol.getJSCSArray());
}
/**
* The class doing the rendering.
*/
private static class CheckBoxListCellRenderer extends JCheckBox implements
ListCellRenderer {
public CheckBoxListCellRenderer() {
super();
}
/**
* Prepares component to painted.
*/
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
String s = ((CheckBoxListItem) value).getName();
setBackground(list.getBackground());
setForeground(list.getForeground());
setText(s);
setSelected(((CheckBoxListItem) value).isSelected());
setEnabled(list.isEnabled());
setFont(list.getFont());
return this;
}
}
private String fieldBorderTitle = "";
private final HashMap