Title: Scaling Up With the Java Foundation Classes JFCSwing API Large Data Sets and Complex Beans
1Scaling Up With the Java Foundation Classes
(JFC/Swing) APILarge Data Sets and Complex Beans
- David Zeleznik
- Manager Gantt RD
- ILOG, Inc.
Yunpeng Zhao, PhD Software Engineer ILOG, Inc.
2Origins of This Talk
- Project to create a complex Gantt chart bean
- Large hierarchical data sets
- Time constraints forced to use JTable and JTree
for the JFC/Swing 1.03 release - Plan to migrate to later releases of JFC/Swing
and/or custom table and tree components
3Roadmap
- JFC/Swing Architecture David
- Data Caching Guidelines David
- JTable David
- JTree David
- TreeTable David
- Complex Beans Yunpeng
4Roadmap
- JFC/Swing Architecture
- MVC Overview
- Scrolling With JFC/Swing
- Data Caching Guidelines
- JTable
- JTree
- TreeTable
- Complex Beans
5MVC Overview(the 60 second recap)
- Model-View-Controller architecture decouples 3
aspects of a user-interface - Model the data being displayed
- View how the data is displayed
- Controller how the user interacts with the
displayed data
- JFC/Swing variant of MVC separable model
architecture. - Provides benefits of complete MV separation.
- Easier to use because it bundles view and
controller together.
6Scrolling With JFC/Swing
- Why?
- Standard means to display subsets of large
dataset - Intuitive navigation through dataset
- Scrolling architecture impacts scalability
- Attributes
- General purpose
- No dependency on underlying data
- Scalable
- Architecture
- Child component to be scrolled
- JViewport
- JScrollPane
7JViewport
- Provides a clipped and translated view of another
component - Can be used independently of a JScrollPane
- Scrolling modes in order of increasing
performance - SIMPLE_SCROLL_MODE
- BACKINGSTORE_SCROLL_MODE
- This was default for JTable in earlier JDK's
- BLIT_SCROLL_MODE
- Very buggy version was available in JFC/Swing
1.1.1 - There are still some bugs with this mode,
especially using javaw inside IDEs
8JScrollPane
- Responsible for up to 9 children
- 3 JViewports
- main viewport
- row header
- column header
- 2 scrollbars
- 4 corner components
- ScrollPaneLayout controls the arrangement of the
9 components
9Scrolling a JTable
10Roadmap
- JFC/Swing Architecture
- Data Caching
- JTable
- JTree
- TreeTable
- Complex Beans
11Data Caching
- Large Data Sets
- GIS geographical data
- Project or factory scheduling and planning
- High Retrieval Latencies
- Slow connections to back-end datastore
- Database queries
- Optimizes speed in exchange for memory usage
- Can combine with eager or lazy background
evaluation
12UI Data Caching Guidelines
- User interface should not cache model data
- duplicate views duplicate caching
- difficulty replacing caching strategy to suit
different deployment needs - caching may already be implemented in ORB,
database, server, etc. - User interface should only access data model for
items being painted - UI should pull data from model as needed during
redraw - Redrawing a data item should not require access
to data not being redrawn - Good example JTable paints a cell using only the
data for that cell - Poor example JTree calculates horizontal indent
position to paint a node by querying for nodes
ancestors
13Where to Cache
- Strategy pattern a single class to encapsulate
policy - ability to create deterministic policy
- easier tuning and analysis
- single interface pluggability
- Model interface provides existing interface
- The Proxy or Decorator pattern
- The Cache Management Pattern
- Patterns in JavaTM by Mark Grand, John Wiley
and Sons
14JTable Caching Example
JTable
TableModel Interface
15DataModel Caching - Static
16DataModel Caching - Collaboration
17Roadmap
- JFC/Swing Architecture
- Data Caching
- JTable
- Scalability
- Demos
- JTree
- TreeTable
- Complex Beans
18JTable Is Very Scalable
- Does not cache any data objects from TableModel
- Painting a cell uses only data object for that
cell - Fixed row height in JDKTM 1.2 release, variable
row heights in JDKTM 1.3 release - SelectionModel references cell addresses
- TableModel events reference cell addresses
19Verifying That JTable Does Not Cache
20Demos
- Code available on ILOG website
- www.ilog.com/products/jviews/javaone
- Static data models
- No insert/delete events
21Table Demo 1
- 100 x 100
- No scrolling
- table is fixed size
- null layout
- Data objects from TableModel are not cached
- A new data object instance is created and
returned for every call to TableModel.getValueAt()
- Data objects do not have to implement logical
equality because JTable does no caching or
comparison testing
22Table Demo 1 Notes
- JFC/Swing double-buffering is enabled
- All cells are repainted and data model is
accessed during frame resize - Double-buffering eliminates flashing
- Does not deal with backing store and
intelligent management of invalid regions like
JScrollPane - Data model access during mouse movement is due to
tooltip mechanism - In JTable and JTree, tooltip text is obtained
from the cell renderer - Every mouse move JTable.prepareRenderer()
- Override JTable.getToolTipText(MouseEvent) or
unregister table from ToolTipManager
23Table Demo 2
- JTable is in a JScrollPane
- TableModel is same as demo 1
- New JViewport behavior in JDK 1.3 release
- BitBlt mode is default in JDK 1.3 release
- Backing store mode is default in JDK 1.2 release
24Table Demo 3
- Cached data
- TableModel.getValueAt() is only invoked once per
table cell for lifetime of data model - Optimized for scrolling over previously viewed
areas - at expense of memory usage
- Does not help when scrolling over new areas of
table when time delay obtaining data is large
25Table Demo 3 Notes
- Each cell is cached individually
- keyed by row,col cell address
- Alternative cache by rows
- Each column is a database field
- RecordSets
- java.util.Hashtable is inefficient when using
primitive ints - Keys must be Objects
- Expensive to create Integer or other object keys
from int rows and columns - Custom Hashtable that uses primitive ints or
longs as lookup keys
26Table Demo 3L
- 10,000 x 10,000 table
- JFC/Swing 1.1.1
- large memory usage and time spent initializing
TableColumn objects - Individual headerRenderers, Borders, etc.
- DefaultTableColumnModel continually recalcs total
column width - JDK 1.3 release
- Default TableColumns share a common
headerRenderer in the JTableHeader component - DefaultTableColumnModel lazy calculates total
column width
27JTable Performance Issues
- Startup
- AbstractTableModel.getColumnName()uses String
operator - Instantiation of TableColumns
- Scrolling
- JTable.getCellRect iterates through all columns
to accumulate column x position - Moving first column is faster than moving last
column - New SizeSequence class is used to store variable
row heights in an efficient manner. Use same
approach to store column positions - DefaultTableColumnModel uses a synchronized
Vector to store the TableColumns.
28Table Demo 4
- Cached data model which defers fetching data in a
separate thread - Future Pattern supply a surrogate value
immediately. Defer actual data asynchronously - JTable must be notified on the JFC/Swing event
thread - SwingWorker
29Roadmap
- JFC/Swing Architecture
- Data Caching
- JTable
- JTree
- TreeTable
- Complex Beans
30 JTree
- Is JTree as scalable as JTable?
- Architecture
- TreePaths
- Internal Caches
31JTree Architecture
32TreePaths (1)
lastPathComponent
parentPath
33TreePaths (2)
- A child TreePath prevents all of its ancestors
from being gcd - Used throughout JTree as a unique address for a
tree node - Allows equal() node objects to be used in the
same tree - Some duplicate functionality with the data model
- trace node parentage
- indent level
- higher speed, more memory
34JTree expandedState Cache
- Treepath used as key, value is Boolean
- Cache entry is not removed when node is collapsed
- Child entry is not removed if parent is collapsed
- JTree "remembers" expansion state of child nodes
Boolean.TRUE
Boolean.TRUE
Boolean.TRUE
Boolean.TRUE
35JTree Layout Cache
- BasicTreeUI treePathMapping table
- Position and bounding box info for visible nodes
- VariableHeightLayoutCache
- Is JTree default
- Caches all visible nodes
- FixedHeightLayoutCache
- Caches only expanded visible nodes
- Only enabled if JTree largeModel true and rows
are explicitly set to a fixed height
36JTree Caching Summary
expanded state
variable height
fixed height
previously expanded
37JTable and JTree Summary
- JDK 1.3 release
- JViewport BLIT_SCROLL_MODE
- Fixed row heights
- JTable is inherently scalable
- JTree can be made scalable
- Keep node objects small (db keys)
- Set largeModel true
- Consider
- SoftReferences
- Deleting invisible nodes
38Roadmap
- JFC/Swing Architecture
- Data Caching
- JTable
- JTree
- TreeTable
- Complex Beans
39JavaSoft TreeTable Demo
40TreeTable Design
- Pros
- Subclass of JTable
- JTree is used as a TableCellRenderer
- Can place tree in any column
- Limitations
- Only 1 tree column per table
- Input event handling is mixture of JTree and
JTable - TreeTableModel is complex mixture of TreeModel
and TableModel - TreeTableModel has no row, column events
- Unneeded complexity of synchronizing selections
in JTree and JTable - Tree column is not editable
41TreeTableModel
- TreeModel
- Object getRoot()
- Object getChild(Object parent, int index)
- int getChildCount(Object parent)
- boolean isLeaf(Object node)
- void valueForPathChanged(TreePath path, Object
newValue) - int getIndexOfChild(Object parent, Object child)
- void addTreeModelListener(TreeModelListener l)
- void removeTreeModelListener(TreeModelListener l)
- Table methods
- int getColumnCount()
- String getColumnName(int column)
- Class getColumnClass(int column)
- Object getValueAt(Object node, int column)
- boolean isCellEditable(Object node, int column)
- void setValueAt(Object aValue, Object node, int
column)
42TreeTable Solutions
- Allow users to install multiple JTree
cellRenderers - Keep JTree passive, allow JTable to handle all
input events - Navigation is consistent with remainder of table
- Editing is handled by table
- JTree does not need to track tables selection
- JTable simulates mouse handling of
expand/collapse - Simplify the TreeTableModel
43Roadmap
- JFC/Swing Architecture
- Data Caching
- JTable
- JTree
- TreeTable
- Complex Beans
44Complex Beans Overview
- What are complex beans?
- Internal bean behaviors
- Behavior synchronization
- Demonstration
- Conclusions
45Are JTree and JTable complex beans?
- JTree and JTable are complex
- Are JTree and JTable complex beans?
46JTree and JTable Model/View Architecture (1)
- JTree
- TreeModel representing hierarchical data
- A tree view on the data
- JTable
- TableModel Data can be arranged by rows and by
columns - A table view on the data
47JTree and JTable Model/View Architecture (2)
- JTree and JTable both have
- One data model having a specific structure
- One data view designed to show the data model
Dedicated Data View
Specific Data Model
- JTree and JTable are simple beans, they are not
complex beans
48A Scheduling Bean (1)User Interface
2000 01 02 03 04 05 06 07 08 09 10 11 12
Start End Jan 2000 Dec 2000 Jan 2000 Apr 2000 Mar
2000 Jun 2000 May 2000 Aug 2000 Jul 2000 Sep
2000 Apr 2000 Jul 2000 Aug 2000 Dec 2000 Aug
2000 Nov 2000
Name Project Summary Gather Requirements Talk
to customers Compile customer list Contact
customers Write up requirements Marketing
Specifications First specification
49A Scheduling Bean (2)Complex Data Model
- The scheduling data is complex
- It is arranged by rows and columns
- It has a hierarchy
- The scheduling data can not be completely
presented by a simple bean such as a JTree or a
JTable - The solution is to combine a JTree and a JTable
into a complex bean
50A Complex Bean to Show Scheduling Data
Complex Bean
Scheduling Data Model
51Complex Beans vs. Multiple-Document Interface
(MDI)
- Internal beans are managed by a layout
- Internal beans usually have different
architectures - Internal beans must work harmoniously
- Complex beans expose common behaviors of the
internal beans (Façade pattern)
52A Complex Bean Is
- Complex beans are made by combining simple
internal beans - Internal beans show different views of a complex
data model - Internal beans must have synchronized behaviors
- Internal beans should be easily added or replaced
to satisfy new UI requirements - Complex beans must have a high level API
53Internal Bean Behaviors
- What are behaviors? Ex
- Selecting
- Scrolling (HV)
- Zooming
- Visibility
- Behaviors are more general than properties
- Behaviors consist of
- APIs to query the state
- APIs to change the state
- APIs to add state listeners
- The state can be listened to
- Fire event when the state has changed
54JTree Selection Behavior
- Get if a node is selected
- isPathSelected(TreePath path)
- Select a node
- addSelectionPath(TreePath path)
- Add selection listener
- addTreeSelectionListener(
- TreeSelectionListener tsl)
55JTable Selection Behavior
- Get if a row is selected
- isRowSelected(int row)
- Select a row or rows
- addRowSelectionInterval(int from,
- int to)
- Add selection listener
- getSelectionModel().
- addListSelectionListener(
- ListSelectionListener lsl)
56Selection Behaviors of JTree and JTable
- JTree and JTable selections work separately
- How to synchronize the selection behaviors of
JTree and JTable?
57Selection SynchronizationMediator Pattern
JTable
Mediator
JTree
List Selection Model
ListSelectionListener
TreeSelectionListener
58Selection SynchronizationInfinite Loop(1)
JTable
Mediator
JTree
List Selection Model
ListSelectionListener
TreeSelectionListener
59Selection SynchronizationMediator Pros and Cons
- Easy to implement
- Difficult to expose the selection behavior
through the façade component - Suitable for two components
- Not scalable
60Selection SynchronizationScaling Up
JTable
Mediator
JTree
List Selection Model
61Selection SynchronizationObserver/Adapter
Patterns
JTable
JTree
RowSelectable Adapter
RowSelectable Adapter
RowSelectionController
62Selection SynchronizationRow-Selectable
Interface
public interface RowSelectable public void
isRowSelected(int row) public void
selectRow(int row, boolean select) public void
addRowSelectionListener (RowSelectionListener
rsl)
63Selection SynchronizationRow Selection Listener
public interface RowSelectionListener extends
java.util.EventListener public void
rowSelected(RowSelectionEvent event)
64Selection SynchronizationRow Selection Event
public RowSelectionEvent extends
java.util.EventObject private int
_row private boolean _selected public
RowSelectionEvent(Object source, int row,
boolean selected) super(source) _row
row _selected selected public int
getSelectedRow() return _row) public
boolean isSelected() return _selected
65Selection SynchronizationInfinite Loops
JTable
JTree
RowSelectable Adapter
RowSelectable Adapter
2
3
5
1
4
RowSelectionController
66Exposing Row Selection Behavior (Facade Pattern)
JTable
JTree
Other Internal Components ...
RowSelectable Adapter
RowSelectable Adapter
RowSelectable Adapter
RowSelectable Adapter
RowSelectionController
RowSelectable Interface
67Behavior Synchronization Demonstrations
- Selecting
- Scrolling
- Zooming
- Visibility
68Conclusions
- Concentrate your efforts on modeling your data
- Combine appropriate JFC API-based beans to
present your data model - Use Observer/Adapter patterns to make internal
beans work harmoniously - JFC technology-based beans can be scaled up!
69- David Zeleznik
- zeleznik_at_ilog.com
Yunpeng Zhao zhao_at_ilog.fr