1 package ch.odi.justblog.gui.swing;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.util.Hashtable;
6
7 import javax.swing.Action;
8 import javax.swing.JButton;
9 import javax.swing.JEditorPane;
10 import javax.swing.JPanel;
11 import javax.swing.JToolBar;
12 import javax.swing.text.StyledEditorKit;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 /***
18 * This is a graphical HTML editor for Blog entries.
19 *
20 * @author oglueck
21 */
22 public class Editor extends JPanel {
23 private Hashtable actions = new Hashtable();
24 private JEditorPane ep;
25 private JToolBar toolBar;
26
27 /***
28 * Creates a new editor component.
29 */
30 public Editor() {
31 ep = new JEditorPane("text/html", "");
32 setLayout(new BorderLayout());
33 add(ep, BorderLayout.CENTER);
34
35 initActionTable();
36 initToolbar();
37 }
38
39 public void setHtml(String html) {
40 ep.setText(html);
41 }
42
43 public String getHtml() {
44 return ep.getText();
45 }
46
47 public void setEnabled(boolean enabled) {
48 ep.setEnabled(enabled);
49 toolBar.setEnabled(enabled);
50 }
51
52 private void initActionTable() {
53 Log log = LogFactory.getLog(this.getClass());
54 StyledEditorKit editorKit = (StyledEditorKit) ep.getEditorKit();
55 Action[] actionArray = editorKit.getActions();
56 for (int i = 0; i < actionArray.length; i++) {
57 Action action = actionArray[i];
58 actions.put(action.getValue(Action.NAME), action);
59 log.debug("editor supports action: "+ action.getValue(Action.NAME));
60 }
61 }
62
63 private void initToolbar() {
64 toolBar = new MyToolBar();
65 add(toolBar, BorderLayout.NORTH);
66 }
67
68 private class MyToolBar extends JToolBar {
69 public MyToolBar() {
70 JButton boldButton = new JButton("bold");
71 boldButton.addActionListener((Action) actions.get("font-bold"));
72 this.add(boldButton);
73
74 JButton italicButton = new JButton("italic");
75 italicButton.addActionListener((Action) actions.get("font-italic"));
76 this.add(italicButton);
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 * Sets the status of all the buttons.
106 */
107 public void setEnabled(boolean enabled) {
108 super.setEnabled(enabled);
109 Component[] components = this.getComponents();
110 for (int i = 0; i < components.length; i++) {
111 components[i].setEnabled(enabled);
112 }
113 }
114 }
115 }