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.text;
021:
022: import java.awt.Component;
023: import java.awt.Container;
024: import java.awt.Dimension;
025: import java.awt.Graphics;
026: import java.awt.LayoutManager;
027: import java.awt.Rectangle;
028: import java.awt.Shape;
029: import java.util.HashMap;
030:
031: import javax.swing.text.Position.Bias;
032:
033: import org.apache.harmony.awt.text.TextUtils;
034:
035: import org.apache.harmony.x.swing.internal.nls.Messages;
036:
037: public class ComponentView extends View {
038:
039: private static final class ComponentViewLayout implements
040: LayoutManager {
041: private static final HashMap componentViews = new HashMap();
042:
043: public void layoutContainer(final Container c) {
044: for (int i = 0; i < c.getComponentCount(); i++) {
045: Component child = c.getComponent(i);
046: View componentView = (View) componentViews.get(child);
047: if (componentView == null) {
048: break;
049: }
050: Shape bounds1 = null;
051: Shape bounds2 = null;
052: try {
053: final JTextComponent textComponent = (JTextComponent) c;
054: bounds1 = textComponent.modelToView(componentView
055: .getStartOffset());
056: bounds2 = textComponent.modelToView(componentView
057: .getEndOffset());
058: } catch (BadLocationException e) {
059: }
060: if (bounds1 == null || bounds2 == null) {
061: return;
062: }
063: Rectangle bounds = ((Rectangle) bounds1)
064: .union((Rectangle) bounds2);
065: child.setBounds(bounds.x, bounds.y, bounds.width,
066: bounds.height);
067: }
068: }
069:
070: public Dimension minimumLayoutSize(Container c) {
071: return null;
072: }
073:
074: public Dimension preferredLayoutSize(Container c) {
075: return null;
076: }
077:
078: public void addLayoutComponent(String name, Component c) {
079: }
080:
081: public void removeLayoutComponent(Component c) {
082: }
083: }
084:
085: private static final int EMPTY_SPAN = 0;
086: private Component component;
087:
088: public ComponentView(final Element element) {
089: super (element);
090: }
091:
092: public final Component getComponent() {
093: return component;
094: }
095:
096: public float getPreferredSpan(final int axis) {
097: isAxisValid(axis);
098: if (getParent() != null) {
099: if (axis == View.X_AXIS) {
100: return component.getPreferredSize().width + 2;
101: }
102: return component.getPreferredSize().height;
103: }
104: return EMPTY_SPAN;
105: }
106:
107: public float getMinimumSpan(final int axis) {
108: isAxisValid(axis);
109: if (getParent() != null) {
110: if (axis == View.X_AXIS) {
111: return component.getMinimumSize().width + 2;
112: }
113: return component.getMinimumSize().height;
114: }
115: return EMPTY_SPAN;
116: }
117:
118: public float getMaximumSpan(final int axis) {
119: isAxisValid(axis);
120: if (getParent() != null) {
121: if (axis == View.X_AXIS) {
122: return component.getMaximumSize().width + 2;
123: }
124: return component.getMaximumSize().height;
125: }
126: return EMPTY_SPAN;
127: }
128:
129: public float getAlignment(final int axis) {
130: if (component != null) {
131: if (axis == View.X_AXIS) {
132: return component.getAlignmentX();
133: }
134: if (axis == View.Y_AXIS) {
135: return component.getAlignmentY();
136: }
137: }
138: return View.ALIGN_CENTER;
139: }
140:
141: public Shape modelToView(final int pos, final Shape shape,
142: final Bias bias) throws BadLocationException {
143: return TextUtils.modelToIconOrComponentView(this , pos, shape,
144: bias);
145: }
146:
147: public int viewToModel(final float x, final float y,
148: final Shape shape, final Bias[] biasReturn) {
149:
150: final Rectangle bounds = shape.getBounds();
151: if (x > bounds.width / 2 + bounds.x - 1) {
152: biasReturn[0] = Position.Bias.Backward;
153: return getEndOffset();
154: }
155: biasReturn[0] = Position.Bias.Forward;
156: return getStartOffset();
157: }
158:
159: public void setParent(final View parent) {
160: if (parent == null) {
161: if (component != null && component.getParent() != null) {
162: component.getParent().remove(component);
163: }
164: super .setParent(parent);
165: } else {
166: if (getParent() == null) {
167: super .setParent(parent);
168: if (component == null) {
169: component = createComponent();
170: ComponentViewLayout.componentViews.put(component,
171: this );
172: }
173: }
174: final Container container = getContainer();
175: if (container != null) {
176: container.add(component);
177: if (container.getLayout() == null) {
178: container.setLayout(new ComponentViewLayout());
179: }
180: }
181: }
182: }
183:
184: public void paint(final Graphics g, final Shape shape) {
185: }
186:
187: protected Component createComponent() {
188: return StyleConstants.getComponent(getAttributes());
189: }
190:
191: private void isAxisValid(final int axis) {
192: if (axis != X_AXIS && axis != Y_AXIS) {
193: throw new IllegalArgumentException(Messages.getString(
194: "swing.00", axis)); //$NON-NLS-1$
195: }
196: }
197:
198: }
|