com.lightdev.app.shtm

Class SHTMLEditorPane

public class SHTMLEditorPane extends JEditorPane implements DropTargetListener, DragSourceListener, DragGestureListener

An editor pane for application SimplyHTML.

This is extending JEditorPane by cut and paste and drag and drop for HTML text. JEditorPane inherits cut and paste from JTextComponent where handling for plain text is implemented only. JEditorPane has no additional functionality to add cut and paste for the various content types it supports (such as 'text/html').

In stage 4 support for caret movement inside tables and table manipulation methods are added.

In stage 6 support for list manipulation was added.

Author: Ulrich Hilger Light Development http://www.lightdev.com info@lightdev.com published under the terms and conditions of the GNU General Public License, for details see file gpl.txt in the distribution package of this software

See Also: HTMLText HTMLTextSelection

Field Summary
static intALL_CELLS
range indicator for applying attributes to all cells
static StringdeleteNextCharAction
static StringdeletePrevCharAction
static StringDEFAULT_TABLE_WIDTH
default table width
static StringDEFAULT_VERTICAL_ALIGN
default vertical alignment
static StringendAction
static StringhomeAction
static StringinsertLineBreakAction
static StringmoveDownAction
static StringmoveUpAction
static StringnewListItemAction
static StringshiftEndAction
static StringshiftHomeAction
static intTHIS_CELL
range indicator for applying attributes to the current cell only
static intTHIS_COLUMN
range indicator for applying attributes to cells of the current column only
static intTHIS_ROW
range indicator for applying attributes to cells of the current row only
Constructor Summary
SHTMLEditorPane()
construct a new SHTMLEditorPane
Method Summary
voidappendTableColumn()
append a table column after the last column (if any)
voidappendTableRow()
Appends a row to a table if the caret is inside a table.
voidapplyAttributes(AttributeSet attributeSet, boolean applyToCompleteParagraph)
voidapplyAttributes(AttributeSet attributeSet, boolean applyToCompleteParagraph, boolean replace)
Sets the attributes for a given part of this editor.
voidapplyCellAttributes(AttributeSet a, int range)
apply a set of attributes to a given range of cells of the table the caret is currently in (if any)
voidapplyCharacterTag(String tag)
(Unfinished.)
voidapplyListAttributes(AttributeSet a)
apply a set of attributes to the list the caret is currently in (if any)
voidapplyParagraphTag(String tag, Vector overwritableTags)
Switches the elements in the current selection to the given tag.
voidapplyTableAttributes(AttributeSet a)
apply a set of attributes to the table the caret is currently in (if any)
voiddeleteTableCol()
delete the column of the table the caret is currently in (if any)

width of adjacent column is adjusted, if there is more than one column in the table.

voiddeleteTableRow()
delete the row of the table the caret is currently in (if any)
voiddragDropEnd(DragSourceDropEvent event)
this message goes to DragSourceListener, informing it that the dragging has ended
voiddragEnter(DropTargetDragEvent event)
is invoked when you are dragging over the DropSite
voiddragEnter(DragSourceDragEvent event)
this message goes to DragSourceListener, informing it that the dragging has entered the DropSite
voiddragExit(DropTargetEvent event)
is invoked when you are exit the DropSite without dropping
voiddragExit(DragSourceEvent event)
this message goes to DragSourceListener, informing it that the dragging has exited the DropSite
voiddragGestureRecognized(DragGestureEvent event)
a drag gesture has been initiated
voiddragOver(DropTargetDragEvent event)
is invoked when a drag operation is going on
voiddragOver(DragSourceDragEvent event)
this message goes to DragSourceListener, informing it that the dragging is currently ocurring over the DropSite
voiddrop(DropTargetDropEvent event)
a drop has occurred.
voiddropActionChanged(DropTargetDragEvent event)
is invoked if the user modifies the current drop gesture
voiddropActionChanged(DragSourceDragEvent event)
is invoked when the user changes the dropAction
StringelementToHTML(Element element)
Returns the string HTML representation of the element.
ElementgetCurrentLinkElement()
ElementgetCurrentParagraphElement()
Gets the paragraph element in which the caret is located.
ElementgetCurrentTableCell()
Gets the table cell at the current caret position.
JPopupMenugetPopup()
SHTMLDocumentgetSHTMLDocument()
ElementgetTableCell(int position)
Gets the table cell element at the given position, or null if none.
TransferHandlergetTransferHandler()
voidgoNextCell(Element cell)
voidgoPrevCell(Element cell)
voidinitDnd()
Initialize the drag and drop implementation for this component.
voidinsertAnchor(String anchorName)
apply a new anchor to the currently selected text

