You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Position.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * 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. /* $Id$ */
  18. package org.apache.fop.layoutmgr;
  19. /** A position. */
  20. public class Position {
  21. private LayoutManager layoutManager;
  22. private int index = -1;
  23. /**
  24. * Construct a position.
  25. * @param lm the associated layout manager
  26. */
  27. public Position(LayoutManager lm) {
  28. layoutManager = lm;
  29. }
  30. /**
  31. * Construct a position.
  32. * @param lm the associated layout manager
  33. * @param index the index
  34. */
  35. public Position(LayoutManager lm, int index) {
  36. this(lm);
  37. setIndex(index);
  38. }
  39. /** @return associated layout manager */
  40. public LayoutManager getLM() {
  41. return layoutManager;
  42. }
  43. /**
  44. * @param depth the depth at which the LM in this position is found
  45. * @return associated layout manager
  46. */
  47. public LayoutManager getLM(int depth) {
  48. Position subPos = getPosition(depth);
  49. if (subPos == null) {
  50. return null;
  51. } else {
  52. return subPos.getLM();
  53. }
  54. }
  55. /**
  56. * Overridden by NonLeafPosition to return the Position of its child LM.
  57. * @return a position or null
  58. */
  59. public Position getPosition() {
  60. return null;
  61. }
  62. /**
  63. * Overridden by NonLeafPosition to return the Position of its child LM.
  64. * @param depth the depth at which the position in this position is found
  65. * @return a position or null
  66. */
  67. public Position getPosition(int depth) {
  68. Position subPos = this;
  69. for (int i = 0; i < depth && subPos != null; ++i, subPos = subPos.getPosition()) {
  70. // no-op
  71. }
  72. return subPos;
  73. }
  74. /** @return true if generates areas */
  75. public boolean generatesAreas() {
  76. return false;
  77. }
  78. /**
  79. * Sets the index of this position in the sequence of Position elements.
  80. *
  81. * @param value this position's index
  82. */
  83. public void setIndex(int value) {
  84. this.index = value;
  85. }
  86. /**
  87. * Returns the index of this position in the sequence of Position elements.
  88. *
  89. * @return the index of this position in the sequence of Position elements
  90. */
  91. public int getIndex() {
  92. return this.index;
  93. }
  94. /** @return short name of associated layout manager */
  95. protected String getShortLMName() {
  96. if (getLM() != null) {
  97. String lm = getLM().toString();
  98. int idx = lm.lastIndexOf('.');
  99. if (idx >= 0 && lm.indexOf('@') > 0) {
  100. return lm.substring(idx + 1);
  101. } else {
  102. return lm;
  103. }
  104. } else {
  105. return "null";
  106. }
  107. }
  108. /** {@inheritDoc} */
  109. public String toString() {
  110. StringBuffer sb = new StringBuffer();
  111. sb.append("Position:").append(getIndex()).append("(");
  112. sb.append(getShortLMName());
  113. sb.append(")");
  114. return sb.toString();
  115. }
  116. }