Source code: openfuture/editxml/applet/EditorController.java
1 package openfuture.editxml.applet;
2
3 /*
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.<p>
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.<p>
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br>
17 * http://www.gnu.org/copyleft/lesser.html
18 */
19
20 import java.awt.Container;
21 import java.awt.event.KeyEvent;
22 import java.awt.event.KeyListener;
23 import java.util.EventObject;
24 import java.util.ResourceBundle;
25 import javax.swing.JEditorPane;
26 import javax.swing.SingleSelectionModel;
27 import javax.swing.event.ChangeEvent;
28 import javax.swing.event.ChangeListener;
29 import javax.swing.undo.UndoManager;
30 import java.awt.event.KeyEvent;
31 import java.awt.event.KeyListener;
32
33 /**
34 * Configuration Management Information:
35 * -------------------------------------
36 * $Id: EditorController.java,v 1.4 2001/09/29 17:51:35 wreissen Exp $
37 *
38 * Version History:
39 * ----------------
40 * $Log: EditorController.java,v $
41 * Revision 1.4 2001/09/29 17:51:35 wreissen
42 * undo handling moved to class DocumentHistory
43 *
44 * Revision 1.3 2001/09/24 21:31:25 wreissen
45 * toolbar integrated.
46 *
47 * Revision 1.2 2001/07/22 09:20:46 wreissen
48 * - resource bundle usage added
49 * - HTML editor events integrated
50 *
51 * Revision 1.1 2001/07/08 20:26:05 wreissen
52 * initial version
53 *
54 *
55 */
56
57 /**
58 * Controller for the XML editors.<p>
59 *
60 *
61 * Created: Sun Jul 08 21:24:03 2001
62 *
63 * @author <a href="mailto: wolfgang@openfuture.de">Wolfgang Reissenberger</a>
64 * @version $Revision: 1.4 $
65 */
66
67 public class EditorController extends AbsController
68 implements KeyListener, ChangeListener {
69
70 private RootController parentController;
71 private MainPanel parentPanel;
72 private UndoManager undo;
73
74
75
76 /**
77 * Creates a new <code>EditorController</code> instance.
78 *
79 * @param parentPanel the parent panel
80 * @param resourceBundle the current resource bundle
81 * @param parentController the parent controller
82 */
83 public EditorController (MainPanel parentPanel,
84 ResourceBundle resourceBundle,
85 RootController parentController) {
86 super(resourceBundle);
87 setParentPanel(parentPanel);
88 setParentController(parentController);
89 }
90
91 // implementation of javax.swing.event.ChangeListener interface
92
93 /**
94 * Recognize, if the panel selection changes.
95 *
96 * @param ce a <code>ChangeEvent</code> value
97 */
98 public void stateChanged(ChangeEvent ce) {
99 Object source = ce.getSource();
100
101 if (source instanceof SingleSelectionModel) {
102 int index = ((SingleSelectionModel) source).getSelectedIndex();
103 if (index == 0) {
104 // text editor selected
105 // does nothing
106 } else {
107 // HTML previewer selected
108 updateHtmlEditor();
109 }
110 } else {
111 System.out.println("Unexpected object state change: " + source.getClass().getName());
112 }
113 }
114
115
116 // implementation of java.awt.event.KeyListener interface
117
118 /**
119 * Does nothing.
120 *
121 * @param e an <code>KeyEvent</code> value
122 */
123 public void keyTyped(KeyEvent e) {
124 }
125
126 /**
127 * Does nothing.
128 *
129 * @param e an <code>KeyEvent</code> value
130 */
131 public void keyPressed(KeyEvent e) {
132 }
133
134 /**
135 * Refresh the display. Actually calls
136 * {@link openfuture.editxml.applet.MainPanel#refreshView()
137 * MainPanel.refreshView()}
138 *
139 * @param e an <code>KeyEvent</code> value
140 */
141 public void keyReleased(KeyEvent e) {
142 getParentPanel().refreshView();
143 }
144
145 /**
146 * Central event handler. Handles all events from the task bar.
147 * All other events are redirected to {@link #getParentController()}.
148 *
149 * @param event the event
150 * @param source the source triggering the event
151 * @param name source name
152 */
153 void handleEvents (EventObject event, Object source, String name) {
154 if (getIgnoreEvents()) return;
155
156 if (name == null) {
157 System.out.println("Unexpected event: " + event.getClass().getName());
158 return;
159 }
160
161 if (name.equals(TextEditorPanel.BOLD_BUTTON)) {
162 wrapTag("b", "");
163 } else if (name.equals(TextEditorPanel.ITALIC_BUTTON)) {
164 wrapTag("i", "");
165 } else if (name.equals(TextEditorPanel.UL_BUTTON)) {
166 insertHtml("\n<ul>\n <li></li>\n</ul>\n", 12);
167 } else if (name.equals(TextEditorPanel.LI_BUTTON)) {
168 insertHtml(" <li></li>\n", 6);
169 } else if (name.equals(TextEditorPanel.TABLE_BUTTON)) {
170 insertHtml("\n<table border=\"1\">\n<tr>\n <td></td>\n</tr>\n</table>\n", 31);
171 } else if (name.equals(TextEditorPanel.TR_BUTTON)) {
172 insertHtml("<tr>\n <td></td>\n</tr>\n", 11);
173 } else if (name.equals(TextEditorPanel.TD_BUTTON)) {
174 insertHtml(" <td></td>\n", 6);
175 } else if (name.equals(TextEditorPanel.LINK_BUTTON)) {
176 wrapTag("a", "href=\"\"");
177 } else if (name.equals(TextEditorPanel.UNDO_BUTTON)) {
178 getParentPanel().getTextEditor().getUndo().undo();
179 } else if (name.equals(TextEditorPanel.REDO_BUTTON)) {
180 getParentPanel().getTextEditor().getUndo().redo();
181 } else {
182 getParentController().handleEvents(event, source, name);
183 }
184 getParentPanel().refreshView();
185 }
186
187
188 /**
189 * Extract the text from a HTML document contained inside the
190 * <code><body></code> tag.
191 *
192 * @param text HTML text
193 * @return body text
194 */
195 public static String extractHtmlBody (String text) {
196
197 int start = text.toLowerCase().indexOf("<body>");
198
199 if (start >= 0) {
200 int end = text.toLowerCase().indexOf("</body>");
201 return (text.substring(start+6, end));
202 } else return text;
203 }
204
205
206 /**
207 * Update the content of the HTML editor
208 *
209 */
210 protected void updateHtmlEditor() {
211 String content = getParentPanel().getTextEditor().getEditor().getText();
212 getParentPanel().getHtmlEditor().setText(content);
213 }
214
215
216
217 /**
218 * Update the content of the text editor
219 *
220 */
221 protected void updateTextEditor() {
222 String content = extractHtmlBody(getParentPanel().getHtmlEditor().getText());
223 getParentPanel().getTextEditor().getEditor().setText(content);
224 }
225
226
227 /**
228 * Wrap an HTML tag around the current selection. If nothing
229 * is selected, nothing happens. This action is applied to the
230 * {@link openfuture.editxml.applet.MainPanel#getTextEditor()
231 * text editor} of the main panel.
232 *
233 * @param tag HTML tag name
234 * @param attributes additional HTML attributes (as String)
235 */
236 protected void wrapTag(String tag, String attributes) {
237 JEditorPane editor = getParentPanel().getTextEditor().getEditor();
238 int dot = editor.getCaret().getDot();
239 int mark = editor.getCaret().getMark();
240 String selection = editor.getSelectedText();
241
242 // add HTML tag around the selection
243 if (selection != null) {
244 String prefix = "<" + tag + ">";
245 if (attributes != null && ! attributes.equals("")) {
246 prefix = "<" + tag + " " + attributes + ">";
247 }
248 selection = prefix + selection + "</" + tag + ">";
249 editor.replaceSelection(selection);
250
251 // mark new region
252 editor.getCaret().setDot(mark + prefix.length());
253 editor.getCaret().moveDot(dot + prefix.length());
254
255 // request the focus, since the focus currently is set on
256 // a button in the task bar
257 editor.requestFocus();
258 }
259 }
260
261 /**
262 * Inserts an HTML fragment at the current
263 * {@link javax.swing.text.JEditorPane#getSelectedText()
264 * selection}. This action is applied to the
265 * {@link openfuture.editxml.applet.MainPanel#getTextEditor()
266 * text editor} of the main panel.
267 *
268 * @param text HTML fragment
269 * @param advance number of positions the caret
270 * should be advanced after inserting the text
271 */
272 protected void insertHtml(String text, int advance) {
273 JEditorPane editor = getParentPanel().getTextEditor().getEditor();
274 int position = editor.getCaretPosition();
275 editor.replaceSelection(text);
276 editor.setCaretPosition(position + advance);
277 // request the focus, since the focus currently is set on
278 // a button in the task bar
279 editor.requestFocus();
280 }
281
282 /**
283 * Get the value of parentController.
284 * @return value of parentController.
285 */
286 public RootController getParentController() {
287 return parentController;
288 }
289
290 /**
291 * Set the value of parentController.
292 * @param v Value to assign to parentController.
293 */
294 public void setParentController(RootController v) {
295 this.parentController = v;
296 }
297
298
299 /**
300 * Get the value of parentPanel.
301 * @return value of parentPanel.
302 */
303 public MainPanel getParentPanel() {
304 return parentPanel;
305 }
306
307 /**
308 * Set the value of parentPanel.
309 * @param v Value to assign to parentPanel.
310 */
311 public void setParentPanel(MainPanel v) {
312 this.parentPanel = v;
313 }
314
315 }// EditorController