📄 imagedcomboboxcelleditorfactory.java
字号:
/**
* Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package jmt.gui.common.editors;
import jmt.gui.common.definitions.StationDefinition;
import jmt.gui.common.definitions.ClassDefinition;
import jmt.gui.common.resources.ImageLoader;
import jmt.gui.common.CommonConstants;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableCellEditor;
import java.util.HashMap;
import java.util.Vector;
import java.awt.*;
/**
* <p>Title: Imaged ComboBox Table CellViewer/CellEditor Factory</p>
* <p>Description: A component that creates Comboboxes with station or class names
* and images to be used as both a viewer and an editor in a table.
* Uses internal caching to speed up visualization.</p>
*
* @author Bertoli Marco
* Date: 11-may-2006
* Time: 17.03.54
*/
public class ImagedComboBoxCellEditorFactory {
/** Station Definition data structure */
protected StationDefinition sd;
/** Class Definition data structure */
protected ClassDefinition cd;
/** Tells if modify classes or stations */
protected boolean isStation;
/** Tells if modify strings with images */
protected boolean isString;
/** Cache for components */
protected HashMap cache = new HashMap();
/** Renderer instance */
protected TableCellRenderer renderer;
/** Editor instance */
protected ImagedComboEditor editor;
/** Null element renderer */
protected LabelRenderer nullRenderer;
/** Allows null as a valid component */
protected boolean allowsNull = false;
/**
* Creates a new ImagedComboBoxCellEditorFactory used to display
* station comboboxes
* @param sd reference to station definition datastructure
*/
public ImagedComboBoxCellEditorFactory(StationDefinition sd) {
setData(sd);
}
/**
* Creates a new ImagedComboBoxCellEditorFactory used to display
* station comboboxes
* @param cd reference to class definition datastructure
*/
public ImagedComboBoxCellEditorFactory(ClassDefinition cd) {
setData(cd);
}
/**
* Creates a new ImagedComboBoxCellEditorFactory used to display
* strings with images. Every image must be called <code>[String] + "Combo"</code>
* where <code>[String]</code> is the content of the string.
*/
public ImagedComboBoxCellEditorFactory() {
isString = true;
isStation = false;
}
/**
* Changes stored reference to station data structure
* @param sd reference to station definition datastructure
*/
public void setData(StationDefinition sd) {
isStation = true;
isString = false;
this.sd = sd;
clearCache();
}
/**
* Changes stored reference to station data structure
* @param cd reference to class definition datastructure
*/
public void setData(ClassDefinition cd) {
isStation = false;
isString = false;
this.cd = cd;
clearCache();
}
/**
* Clears inner component cache. Must be called each time name or
* type of a class/station changes.
*/
public void clearCache() {
cache.clear();
nullRenderer = null;
}
/**
* Returns an instance of editor, given search key for elements to be shown
* @param data array with search's key for elements to be shown
*/
public TableCellEditor getEditor(Object[] data) {
if (editor == null)
editor = new ImagedComboEditor();
LabelRenderer[] rend;
if (allowsNull) {
rend = new LabelRenderer[data.length+1];
rend[0] = getDrawComponent(null);
for (int i=1; i<rend.length; i++)
rend[i] = getDrawComponent(data[i - 1]);
}
else {
rend = new LabelRenderer[data.length];
for (int i=0; i<data.length; i++)
rend[i] = getDrawComponent(data[i]);
}
editor.setData(rend);
return editor;
}
/**
* Returns an instance of editor, given search key for elements to be shown
* @param data vector with search's key for elements to be shown
*/
public TableCellEditor getEditor(Vector data) {
if (editor == null)
editor = new ImagedComboEditor();
LabelRenderer[] rend;
if (allowsNull) {
rend = new LabelRenderer[data.size()+1];
rend[0] = getDrawComponent(null);
for (int i=1; i<rend.length; i++)
rend[i] = getDrawComponent(data.get(i - 1));
}
else {
rend = new LabelRenderer[data.size()];
for (int i=0; i<data.size(); i++)
rend[i] = getDrawComponent(data.get(i));
}
editor.setData(rend);
return editor;
}
/**
* Tells if comboBoxes should allow null values selection
* @param value true if selection is allowed, false otherwise
*/
public void setAllowsNull(boolean value) {
allowsNull = value;
}
/**
* Returns component to draw a station or a class in a comboBox.
* Provides caching functionalities.
* @param key search's key for component to be shown
* @return created component
*/
protected LabelRenderer getDrawComponent(Object key) {
LabelRenderer label;
if (key == null) {
if (nullRenderer == null)
nullRenderer = new LabelRenderer(null);
return nullRenderer;
}
if (cache.containsKey(key))
label = (LabelRenderer) cache.get(key);
else {
label = new LabelRenderer(key);
cache.put(key, label);
}
return label;
}
/**
* Class used to display a component in a combobox
*/
protected class LabelRenderer extends JLabel {
protected Object key;
/**
* Construst a new labelrenderer with given object
* @param key search's key for class or station
*/
public LabelRenderer(Object key) {
super();
setHorizontalTextPosition(JLabel.RIGHT);
setOpaque(true);
setBorder(BorderFactory.createEmptyBorder(0,2,0,2));
this.key = key;
if (isStation) {
// This is a station
if (sd.getStationName(key) != null && !sd.getStationName(key).equals("")) {
setText(sd.getStationName(key));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -