gitzel.util

Class TableGUI

Implemented Interfaces:
ActionListener, AdjustmentListener, FocusListener

public class TableGUI
extends JPanel
implements ActionListener, AdjustmentListener, FocusListener

This class can be used to customize tables. It provides methods for adding and removing entries. Due to the rather limited size and limited distinction between model and view, there is no division into two classes.

Nested Class Summary

static class
TableGUI.ChoiceEntry
A table entry that is a JComboBox
static class
TableGUI.StringEntry
Just a JTextField that implements TableEntry -> The standard access to table content.
static interface
TableGUI.TableEntry
Use this interface for classes that are special table entries

Field Summary

protected JButton
AddButton
The buttons to add a new entry
protected JPanel
ButtonBar
The add/remove button bar
private String[]
CurrNewline
The values in the new-line -> save when scrolling etc.
protected JPanel
DisplayZone
Contains TableZone and ScrollBar
private JLabel[]
FieldNames
The column titles.
private String
KeyNull
The text to be written whenever a key is set to "" which must not happen!
private TableGUI.TableEntry
LastFocus
The element that was the last one to gain focus!
private TableGUI.TableEntry[]
NewLine
Which TableEntry-implementing object is to be used with the given column.
private int
Offset
The current offset value
private String
OldVal
The last value of a field before focus was lost!
private PropertyChangeSupport
PListen
protected JScrollBar
Scroll
The scrollbar to move through the table
private Map
Tabelle
The current table entries
private Set
TableErrLis
This set contains all TableErrorListeners
protected JPanel
TableZone
Here the table is displayed
private String
ValInUse
The text to be written when value for key is already in use!
protected JComponent[][]
VisiParts
The array of visible table components

Constructor Summary

TableGUI(String[] fieldNames, TableGUI.TableEntry[] entryTypes, int rows)
Defines a table that can be used to customize tables in objects visually.

Method Summary

void
actionPerformed(ActionEvent e)
Update the table according to the buttons pressed etc.
void
addPropertyChangeListener(PropertyChangeListener lis)
Adds a propertyChangeListener.
void
addTableErrorListener(TableErrorListener lis)
Add a TableErrorListener to this object - these Listeners will be notified with an TableError Object whenever there is a wrong input.
void
adjustmentValueChanged(AdjustmentEvent e)
This feature handles the scroll bar and the changes according to it
protected void
fireTableError(String what)
void
focusGained(FocusEvent e)
If focus is gained we save the old value of that field in OldVal.
void
focusLost(FocusEvent e)
Update table entries when values are changed.
String[][]
getTable()
Read the data contained in the table.
String[]
getTableEntry(String key)
Returns a table entry.
private void
initDisplay()
Set up the display to make it suitable for the table.
void
removePropertyChangeListener(PropertyChangeListener lis)
void
removeTableEntry(String key)
See description of setTableEntry for details
void
removeTableErrorListener(TableErrorListener lis)
void
setAddButtonText(String txt)
Sets the text on the button that adds a new table entry.
void
setKeyNullText(String text)
Set the error message text to use when key is empty
void
setTable(String[][] data)
Fill the table with information from the underlying data structure.
void
setTableEntry(String key, String[] data)
Changes a table entry to the given values.
void
setValueInUseText(String text)
Set the error message text to use when key value already taken.
protected void
updateLines()
private void
updateTableEntry(JComponent c)
Called by focusLost and its ilk to update table entries due to changes in text fields, scrolling etc.

Field Details

AddButton

protected JButton AddButton
The buttons to add a new entry

ButtonBar

protected JPanel ButtonBar
The add/remove button bar

CurrNewline

private String[] CurrNewline
The values in the new-line -> save when scrolling etc.

DisplayZone

protected JPanel DisplayZone
Contains TableZone and ScrollBar

FieldNames

private JLabel[] FieldNames
The column titles. The first one is the key by which table entries are identified.

KeyNull

private String KeyNull
The text to be written whenever a key is set to "" which must not happen!

LastFocus

private TableGUI.TableEntry LastFocus
The element that was the last one to gain focus!

NewLine

private TableGUI.TableEntry[] NewLine
Which TableEntry-implementing object is to be used with the given column.

Offset

private int Offset
The current offset value

OldVal

private String OldVal
The last value of a field before focus was lost!

PListen

private PropertyChangeSupport PListen

Scroll

protected JScrollBar Scroll
The scrollbar to move through the table

Tabelle

private Map Tabelle
The current table entries

TableErrLis

private Set TableErrLis
This set contains all TableErrorListeners

TableZone

protected JPanel TableZone
Here the table is displayed

ValInUse

private String ValInUse
The text to be written when value for key is already in use!

VisiParts

protected JComponent[][] VisiParts
The array of visible table components

Constructor Details

TableGUI

public TableGUI(String[] fieldNames,
                TableGUI.TableEntry[] entryTypes,
                int rows)
Defines a table that can be used to customize tables in objects visually.
Parameters:
fieldNames - The column titles. The first one is the key by which table entries are identified.
entryTypes - Which TableEntry-implementing object is to be used with the given column.
rows - The number of rows visible without scrolling.

Method Details

actionPerformed

public void actionPerformed(ActionEvent e)
Update the table according to the buttons pressed etc. While this might seem unclean (no proper model/view separation) the small scope might excuse this.

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener lis)
Adds a propertyChangeListener. The only bound property is TableEntry, i.e. a propertyChange occurs every time a table entry is set. If there was an old one with the same key we get the old value, otherwise it is null!

addTableErrorListener

public void addTableErrorListener(TableErrorListener lis)
Add a TableErrorListener to this object - these Listeners will be notified with an TableError Object whenever there is a wrong input.

adjustmentValueChanged

public void adjustmentValueChanged(AdjustmentEvent e)
This feature handles the scroll bar and the changes according to it

fireTableError

protected void fireTableError(String what)

focusGained

public void focusGained(FocusEvent e)
If focus is gained we save the old value of that field in OldVal.

focusLost

public void focusLost(FocusEvent e)
Update table entries when values are changed. Important: No lineUpdate is called here to avoid a reshuffle of the entries.

getTable

public String[][] getTable()
Read the data contained in the table. In a way this can be seen as a short form for getTableEntry().

getTableEntry

public String[] getTableEntry(String key)
Returns a table entry.

initDisplay

private void initDisplay()
Set up the display to make it suitable for the table.

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener lis)

removeTableEntry

public void removeTableEntry(String key)
See description of setTableEntry for details

removeTableErrorListener

public void removeTableErrorListener(TableErrorListener lis)

setAddButtonText

public void setAddButtonText(String txt)
Sets the text on the button that adds a new table entry. Use this for localization

setKeyNullText

public void setKeyNullText(String text)
Set the error message text to use when key is empty

setTable

public void setTable(String[][] data)
Fill the table with information from the underlying data structure. This is a shortcut for using addTableEntry() inputting data in a rawer form. Note that this fires PropertyChangeEvents!
Parameters:
data - first index is the row number, second one the column.

setTableEntry

public void setTableEntry(String key,
                          String[] data)
Changes a table entry to the given values. Note that this is a bound property. The values (old and new) take the form of String arrays where the first entry is the key and the rest are the data part. If a key is removed (see removeTableEntry()) the new value will be null!

setValueInUseText

public void setValueInUseText(String text)
Set the error message text to use when key value already taken.

updateLines

protected void updateLines()

updateTableEntry

private void updateTableEntry(JComponent c)
Called by focusLost and its ilk to update table entries due to changes in text fields, scrolling etc.