Upload
lisa-pitts
View
216
Download
0
Embed Size (px)
Citation preview
W14 - Building World-Class UIs with JFC - Ted Faison
1
Building World-Class User Interfaces with Java Foundation Classes
Ted FaisonFaison Computing [email protected]
W14 - Building World-Class UIs with JFC - Ted Faison
2
Work is still in progress !
Several areas aren’t yet finalized:• Drag and Drop• Multiplexing UIFactories• 2D API, Shape support, Batch Painting• Focus navigation and keyboard
support• Several high-level controls• The text support framework
W14 - Building World-Class UIs with JFC - Ted Faison
3
The AWT isn’t enough !
W14 - Building World-Class UIs with JFC - Ted Faison
4
AWT Limitations
• Few controls• Uses heavyweight objects (peers)• Platform-dependent Look and Feel
– no support for pluggable L&Fs
• no support for 2D or 3D operations– Bezier Curves, Shapes, Transparency– Rotation, Shading, Clipping
W14 - Building World-Class UIs with JFC - Ted Faison
5
JFC Goals
• 100% pure Java• Lightweight• Pluggable L&F• Keyboard support• High-level controls• Simple class hierarchy
W14 - Building World-Class UIs with JFC - Ted Faison
6
Comparing hierarchies
Menu/Button hierarchies under AWT:
Component
Button
Checkbox
Object
MenuComponent
MenuItem
CheckboxMenuItem
W14 - Building World-Class UIs with JFC - Ted Faison
7
Comparing hierarchies
• Menu/Button hierarchies under JFC:JComponent
AbstractButton
JButton
JMenuButton
JCheckboxMenuItem
JMenu
JToggleButton
JCheckbox
JRadioButton
W14 - Building World-Class UIs with JFC - Ted Faison
8
Some JFC high-level controls• JSplitPane, JTabbedPane• JTree• JProgressBar• JSlider• JSpinner• JTextPane• JTable
W14 - Building World-Class UIs with JFC - Ted Faison
9
Other useful JFC controls
• JInternalFrame– A lightweight frame– Z order set using ‘layer’– example
• JToolBar• JToolTip• JLayeredPane
– allows management of children in “layers”
W14 - Building World-Class UIs with JFC - Ted Faison
10
The MVC Architecture
• Model holds view-independent data• View observes model
– provides screen & printer output
• Controller interacts with user– user interacts with controller through views
• in JFC, controller and view are integrated– ComponentUI, used by all JComponents
W14 - Building World-Class UIs with JFC - Ted Faison
11
The ComponentUI Interfacepublic interface ComponentUI
{
void installUI(JComponent c);
void deinstallUI(JComponent c);
void paint(Graphics g, JComponent c);
Dimension getPreferredSize(JComponent c);
Dimension getMinimumSize(JComponent c);
Dimension getMaximumSize(JComponent c);
Insets getInsets(JComponent c);
}
W14 - Building World-Class UIs with JFC - Ted Faison
12
Pluggable Look & Feel
• Provided through a delegation model– components delegate rendering and
event handling to their ComponentUI
• The ComponentUI derived classesBasicComponentUI, ButtonUI, ComboBoxUI, InternalFrameUI, LabelUI, ListBoxUI, MenuBarUI,
MenuItemUI, MenuUI, PopupMenuUI, etc.
• You can extend ComponentUI for your own classes
W14 - Building World-Class UIs with JFC - Ted Faison
13
Creating a JFC component
• Create a model• Create a componentUI
– UIFactory
• Set the UI
W14 - Building World-Class UIs with JFC - Ted Faison
14
Creating a JFC componentpublic class JTree extends JComponent {
protected TreeModel treeModel = new TreeModel();
public JTree () {
setModel(treeModel);
String fallbackUI =
"com.sun.java.swing.BasicTreeUI";
BasicTreeUI treeUI =
(BasicTreeUI) UIManager.getUI("BasicTreeUI",
fallbackUI,
this);
setUI(treeUI);
}
}
W14 - Building World-Class UIs with JFC - Ted Faison
15
Pluggable L&F
• JFC comes with a built-in Win95 L&F– The Basic… ComponentUIs
• Win95 is the normal fallback L&F• You can create your own custom,
or corporate L&F
W14 - Building World-Class UIs with JFC - Ted Faison
16
Changing L&F
• At compile time– using a different ComponentUI
• At runtime– using a different UIFactory
UIManager.setUIFactory(UIFactory f,
Container c);
– Passing a container will reset all its children
W14 - Building World-Class UIs with JFC - Ted Faison
17
Multiplexing UIFactories
• Allow one factory to control the L&F of an entire group of components
• API not fully finalized yet• Use:
setUI((BasicTreeUI) UIManager.getUI(
"BasicTreeUI",
"com.sun.java.swing.basic.BasicTreeUI",
this));
W14 - Building World-Class UIs with JFC - Ted Faison
18
JFC Components delegate painting to their ComponentUIpublic class JComponent {
// …
public void paint(Graphics g) {
if (ui != null)
ui.paint(g, this);
super.paint(g); // paint children if any
}
}
W14 - Building World-Class UIs with JFC - Ted Faison
19
JFC MFC Model objects
• They implement a model interface
interface TreeModel {…}
class JTreeModel implements TreeModel {…}
• They support a listener, which is typically a JComponent, e.g. JTree.– Changes are broadcast to the listener– The listener syncs the UI with the
model
W14 - Building World-Class UIs with JFC - Ted Faison
20
Keyboard Focus Management• Navigating inside groups and
between groups– Arrow and tab keys
• Focus navigation keys are hardwired in native GUIs and AWT
• Navigator can be customized in JFC• Implementation not released yet
• Default focus navigator moves focus in locale’s natural reading order
W14 - Building World-Class UIs with JFC - Ted Faison
21
Accelerator Key Management• Not finalized yet• The Keyboard Action registry
registerKeyboardAction(JAction a, JKeyStroke k,
int when);
• JAction: objects that can receive an actionPerformed(ActionEvent) call.
• JKeyStroke encapsulates charCode and modifier
W14 - Building World-Class UIs with JFC - Ted Faison
22
Accelerator Key Management• The When Condition:
• WHEN_FOCUSED•WHEN_FOCUSED_COMPONENT_PARENT
• WHEN_IN_FOCUSED_WINDOW• ALWAYS
W14 - Building World-Class UIs with JFC - Ted Faison
23
An example
class MyComponent extends JComponent {
MyComponent() {
myComponent.registerKeyboardAction(
new AltKHandler(),
JKeyStroke.getKeyStroke('K',
InputEvent.ALT_MASK),
WHEN_IN_FOCUSED_WINDOW);
}
class AltKHandler extends JAction {
public void actionPerformed(ActionEvent e) {…}
}
W14 - Building World-Class UIs with JFC - Ted Faison
24
Accelerator key management• Default key dispatching policy:
– The focused component– The focused component parent’s tree– Components with same parent as
focused component– Components with condition =
ALWAYS
W14 - Building World-Class UIs with JFC - Ted Faison
25
Advanced text handling
• Support for rich text, e.g. multiple fonts, sizes, colors, etc.
• JTextComponent– designed to handle SGML-type
capabilities
• TextUI – the viewer– An abstract class
W14 - Building World-Class UIs with JFC - Ted Faison
26
The Document interface
• Structures text as arrays of elements• Each element is a stream of
characters• The location of characters
– class Position (not finalized)
• Selecting text– class Range
• encodes pairs of Positions (not finalized)
W14 - Building World-Class UIs with JFC - Ted Faison
27
The Document Interface
• A quick look at the decompiled code
• The interface doesn’t enforce a policy of character management or storage
W14 - Building World-Class UIs with JFC - Ted Faison
28
Document events and listeners• Listeners are document viewers• Events indicate changes to the
underlying text.
W14 - Building World-Class UIs with JFC - Ted Faison
29
Interface DocumentEvent
public interface DocumentEvent {
Range getRange();
Document getDocument();
UndoableEdit getEdit();
boolean isModified(Element p0);
Element[] elementsModified();
Element[] childrenRemoved(Element p0);
Element[] childrenAdded(Element p0);
}
W14 - Building World-Class UIs with JFC - Ted Faison
30
Advanced Text Features• Highlighting
– coloring the background of text
• Generic attributes– custom text attributes, with (key, value)
pairs
• Flow control– Elements can be assigned to “boxes” that
constrain their exact position and flow.
• Embedded pictures– class JIconView
W14 - Building World-Class UIs with JFC - Ted Faison
31
Advanced Text Features
• Integration with Java 2D API– Transparency– Rotation, Scaling, Sheering– Characters along paths– using characters as clipping paths
• custom character fill-in
W14 - Building World-Class UIs with JFC - Ted Faison
32
Other JFC enhancements
• Standardized Component Borders– All JComponents use JBorders
• lines, grooves, titled, raised, recessed borders
• Support for an Undo/Redo framework• Popup menus• Tables• Drag and Drop
W14 - Building World-Class UIs with JFC - Ted Faison
33
Other JFC enhancements
• Optimized drawing (not finalized yet)– Reduces multiple screen updates to a
single operation– Based on the new class DirtyRegionManger– Manager stores a list of the out-of-date
regions– Regions can have arbitrary shape, which
are objects implementing the Shape interface
W14 - Building World-Class UIs with JFC - Ted Faison
34
The DirtyRegionManager
interface DirtyRegionManager {
public void addDirtyRegion(Component c,
Shape dirtyRegion);
public void addDirtyRegion(Component c, int x,
int y, nt w, int h);
public void paintDirtyRegions();
public void markCompletelyDirty();
public void markCompletelyClean();
}
W14 - Building World-Class UIs with JFC - Ted Faison
35
Installing a custom manager• Call the JComponent method:
setManagerForComponent(DirtyRegionManager m,
Component c) {…}
• Getting the current manager:
getManagerForComponent(Component c) {…}
W14 - Building World-Class UIs with JFC - Ted Faison
36
Drag and Drop• Not finalized yet• Diverges from earlier JDK 1.1 model,
due to JFC MVC addition• Platform independent
– works across Java and native applications
• Uses a DragSource and a DropTarget• Data can be moved or copied• MIME DataFlavors
W14 - Building World-Class UIs with JFC - Ted Faison
37
Conclusion
• The JFC is a big deal• JDK 1.2 will contain the JFC• JFC positions Java for high-end
applications• JFC leverages existing JDK work• Migration of existing code to JFC is not too
hard• JFC is designed to use services provided
by the upcoming Java 2D API