If nothing is selected, this method does nothing

voidinsertBreak()
insert a line break (i.e. a break for which paragraph spacing is not applied)
voidinsertNewTable(int colCount)
Insert a new table.
voidinsertTableColumn()
insert a table column before the current column (if any)
voidinsertTableRow(String forcedCellName)
Inserts a row to a table, assuming the caret is currently inside a table.
protected booleanprocessKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
voidremoveAnchor(String anchorName)
remove an anchor with a given name
voidremoveCharacterAttributes()
voidremoveParagraphAttributes()
voidreplaceSelection(HTMLText replacementHTMLText)
Replaces the currently selected content with new content represented by the given HTMLText.
voidsetLink(String linkText, String href, String className)
set a text link at the current selection replacing the selection with a given text.
voidsetLink(String linkText, String href, String className, String linkImage, Dimension size)
Sets a hyperlink at the current selection, replacing the selection with the given text or image.
voidsetPopup(JPopupMenu popup)
voidsetText(String sText)
Convenience method for setting the document text contains hack around JDK bug 4799813 see http://developer.java.sun.com/developer/bugParade/bugs/4799813.html regression in 1.4.x, to be fixed in 1.5 When setting the text to be "& footext", it becomes "&footext" (space disappears) same ocurrs for "</a> &amp;", it becomes "</a>&amp;" (space disappears) with the hack it now does not occur anymore.
voidtoggleList(String listTag, AttributeSet attributeSet, boolean forceOff)
toggle list formatting on or off for the currently selected text portion.
voidtoggleTableHeaderCell()
For each cell within the selection, turns a table data cell into a table header cell or vice versa.
booleantryDefaultKeyStrokeActionWithinCell(int keyCode, int modifiers, ActionEvent event)
Performs the default key stroke action, assuming that the caret is within a table cell and that the action is a cursor move; if the cursor leaves the current table cell, undoes the action.

Field Detail

ALL_CELLS

public static final int ALL_CELLS
range indicator for applying attributes to all cells

deleteNextCharAction

public static final String deleteNextCharAction

deletePrevCharAction

public static final String deletePrevCharAction

DEFAULT_TABLE_WIDTH

public static final String DEFAULT_TABLE_WIDTH
default table width

DEFAULT_VERTICAL_ALIGN

public static final String DEFAULT_VERTICAL_ALIGN
default vertical alignment

endAction

public static final String endAction

homeAction

public static final String homeAction

insertLineBreakAction

public static final String insertLineBreakAction

moveDownAction

public static final String moveDownAction

moveUpAction

public static final String moveUpAction

newListItemAction

public static final String newListItemAction

shiftEndAction

public static final String shiftEndAction

shiftHomeAction

public static final String shiftHomeAction

THIS_CELL

public static final int THIS_CELL
range indicator for applying attributes to the current cell only

THIS_COLUMN

public static final int THIS_COLUMN
range indicator for applying attributes to cells of the current column only

THIS_ROW

public static final int THIS_ROW
range indicator for applying attributes to cells of the current row only

Constructor Detail

SHTMLEditorPane

public SHTMLEditorPane()
construct a new SHTMLEditorPane

Method Detail

appendTableColumn

public void appendTableColumn()
append a table column after the last column (if any)

appendTableRow

public void appendTableRow()
Appends a row to a table if the caret is inside a table.

applyAttributes

public void applyAttributes(AttributeSet attributeSet, boolean applyToCompleteParagraph)

applyAttributes

