KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > impl > RichTextAreaImplStandard


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.user.client.ui.impl;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.Element;
20 import com.google.gwt.user.client.ui.RichTextArea;
21 import com.google.gwt.user.client.ui.RichTextArea.FontSize;
22 import com.google.gwt.user.client.ui.RichTextArea.Justification;
23
24 /**
25  * Basic rich text platform implementation.
26  */

27 public class RichTextAreaImplStandard extends RichTextAreaImpl implements
28     RichTextArea.BasicFormatter, RichTextArea.ExtendedFormatter {
29
30   /**
31    * Holds a cached copy of any user setHTML/setText actions until the real
32    * text area is fully initialized. Becomes <code>null</code> after init.
33    */

34   private Element beforeInitPlaceholder = DOM.createDiv();
35
36   public native Element createElement() /*-{
37     return $doc.createElement('iframe');
38   }-*/
;
39
40   public void createLink(String JavaDoc url) {
41     execCommand("CreateLink", url);
42   }
43
44   public String JavaDoc getBackColor() {
45     return queryCommandValue("BackColor");
46   }
47
48   public String JavaDoc getForeColor() {
49     return queryCommandValue("ForeColor");
50   }
51
52   public final String JavaDoc getHTML() {
53     return beforeInitPlaceholder == null ? getHTMLImpl() : DOM.getInnerHTML(beforeInitPlaceholder);
54   }
55
56   public final String JavaDoc getText() {
57     return beforeInitPlaceholder == null ? getTextImpl() : DOM.getInnerText(beforeInitPlaceholder);
58   }
59
60   public native void hookEvents(RichTextArea owner) /*-{
61     this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.__listener = owner;
62   }-*/
;
63
64   public native void initElement() /*-{
65     // Some browsers don't like setting designMode until slightly _after_
66     // the iframe becomes attached to the DOM. Any non-zero timeout will do
67     // just fine.
68     var _this = this;
69     setTimeout(function() {
70       // Turn on design mode.
71       _this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.contentWindow.document.designMode = 'On';
72
73       // Initialize event handling.
74       _this.@com.google.gwt.user.client.ui.impl.RichTextAreaImplStandard::initEvents()();
75       
76       // Send notification that the iframe has reached design mode.
77       _this.@com.google.gwt.user.client.ui.impl.RichTextAreaImplStandard::onElementInitialized()();
78     }, 1);
79   }-*/
;
80
81   public void insertHorizontalRule() {
82     execCommand("InsertHorizontalRule", null);
83   }
84
85   public void insertImage(String JavaDoc url) {
86     execCommand("InsertImage", url);
87   }
88
89   public void insertOrderedList() {
90     execCommand("InsertOrderedList", null);
91   }
92
93   public void insertUnorderedList() {
94     execCommand("InsertUnorderedList", null);
95   }
96
97   public boolean isBasicEditingSupported() {
98     return true;
99   }
100
101   public boolean isBold() {
102     return queryCommandState("Bold");
103   }
104
105   public boolean isExtendedEditingSupported() {
106     return true;
107   }
108
109   public boolean isItalic() {
110     return queryCommandState("Italic");
111   }
112
113   public boolean isStrikethrough() {
114     return queryCommandState("Strikethrough");
115   }
116
117   public boolean isSubscript() {
118     return queryCommandState("Subscript");
119   }
120
121   public boolean isSuperscript() {
122     return queryCommandState("Superscript");
123   }
124
125   public boolean isUnderlined() {
126     return queryCommandState("Underline");
127   }
128
129   public void leftIndent() {
130     execCommand("Outdent", null);
131   }
132
133   public void removeFormat() {
134     execCommand("RemoveFormat", null);
135   }
136
137   public void removeLink() {
138     execCommand("Unlink", "false");
139   }
140
141   public void rightIndent() {
142     execCommand("Indent", null);
143   }
144
145   public void selectAll() {
146     execCommand("SelectAll", null);
147   }
148
149   public void setBackColor(String JavaDoc color) {
150     execCommand("BackColor", color);
151   }
152
153   public native void setFocus(boolean focused) /*-{
154     if (focused) {
155       this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.contentWindow.focus();
156     } else {
157       this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.contentWindow.blur();
158     }
159   }-*/
;
160
161   public void setFontName(String JavaDoc name) {
162     execCommand("FontName", name);
163   }
164
165   public void setFontSize(FontSize fontSize) {
166     execCommand("FontSize", Integer.toString(fontSize.getNumber()));
167   }
168
169   public void setForeColor(String JavaDoc color) {
170     execCommand("ForeColor", color);
171   }
172
173   public final void setHTML(String JavaDoc html) {
174     if (beforeInitPlaceholder == null) {
175       setHTMLImpl(html);
176     } else {
177       DOM.setInnerHTML(beforeInitPlaceholder, html);
178     }
179   }
180
181   public void setJustification(Justification justification) {
182     if (justification == Justification.CENTER) {
183       execCommand("JustifyCenter", null);
184     } else if (justification == Justification.LEFT) {
185       execCommand("JustifyLeft", null);
186     } else if (justification == Justification.RIGHT) {
187       execCommand("JustifyRight", null);
188     }
189   }
190
191   public final void setText(String JavaDoc text) {
192     if (beforeInitPlaceholder == null) {
193       setTextImpl(text);
194     } else {
195       DOM.setInnerText(beforeInitPlaceholder, text);
196     }
197   }
198
199   public void toggleBold() {
200     execCommand("Bold", "false");
201   }
202
203   public void toggleItalic() {
204     execCommand("Italic", "false");
205   }
206
207   public void toggleStrikethrough() {
208     execCommand("Strikethrough", "false");
209   }
210
211   public void toggleSubscript() {
212     execCommand("Subscript", "false");
213   }
214
215   public void toggleSuperscript() {
216     execCommand("Superscript", "false");
217   }
218
219   public void toggleUnderline() {
220     execCommand("Underline", "False");
221   }
222
223   protected native String JavaDoc getHTMLImpl() /*-{
224     return this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.contentWindow.document.body.innerHTML;
225   }-*/
;
226
227   protected native String JavaDoc getTextImpl() /*-{
228     return this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.contentWindow.document.body.textContent;
229   }-*/
;
230
231   protected native void setHTMLImpl(String JavaDoc html) /*-{
232     this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.contentWindow.document.body.innerHTML = html;
233   }-*/
;
234
235   protected native void setTextImpl(String JavaDoc text) /*-{
236     this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.contentWindow.document.body.textContent = text;
237   }-*/
;
238
239   void execCommand(String JavaDoc cmd, String JavaDoc param) {
240     if (isRichEditingActive(elem)) {
241       // When executing a command, focus the iframe first, since some commands
242
// don't take properly when it's not focused.
243
setFocus(true);
244       execCommandAssumingFocus(cmd, param);
245     }
246   }
247
248   native void execCommandAssumingFocus(String JavaDoc cmd, String JavaDoc param) /*-{
249     this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.contentWindow.document.execCommand(cmd, false, param);
250   }-*/
;
251
252   native void initEvents() /*-{
253     var elem = this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem;
254     var handler = function(evt) {
255       if (elem.__listener) {
256         elem.__listener.@com.google.gwt.user.client.ui.RichTextArea::onBrowserEvent(Lcom/google/gwt/user/client/Event;)(evt);
257       }
258     };
259
260     var wnd = elem.contentWindow;
261     wnd.addEventListener('keydown', handler, true);
262     wnd.addEventListener('keyup', handler, true);
263     wnd.addEventListener('keypress', handler, true);
264     wnd.addEventListener('mousedown', handler, true);
265     wnd.addEventListener('mouseup', handler, true);
266     wnd.addEventListener('mousemove', handler, true);
267     wnd.addEventListener('mouseover', handler, true);
268     wnd.addEventListener('mouseout', handler, true);
269     wnd.addEventListener('click', handler, true);
270   }-*/
;
271
272   native boolean isRichEditingActive(Element e) /*-{
273     return ((e.contentWindow.document.designMode).toUpperCase()) == 'ON';
274   }-*/
;
275
276   void onElementInitialized() {
277     // When the iframe is ready, ensure cached content is set.
278
if (beforeInitPlaceholder != null) {
279       setHTMLImpl(DOM.getInnerHTML(beforeInitPlaceholder));
280       beforeInitPlaceholder = null;
281     }
282   }
283
284   boolean queryCommandState(String JavaDoc cmd) {
285     if (isRichEditingActive(elem)) {
286       // When executing a command, focus the iframe first, since some commands
287
// don't take properly when it's not focused.
288
setFocus(true);
289       return queryCommandStateAssumingFocus(cmd);
290     } else {
291       return false;
292     }
293   }
294
295   native boolean queryCommandStateAssumingFocus(String JavaDoc cmd) /*-{
296     return !!this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.contentWindow.document.queryCommandState(cmd);
297   }-*/
;
298
299   String JavaDoc queryCommandValue(String JavaDoc cmd) {
300     // When executing a command, focus the iframe first, since some commands
301
// don't take properly when it's not focused.
302
setFocus(true);
303     return queryCommandValueAssumingFocus(cmd);
304   }
305
306   native String JavaDoc queryCommandValueAssumingFocus(String JavaDoc cmd) /*-{
307     return this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.contentWindow.document.queryCommandValue(cmd);
308   }-*/
;
309 }
310
Popular Tags