001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.core.client.GWT;
019: import com.google.gwt.user.client.DOM;
020: import com.google.gwt.user.client.Event;
021: import com.google.gwt.user.client.ui.impl.RichTextAreaImpl;
022:
023: /**
024: * A rich text editor that allows complex styling and formatting.
025: *
026: * Because some browsers do not support rich text editing, and others support
027: * only a limited subset of functionality, there are two formatter interfaces,
028: * accessed via {@link #getBasicFormatter()} and {@link #getExtendedFormatter()}.
029: * A browser that does not support rich text editing at all will return
030: * <code>null</code> for both of these, while one that supports only the basic
031: * functionality will return <code>null</code> for the latter.
032: *
033: * <p>
034: * <img class='gallery' src='RichTextArea.png'/>
035: * </p>
036: *
037: * <h3>CSS Style Rules</h3>
038: * <ul class="css">
039: * <li>.gwt-RichTextArea { }</li>
040: * </ul>
041: */
042: public class RichTextArea extends FocusWidget implements HasHTML,
043: SourcesMouseEvents {
044:
045: /**
046: * This interface is used to access basic formatting options, when available.
047: * If the implementation supports basic formatting, then
048: * {@link RichTextArea#getBasicFormatter()} will return an instance of this
049: * class.
050: */
051: public interface BasicFormatter {
052:
053: /**
054: * Gets the background color.
055: *
056: * @return the background color
057: */
058: String getBackColor();
059:
060: /**
061: * Gets the foreground color.
062: *
063: * @return the foreground color
064: */
065: String getForeColor();
066:
067: /**
068: * Is the current region bold?
069: *
070: * @return true if the current region is bold
071: */
072: boolean isBold();
073:
074: /**
075: * Is the current region italic?
076: *
077: * @return true if the current region is italic
078: */
079: boolean isItalic();
080:
081: /**
082: * Is the current region subscript?
083: *
084: * @return true if the current region is subscript
085: */
086: boolean isSubscript();
087:
088: /**
089: * Is the current region superscript?
090: *
091: * @return true if the current region is superscript
092: */
093: boolean isSuperscript();
094:
095: /**
096: * Is the current region underlined?
097: *
098: * @return true if the current region is underlined
099: */
100: boolean isUnderlined();
101:
102: /**
103: * Selects all the text.
104: */
105: void selectAll();
106:
107: /**
108: * Sets the background color.
109: *
110: * @param color the new background color
111: */
112: void setBackColor(String color);
113:
114: /**
115: * Sets the font name.
116: *
117: * @param name the new font name
118: */
119: void setFontName(String name);
120:
121: /**
122: * Sets the font size.
123: *
124: * @param fontSize the new font size
125: */
126: void setFontSize(FontSize fontSize);
127:
128: /**
129: * Sets the foreground color.
130: *
131: * @param color the new foreground color
132: */
133: void setForeColor(String color);
134:
135: /**
136: * Sets the justification.
137: *
138: * @param justification the new justification
139: */
140: void setJustification(Justification justification);
141:
142: /**
143: * Toggles bold.
144: */
145: void toggleBold();
146:
147: /**
148: * Toggles italic.
149: */
150: void toggleItalic();
151:
152: /**
153: * Toggles subscript.
154: */
155: void toggleSubscript();
156:
157: /**
158: * Toggles superscript.
159: */
160: void toggleSuperscript();
161:
162: /**
163: * Toggles underline.
164: */
165: void toggleUnderline();
166: }
167:
168: /**
169: * This interface is used to access full formatting options, when available.
170: * If the implementation supports full formatting, then
171: * {@link RichTextArea#getExtendedFormatter()} will return an instance of this
172: * class.
173: */
174: public interface ExtendedFormatter extends BasicFormatter {
175:
176: /**
177: * Creates a link to the supplied URL.
178: *
179: * @param url the URL to be linked to
180: */
181: void createLink(String url);
182:
183: /**
184: * Inserts a horizontal rule.
185: */
186: void insertHorizontalRule();
187:
188: /**
189: * Inserts an image element.
190: *
191: * @param url the url of the image to be inserted
192: */
193: void insertImage(String url);
194:
195: /**
196: * Starts an numbered list. Indentation will create nested items.
197: */
198: void insertOrderedList();
199:
200: /**
201: * Starts an bulleted list. Indentation will create nested items.
202: */
203: void insertUnorderedList();
204:
205: /**
206: * Is the current region strikethrough?
207: *
208: * @return true if the current region is strikethrough
209: */
210: boolean isStrikethrough();
211:
212: /**
213: * Left indent.
214: */
215: void leftIndent();
216:
217: /**
218: * Removes all formatting on the selected text.
219: */
220: void removeFormat();
221:
222: /**
223: * Removes any link from the selected text.
224: */
225: void removeLink();
226:
227: /**
228: * Right indent.
229: */
230: void rightIndent();
231:
232: /**
233: * Toggles strikethrough.
234: */
235: void toggleStrikethrough();
236: }
237:
238: /**
239: * Font size enumeration. Represents the seven basic HTML font sizes, as
240: * defined in CSS.
241: */
242: public static class FontSize {
243:
244: /**
245: * Represents an XX-Small font.
246: */
247: public static final FontSize XX_SMALL = new FontSize(1);
248:
249: /**
250: * Represents an X-Small font.
251: */
252: public static final FontSize X_SMALL = new FontSize(2);
253:
254: /**
255: * Represents a Small font.
256: */
257: public static final FontSize SMALL = new FontSize(3);
258:
259: /**
260: * Represents a Medium font.
261: */
262: public static final FontSize MEDIUM = new FontSize(4);
263:
264: /**
265: * Represents a Large font.
266: */
267: public static final FontSize LARGE = new FontSize(5);
268:
269: /**
270: * Represents an X-Large font.
271: */
272: public static final FontSize X_LARGE = new FontSize(6);
273:
274: /**
275: * Represents an XX-Large font.
276: */
277: public static final FontSize XX_LARGE = new FontSize(7);
278:
279: private int number;
280:
281: private FontSize(int number) {
282: this .number = number;
283: }
284:
285: /**
286: * Gets the HTML font number associated with this font size.
287: *
288: * @return an integer from 1 to 7 inclusive
289: */
290: public int getNumber() {
291: return number;
292: }
293:
294: @Override
295: public String toString() {
296: return Integer.toString(number);
297: }
298: }
299:
300: /**
301: * Justification enumeration. The three values are <code>left</code>,
302: * <code>right</code>, <code>center</code>.
303: */
304: public static class Justification {
305:
306: /**
307: * Center justification.
308: */
309: public static final Justification CENTER = new Justification(
310: "Center");
311:
312: /**
313: * Left justification.
314: */
315: public static final Justification LEFT = new Justification(
316: "Left");
317:
318: /**
319: * Right justification.
320: */
321: public static final Justification RIGHT = new Justification(
322: "Right");
323:
324: private String tag;
325:
326: private Justification(String tag) {
327: this .tag = tag;
328: }
329:
330: @Override
331: public String toString() {
332: return "Justify " + tag;
333: }
334: }
335:
336: private RichTextAreaImpl impl = GWT.create(RichTextAreaImpl.class);
337: private MouseListenerCollection mouseListeners;
338:
339: /**
340: * Creates a new, blank {@link RichTextArea} object with no stylesheet.
341: */
342: public RichTextArea() {
343: setElement(impl.getElement());
344: setStyleName("gwt-RichTextArea");
345: }
346:
347: public void addMouseListener(MouseListener listener) {
348: if (mouseListeners == null) {
349: mouseListeners = new MouseListenerCollection();
350: }
351: mouseListeners.add(listener);
352: }
353:
354: /**
355: * Gets the basic rich text formatting interface.
356: *
357: * @return <code>null</code> if basic formatting is not supported
358: */
359: public BasicFormatter getBasicFormatter() {
360: if ((impl instanceof BasicFormatter)
361: && (impl.isBasicEditingSupported())) {
362: return (BasicFormatter) impl;
363: }
364: return null;
365: }
366:
367: /**
368: * Gets the full rich text formatting interface.
369: *
370: * @return <code>null</code> if full formatting is not supported
371: */
372: public ExtendedFormatter getExtendedFormatter() {
373: if ((impl instanceof ExtendedFormatter)
374: && (impl.isExtendedEditingSupported())) {
375: return (ExtendedFormatter) impl;
376: }
377: return null;
378: }
379:
380: public String getHTML() {
381: return impl.getHTML();
382: }
383:
384: public String getText() {
385: return impl.getText();
386: }
387:
388: @Override
389: public void onBrowserEvent(Event event) {
390: switch (DOM.eventGetType(event)) {
391: case Event.ONMOUSEDOWN:
392: case Event.ONMOUSEUP:
393: case Event.ONMOUSEMOVE:
394: case Event.ONMOUSEOVER:
395: case Event.ONMOUSEOUT:
396: if (mouseListeners != null) {
397: mouseListeners.fireMouseEvent(this , event);
398: }
399: break;
400:
401: default:
402: // ClickEvents, KeyboardEvents, and FocusEvents
403: super .onBrowserEvent(event);
404: }
405: }
406:
407: public void removeMouseListener(MouseListener listener) {
408: if (mouseListeners != null) {
409: mouseListeners.remove(listener);
410: }
411: }
412:
413: @Override
414: public void setFocus(boolean focused) {
415: // There are different problems on each browser when you try to focus an
416: // unattached rich text iframe, so just cut it off early.
417: if (isAttached()) {
418: impl.setFocus(focused);
419: }
420: }
421:
422: public void setHTML(String html) {
423: impl.setHTML(html);
424: }
425:
426: public void setText(String text) {
427: impl.setText(text);
428: }
429:
430: @Override
431: protected void onAttach() {
432: super .onAttach();
433: impl.initElement();
434: }
435:
436: @Override
437: protected void onDetach() {
438: super.onDetach();
439: impl.uninitElement();
440: }
441: }
|