ComponentListener
, ImageObserver
, MenuContainer
, Serializable
, EventListener
, Accessible
, IDocumentContainer
, ITemplateContainer
, ITemplatePanel
, IXHTMLPanel
, org.xhtmlrenderer.extend.FSCanvas
, org.xhtmlrenderer.extend.UserInterface
, org.xhtmlrenderer.simple.extend.FormSubmissionListener
, org.xhtmlrenderer.swing.RepaintListener
public class TemplatePanel extends XHTMLPanel implements ITemplatePanel
Additional features:
Template functionality
To allow customisation of an HTML page without having to do HTML construction in Java
code, templates were introduced. The template language is quite simple, just enough to
enable conditionals and variable substitution. To set variables, either use
setData(java.util.Properties)
to bind to your own Properties
object, or use
data()
.setProperty
to set variables directly;
note that only the former will automatically refresh()
as well.
Variables are used in document templates as explained by TemplateDocument
.
A basic example is shown below.
String page =
"<html>" +
" <head><title>Hi</title></head>" +
" <body>" +
" <h1>Hi</h1>" +
" <p>Please <a href='${url}'>visit our webpage</a>.</p>" +
" <p if='${special}'>Please note that we have a special offer for you.</p>" +
" <p c='${copyright}'/>" +
" </body>" +
"</html>";
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(page.getBytes()));
TemplatePanel panel = new TemplatePanel();
panel.data().setProperty("url", "http://www.example.com/");
if (Calendar.getInstance().get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
panel.data().setProperty("special", "true");
panel.data().setProperty("copyright", "© by <i>Example</i> Inc.");
panel.setDocument(document);
Additionally, HTML form elements are automatically bound to properties. This means
that each form element is bound to a property (see setData(java.util.Properties)
and data
)
with the name specified by the element's name attribute. So the HTML item
<label for="name">Your name:</label> <input type="text" name="name" id="name"/>
would enable the user to enter his/her name. When the name is changed, the corresponding
property name is updated as well, so panel.data().getProperty("name")
would return the text entered by the user.
Note that a form element's initial value is set from the property when available on document loading. If no such property exists, its value is set from the one specified in HTML (like value="foo" or selected="selected").
To handle form submission from the HTML form, use setSubmitAction(javax.swing.Action)
.
The input button element has a special attribute href
that opens the page
it points to using the installed linklistener(s). When one hooks that, it is possible
to generate actions by button presses.
It may be desirable to keep the user from editing certain variables under specific
circumstances. This is done by setting the property propertyname.lock
to true, which enables the readonly attribute on the form element
(this functionality is implemented by TemplateDocument
).
Modifier and Type | Class | Description |
---|---|---|
protected class |
TemplatePanel.TemplateSwingReplacedElementFactory |
Class binds HTML form components to their respective properties.
|
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Container.AccessibleAWTContainer
JComponent.AccessibleJComponent
JPanel.AccessibleJPanel
Modifier and Type | Field | Description |
---|---|---|
protected Properties |
data |
|
protected HashMap<String,Component> |
formelements |
|
protected Action |
submitAction |
|
protected TemplateDocument |
template |
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Constructor | Description |
---|---|
TemplatePanel() |
Construct a new, empty TemplatePanel with empty properties.
|
TemplatePanel(String src) |
Construct a new TemplatePanel with empty Properties, and set the source document.
|
Modifier and Type | Method | Description |
---|---|---|
Properties |
data() |
Return the properties.
|
Component |
getFormComponent(String name) |
Return the Swing/AWT component for a form element by name.
|
Map<String,Component> |
getFormComponents() |
Return the list of Swing/AWT components.
|
boolean |
refresh() |
Refresh the contents so that all parsing is redone.
|
void |
setData(Properties p) |
Set the properties to use for the template and form elements.
|
void |
setDocument(InputStream stream,
String url) |
Renders a Document read from an InputStream using a URL
as a base URL for relative paths.
|
void |
setDocument(TemplateDocument doc) |
Renders an ITemplateDocument instance.
|
void |
setDocument(TemplateDocument doc,
org.xhtmlrenderer.extend.NamespaceHandler nsh) |
|
void |
setDocument(Document doc,
String url) |
Renders a Document using a URL as a base URL for relative
paths.
|
void |
setDocument(Document doc,
String url,
org.xhtmlrenderer.extend.NamespaceHandler nsh) |
Override of
setDocument to process the template. |
void |
setSubmitAction(Action e) |
Set the action to perform on form submission.
|
void |
submit(String url) |
Event handler for form submission.
|
addDocumentListener, addMouseTrackingListener, assignPagePrintPositions, doRender, getDocument, getDocumentTitle, getFixedRectangle, getMouseTrackingListeners, getSharedContext, getURL, isActive, isCenteredPagedView, isFocus, isHover, isInteractive, isOpaque, loadDocument, paintComponent, paintPage, printTree, reloadDocument, reloadDocument, removeDocumentListener, removeMouseTrackingListener, resetMouseTracker, scrollTo, setCenteredPagedView, setDocument, setDocument, setDocumentFromString, setDocumentRelative, setFormSubmissionListener, setInteractive, setLayout, setOpaque, setSize
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
componentHidden, componentMoved, componentResized, componentShown
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, transferFocusDownCycle, validateTree
getDocument, getDocumentTitle, getURL, reloadDocument, reloadDocument, setDocument, setDocument, setDocument, setDocument, setDocument, setDocumentFromString
addMouseTrackingListener, getMouseTrackingListeners, getSharedContext, print, removeMouseTrackingListener, replaceLinkListener, setFormSubmissionListener, setSharedContext
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addNotify, componentHidden, componentMoved, componentResized, componentShown, doDocumentLayout, find, find, fireDocumentLoaded, fireDocumentStarted, fireOnLayoutException, fireOnRenderException, getCellRendererPane, getLayoutContext, getLayoutWidth, getRootBox, getRootLayer, getScreenExtents, init, isDefaultFontFromComponent, isExtentsHaveChanged, isNeedRelayout, isPrintView, newLayoutContext, newRenderingContext, removeNotify, repaintRequested, resetScrollPosition, setDefaultFontFromComponent, setEnclosingScrollPane, setNeedRelayout, setRootBox, validate
print, replaceLinkListener
protected Properties data
protected TemplateDocument template
protected Action submitAction
public TemplatePanel()
public TemplatePanel(String src) throws IOException
IOException
public void setData(Properties p)
Also reparses the document and updates the panel, if a template was set.
setData
in interface ITemplateContainer
p
- New properties to setpublic Properties data()
Use this to retrieve data from form elements. The name of each element has a corresponding property key, and the value of the property is updated when the form element changes.
data
in interface ITemplateContainer
public boolean refresh()
Note that the template is _not_ retrieved again from its source.
This happens automatically in setData(java.util.Properties)
and setDocument(nl.nikhef.xhtmlrenderer.swing.TemplateDocument)
.
refresh
in interface ITemplateContainer
setDocument(nl.nikhef.xhtmlrenderer.swing.TemplateDocument)
was called with a Document
as argument but no base uri was specified).public void setSubmitAction(Action e)
If this is set to null, the standard behaviour is done: posting data
to the url supplied by the form. By default this class's
submit(java.lang.String)
is called.
setSubmitAction
in interface ITemplateContainer
public void submit(String url)
You can override this in your class to handle form submission. The
The default implementation calls the Action defined by
setSubmitAction(javax.swing.Action)
with its command string set to the name
of the originating submit button. In this way you can discriminate
between different forms on a single HTML page.
submit
in interface org.xhtmlrenderer.simple.extend.FormSubmissionListener
submit
in class org.xhtmlrenderer.swing.BasicPanel
url
- URL to submit to, not used in the default implementation.public Component getFormComponent(String name)
ITemplatePanel
Note that multiple radio buttons have the same name, so for these the
name is actually in the form name.value
.
getFormComponent
in interface ITemplatePanel
name
- like myfield
in >input type="text" name="myfield" /<
public Map<String,Component> getFormComponents()
ITemplatePanel
getFormComponents
in interface ITemplatePanel
ITemplatePanel.getFormComponent(java.lang.String)
public void setDocument(TemplateDocument doc)
ITemplateContainer
setDocument
in interface ITemplateContainer
doc
- template to show, this will be processed with the data
supplied by this container.public void setDocument(TemplateDocument doc, org.xhtmlrenderer.extend.NamespaceHandler nsh)
public void setDocument(Document doc, String url, org.xhtmlrenderer.extend.NamespaceHandler nsh)
setDocument
to process the template.setDocument
in class org.xhtmlrenderer.swing.RootPanel
public void setDocument(Document doc, String url)
IDocumentContainer
setDocument
in interface IDocumentContainer
setDocument
in class org.xhtmlrenderer.simple.XHTMLPanel
doc
- The new document valueurl
- The new document valuepublic void setDocument(InputStream stream, String url)
IDocumentContainer
setDocument
in interface IDocumentContainer
setDocument
in class org.xhtmlrenderer.simple.XHTMLPanel
stream
- The stream to read the Document from.url
- The URL used to resolve relative path references.Copyright © 2010-2018 Nikhef / Stichting FOM. All Rights Reserved.