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 Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Component;
023: import java.awt.Container;
024: import java.awt.Graphics;
025: import java.awt.Shape;
026: import javax.swing.event.DocumentEvent;
027: import javax.swing.text.Position.Bias;
028:
029: import org.apache.harmony.awt.ComponentInternals;
030: import org.apache.harmony.awt.text.RootViewContext;
031:
032: class RootView extends View {
033: private View son;
034: private Document document;
035: private RootViewContext.ViewFactoryGetter viewFactoryGetter;
036:
037: /**
038: * Parent of the view hierarchy. If model changes, only child of root view
039: * is replaced.
040: */
041: RootViewContext rootViewContext = new RootViewContext() {
042: public View getView() {
043: return RootView.this ;
044: }
045:
046: public void setComponent(final Component c) {
047: component = c;
048: }
049:
050: public void setDocument(final Document d) {
051: document = d;
052: }
053:
054: public void setViewFactoryGetter(final ViewFactoryGetter vfg) {
055: viewFactoryGetter = vfg;
056: }
057:
058: };
059:
060: RootView(final Element e) {
061: super (e);
062: }
063:
064: public void preferenceChanged(final View view, final boolean b1,
065: final boolean b2) {
066: if (component != null && (b1 || b2)) {
067: ComponentInternals.getComponentInternals().getTextKit(
068: component).revalidate();
069: }
070: }
071:
072: public Shape getChildAllocation(final int i, final Shape shape) {
073: return shape;
074: }
075:
076: public int getResizeWeight(final int i) {
077: int resizeWeight;
078: readLock();
079: try {
080: resizeWeight = son.getResizeWeight(i);
081: } finally {
082: readUnlock();
083: }
084: return resizeWeight;
085: }
086:
087: public int getNextVisualPositionFrom(final int p, final Bias b,
088: final Shape shape, final int direction, final Bias[] biasRet)
089: throws BadLocationException {
090: int position;
091: readLock();
092: try {
093: position = (son != null) ? son.getNextVisualPositionFrom(p,
094: b, shape, direction, biasRet) : 0;
095: } finally {
096: readUnlock();
097: }
098: return position;
099: }
100:
101: public float getMaximumSpan(final int axis) {
102: //son.getMaximumSpan() sometimes returns very strange things
103: //So getMaximumSpan(View.Y_AXIS) may be more than
104: //getMinimumSpan(View.Y_AXIS) (TextComponentDemo, for example).
105: return Integer.MAX_VALUE;
106: }
107:
108: public float getMinimumSpan(final int axis) {
109: float minSpan;
110: readLock();
111: try {
112: minSpan = (son != null) ? son.getMinimumSpan(axis) : 10;
113: } finally {
114: readUnlock();
115: }
116: return minSpan;
117: }
118:
119: public Element getElement() {
120: return son.getElement();
121: }
122:
123: public int getEndOffset() {
124: int result;
125: readLock();
126: try {
127: result = (son != null) ? son.getEndOffset() : 0;
128: } finally {
129: readUnlock();
130: }
131: return result;
132: }
133:
134: public int getStartOffset() {
135: int result;
136: readLock();
137: try {
138: result = (son != null) ? son.getStartOffset() : 0;
139: } finally {
140: readUnlock();
141: }
142: return result;
143: }
144:
145: public Document getDocument() {
146: return document;
147: }
148:
149: public ViewFactory getViewFactory() {
150: return viewFactoryGetter.getViewFactory();
151: }
152:
153: public View getParent() {
154: return null;
155: }
156:
157: public float getPreferredSpan(final int axis) {
158: float prefSpan;
159: readLock();
160: try {
161: prefSpan = (son != null) ? son.getPreferredSpan(axis) : 10;
162: } finally {
163: readUnlock();
164: }
165: return prefSpan;
166: }
167:
168: public void insertUpdate(final DocumentEvent changes,
169: final Shape a, final ViewFactory f) {
170: if (son != null) {
171: son.insertUpdate(changes, a, f);
172: }
173: }
174:
175: public Shape modelToView(final int p, final Shape shape,
176: final Position.Bias b) throws BadLocationException {
177: Shape sh = null;
178: readLock();
179: try {
180: sh = (son != null) ? son.modelToView(p, shape, b) : null;
181: } finally {
182: readUnlock();
183: }
184: return sh;
185: }
186:
187: public Shape modelToView(final int p0, final Bias b0, final int p1,
188: final Bias b1, final Shape shape)
189: throws BadLocationException {
190: Shape sh = null;
191: readLock();
192: try {
193: sh = (son != null) ? son.modelToView(p0, b0, p1, b1, shape)
194: : null;
195: } finally {
196: readUnlock();
197: }
198: return sh;
199: }
200:
201: public void paint(final Graphics g, final Shape a) {
202: if (son != null) {
203: son.paint(g, a);
204: }
205: }
206:
207: public void removeUpdate(final DocumentEvent changes,
208: final Shape a, final ViewFactory f) {
209: son.removeUpdate(changes, a, f);
210: }
211:
212: public void changedUpdate(final DocumentEvent changes,
213: final Shape a, final ViewFactory f) {
214: son.changedUpdate(changes, a, f);
215: }
216:
217: public void setSize(final float width, final float height) {
218: if (son == null) {
219: return;
220: }
221: readLock();
222: try {
223: son.setSize(width, height);
224: } finally {
225: readUnlock();
226: }
227: }
228:
229: public int viewToModel(final float fx, final float fy,
230: final Shape a, final Position.Bias[] bias) {
231: int offset;
232: readLock();
233: try {
234: offset = (son != null) ? son.viewToModel(fx, fy, a, bias)
235: : 0;
236: } finally {
237: readUnlock();
238: }
239: return offset;
240: }
241:
242: public Container getContainer() {
243: if (component instanceof Container) {
244: return (Container) component;
245: }
246:
247: return null;
248: }
249:
250: Component getComponent() {
251: return component;
252: }
253:
254: public void replace(final int i1, final int i2, final View[] views) {
255: if (son != null) {
256: son.setParent(null);
257: }
258: son = views[0];
259: son.setParent(this );
260: }
261:
262: public int getViewCount() {
263: return 1;
264: }
265:
266: public View getView(final int index) {
267: return son;
268: }
269:
270: public AttributeSet getAttributes() {
271: return null;
272: }
273:
274: public String getToolTipText(final float x, final float y,
275: final Shape shape) {
276: String text = null;
277: readLock();
278: try {
279: text = (son != null) ? son.getToolTipText(x, y, shape)
280: : null;
281: } finally {
282: readUnlock();
283: }
284: return text;
285: }
286:
287: private void readLock() {
288: if (document instanceof AbstractDocument) {
289: ((AbstractDocument) document).readLock();
290: }
291: }
292:
293: private void readUnlock() {
294: if (document instanceof AbstractDocument) {
295: ((AbstractDocument) document).readUnlock();
296: }
297: }
298: }
|