📄 flowview.java
字号:
{ int index = -1; int i = 0; for (Iterator it = children.iterator(); it.hasNext(); i++) { View child = (View) it.next(); if (child.getStartOffset() >= pos && child.getEndOffset() < pos) { index = i; break; } } return index; } /** * Throws an AssertionError because it must never be called. LogicalView * only serves as a holder for child views and has no visual * representation. */ public float getPreferredSpan(int axis) { throw new AssertionError("This method must not be called in " + "LogicalView."); } /** * Throws an AssertionError because it must never be called. LogicalView * only serves as a holder for child views and has no visual * representation. */ public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { throw new AssertionError("This method must not be called in " + "LogicalView."); } /** * Throws an AssertionError because it must never be called. LogicalView * only serves as a holder for child views and has no visual * representation. */ public void paint(Graphics g, Shape s) { throw new AssertionError("This method must not be called in " + "LogicalView."); } /** * Throws an AssertionError because it must never be called. LogicalView * only serves as a holder for child views and has no visual * representation. */ public int viewToModel(float x, float y, Shape a, Position.Bias[] b) { throw new AssertionError("This method must not be called in " + "LogicalView."); } /** * Returns the document position that is (visually) nearest to the given * document position <code>pos</code> in the given direction <code>d</code>. * * @param c the text component * @param pos the document position * @param b the bias for <code>pos</code> * @param d the direction, must be either {@link SwingConstants#NORTH}, * {@link SwingConstants#SOUTH}, {@link SwingConstants#WEST} or * {@link SwingConstants#EAST} * @param biasRet an array of {@link Position.Bias} that can hold at least * one element, which is filled with the bias of the return position * on method exit * * @return the document position that is (visually) nearest to the given * document position <code>pos</code> in the given direction * <code>d</code> * * @throws BadLocationException if <code>pos</code> is not a valid offset in * the document model */ public int getNextVisualPositionFrom(JTextComponent c, int pos, Position.Bias b, int d, Position.Bias[] biasRet) throws BadLocationException { assert false : "getNextVisualPositionFrom() must not be called in " + "LogicalView"; return 0; } } /** * The shared instance of FlowStrategy. */ static final FlowStrategy sharedStrategy = new FlowStrategy(); /** * The span of the <code>FlowView</code> that should be flowed. */ protected int layoutSpan; /** * Represents the logical child elements of this view, encapsulated within * one parent view (an instance of a package private <code>LogicalView</code> * class). These will be translated to a set of real views that are then * displayed on screen. This translation is performed by the inner class * {@link FlowStrategy}. */ protected View layoutPool; /** * The <code>FlowStrategy</code> to use for translating between the * logical and physical view. */ protected FlowStrategy strategy; /** * Creates a new <code>FlowView</code> for the given * <code>Element</code> and <code>axis</code>. * * @param element the element that is rendered by this FlowView * @param axis the axis along which the view is tiled, either * <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>, the flow * axis is orthogonal to this one */ public FlowView(Element element, int axis) { super(element, axis); strategy = sharedStrategy; } /** * Returns the axis along which the view should be flowed. This is * orthogonal to the axis along which the boxes are tiled. * * @return the axis along which the view should be flowed */ public int getFlowAxis() { int axis = getAxis(); int flowAxis; if (axis == X_AXIS) flowAxis = Y_AXIS; else flowAxis = X_AXIS; return flowAxis; } /** * Returns the span of the flow for the specified child view. A flow * layout can be shaped by providing different span values for different * child indices. The default implementation returns the entire available * span inside the view. * * @param index the index of the child for which to return the span * * @return the span of the flow for the specified child view */ public int getFlowSpan(int index) { return layoutSpan; } /** * Returns the location along the flow axis where the flow span starts * given a child view index. The flow can be shaped by providing * different values here. * * @param index the index of the child for which to return the flow location * * @return the location along the flow axis where the flow span starts */ public int getFlowStart(int index) { return getLeftInset(); // TODO: Is this correct? } /** * Creates a new view that represents a row within a flow. * * @return a view for a new row */ protected abstract View createRow(); /** * Loads the children of this view. The <code>FlowView</code> does not * directly load its children. Instead it creates a logical view * (@{link #layoutPool}) which is filled by the logical child views. * The real children are created at layout time and each represent one * row. * * This method is called by {@link View#setParent} in order to initialize * the view. * * @param vf the view factory to use for creating the child views */ protected void loadChildren(ViewFactory vf) { if (layoutPool == null) { layoutPool = new LogicalView(getElement()); Element el = getElement(); int count = el.getElementCount(); for (int i = 0; i < count; ++i) { Element childEl = el.getElement(i); View childView = vf.create(childEl); layoutPool.append(childView); } } } /** * Performs the layout of this view. If the span along the flow axis changed, * this first calls {@link FlowStrategy#layout} in order to rebuild the * rows of this view. Then the superclass's behaviour is called to arrange * the rows within the box. * * @param width the width of the view * @param height the height of the view */ protected void layout(int width, int height) { boolean rebuild = false; int flowAxis = getFlowAxis(); if (flowAxis == X_AXIS) { rebuild = !(width == layoutSpan); layoutSpan = width; } else { rebuild = !(height == layoutSpan); layoutSpan = height; } if (rebuild) strategy.layout(this); // TODO: If the span along the box axis has changed in the process of // relayouting the rows (that is, if rows have been added or removed), // call preferenceChanged in order to throw away cached layout information // of the surrounding BoxView. super.layout(width, height); } /** * Receice notification that some content has been inserted in the region * that this view is responsible for. This calls * {@link FlowStrategy#insertUpdate}. * * @param changes the document event describing the changes * @param a the current allocation of the view * @param vf the view factory that is used for creating new child views */ public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory vf) { // First we must send the insertUpdate to the logical view so it can // be updated accordingly. layoutPool.insertUpdate(changes, a, vf); strategy.insertUpdate(this, changes, getInsideAllocation(a)); } /** * Receice notification that some content has been removed from the region * that this view is responsible for. This calls * {@link FlowStrategy#removeUpdate}. * * @param changes the document event describing the changes * @param a the current allocation of the view * @param vf the view factory that is used for creating new child views */ public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory vf) { strategy.removeUpdate(this, changes, getInsideAllocation(a)); } /** * Receice notification that some attributes changed in the region * that this view is responsible for. This calls * {@link FlowStrategy#changedUpdate}. * * @param changes the document event describing the changes * @param a the current allocation of the view * @param vf the view factory that is used for creating new child views */ public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory vf) { strategy.changedUpdate(this, changes, getInsideAllocation(a)); } /** * Returns the index of the child <code>View</code> for the given model * position. * * This is implemented to iterate over the children of this * view (the rows) and return the index of the first view that contains * the given position. * * @param pos the model position for whicht the child <code>View</code> is * queried * * @return the index of the child <code>View</code> for the given model * position */ protected int getViewIndexAtPosition(int pos) { // First make sure we have a valid layout. if (!isAllocationValid()) layout(getWidth(), getHeight()); int count = getViewCount(); int result = -1; for (int i = 0; i < count; ++i) { View child = getView(i); int start = child.getStartOffset(); int end = child.getEndOffset(); if (start <= pos && end > pos) { result = i; break; } } return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -