package html;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import javax.swing.Action;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.text.Element;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;
import javax.swing.text.html.FormView;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
public class HTMLEditor extends JFrame {
public HTMLEditor() {
super();
HTMLEditorPane editor = new HTMLEditorPane();
getContentPane().setLayout(new BorderLayout());
JToolBar tb = new JToolBar();
JButton bold = new JButton("Fett");
bold.setAction(new StyledEditorKit.StyledTextAction("Fett") {
public void actionPerformed(ActionEvent e) {
JEditorPane editor = getEditor(e);
if (editor != null) {
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean bold = (StyleConstants.isBold(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setBold(sas, bold);
setCharacterAttributes(editor, sas, false);
}
}
});
tb.add(bold);
JButton italic = new JButton("Kursiv");
italic.setAction(new StyledEditorKit.StyledTextAction("Kursiv") {
public void actionPerformed(ActionEvent e) {
JEditorPane editor = getEditor(e);
if (editor != null) {
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean italic = (StyleConstants.isItalic(attr)) ? false
: true;
StyleConstants.setItalic(sas, italic);
setCharacterAttributes(editor, sas, false);
}
}
});
tb.add(italic);
getContentPane().add(tb, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(editor), BorderLayout.CENTER);
pack();
setSize(600, 400);
setVisible(true);
}
public class HTMLEditorPane extends JEditorPane {
public HTMLEditorPane() {
super("text/html", "");
setEditorKit(new HTMLEditorKit() {
public ViewFactory getViewFactory() {
return new HTMLEditorKit.HTMLFactory() {
public View create(Element elem) {
Object o = elem.getAttributes().getAttribute(
StyleConstants.NameAttribute);
if (o instanceof HTML.Tag) {
HTML.Tag kind = (HTML.Tag) o;
Object type = elem.getAttributes()
.getAttribute(HTML.Attribute.TYPE);
if (kind == HTML.Tag.INPUT
&& type != null
&& type.toString().equalsIgnoreCase(
"submit"))
return new FormView(elem) {
protected void submitData(String data) {
System.out.println(data);
super.submitData(data);
}
};
}
return super.create(elem);
}
};
}
});
setText("Testzeile");
setEditable(true);
}
}
public static void main(String[] args) throws Exception {
new HTMLEditor();
}
}