public void applyAttributes(AttributeSet attributeSet, boolean applyToCompleteParagraph, boolean replace)
Sets the attributes for a given part of this editor. If a range of text is selected, the attributes are applied to the selection. If nothing is selected, the input attributes of the given editor are set thus applying the given attributes to future inputs.

Parameters: attributeSet the set of attributes to apply applyToCompleteParagraph true, if the attributes shall be applied to the whole paragraph, false, if only the selected range of characters shall have them replace true, if existing attribtes are to be replaced, false if not

applyCellAttributes

public void applyCellAttributes(AttributeSet a, int range)
apply a set of attributes to a given range of cells of the table the caret is currently in (if any)

Parameters: a the set of attributes to apply range the range of cells to apply attributes to

applyCharacterTag

public void applyCharacterTag(String tag)
(Unfinished.)

applyListAttributes

public void applyListAttributes(AttributeSet a)
apply a set of attributes to the list the caret is currently in (if any)

Parameters: a the set of attributes to apply

applyParagraphTag

public void applyParagraphTag(String tag, Vector overwritableTags)
Switches the elements in the current selection to the given tag. If allowedTags is non-null, applies the tag only if it is contained in allowedTags. TODO: The new parameter does not work. So the method only works for paragraph tags, like H1, H2 etc. --Dan

Parameters: tag the tag name to switche elements to overwritableTags Tags that may be overwritten by the new tag.

applyTableAttributes

public void applyTableAttributes(AttributeSet a)
apply a set of attributes to the table the caret is currently in (if any)

Parameters: a the set of attributes to apply

deleteTableCol

public void deleteTableCol()
delete the column of the table the caret is currently in (if any)

width of adjacent column is adjusted, if there is more than one column in the table. Width adjustment only works, if width attributes of both the column to remove and its adjacent column have the same unit (pt or %).

If there is only one cell or if the caret is not in a table, this method does nothing

Smart border handling automatically sets the left border of a cell to zero, if the cell on the left of that cell has a right border and both cells have no margin. In that case removing the first column will cause all cells of the new first column to have no left border.

deleteTableRow

public void deleteTableRow()
delete the row of the table the caret is currently in (if any)

dragDropEnd

public void dragDropEnd(DragSourceDropEvent event)
this message goes to DragSourceListener, informing it that the dragging has ended

dragEnter

public void dragEnter(DropTargetDragEvent event)
is invoked when you are dragging over the DropSite

dragEnter

public void dragEnter(DragSourceDragEvent event)
this message goes to DragSourceListener, informing it that the dragging has entered the DropSite

dragExit

public void dragExit(DropTargetEvent event)
is invoked when you are exit the DropSite without dropping

dragExit

public void dragExit(DragSourceEvent event)
this message goes to DragSourceListener, informing it that the dragging has exited the DropSite

dragGestureRecognized

public void dragGestureRecognized(DragGestureEvent event)
a drag gesture has been initiated

dragOver

public void dragOver(DropTargetDragEvent event)
is invoked when a drag operation is going on

dragOver

public void dragOver(DragSourceDragEvent event)
this message goes to DragSourceListener, informing it that the dragging is currently ocurring over the DropSite

drop

public void drop(DropTargetDropEvent event)
a drop has occurred. If the dragged element has a suitable DataFlavor, do the drop.

Parameters: event - the event specifiying the drop operation

See Also: java.awt.datatransfer.DataFlavor

dropActionChanged

public void dropActionChanged(DropTargetDragEvent event)
is invoked if the user modifies the current drop gesture

dropActionChanged

public void dropActionChanged(DragSourceDragEvent event)
is invoked when the user changes the dropAction

elementToHTML

public String elementToHTML(Element element)
Returns the string HTML representation of the element.

getCurrentLinkElement

public Element getCurrentLinkElement()

getCurrentParagraphElement

public Element getCurrentParagraphElement()
Gets the paragraph element in which the caret is located.

getCurrentTableCell

public Element getCurrentTableCell()
Gets the table cell at the current caret position.

Returns: the Element having the current table cell or null if the caret is not inside a table cell

getPopup

public JPopupMenu getPopup()

getSHTMLDocument

public SHTMLDocument getSHTMLDocument()

getTableCell

