Format editing in HTML view with JEditorPane
Hi
I wonder if anybody can help me out in this: my code is getting more and more complex, but I'm just not getting there:
I want to make a text-editor, which can do some simple text-formatting too. For this I am using a JEditorPane in HTML-mode.
By selecting a part of the text, and then clicking a 'Bold'-button, I want to make the selected part of the text bold. (Like in MS Word)
The program should simply surround the selected text with <b>...</b> tags, to make it bold.
This sounds really easy, but it is a pain in the neck.
Problem is, that I select the text in HTML-formatted view (which is the only thing I want to see really), but the tags need to be set in the HTML-code in plain-text-view.
The methods getSelectionStart and getSelectionEnd refer only to the HTML-view.
Now I need to get the right corresponding Start- and End-points for the HTML-code in plain-text-view, where the tags need to be put.
For this I iterate through the HTML-code, but it gets quite complicated, and I just don’t get it right, as it keeps making mistakes, if I play around a bit.
I just feel there should be an easier answer to this.
Can anybody help?
Cheers, Peter
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;
public class MyPane1 extends JFrame implements ActionListener {
private JEditorPane editorPane;
private JPanel control;
private JScrollPane editorScrollPane;
private JButton bold, italic;
private JButton code, clear;
public MyPane1() {
editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setEditable(true);
editorPane.setText("<h1>Hello</h1><i>this is italic</i><br>this is a <a href='123'>referenz</a><br>the end");
editorScrollPane = new JScrollPane(editorPane);
control = new JPanel();
bold = new JButton("<html><b>B");
bold.addActionListener(this);
control.add(bold);
italic = new JButton("<html><i>I");
italic.addActionListener(this);
control.add(italic);
code = new JButton("code");
code.addActionListener(this);
control.add(code);
clear = new JButton("clear");
clear.addActionListener(this);
control.add(clear);
setSize(300, 300);
setLayout(new BorderLayout());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(editorScrollPane, BorderLayout.CENTER);
add(control, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(bold)) {
editing("b");
}
if (e.getSource().equals(italic)) {
editing("i");
}
if (e.getSource().equals(clear)) {
editorPane.setText(null);
}
if (e.getSource().equals(code)) {
String t = editorPane.getText();
if (editorPane.getContentType().equals("text/plain")) {
editorPane.setContentType("text/html");
} else {
editorPane.setContentType("text/plain");
}
editorPane.setText(t);
}
}
public void editing(String what) {
int codeSelectStart = 0;
int codeSelectEnd = 0;
String text = editorPane.getText();
int selectStart = editorPane.getSelectionStart();
int selectEnd = editorPane.getSelectionEnd();
int max = text.length();
String tag = "";
Boolean tagged = false;
int ii = 0;
for (int i = 0; i < max; i++) {
char ch = text.charAt(i);
if (ch == '<' && !tagged) {
tagged = true;
tag = "";
} else {
if (ch == '>' && tagged) {
tagged = false;
if (tag.equals("br") || tag.equals("/h1")) { // several other tags would qualify, but are not used in this example
ii++;
if (selectStart == ii) {
codeSelectStart = i + 1;
}
if (selectEnd == ii) {
codeSelectEnd = i + 1;
}
}
} else {
if (tagged) {
tag = tag + ch;
} else {
try {
if (editorPane.getText(ii + 1, 1).equals(" ") || Character.getNumericValue(ch) > -1) {
ii++;
if (ii == selectStart) {
codeSelectStart = i;
}
if (ii == selectEnd - 1) {
codeSelectEnd = i + 1;
}
System.out.print("(" + ii + ")" + ch);
}
} catch (BadLocationException ex) {
Logger.getLogger(MyPane1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
text = text.substring(0, codeSelectStart) + "<" + what + ">" + text.substring(codeSelectStart, codeSelectEnd) + "</" + what + ">" + text.substring(codeSelectEnd, max);
editorPane.setText(text);
}
public static void main(String[] args) {
new MyPane1();
}
}