KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > sample > kitchensink > client > RichTextToolbar


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.sample.kitchensink.client;
17
18 import com.google.gwt.core.client.GWT;
19 import com.google.gwt.i18n.client.Constants;
20 import com.google.gwt.user.client.Window;
21 import com.google.gwt.user.client.ui.AbstractImagePrototype;
22 import com.google.gwt.user.client.ui.ChangeListener;
23 import com.google.gwt.user.client.ui.ClickListener;
24 import com.google.gwt.user.client.ui.Composite;
25 import com.google.gwt.user.client.ui.HorizontalPanel;
26 import com.google.gwt.user.client.ui.ImageBundle;
27 import com.google.gwt.user.client.ui.KeyboardListener;
28 import com.google.gwt.user.client.ui.ListBox;
29 import com.google.gwt.user.client.ui.PushButton;
30 import com.google.gwt.user.client.ui.RichTextArea;
31 import com.google.gwt.user.client.ui.ToggleButton;
32 import com.google.gwt.user.client.ui.VerticalPanel;
33 import com.google.gwt.user.client.ui.Widget;
34
35 /**
36  * A sample toolbar for use with {@link RichTextArea}. It provides a simple UI
37  * for all rich text formatting, dynamically displayed only for the available
38  * functionality.
39  */

40 public class RichTextToolbar extends Composite {
41
42   /**
43    * This {@link ImageBundle} is used for all the button icons. Using an image
44    * bundle allows all of these images to be packed into a single image, which
45    * saves a lot of HTTP requests, drastically improving startup time.
46    */

47   public interface Images extends ImageBundle {
48
49     /**
50      * @gwt.resource bold.gif
51      */

52     AbstractImagePrototype bold();
53
54     /**
55      * @gwt.resource createLink.gif
56      */

57     AbstractImagePrototype createLink();
58
59     /**
60      * @gwt.resource hr.gif
61      */

62     AbstractImagePrototype hr();
63
64     /**
65      * @gwt.resource indent.gif
66      */

67     AbstractImagePrototype indent();
68
69     /**
70      * @gwt.resource insertImage.gif
71      */

72     AbstractImagePrototype insertImage();
73
74     /**
75      * @gwt.resource italic.gif
76      */

77     AbstractImagePrototype italic();
78
79     /**
80      * @gwt.resource justifyCenter.gif
81      */

82     AbstractImagePrototype justifyCenter();
83
84     /**
85      * @gwt.resource justifyLeft.gif
86      */

87     AbstractImagePrototype justifyLeft();
88
89     /**
90      * @gwt.resource justifyRight.gif
91      */

92     AbstractImagePrototype justifyRight();
93
94     /**
95      * @gwt.resource ol.gif
96      */

97     AbstractImagePrototype ol();
98
99     /**
100      * @gwt.resource outdent.gif
101      */

102     AbstractImagePrototype outdent();
103
104     /**
105      * @gwt.resource removeFormat.gif
106      */

107     AbstractImagePrototype removeFormat();
108
109     /**
110      * @gwt.resource removeLink.gif
111      */

112     AbstractImagePrototype removeLink();
113
114     /**
115      * @gwt.resource strikeThrough.gif
116      */

117     AbstractImagePrototype strikeThrough();
118
119     /**
120      * @gwt.resource subscript.gif
121      */

122     AbstractImagePrototype subscript();
123
124     /**
125      * @gwt.resource superscript.gif
126      */

127     AbstractImagePrototype superscript();
128
129     /**
130      * @gwt.resource ul.gif
131      */

132     AbstractImagePrototype ul();
133
134     /**
135      * @gwt.resource underline.gif
136      */

137     AbstractImagePrototype underline();
138   }
139
140   /**
141    * This {@link Constants} interface is used to make the toolbar's strings
142    * internationalizable.
143    */

144   public interface Strings extends Constants {
145
146     String JavaDoc black();
147
148     String JavaDoc blue();
149
150     String JavaDoc bold();
151
152     String JavaDoc color();
153
154     String JavaDoc createLink();
155
156     String JavaDoc font();
157
158     String JavaDoc green();
159
160     String JavaDoc hr();
161
162     String JavaDoc indent();
163
164     String JavaDoc insertImage();
165
166     String JavaDoc italic();
167
168     String JavaDoc justifyCenter();
169
170     String JavaDoc justifyLeft();
171
172     String JavaDoc justifyRight();
173
174     String JavaDoc large();
175
176     String JavaDoc medium();
177
178     String JavaDoc normal();
179
180     String JavaDoc ol();
181
182     String JavaDoc outdent();
183
184     String JavaDoc red();
185
186     String JavaDoc removeFormat();
187
188     String JavaDoc removeLink();
189
190     String JavaDoc size();
191
192     String JavaDoc small();
193
194     String JavaDoc strikeThrough();
195
196     String JavaDoc subscript();
197
198     String JavaDoc superscript();
199
200     String JavaDoc ul();
201
202     String JavaDoc underline();
203
204     String JavaDoc white();
205
206     String JavaDoc xlarge();
207
208     String JavaDoc xsmall();
209
210     String JavaDoc xxlarge();
211
212     String JavaDoc xxsmall();
213
214     String JavaDoc yellow();
215   }
216
217   /**
218    * We use an inner EventListener class to avoid exposing event methods on the
219    * RichTextToolbar itself.
220    */

221   private class EventListener implements ClickListener, ChangeListener,
222       KeyboardListener {
223
224     public void onChange(Widget sender) {
225       if (sender == backColors) {
226         basic.setBackColor(backColors.getValue(backColors.getSelectedIndex()));
227         backColors.setSelectedIndex(0);
228       } else if (sender == foreColors) {
229         basic.setForeColor(foreColors.getValue(foreColors.getSelectedIndex()));
230         foreColors.setSelectedIndex(0);
231       } else if (sender == fonts) {
232         basic.setFontName(fonts.getValue(fonts.getSelectedIndex()));
233         fonts.setSelectedIndex(0);
234       } else if (sender == fontSizes) {
235         basic.setFontSize(fontSizesConstants[fontSizes.getSelectedIndex() - 1]);
236         fontSizes.setSelectedIndex(0);
237       }
238     }
239
240     public void onClick(Widget sender) {
241       if (sender == bold) {
242         basic.toggleBold();
243       } else if (sender == italic) {
244         basic.toggleItalic();
245       } else if (sender == underline) {
246         basic.toggleUnderline();
247       } else if (sender == subscript) {
248         basic.toggleSubscript();
249       } else if (sender == superscript) {
250         basic.toggleSuperscript();
251       } else if (sender == strikethrough) {
252         extended.toggleStrikethrough();
253       } else if (sender == indent) {
254         extended.rightIndent();
255       } else if (sender == outdent) {
256         extended.leftIndent();
257       } else if (sender == justifyLeft) {
258         basic.setJustification(RichTextArea.Justification.LEFT);
259       } else if (sender == justifyCenter) {
260         basic.setJustification(RichTextArea.Justification.CENTER);
261       } else if (sender == justifyRight) {
262         basic.setJustification(RichTextArea.Justification.RIGHT);
263       } else if (sender == insertImage) {
264         String JavaDoc url = Window.prompt("Enter an image URL:", "http://");
265         if (url != null) {
266           extended.insertImage(url);
267         }
268       } else if (sender == createLink) {
269         String JavaDoc url = Window.prompt("Enter a link URL:", "http://");
270         if (url != null) {
271           extended.createLink(url);
272         }
273       } else if (sender == removeLink) {
274         extended.removeLink();
275       } else if (sender == hr) {
276         extended.insertHorizontalRule();
277       } else if (sender == ol) {
278         extended.insertOrderedList();
279       } else if (sender == ul) {
280         extended.insertUnorderedList();
281       } else if (sender == removeFormat) {
282         extended.removeFormat();
283       } else if (sender == richText) {
284         // We use the RichTextArea's onKeyUp event to update the toolbar status.
285
// This will catch any cases where the user moves the cursur using the
286
// keyboard, or uses one of the browser's built-in keyboard shortcuts.
287
updateStatus();
288       }
289     }
290
291     public void onKeyDown(Widget sender, char keyCode, int modifiers) {
292     }
293
294     public void onKeyPress(Widget sender, char keyCode, int modifiers) {
295     }
296
297     public void onKeyUp(Widget sender, char keyCode, int modifiers) {
298       if (sender == richText) {
299         // We use the RichTextArea's onKeyUp event to update the toolbar status.
300
// This will catch any cases where the user moves the cursur using the
301
// keyboard, or uses one of the browser's built-in keyboard shortcuts.
302
updateStatus();
303       }
304     }
305   }
306
307   private static final RichTextArea.FontSize[] fontSizesConstants = new RichTextArea.FontSize[] {
308       RichTextArea.FontSize.XX_SMALL, RichTextArea.FontSize.X_SMALL,
309       RichTextArea.FontSize.SMALL, RichTextArea.FontSize.MEDIUM,
310       RichTextArea.FontSize.LARGE, RichTextArea.FontSize.X_LARGE,
311       RichTextArea.FontSize.XX_LARGE};
312
313   private Images images = (Images) GWT.create(Images.class);
314   private Strings strings = (Strings) GWT.create(Strings.class);
315   private EventListener listener = new EventListener();
316
317   private RichTextArea richText;
318   private RichTextArea.BasicFormatter basic;
319   private RichTextArea.ExtendedFormatter extended;
320
321   private VerticalPanel outer = new VerticalPanel();
322   private HorizontalPanel topPanel = new HorizontalPanel();
323   private HorizontalPanel bottomPanel = new HorizontalPanel();
324   private ToggleButton bold;
325   private ToggleButton italic;
326   private ToggleButton underline;
327   private ToggleButton subscript;
328   private ToggleButton superscript;
329   private ToggleButton strikethrough;
330   private PushButton indent;
331   private PushButton outdent;
332   private PushButton justifyLeft;
333   private PushButton justifyCenter;
334   private PushButton justifyRight;
335   private PushButton hr;
336   private PushButton ol;
337   private PushButton ul;
338   private PushButton insertImage;
339   private PushButton createLink;
340   private PushButton removeLink;
341   private PushButton removeFormat;
342
343   private ListBox backColors;
344   private ListBox foreColors;
345   private ListBox fonts;
346   private ListBox fontSizes;
347
348   /**
349    * Creates a new toolbar that drives the given rich text area.
350    *
351    * @param richText the rich text area to be controlled
352    */

353   public RichTextToolbar(RichTextArea richText) {
354     this.richText = richText;
355     this.basic = richText.getBasicFormatter();
356     this.extended = richText.getExtendedFormatter();
357
358     outer.add(topPanel);
359     outer.add(bottomPanel);
360     topPanel.setWidth("100%");
361     bottomPanel.setWidth("100%");
362
363     initWidget(outer);
364     setStyleName("gwt-RichTextToolbar");
365
366     if (basic != null) {
367       topPanel.add(bold = createToggleButton(images.bold(), strings.bold()));
368       topPanel.add(italic = createToggleButton(images.italic(), strings.italic()));
369       topPanel.add(underline = createToggleButton(images.underline(),
370           strings.underline()));
371       topPanel.add(subscript = createToggleButton(images.subscript(),
372           strings.subscript()));
373       topPanel.add(superscript = createToggleButton(images.superscript(),
374           strings.superscript()));
375       topPanel.add(justifyLeft = createPushButton(images.justifyLeft(),
376           strings.justifyLeft()));
377       topPanel.add(justifyCenter = createPushButton(images.justifyCenter(),
378           strings.justifyCenter()));
379       topPanel.add(justifyRight = createPushButton(images.justifyRight(),
380           strings.justifyRight()));
381     }
382
383     if (extended != null) {
384       topPanel.add(strikethrough = createToggleButton(images.strikeThrough(),
385           strings.strikeThrough()));
386       topPanel.add(indent = createPushButton(images.indent(), strings.indent()));
387       topPanel.add(outdent = createPushButton(images.outdent(), strings.outdent()));
388       topPanel.add(hr = createPushButton(images.hr(), strings.hr()));
389       topPanel.add(ol = createPushButton(images.ol(), strings.ol()));
390       topPanel.add(ul = createPushButton(images.ul(), strings.ul()));
391       topPanel.add(insertImage = createPushButton(images.insertImage(),
392           strings.insertImage()));
393       topPanel.add(createLink = createPushButton(images.createLink(),
394           strings.createLink()));
395       topPanel.add(removeLink = createPushButton(images.removeLink(),
396           strings.removeLink()));
397       topPanel.add(removeFormat = createPushButton(images.removeFormat(),
398           strings.removeFormat()));
399     }
400
401     if (basic != null) {
402       bottomPanel.add(backColors = createColorList("Background"));
403       bottomPanel.add(foreColors = createColorList("Foreground"));
404       bottomPanel.add(fonts = createFontList());
405       bottomPanel.add(fontSizes = createFontSizes());
406
407       // We only use these listeners for updating status, so don't hook them up
408
// unless at least basic editing is supported.
409
richText.addKeyboardListener(listener);
410       richText.addClickListener(listener);
411     }
412   }
413
414   private ListBox createColorList(String JavaDoc caption) {
415     ListBox lb = new ListBox();
416     lb.addChangeListener(listener);
417     lb.setVisibleItemCount(1);
418
419     lb.addItem(caption);
420     lb.addItem(strings.white(), "white");
421     lb.addItem(strings.black(), "black");
422     lb.addItem(strings.red(), "red");
423     lb.addItem(strings.green(), "green");
424     lb.addItem(strings.yellow(), "yellow");
425     lb.addItem(strings.blue(), "blue");
426     return lb;
427   }
428
429   private ListBox createFontList() {
430     ListBox lb = new ListBox();
431     lb.addChangeListener(listener);
432     lb.setVisibleItemCount(1);
433
434     lb.addItem(strings.font(), "");
435     lb.addItem(strings.normal(), "");
436     lb.addItem("Times New Roman", "Times New Roman");
437     lb.addItem("Arial", "Arial");
438     lb.addItem("Courier New", "Courier New");
439     lb.addItem("Georgia", "Georgia");
440     lb.addItem("Trebuchet", "Trebuchet");
441     lb.addItem("Verdana", "Verdana");
442     return lb;
443   }
444
445   private ListBox createFontSizes() {
446     ListBox lb = new ListBox();
447     lb.addChangeListener(listener);
448     lb.setVisibleItemCount(1);
449
450     lb.addItem(strings.size());
451     lb.addItem(strings.xxsmall());
452     lb.addItem(strings.xsmall());
453     lb.addItem(strings.small());
454     lb.addItem(strings.medium());
455     lb.addItem(strings.large());
456     lb.addItem(strings.xlarge());
457     lb.addItem(strings.xxlarge());
458     return lb;
459   }
460
461   private PushButton createPushButton(AbstractImagePrototype img, String JavaDoc tip) {
462     PushButton pb = new PushButton(img.createImage());
463     pb.addClickListener(listener);
464     pb.setTitle(tip);
465     return pb;
466   }
467
468   private ToggleButton createToggleButton(AbstractImagePrototype img, String JavaDoc tip) {
469     ToggleButton tb = new ToggleButton(img.createImage());
470     tb.addClickListener(listener);
471     tb.setTitle(tip);
472     return tb;
473   }
474
475   /**
476    * Updates the status of all the stateful buttons.
477    */

478   private void updateStatus() {
479     if (basic != null) {
480       bold.setDown(basic.isBold());
481       italic.setDown(basic.isItalic());
482       underline.setDown(basic.isUnderlined());
483       subscript.setDown(basic.isSubscript());
484       superscript.setDown(basic.isSuperscript());
485     }
486
487     if (extended != null) {
488       strikethrough.setDown(extended.isStrikethrough());
489     }
490   }
491 }
492
493
Popular Tags