/
MainPanel.java
126 lines (116 loc) · 4.42 KB
/
MainPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new BorderLayout());
JTextPane textPane = new JTextPane();
textPane.setComponentPopupMenu(new HtmlColorPopupMenu());
// textPane.setEditorKit(new HTMLEditorKit());
textPane.setContentType("text/html");
JTextArea textArea = new JTextArea();
textArea.setText(textPane.getText());
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("JTextPane", new JScrollPane(textPane));
tabbedPane.addTab("JTextArea", new JScrollPane(textArea));
tabbedPane.addChangeListener(e -> {
JTabbedPane t = (JTabbedPane) e.getSource();
boolean isHtmlMode = t.getSelectedIndex() == 0;
if (isHtmlMode) {
textPane.setText(textArea.getText());
// textPane.setText("");
// HTMLEditorKit hek = (HTMLEditorKit) textPane.getEditorKit();
// HTMLDocument doc = (HTMLDocument) textPane.getStyledDocument();
// hek.insertHTML(doc, 0, textArea.getText(), 0, 0, null);
} else {
String str = textPane.getText();
textArea.setText(str);
// // Removing HTML from a Java String - Stack Overflow
// // https://stackoverflow.com/questions/240546/removing-html-from-a-java-string
// // Test >>>>
// ParserDelegator delegator = new ParserDelegator();
// StringBuilder s = new StringBuilder();
// delegator.parse(new StringReader(str), new HTMLEditorKit.ParserCallback() {
// @Override public void handleText(char[] text, int pos) {
// s.append(text);
// }
// }, Boolean.TRUE);
// System.out.println(s.toString());
// // <<<<
}
t.revalidate();
});
add(tabbedPane);
setPreferredSize(new Dimension(320, 240));
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException ignored) {
Toolkit.getDefaultToolkit().beep();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
return;
}
JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
final class HtmlColorPopupMenu extends JPopupMenu {
/* default */ HtmlColorPopupMenu() {
super();
MutableAttributeSet red = new SimpleAttributeSet();
StyleConstants.setForeground(red, Color.RED);
MutableAttributeSet green = new SimpleAttributeSet();
StyleConstants.setForeground(green, Color.GREEN);
MutableAttributeSet blue = new SimpleAttributeSet();
StyleConstants.setForeground(blue, Color.BLUE);
add("Red").addActionListener(e -> {
JTextPane t = (JTextPane) getInvoker();
StyledDocument doc = t.getStyledDocument();
int start = t.getSelectionStart();
int end = t.getSelectionEnd();
doc.setCharacterAttributes(start, end - start, red, false);
});
add("Green").addActionListener(e -> {
JTextPane t = (JTextPane) getInvoker();
StyledDocument doc = t.getStyledDocument();
int start = t.getSelectionStart();
int end = t.getSelectionEnd();
doc.setCharacterAttributes(start, end - start, green, false);
});
add("Blue").addActionListener(e -> {
JTextPane t = (JTextPane) getInvoker();
StyledDocument doc = t.getStyledDocument();
int start = t.getSelectionStart();
int end = t.getSelectionEnd();
doc.setCharacterAttributes(start, end - start, blue, false);
});
}
@Override public void show(Component c, int x, int y) {
if (c instanceof JTextPane) {
JTextPane t = (JTextPane) c;
int start = t.getSelectionStart();
int end = t.getSelectionEnd();
boolean flag = end - start > 0;
for (MenuElement me : getSubElements()) {
me.getComponent().setEnabled(flag);
}
super.show(c, x, y);
}
}
}