Source code: openfuture/editxml/applet/TextEditorPanel.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.net.MalformedURLException;
21 import java.net.URL;
22 import java.util.Locale;
23 import java.util.ResourceBundle;
24 import javax.swing.JButton;
25 import javax.swing.JEditorPane;
26 import javax.swing.JToolBar;
27 import javax.swing.text.PlainDocument;
28 import javax.swing.undo.UndoManager;
29 import openfuture.util.layout.XmFormLayout;
30 import openfuture.util.misc.DocumentHistory;
31
32 /**
33 * Configuration Management Information:
34 * -------------------------------------
35 * $Id: TextEditorPanel.java,v 1.2 2001/09/29 18:05:12 wreissen Exp $
36 *
37 * Version History:
38 * ----------------
39 * $Log: TextEditorPanel.java,v $
40 * Revision 1.2 2001/09/29 18:05:12 wreissen
41 * undo handling moved to class DocumentHistory
42 *
43 * Revision 1.1 2001/09/24 21:35:17 wreissen
44 * initial version.
45 *
46 *
47 */
48
49 /**
50 * TextEditorPanel.java
51 *
52 *
53 * Created: Fri Sep 14 13:30:25 2001
54 *
55 * @author <a href="mailto:wolfgang@openfuture.de">Wolfgang Reissenberger</a>
56 * @version $Revision: 1.2 $
57 */
58
59 public class TextEditorPanel extends AbsPanel {
60
61 public static final String BOLD_BUTTON = "button.bold";
62 public static final String INFO_BUTTON = "button.info";
63 public static final String ITALIC_BUTTON = "button.italic";
64 public static final String LINK_BUTTON = "button.link";
65 public static final String LI_BUTTON = "button.li";
66 public static final String REDO_BUTTON = "button.redo";
67 public static final String TABLE_BUTTON = "button.table";
68 public static final String TD_BUTTON = "button.table.cell";
69 public static final String TR_BUTTON = "button.table.row";
70 public static final String UL_BUTTON = "button.ul";
71 public static final String UNDO_BUTTON = "button.undo";
72
73 public static String TEXT_EDITOR = "Text Editor";
74
75 protected JEditorPane textEditor;
76 protected JToolBar toolbar;
77 protected JButton redoButton;
78 protected JButton undoButton;
79
80 private String imagedirURL;
81 private EditorController editorController;
82 private DocumentHistory documentHistory;
83
84 /**
85 * Creates a new <code>TextEditorPanel</code> instance.
86 *
87 * @param controller the controller of this panel
88 * @param resourceBundle the current resource bundle
89 * @param locale the current locale
90 */
91 public TextEditorPanel (EditorController controller,
92 ResourceBundle resourceBundle,
93 Locale locale, String imagedirURL) {
94 super(locale);
95 init(controller, resourceBundle, imagedirURL);
96
97 }
98 /**
99 * Initialize the panel.
100 *
101 * @param editorController the editor controller of this panel
102 * @param resourceBundle the current resource bundle
103 */
104 public void init(EditorController editorController,
105 ResourceBundle resourceBundle, String imagedirURL) {
106
107
108 String[] constraintsTable = {
109 "Toolbar.top = form.top",
110 "Toolbar.left = form.left",
111
112 "Editor.top = Toolbar.bottom",
113 "Editor.left = form.left",
114 "Editor.right = form.right",
115 "Editor.bottom = form.bottom",
116 };
117
118 setEditorController(editorController);
119 setImagedirURL(imagedirURL);
120 setEditor(new JEditorPane());
121 getEditor().setName(TEXT_EDITOR);
122
123 createToolBar();
124
125 setLayout(new XmFormLayout(constraintsTable));
126
127 add(toolbar, "Toolbar");
128 add(getEditor(), "Editor");
129
130 // add listeners
131 getEditor().addKeyListener(editorController);
132 refreshView();
133 }
134
135
136 /**
137 * Defines the tool bar.
138 *
139 */
140 protected void createToolBar() {
141 toolbar = new JToolBar();
142 toolbar.setFloatable(false);
143
144 try {
145 redoButton = createIconButton(REDO_BUTTON, new URL(getImagedirURL() + "/jlfgr/toolbarButtonGraphics/general/Redo16.gif"), "tooltip.redo");
146 undoButton = createIconButton(UNDO_BUTTON, new URL(getImagedirURL() + "/jlfgr/toolbarButtonGraphics/general/Undo16.gif"), "tooltip.undo");
147
148 toolbar.add(redoButton);
149 toolbar.add(undoButton);
150
151 toolbar.addSeparator();
152
153 toolbar.add(createIconButton(BOLD_BUTTON, new URL(getImagedirURL() + "/jlfgr/toolbarButtonGraphics/text/Bold16.gif"), "tooltip.bold"));
154
155 toolbar.add(createIconButton(ITALIC_BUTTON, new URL(getImagedirURL() + "/jlfgr/toolbarButtonGraphics/text/Italic16.gif"), "tooltip.italic"));
156 toolbar.addSeparator();
157
158 toolbar.add(createIconButton(UL_BUTTON, new URL(getImagedirURL() + "/unorderedlist16.jpg"), "tooltip.list.unordered"));
159 toolbar.add(createIconButton(LI_BUTTON, new URL(getImagedirURL() + "/listitem16.jpg"), "tooltip.list.item"));
160 toolbar.addSeparator();
161
162 toolbar.add(createIconButton(TABLE_BUTTON, new URL(getImagedirURL() + "/table16.jpg"), "tooltip.table"));
163 toolbar.add(createIconButton(TR_BUTTON, new URL(getImagedirURL() + "/table-row16.jpg"), "tooltip.table.row"));
164 toolbar.add(createIconButton(TD_BUTTON, new URL(getImagedirURL() + "/table-cell16.jpg"), "tooltip.table.cell"));
165
166 toolbar.addSeparator();
167 toolbar.add(createIconButton(LINK_BUTTON, new URL(getImagedirURL() + "/jlfgr/toolbarButtonGraphics/development/Applet16.gif"), "tooltip.link"));
168
169 toolbar.addSeparator();
170 toolbar.addSeparator();
171
172 toolbar.add(createIconButton(INFO_BUTTON, new URL(getImagedirURL() + "/jlfgr/toolbarButtonGraphics/general/Information16.gif"), "tooltip.information"));
173 } catch (MalformedURLException e) {
174 getEditorController().showError(e);
175 }
176 }
177
178
179
180 /**
181 * Refresh the display. Actually enables/disables the
182 * undo/redo buttons.
183 *
184 */
185 public void refreshView() {
186 if (getUndo() != null) {
187 redoButton.setEnabled(getUndo().canRedo());
188 undoButton.setEnabled(getUndo().canUndo());
189 } else {
190 redoButton.setEnabled(false);
191 undoButton.setEnabled(false);
192 }
193 }
194
195
196 /**
197 * Get the value of textEditor.
198 * @return value of textEditor.
199 */
200 public JEditorPane getEditor() {
201 return textEditor;
202 }
203
204 /**
205 * Set the value of textEditor.
206 * @param v Value to assign to textEditor.
207 */
208 public void setEditor(JEditorPane v) {
209 this.textEditor = v;
210 }
211
212
213 /**
214 * Get the value of documentHistory.
215 * @return value of documentHistory.
216 */
217 public DocumentHistory getDocumentHistory() {
218 return documentHistory;
219 }
220
221 /**
222 * Set the value of documentHistory.
223 * @param v Value to assign to documentHistory.
224 */
225 public void setDocumentHistory(DocumentHistory v) {
226 DocumentHistory old = getDocumentHistory();
227 if (old != null) {
228 getEditor().getDocument().removeUndoableEditListener(old);
229 }
230 getEditor().getDocument().addUndoableEditListener(v);
231 getEditor().setDocument(v.getDocument());
232 this.documentHistory = v;
233 refreshView();
234 }
235
236 /**
237 * Get the value of imagedirURL.
238 * @return value of imagedirURL.
239 */
240 public String getImagedirURL() {
241 return imagedirURL;
242 }
243
244 /**
245 * Set the value of imagedirURL.
246 * @param v Value to assign to imagedirURL.
247 */
248 public void setImagedirURL(String v) {
249 this.imagedirURL = v;
250 }
251
252
253 /**
254 * Get the value of editorController.
255 * @return value of editorController.
256 */
257 public EditorController getEditorController() {
258 return editorController;
259 }
260
261 /**
262 * Set the value of editorController.
263 * @param v Value to assign to editorController.
264 */
265 public void setEditorController(EditorController v) {
266 this.editorController = v;
267 setMyActionListener(v);
268 }
269
270 /**
271 * Get the value of undo.
272 * @return value of undo.
273 */
274 public UndoManager getUndo() {
275 if (getDocumentHistory() == null) return null;
276 else return getDocumentHistory().getUndo();
277 }
278
279
280 /**
281 * Get the value of text.
282 * @return value of text.
283 */
284 public String getText() {
285 return getEditor().getText();
286 }
287
288 /**
289 * Set the value of text.
290 * @param v Value to assign to text.
291 */
292 public void setText(String v) {
293 PlainDocument doc = new PlainDocument();
294 getEditor().setDocument(doc);
295 getEditor().setText(v);
296
297 setDocumentHistory(new DocumentHistory(doc));
298 refreshView();
299 }
300
301 }// TextEditorPanel