问题
My problem is as follows:
I want to enable the user of my little HTML editor to switch between
different background colors for the text being entered. I first tried
to use CSS styles for that purpose. The different styles define
different background colors and through a JComboBox
the user could
switch between these styles. Upon selection of a style in the
respective position inside the HTMLDocument
a new HTML element of type
<span class="style">
would be entered.
Unfortunately I could not manage to get this work. The span elements
were simply not created (see my question regarding this problem).
In between I took a look at the class StyledEditorKit.ForegroundAction
to learn how this functions. Upon execution it simply modifies the
input attributes of the StyledEditorKit
in use setting a new
foreground color. Text that is entered afterwards is displayed with
the new foreground color. And when writing the HTML code into a file,
the text is automagically enclosed in <font color=".."> ... </font>
HTML elements. And all that even works on selected text which might
run over multiple paragraphs. In this case obviously the affected
text inside all affected paragraphs is enclosed into <font ...>
HTML tags.
I want to accomplish the same functionality for setting the background color on arbitrary chunks of text. But surprisingly this does not seem to be so easy:-(
I didn't find a predefined action class for that purpose similar to the
StyledEditorKit.foregroundAction
in the Java 7 JDK. Creating
such a class does not seem to be complex; it's almost the same as the
ForegroundAction
with the actionPerformed
method changed to set the
background instead of the foreground attribute.
But how to create valid HTML code that sets a specific background
color for parts of the contained text?
Until now I don't know which part of the HTMLEditorKit
performs the
creation of all the <font>
elements for text in the HTMLDocument
that
has the foreground attribute set. I think from the existing code
creating the <font>
elements it should not be too hard to derive an
implementation that creates <span style="background-color:...">
elements instead of <font>
elements for setting the background color
for arbitrary regions of text. Or is all this already available and I
only didn't notice? Any help would be appreciated!
In between I made a significant step forward and thanks to a piece of code found here
I managed to create valid <span>
elements. In the span elements I use the class
attribute to assign a predefined style.
Here's my code:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleContext;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
public class SimpleEditor extends JFrame {
private static final long serialVersionUID = 1L;
private final JTextPane textPane;
private final HTMLEditorKit edtKit;
private final HTMLDocument doc;
private final StyleSheet predefStyles;
public static void main(String[] args) throws BadLocationException, IOException {
final SimpleEditor editor = new SimpleEditor();
editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editor.setVisible(true);
}
public SimpleEditor() throws BadLocationException, IOException {
super("Very Simple HTML Editor");
textPane = new JTextPane();
edtKit = new HTMLEditorKit();
textPane.setEditorKit(edtKit);
predefStyles = new StyleSheet();
predefStyles.addRule(".MyStyle1 { color:#cc0000; background-color:silver }\n" +
".MyStyle2 { color:#0000cc; background-color:aqua }");
doc = new HTMLDocument(predefStyles);
textPane.setDocument(doc);
final Container content = getContentPane();
content.add(textPane, BorderLayout.CENTER);
content.add(createToolBar(), BorderLayout.NORTH);
setJMenuBar(createMenuBar());
setSize(500, 240);
}
private JToolBar createToolBar() {
final Vector<String> styleNames = new Vector<String>();
final Enumeration<?> names = predefStyles.getStyleNames();
while (names.hasMoreElements()) {
styleNames.add((String) names.nextElement());
}
final DefaultComboBoxModel<String> stylesModel =
new DefaultComboBoxModel<String>(styleNames);
final JComboBox<String> cbStyleSel = new JComboBox<String>(stylesModel);
final JToolBar bar = new JToolBar();
Action dumpAction = null;
for (final Action act : edtKit.getActions()) {
if (act.getValue(Action.NAME).equals("dump-model")) {
dumpAction = act;
break;
}
}
bar.add(dumpAction);
cbStyleSel.setEditable(false);
cbStyleSel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
e.getSource();
@SuppressWarnings("unchecked")
final JComboBox<CondStyle> cboStyleSel = (JComboBox<CondStyle>) e.getSource();
final String selItem = (String) cboStyleSel.getSelectedItem();
final MutableAttributeSet divAttributes = new SimpleAttributeSet();
if (selItem.equals("default")) {
// This does not work!
final Style defStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
divAttributes.addAttribute(HTML.Tag.CONTENT, defStyle);
textPane.setCharacterAttributes(divAttributes, true);
} else {
divAttributes.addAttribute(HTML.Attribute.CLASS, selItem.substring(1));
final MutableAttributeSet tagAttributes = new SimpleAttributeSet();
tagAttributes.addAttribute(HTML.Tag.SPAN, divAttributes);
textPane.setCharacterAttributes(tagAttributes, false);
}
textPane.requestFocusInWindow();
}
});
bar.add(cbStyleSel);
return bar;
}
/**
* Extracts the style attributes except the style's name
* @param aStyle The style to be processed
* @return The visual attributes extracted from the style
*/
AttributeSet extractStyleAttribs(Style aStyle) {
final MutableAttributeSet attribs = new SimpleAttributeSet();
final Enumeration<?> attribNames = aStyle.getAttributeNames();
while (attribNames.hasMoreElements()) {
final Object attribName = attribNames.nextElement();
if (attribName == Style.NameAttribute) {
continue;
}
attribs.addAttribute(attribName, aStyle.getAttribute(attribName));
}
return attribs;
}
private JMenuBar createMenuBar() {
final JMenuBar menubar = new JMenuBar();
final JMenu mnuFile = new JMenu("File");
menubar.add(mnuFile);
final SaveAction actSave = new SaveAction();
mnuFile.add(actSave);
return menubar;
}
class SaveAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public SaveAction() {
super("Save");
}
@Override
public void actionPerformed(ActionEvent ev) {
final JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(SimpleEditor.this) != JFileChooser.APPROVE_OPTION)
return;
final File file = chooser.getSelectedFile();
if (file == null)
return;
FileWriter writer = null;
try {
writer = new FileWriter(file);
textPane.write(writer);
} catch (final IOException ex) {
JOptionPane.showMessageDialog(SimpleEditor.this,
"File Not Saved", "ERROR",
JOptionPane.ERROR_MESSAGE);
} finally {
if (writer != null) {
try {
writer.close();
} catch (final IOException x) {
}
}
}
}
}
}
So far so good! My only problem with this solution is that I couldn't manage to implement the switch back from text enclosed in <span>
elements to "normal" text, i.e. text that is not placed inside of <span>
elements.
This should be no big deal but unfortunately I couldn't figure out how I can accomplish that. Any ideas would be very welcome!
回答1:
I got it! And it's sooo simple;-)
To switch back from the styled text entry using <span>
elements I only had to remove the HTML.Tag.SPAN
attribute from the input attributes of the current text. This is accomplished as follows:
edtKit.getInputAttributes().removeAttribute(HTML.Tag.SPAN);
So the code for the ActionListener in the example code I posted in my (updated) question now looks as follows:
cbStyleSel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
e.getSource();
@SuppressWarnings("unchecked")
final JComboBox<CondStyle> cboStyleSel = (JComboBox<CondStyle>) e.getSource();
final String selItem = (String) cboStyleSel.getSelectedItem();
if (selItem.equals("default")) {
edtKit.getInputAttributes().removeAttribute(HTML.Tag.SPAN);
} else {
final MutableAttributeSet divAttributes = new SimpleAttributeSet();
divAttributes.addAttribute(HTML.Attribute.CLASS, selItem.substring(1));
final MutableAttributeSet tagAttributes = new SimpleAttributeSet();
tagAttributes.addAttribute(HTML.Tag.SPAN, divAttributes);
textPane.setCharacterAttributes(tagAttributes, false);
}
textPane.requestFocusInWindow();
}
});
I had posted multiple questions regarding my problems implementing that functionality (see here, and here) but got no answers. Maybe the issue was too trivial;-)
来源:https://stackoverflow.com/questions/17840914/how-to-provide-functionalty-for-changing-the-background-color-of-text-parts-in-h