/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* This is the set of things needed by a text component
* to be a reasonably functioning editor for some <em>type</em>
* of text document. This implementation provides a default
* implementation which treats text as styled text and
* provides a minimal set of actions for editing styled text.
*
* @author Timothy Prinzing
*/
/**
* Creates a new EditorKit used for styled documents.
*/
public StyledEditorKit() {
}
/**
* Gets the input attributes for the pane. When
* the caret moves and there is no selection, the
* input attributes are automatically mutated to
* reflect the character attributes of the current
* caret location. The styled editing actions
* use the input attributes to carry out their
* actions.
*
* @return the attribute set
*/
return inputAttributes;
}
/**
* Fetches the element representing the current
* run of character attributes for the caret.
*
* @return the element
*/
return currentRun;
}
// --- EditorKit methods ---------------------------
/**
* Fetches the command list for the editor. This is
* the list of commands supported by the superclass
* augmented by the collection of commands defined
* locally for style operations.
*
* @return the command list
*/
}
/**
* Creates an uninitialized text storage model
* that is appropriate for this type of editor.
*
* @return the model
*/
return new DefaultStyledDocument();
}
/**
* Called when the kit is being installed into
* a JEditorPane.
*
* @param c the JEditorPane
*/
}
}
/**
* Called when the kit is being removed from the
* JEditorPane. This is used to unregister any
* listeners that were attached.
*
* @param c the JEditorPane
*/
// remove references to current document so it can be collected.
currentRun = null;
}
/**
* Fetches a factory that is suitable for producing
* views of any models that are produced by this
* kit. This is implemented to return View implementations
* for the following kinds of elements:
* <ul>
* <li>AbstractDocument.ContentElementName
* <li>AbstractDocument.ParagraphElementName
* <li>AbstractDocument.SectionElementName
* <li>StyleConstants.ComponentElementName
* <li>StyleConstants.IconElementName
* </ul>
*
* @return the factory
*/
return defaultFactory;
}
/**
* Creates a copy of the editor kit.
*
* @return the copy
*/
return o;
}
/**
* Creates the AttributeSet used for the selection.
*/
private void createInputAttributes() {
inputAttributes = new SimpleAttributeSet() {
public AttributeSet getResolveParent() {
return (currentParagraph != null) ?
}
return new SimpleAttributeSet(this);
}
};
}
/**
* Creates a new <code>AttributeTracker</code>.
*/
private void createInputAttributeUpdated() {
inputAttributeUpdater = new AttributeTracker();
}
/**
* This is the set of attributes used to store the
* input attributes.
*/
/**
* This listener will be attached to the caret of
* the text component that the EditorKit gets installed
* into. This should keep the input attributes updated
* for use by the styled actions.
*/
/**
* Tracks caret movement and keeps the input attributes set
* to reflect the current set of attribute definitions at the
* caret position.
* <p>This implements PropertyChangeListener to update the
* input attributes when the Document changes, as if the Document
* changes the attributes will almost certainly change.
*/
/**
* Updates the attributes. <code>dot</code> and <code>mark</code>
* mark give the positions of the selection in <code>c</code>.
*/
// EditorKit might not have installed the StyledDocument yet.
if (!(aDoc instanceof StyledDocument)) {
return ;
}
// record current character attributes.
// If nothing is selected, get the attributes from the character
// before the start of the selection, otherwise get the attributes
// from the character element at the start of the selection.
// Get the attributes from the character at the selection
// if in a different paragrah!
}
else {
}
if (run != currentRun) {
/*
* PENDING(prinz) All attributes that represent a single
* glyph position and can't be inserted into should be
* removed from the input attributes... this requires
* mixing in an interface to indicate that condition.
* When we can add things again this logic needs to be
* improved!!
*/
currentRun = run;
}
}
if ((source instanceof JTextComponent) &&
// New document will have changed selection to 0,0.
}
}
(JTextComponent)e.getSource());
}
}
/**
* <code>set</code>. This does not copy component, icon, or element
* names attributes. Subclasses may wish to refine what is and what
* isn't copied here. But be sure to first remove all the attributes that
* are in <code>set</code>.<p>
* This is called anytime the caret moves over a different location.
*
*/
}
}
// ---- default ViewFactory implementation ---------------------
return new ParagraphView(elem);
return new ComponentView(elem);
}
}
// default to text display
}
}
// --- Action implementations ---------------------------------
new BoldAction(),
new ItalicAction(),
new StyledInsertBreakAction(),
new UnderlineAction()
};
/**
* An action that assumes it's being fired on a JEditorPane
* with a StyledEditorKit (or subclass) installed. This has
* some convenience methods for causing character or paragraph
* level attribute changes. The convenience methods will
* throw an IllegalArgumentException if the assumption of
* a StyledDocument, a JEditorPane, or a StyledEditorKit
* fail to be true.
* <p>
* The component that gets acted upon by the action
* will be the source of the ActionEvent if the source
* can be narrowed to a JEditorPane type. If the source
* can't be narrowed, the most recently focused text
* component is changed. If neither of these are the
* case, the action cannot be performed.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans<sup><font size="-2">TM</font></sup>
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*/
/**
* Creates a new StyledTextAction from a string action name.
*
* @param nm the name of the action
*/
super(nm);
}
/**
* Gets the target editor for an action.
*
* @param e the action event
* @return the editor
*/
if (tcomp instanceof JEditorPane) {
return (JEditorPane) tcomp;
}
return null;
}
/**
* Gets the document associated with an editor pane.
*
* @param e the editor
* @return the document
* @exception IllegalArgumentException for the wrong document type
*/
Document d = e.getDocument();
if (d instanceof StyledDocument) {
return (StyledDocument) d;
}
throw new IllegalArgumentException("document must be StyledDocument");
}
/**
* Gets the editor kit associated with an editor pane.
*
* @param e the editor pane
* @return the kit
* @exception IllegalArgumentException for the wrong document type
*/
EditorKit k = e.getEditorKit();
if (k instanceof StyledEditorKit) {
return (StyledEditorKit) k;
}
throw new IllegalArgumentException("EditorKit must be StyledEditorKit");
}
/**
* Applies the given attributes to character
* content. If there is a selection, the attributes
* are applied to the selection range. If there
* is no selection, the attributes are applied to
* the input attribute set which defines the attributes
* for any new text that gets inserted.
*
* @param editor the editor
* @param attr the attributes
* @param replace if true, then replace the existing attributes first
*/
}
if (replace) {
}
}
/**
* Applies the given attributes to paragraphs. If
* there is a selection, the attributes are applied
* to the paragraphs that intersect the selection.
* if there is no selection, the attributes are applied
* to the paragraph at the current caret position.
*
* @param editor the editor
* @param attr the attributes
* @param replace if true, replace the existing attributes first
*/
}
}
/**
* An action to set the font family in the associated
* JEditorPane. This will use the family specified as
* the command string on the ActionEvent if there is one,
* otherwise the family that was initialized with will be used.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans<sup><font size="-2">TM</font></sup>
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*/
/**
* Creates a new FontFamilyAction.
*
* @param nm the action name
* @param family the font family
*/
super(nm);
}
/**
* Sets the font family.
*
* @param e the event
*/
String s = e.getActionCommand();
if (s != null) {
family = s;
}
}
} else {
}
}
}
}
/**
* An action to set the font size in the associated
* JEditorPane. This will use the size specified as
* the command string on the ActionEvent if there is one,
* otherwise the size that was initialized with will be used.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans<sup><font size="-2">TM</font></sup>
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*/
/**
* Creates a new FontSizeAction.
*
* @param nm the action name
* @param size the font size
*/
super(nm);
}
/**
* Sets the font size.
*
* @param e the action event
*/
String s = e.getActionCommand();
try {
} catch (NumberFormatException nfe) {
}
}
if (size != 0) {
} else {
}
}
}
private int size;
}
/**
* An action to set foreground color. This sets the
* <code>StyleConstants.Foreground</code> attribute for the
* currently selected range of the target JEditorPane.
* This is done by calling
* <code>StyledDocument.setCharacterAttributes</code>
* on the styled document associated with the target
* JEditorPane.
* <p>
* If the target text component is specified as the
* source of the ActionEvent and there is a command string,
* the command string will be interpreted as the foreground
* color. It will be interpreted by called
* <code>Color.decode</code>, and should therefore be
* legal input for that method.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans<sup><font size="-2">TM</font></sup>
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*/
/**
* Creates a new ForegroundAction.
*
* @param nm the action name
* @param fg the foreground color
*/
super(nm);
}
/**
* Sets the foreground color.
*
* @param e the action event
*/
String s = e.getActionCommand();
try {
} catch (NumberFormatException nfe) {
}
}
} else {
}
}
}
}
/**
* An action to set paragraph alignment. This sets the
* <code>StyleConstants.Alignment</code> attribute for the
* currently selected range of the target JEditorPane.
* This is done by calling
* <code>StyledDocument.setParagraphAttributes</code>
* on the styled document associated with the target
* JEditorPane.
* <p>
* If the target text component is specified as the
* source of the ActionEvent and there is a command string,
* the command string will be interpreted as an integer
* that should be one of the legal values for the
* <code>StyleConstants.Alignment</code> attribute.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans<sup><font size="-2">TM</font></sup>
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*/
/**
* Creates a new AlignmentAction.
*
* @param nm the action name
* @param a the alignment >= 0
*/
super(nm);
this.a = a;
}
/**
* Sets the alignment.
*
* @param e the action event
*/
int a = this.a;
String s = e.getActionCommand();
try {
} catch (NumberFormatException nfe) {
}
}
}
}
private int a;
}
/**
* An action to toggle the bold attribute.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans<sup><font size="-2">TM</font></sup>
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*/
/**
* Constructs a new BoldAction.
*/
public BoldAction() {
super("font-bold");
}
/**
* Toggles the bold attribute.
*
* @param e the action event
*/
}
}
}
/**
* An action to toggle the italic attribute.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans<sup><font size="-2">TM</font></sup>
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*/
/**
* Constructs a new ItalicAction.
*/
public ItalicAction() {
super("font-italic");
}
/**
* Toggles the italic attribute.
*
* @param e the action event
*/
}
}
}
/**
* An action to toggle the underline attribute.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans<sup><font size="-2">TM</font></sup>
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*/
/**
* Constructs a new UnderlineAction.
*/
public UnderlineAction() {
super("font-underline");
}
/**
* Toggles the Underline attribute.
*
* @param e the action event
*/
}
}
}
/**
* StyledInsertBreakAction has similar behavior to that of
* <code>DefaultEditorKit.InsertBreakAction</code>. That is when
* its <code>actionPerformed</code> method is invoked, a newline
* is inserted. Beyond that, this will reset the input attributes to
* what they were before the newline was inserted.
*/
super(insertBreakAction);
}
return;
}
}
else {
tempSet = new SimpleAttributeSet();
}
}
else {
// See if we are in a JTextComponent.
return;
}
}
}
}
}
}