01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Roman I. Chernyatchik
19: * @version $Revision$
20: */package javax.swing.text;
21:
22: import java.awt.Graphics;
23: import java.awt.Rectangle;
24: import java.awt.Shape;
25:
26: import javax.swing.Icon;
27: import javax.swing.text.Position.Bias;
28:
29: import org.apache.harmony.awt.text.TextUtils;
30:
31: import org.apache.harmony.x.swing.internal.nls.Messages;
32:
33: public class IconView extends View {
34:
35: public IconView(final Element element) {
36: super (element);
37: }
38:
39: public float getAlignment(final int axis) {
40: return axis == Y_AXIS ? View.ALIGN_RIGHT : View.ALIGN_CENTER;
41: }
42:
43: public float getPreferredSpan(final int axis) {
44: isAxisValid(axis);
45: final Icon icon = (Icon) getAttributes().getAttribute(
46: StyleConstants.IconAttribute);
47: return axis == View.X_AXIS ? icon.getIconWidth() + 2 : icon
48: .getIconHeight();
49: }
50:
51: public Shape modelToView(final int pos, final Shape shape,
52: final Bias bias) throws BadLocationException {
53:
54: return TextUtils.modelToIconOrComponentView(this , pos, shape,
55: bias);
56: }
57:
58: public int viewToModel(final float x, final float y,
59: final Shape shape, final Bias[] biasReturn) {
60:
61: final Rectangle bounds = shape.getBounds();
62: if (x > bounds.width / 2 + bounds.x - 1) {
63: biasReturn[0] = Position.Bias.Backward;
64: return getEndOffset();
65: }
66: biasReturn[0] = Position.Bias.Forward;
67: return getStartOffset();
68: }
69:
70: public void paint(final Graphics g, final Shape shape) {
71:
72: final Rectangle bounds = shape.getBounds();
73: StyleConstants.getIcon(getElement().getAttributes()).paintIcon(
74: getComponent(), g, bounds.x + 1, bounds.y);
75: }
76:
77: private void isAxisValid(final int axis) {
78: if (axis != X_AXIS && axis != Y_AXIS) {
79: throw new IllegalArgumentException(Messages.getString(
80: "swing.00", axis)); //$NON-NLS-1$
81: }
82: }
83: }
|