1 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 40 public class RichTextToolbar extends Composite { 41 42 47 public interface Images extends ImageBundle { 48 49 52 AbstractImagePrototype bold(); 53 54 57 AbstractImagePrototype createLink(); 58 59 62 AbstractImagePrototype hr(); 63 64 67 AbstractImagePrototype indent(); 68 69 72 AbstractImagePrototype insertImage(); 73 74 77 AbstractImagePrototype italic(); 78 79 82 AbstractImagePrototype justifyCenter(); 83 84 87 AbstractImagePrototype justifyLeft(); 88 89 92 AbstractImagePrototype justifyRight(); 93 94 97 AbstractImagePrototype ol(); 98 99 102 AbstractImagePrototype outdent(); 103 104 107 AbstractImagePrototype removeFormat(); 108 109 112 AbstractImagePrototype removeLink(); 113 114 117 AbstractImagePrototype strikeThrough(); 118 119 122 AbstractImagePrototype subscript(); 123 124 127 AbstractImagePrototype superscript(); 128 129 132 AbstractImagePrototype ul(); 133 134 137 AbstractImagePrototype underline(); 138 } 139 140 144 public interface Strings extends Constants { 145 146 String black(); 147 148 String blue(); 149 150 String bold(); 151 152 String color(); 153 154 String createLink(); 155 156 String font(); 157 158 String green(); 159 160 String hr(); 161 162 String indent(); 163 164 String insertImage(); 165 166 String italic(); 167 168 String justifyCenter(); 169 170 String justifyLeft(); 171 172 String justifyRight(); 173 174 String large(); 175 176 String medium(); 177 178 String normal(); 179 180 String ol(); 181 182 String outdent(); 183 184 String red(); 185 186 String removeFormat(); 187 188 String removeLink(); 189 190 String size(); 191 192 String small(); 193 194 String strikeThrough(); 195 196 String subscript(); 197 198 String superscript(); 199 200 String ul(); 201 202 String underline(); 203 204 String white(); 205 206 String xlarge(); 207 208 String xsmall(); 209 210 String xxlarge(); 211 212 String xxsmall(); 213 214 String yellow(); 215 } 216 217 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 url = Window.prompt("Enter an image URL:", "http://"); 265 if (url != null) { 266 extended.insertImage(url); 267 } 268 } else if (sender == createLink) { 269 String 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 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 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 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 richText.addKeyboardListener(listener); 410 richText.addClickListener(listener); 411 } 412 } 413 414 private ListBox createColorList(String 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 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 tip) { 469 ToggleButton tb = new ToggleButton(img.createImage()); 470 tb.addClickListener(listener); 471 tb.setTitle(tip); 472 return tb; 473 } 474 475 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 |