34 import java.awt.Toolkit;
35 import java.awt.Graphics2D;
36 import java.awt.font.FontRenderContext;
37 import java.awt.font.TextLayout;
38 import java.awt.font.TextAttribute;
39
40 import java.text.*;
41 import javax.swing.JComponent;
42 import javax.swing.SwingConstants;
43 import javax.swing.text.ParagraphView.Row;
44 import sun.swing.SwingUtilities2;
45
46 /**
47 * A collection of methods to deal with various text
48 * related activities.
49 *
50 * @author Timothy Prinzing
51 */
52 public class Utilities {
53 /**
54 * If <code>view</code>'s container is a <code>JComponent</code> it
55 * is returned, after casting.
56 */
57 static JComponent getJComponent(View view) {
58 if (view != null) {
59 Component component = view.getContainer();
60 if (component instanceof JComponent) {
61 return (JComponent)component;
62 }
63 }
64 return null;
65 }
66
67 /**
68 * Draws the given text, expanding any tabs that are contained
69 * using the given tab expansion technique. This particular
70 * implementation renders in a 1.1 style coordinate system
71 * where ints are used and 72dpi is assumed.
72 *
73 * @param s the source of the text
74 * @param x the X origin >= 0
967
968 /*
969 * Convenience function for determining ComponentOrientation. Helps us
970 * avoid having Munge directives throughout the code.
971 */
972 static boolean isLeftToRight( java.awt.Component c ) {
973 return c.getComponentOrientation().isLeftToRight();
974 }
975
976
977 /**
978 * Provides a way to determine the next visually represented model
979 * location that one might place a caret. Some views may not be visible,
980 * they might not be in the same order found in the model, or they just
981 * might not allow access to some of the locations in the model.
982 * <p>
983 * This implementation assumes the views are layed out in a logical
984 * manner. That is, that the view at index x + 1 is visually after
985 * the View at index x, and that the View at index x - 1 is visually
986 * before the View at x. There is support for reversing this behavior
987 * only if the passed in <code>View</code> is an instance of
988 * <code>CompositeView</code>. The <code>CompositeView</code>
989 * must then override the <code>flipEastAndWestAtEnds</code> method.
990 *
991 * @param v View to query
992 * @param pos the position to convert >= 0
993 * @param a the allocated region to render into
994 * @param direction the direction from the current position that can
995 * be thought of as the arrow keys typically found on a keyboard;
996 * this may be one of the following:
997 * <ul>
998 * <li><code>SwingConstants.WEST</code>
999 * <li><code>SwingConstants.EAST</code>
1000 * <li><code>SwingConstants.NORTH</code>
1001 * <li><code>SwingConstants.SOUTH</code>
1002 * </ul>
1003 * @param biasRet an array contain the bias that was checked
1004 * @return the location within the model that best represents the next
1005 * location visual position
1006 * @exception BadLocationException
1007 * @exception IllegalArgumentException if <code>direction</code> is invalid
1008 */
1009 static int getNextVisualPositionFrom(View v, int pos, Position.Bias b,
1010 Shape alloc, int direction,
1011 Position.Bias[] biasRet)
1012 throws BadLocationException {
1013 if (v.getViewCount() == 0) {
1014 // Nothing to do.
1015 return pos;
1016 }
1017 boolean top = (direction == SwingConstants.NORTH ||
1018 direction == SwingConstants.WEST);
1019 int retValue;
1020 if (pos == -1) {
1021 // Start from the first View.
1022 int childIndex = (top) ? v.getViewCount() - 1 : 0;
1023 View child = v.getView(childIndex);
1024 Shape childBounds = v.getChildAllocation(childIndex, alloc);
1025 retValue = child.getNextVisualPositionFrom(pos, b, childBounds,
1026 direction, biasRet);
1027 if (retValue == -1 && !top && v.getViewCount() > 1) {
|
34 import java.awt.Toolkit;
35 import java.awt.Graphics2D;
36 import java.awt.font.FontRenderContext;
37 import java.awt.font.TextLayout;
38 import java.awt.font.TextAttribute;
39
40 import java.text.*;
41 import javax.swing.JComponent;
42 import javax.swing.SwingConstants;
43 import javax.swing.text.ParagraphView.Row;
44 import sun.swing.SwingUtilities2;
45
46 /**
47 * A collection of methods to deal with various text
48 * related activities.
49 *
50 * @author Timothy Prinzing
51 */
52 public class Utilities {
53 /**
54 * If {@code view}'s container is a {@code JComponent} it
55 * is returned, after casting.
56 */
57 static JComponent getJComponent(View view) {
58 if (view != null) {
59 Component component = view.getContainer();
60 if (component instanceof JComponent) {
61 return (JComponent)component;
62 }
63 }
64 return null;
65 }
66
67 /**
68 * Draws the given text, expanding any tabs that are contained
69 * using the given tab expansion technique. This particular
70 * implementation renders in a 1.1 style coordinate system
71 * where ints are used and 72dpi is assumed.
72 *
73 * @param s the source of the text
74 * @param x the X origin >= 0
967
968 /*
969 * Convenience function for determining ComponentOrientation. Helps us
970 * avoid having Munge directives throughout the code.
971 */
972 static boolean isLeftToRight( java.awt.Component c ) {
973 return c.getComponentOrientation().isLeftToRight();
974 }
975
976
977 /**
978 * Provides a way to determine the next visually represented model
979 * location that one might place a caret. Some views may not be visible,
980 * they might not be in the same order found in the model, or they just
981 * might not allow access to some of the locations in the model.
982 * <p>
983 * This implementation assumes the views are layed out in a logical
984 * manner. That is, that the view at index x + 1 is visually after
985 * the View at index x, and that the View at index x - 1 is visually
986 * before the View at x. There is support for reversing this behavior
987 * only if the passed in {@code View} is an instance of
988 * {@code CompositeView}. The {@code CompositeView}
989 * must then override the {@code flipEastAndWestAtEnds} method.
990 *
991 * @param v View to query
992 * @param pos the position to convert >= 0
993 * @param a the allocated region to render into
994 * @param direction the direction from the current position that can
995 * be thought of as the arrow keys typically found on a keyboard;
996 * this may be one of the following:
997 * <ul>
998 * <li>{@code SwingConstants.WEST}
999 * <li>{@code SwingConstants.EAST}
1000 * <li>{@code SwingConstants.NORTH}
1001 * <li>{@code SwingConstants.SOUTH}
1002 * </ul>
1003 * @param biasRet an array contain the bias that was checked
1004 * @return the location within the model that best represents the next
1005 * location visual position
1006 * @exception BadLocationException
1007 * @exception IllegalArgumentException if {@code direction} is invalid
1008 */
1009 static int getNextVisualPositionFrom(View v, int pos, Position.Bias b,
1010 Shape alloc, int direction,
1011 Position.Bias[] biasRet)
1012 throws BadLocationException {
1013 if (v.getViewCount() == 0) {
1014 // Nothing to do.
1015 return pos;
1016 }
1017 boolean top = (direction == SwingConstants.NORTH ||
1018 direction == SwingConstants.WEST);
1019 int retValue;
1020 if (pos == -1) {
1021 // Start from the first View.
1022 int childIndex = (top) ? v.getViewCount() - 1 : 0;
1023 View child = v.getView(childIndex);
1024 Shape childBounds = v.getChildAllocation(childIndex, alloc);
1025 retValue = child.getNextVisualPositionFrom(pos, b, childBounds,
1026 direction, biasRet);
1027 if (retValue == -1 && !top && v.getViewCount() > 1) {
|