001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Roman I. Chernyatchik
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Component;
023: import java.awt.MenuContainer;
024: import java.awt.image.ImageObserver;
025: import java.io.Serializable;
026:
027: import javax.accessibility.Accessible;
028: import javax.swing.text.AbstractDocument;
029: import javax.swing.text.AttributeSet;
030: import javax.swing.text.BadLocationException;
031: import javax.swing.text.DefaultEditorKit;
032: import javax.swing.text.Document;
033: import javax.swing.text.EditorKit;
034: import javax.swing.text.MutableAttributeSet;
035: import javax.swing.text.SimpleAttributeSet;
036: import javax.swing.text.Style;
037: import javax.swing.text.StyleConstants;
038: import javax.swing.text.StyledDocument;
039: import javax.swing.text.StyledEditorKit;
040:
041: import org.apache.harmony.awt.text.TextUtils;
042:
043: import org.apache.harmony.x.swing.internal.nls.Messages;
044:
045: public class JTextPane extends JEditorPane implements ImageObserver,
046: MenuContainer, Serializable, Accessible, Scrollable {
047:
048: private static final String uiClassID = "TextPaneUI";
049:
050: public JTextPane() {
051: setEditorKit(new StyledEditorKit());
052: }
053:
054: public JTextPane(final StyledDocument doc) {
055: this ();
056:
057: if (doc == null) {
058: throw new NullPointerException();
059: }
060: }
061:
062: public String getUIClassID() {
063: return uiClassID;
064: }
065:
066: public void setDocument(final Document doc) {
067: if (doc instanceof StyledDocument) {
068: super .setDocument(doc);
069: } else {
070: throw new IllegalArgumentException(Messages.getString(
071: "swing.48", "StyledDocument")); //$NON-NLS-1$ //$NON-NLS-2$
072: }
073: }
074:
075: public void setStyledDocument(final StyledDocument doc) {
076: super .setDocument(doc);
077: }
078:
079: public StyledDocument getStyledDocument() {
080: return (StyledDocument) getDocument();
081: }
082:
083: /**
084: * This method differs from JEditorPane.replaceSelection only in one case.
085: * If there is selection the replacement text should have not the attributes
086: * currently defined for input, but the attributes of the first selected
087: * symbol.
088: */
089: public void replaceSelection(final String content) {
090: if (!isEditable()) {
091: new DefaultEditorKit.BeepAction().actionPerformed(null);
092: return;
093: }
094:
095: final int start = getSelectionStart();
096: final int end = getSelectionEnd();
097: final StyledDocument doc = getStyledDocument();
098:
099: AttributeSet attrs;
100:
101: try {
102: if (start != end) {
103: attrs = doc.getCharacterElement(start).getAttributes();
104:
105: doc.remove(start, end - start);
106:
107: if (StyleConstants.getIcon(attrs) != null) {
108: final MutableAttributeSet newAttrs = new SimpleAttributeSet(
109: attrs);
110: newAttrs
111: .removeAttribute(StyleConstants.IconAttribute);
112: newAttrs
113: .removeAttribute(AbstractDocument.ElementNameAttribute);
114: attrs = newAttrs;
115: }
116: if (StyleConstants.getComponent(attrs) != null) {
117: final MutableAttributeSet newAttrs = new SimpleAttributeSet(
118: attrs);
119: newAttrs
120: .removeAttribute(StyleConstants.ComponentAttribute);
121: newAttrs
122: .removeAttribute(AbstractDocument.ElementNameAttribute);
123: attrs = newAttrs;
124: }
125:
126: } else {
127: attrs = getInputAttributes();
128: }
129:
130: if (content != null) {
131: doc.insertString(start, content, attrs);
132: }
133: } catch (BadLocationException e) {
134: }
135: }
136:
137: /**
138: * To insert component we should insert in the document whitespace with
139: * special attribute StyleConstants.ComponentAttribute
140: */
141: public synchronized void insertComponent(final Component c) {
142: final MutableAttributeSet attrs = new SimpleAttributeSet();
143: StyleConstants.setComponent(attrs, c);
144:
145: replaceObject(getStyledDocument(), getSelectionStart(),
146: getSelectionEnd(), attrs);
147: }
148:
149: /**
150: * To insert icon we should insert in the document whitespace with
151: * special attribute StyleConstants.IconAttribute
152: */
153: public synchronized void insertIcon(final Icon g) {
154: final MutableAttributeSet attrs = new SimpleAttributeSet();
155: StyleConstants.setIcon(attrs, g);
156:
157: replaceObject(getStyledDocument(), getSelectionStart(),
158: getSelectionEnd(), attrs);
159: }
160:
161: public Style addStyle(final String styleName, final Style parent) {
162: return getStyledDocument().addStyle(styleName, parent);
163: }
164:
165: public void removeStyle(final String styleName) {
166: getStyledDocument().removeStyle(styleName);
167: }
168:
169: public Style getStyle(final String styleName) {
170: return getStyledDocument().getStyle(styleName);
171: }
172:
173: public void setLogicalStyle(final Style s) {
174: getStyledDocument().setLogicalStyle(getCaretPosition(), s);
175: }
176:
177: public Style getLogicalStyle() {
178: final int position = getCaretPosition();
179: if (position < 0) {
180: return null;
181: }
182: return getStyledDocument().getLogicalStyle(position);
183: }
184:
185: public AttributeSet getCharacterAttributes() {
186: final int position = getCaretPosition();
187: if (position < 0) {
188: return null;
189: }
190: return this .getStyledDocument().getCharacterElement(position)
191: .getAttributes();
192: }
193:
194: public synchronized void setCharacterAttributes(
195: final AttributeSet attr, final boolean replace) {
196:
197: TextUtils.setCharacterAttributes(attr, replace, this ,
198: getStyledDocument(), getInputAttributes());
199: }
200:
201: public AttributeSet getParagraphAttributes() {
202: final int position = getCaretPosition();
203: if (position < 0) {
204: return null;
205: }
206: return getStyledDocument().getParagraphElement(position)
207: .getAttributes();
208: }
209:
210: public synchronized void setParagraphAttributes(
211: final AttributeSet attr, final boolean replace) {
212:
213: TextUtils.setParagraphAttributes(attr, replace, this ,
214: getStyledDocument());
215: }
216:
217: public final void setEditorKit(final EditorKit kit) {
218: if (kit instanceof StyledEditorKit) {
219: super .setEditorKit(kit);
220: } else {
221: throw new IllegalArgumentException(Messages.getString(
222: "swing.49", "StyledEditorKit")); //$NON-NLS-1$ //$NON-NLS-2$
223: }
224: }
225:
226: public MutableAttributeSet getInputAttributes() {
227: return getStyledEditorKit().getInputAttributes();
228: }
229:
230: protected EditorKit createDefaultEditorKit() {
231: return new StyledEditorKit();
232: }
233:
234: protected final StyledEditorKit getStyledEditorKit() {
235: return (StyledEditorKit) getEditorKit();
236: }
237:
238: private void replaceObject(final StyledDocument doc,
239: final int selectStart, final int selectEnd,
240: final MutableAttributeSet attrs) {
241: try {
242: if (selectStart != selectEnd) {
243: doc.remove(selectStart, selectEnd - selectStart);
244: }
245: doc.insertString(selectStart, " ", attrs);
246: } catch (BadLocationException e) {
247: }
248: }
249: }
|