KickJava   Java API By Example, From Geeks To Geeks.

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


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.Element;
19 import com.google.gwt.user.client.ui.RichTextArea.FontSize;
20
21 /**
22  * Safari rich text platform implementation.
23  */

24 public class RichTextAreaImplSafari extends RichTextAreaImplStandard {
25
26   private static final String JavaDoc[] sizeNumberCSSValues = new String JavaDoc[] {
27       "medium", "xx-small", "x-small", "small", "medium", "large", "x-large",
28       "xx-large"};
29
30   public Element createElement() {
31     Element elem = super.createElement();
32
33     // Use this opportunity to check if this version of Safari has full rich
34
// text support or not.
35
capabilityTest(elem);
36     return elem;
37   }
38
39   public native boolean isBold() /*-{
40     return !!this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.__gwt_isBold;
41   }-*/
;
42
43   public native boolean isExtendedEditingSupported() /*-{
44     // __gwt_fullSupport is set in testCapability().
45     return this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.__gwt_fullSupport;
46   }-*/
;
47
48   public native boolean isItalic() /*-{
49     return !!this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.__gwt_isItalic;
50   }-*/
;
51
52   public native boolean isUnderlined() /*-{
53     return !!this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.__gwt_isUnderlined;
54   }-*/
;
55
56   public native void setFocus(boolean focused) /*-{
57     // Safari needs the *iframe* focused, not its window.
58     if (focused) {
59       this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.focus();
60       this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.__gwt_restoreSelection();
61     } else {
62       this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.blur();
63     }
64   }-*/
;
65
66   public void setFontSize(FontSize fontSize) {
67     // Safari only accepts css-style 'small, medium, large, etc' values.
68
int number = fontSize.getNumber();
69     if ((number >= 0) && (number <= 7)) {
70       execCommand("FontSize", sizeNumberCSSValues[number]);
71       return;
72     }
73   }
74
75   protected native String JavaDoc getTextImpl() /*-{
76     return this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.contentWindow.document.body.innerText;
77   }-*/
;
78
79   protected native void setTextImpl(String JavaDoc text) /*-{
80     this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.contentWindow.document.body.innerText = text;
81   }-*/
;
82
83   native void initEvents() /*-{
84     var elem = this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem;
85     var wnd = elem.contentWindow;
86     var doc = wnd.document;
87
88     // Create an expando on the element to hold the selection state.
89     elem.__gwt_selection = { baseOffset:0, extentOffset:0, baseNode:null, extentNode:null };
90
91     // A function for restoring the selection state.
92     elem.__gwt_restoreSelection = function() {
93       var sel = elem.__gwt_selection;
94       wnd.getSelection().setBaseAndExtent(sel.baseNode, sel.baseOffset, sel.extentNode, sel.extentOffset);
95     };
96
97     // Generic event dispatcher. Also stores selection state.
98     var handler = function(evt) {
99       // Store the editor's selection state.
100       var s = wnd.getSelection();
101       elem.__gwt_selection = {
102         baseOffset:s.baseOffset,
103         extentOffset:s.extentOffset,
104
105         baseNode:s.baseNode,
106         extentNode:s.extentNode
107       };
108
109       // Hang on to bold/italic/underlined states.
110       elem.__gwt_isBold = doc.queryCommandState('Bold');
111       elem.__gwt_isItalic = doc.queryCommandState('Italic');
112       elem.__gwt_isUnderlined = doc.queryCommandState('Underline');
113
114       // Dispatch the event.
115       if (elem.__listener) {
116         elem.__listener.
117         @com.google.gwt.user.client.ui.RichTextArea::onBrowserEvent(Lcom/google/gwt/user/client/Event;)(evt);
118       }
119     };
120
121     wnd.addEventListener('keydown', handler, true);
122     wnd.addEventListener('keyup', handler, true);
123     wnd.addEventListener('keypress', handler, true);
124     wnd.addEventListener('mousedown', handler, true);
125     wnd.addEventListener('mouseup', handler, true);
126     wnd.addEventListener('mousemove', handler, true);
127     wnd.addEventListener('mouseover', handler, true);
128     wnd.addEventListener('mouseout', handler, true);
129     wnd.addEventListener('click', handler, true);
130   }-*/
;
131
132   private native void capabilityTest(Element elem) /*-{
133     elem.__gwt_fullSupport = $doc.queryCommandSupported('insertimage');
134   }-*/
;
135 }
136
Popular Tags