public Element getTableCell(int position)
Gets the table cell element at the given position, or null if none.

getTransferHandler

public TransferHandler getTransferHandler()

goNextCell

public void goNextCell(Element cell)

goPrevCell

public void goPrevCell(Element cell)

initDnd

public void initDnd()
Initialize the drag and drop implementation for this component.

DropTarget, DragSource and DragGestureRecognizer are instantiated and a MouseListener is established to track the selection in drag operations

this ideally is called in the constructor of a class which would like to implement drag and drop

insertAnchor

public void insertAnchor(String anchorName)
apply a new anchor to the currently selected text

If nothing is selected, this method does nothing

Parameters: anchorName the name of the new anchor

insertBreak

public void insertBreak()
insert a line break (i.e. a break for which paragraph spacing is not applied)

insertNewTable

public void insertNewTable(int colCount)
Insert a new table.

Parameters: colCount the number of columns the new table shall have

insertTableColumn

public void insertTableColumn()
insert a table column before the current column (if any)

insertTableRow

public void insertTableRow(String forcedCellName)
Inserts a row to a table, assuming the caret is currently inside a table.

processKeyBinding

protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)

removeAnchor

public void removeAnchor(String anchorName)
remove an anchor with a given name

Parameters: anchorName the name of the anchor to remove

removeCharacterAttributes

public void removeCharacterAttributes()

removeParagraphAttributes

public void removeParagraphAttributes()

replaceSelection

public void replaceSelection(HTMLText replacementHTMLText)
Replaces the currently selected content with new content represented by the given HTMLText. If there is no selection this amounts to an insert of the given text. If there is no replacement text this amounts to a removal of the current selection. This method overrides replaceSelection in JEditorPane for usage of our own HTMLText object.

Parameters: replacementHTMLText the content to replace the selection with

setLink

public void setLink(String linkText, String href, String className)
set a text link at the current selection replacing the selection with a given text.

If nothing is selected, but the caret is inside a link, this will replace the existing link. If nothing is selected and the caret is not inside a link, this method does nothing.

Parameters: linkText the text that shall appear as link at the current selection href the target this link shall refer to className the style class to be used

setLink

public void setLink(String linkText, String href, String className, String linkImage, Dimension size)
Sets a hyperlink at the current selection, replacing the selection with the given text or image.

Parameters: linkText the text to show as link (or null, if an image shall appear instead) href the link reference className the style name to be used for the link linkImage the file name of the image be used for the link (or null, if a text link is to be set instead) size the size of the image or null

setPopup

public void setPopup(JPopupMenu popup)

setText

public void setText(String sText)
Convenience method for setting the document text contains hack around JDK bug 4799813 see http://developer.java.sun.com/developer/bugParade/bugs/4799813.html regression in 1.4.x, to be fixed in 1.5 When setting the text to be "& footext", it becomes "&footext" (space disappears) same ocurrs for "</a> &amp;", it becomes "</a>&amp;" (space disappears) with the hack it now does not occur anymore.

Parameters: sText the html-text of the document

toggleList

public void toggleList(String listTag, AttributeSet attributeSet, boolean forceOff)
toggle list formatting on or off for the currently selected text portion.

Switches list display on for the given type, if the selection contains parts not formatted as list or parts formatted as list of another type.

Switches list formatting off, if the selection contains only parts formatted as list of the given type.

Parameters: listTag the list tag type to toggle on or off (UL or OL) attributeSet the attributes to use for the list to toggle to forceOff indicator for toggle operation. If true, possibly exisiting list formatting inside the selected parts always is switched off. If false, the method decides, if list formatting for the parts inside the selection needs to be switched on or off.

toggleTableHeaderCell

public void toggleTableHeaderCell()
For each cell within the selection, turns a table data cell into a table header cell or vice versa.

tryDefaultKeyStrokeActionWithinCell

public boolean tryDefaultKeyStrokeActionWithinCell(int keyCode, int modifiers, ActionEvent event)
Performs the default key stroke action, assuming that the caret is within a table cell and that the action is a cursor move; if the cursor leaves the current table cell, undoes the action. Returns true if the cursor stayed within the table cell.