選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

GoToXYAction.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.render.intermediate.extensions;
  19. import java.awt.Point;
  20. import org.xml.sax.ContentHandler;
  21. import org.xml.sax.SAXException;
  22. import org.xml.sax.helpers.AttributesImpl;
  23. import org.apache.fop.util.XMLUtil;
  24. /**
  25. * Action class which represents a "go-to" action to an absolute coordinate on a page.
  26. */
  27. public class GoToXYAction extends AbstractAction implements DocumentNavigationExtensionConstants {
  28. private int pageIndex = -1;
  29. private PageIndexRelative pageIndexRelative;
  30. private Point targetLocation;
  31. /**
  32. * Creates a new instance with yet unknown location.
  33. * @param id the identifier for this action
  34. */
  35. public GoToXYAction(String id) {
  36. this(id, -1, null, null);
  37. }
  38. /**
  39. * Creates a new instance.
  40. * @param id the identifier for this action
  41. * @param pageIndex the index (0-based) of the target page, -1 if the page index is
  42. * still unknown
  43. * @param targetLocation the absolute location on the page (coordinates in millipoints),
  44. * or null, if the position isn't known, yet
  45. */
  46. public GoToXYAction(String id, int pageIndex, Point targetLocation, PageIndexRelative pageIndexRelative) {
  47. setID(id);
  48. if (pageIndex < 0 && targetLocation != null) {
  49. throw new IllegalArgumentException(
  50. "Page index may not be null if target location is known!");
  51. }
  52. setPageIndex(pageIndex);
  53. setTargetLocation(targetLocation);
  54. this.pageIndexRelative = pageIndexRelative;
  55. }
  56. public interface PageIndexRelative {
  57. int getPageIndexRelative();
  58. }
  59. /**
  60. * Sets the index of the target page.
  61. * @param pageIndex the index (0-based) of the target page
  62. */
  63. public void setPageIndex(int pageIndex) {
  64. this.pageIndex = pageIndex;
  65. }
  66. /**
  67. * Returns the page index of the target page.
  68. * <p>
  69. * This function will always return a valid value for safety. Use
  70. * {@link #isComplete()} to check if the link is actually complete.
  71. *
  72. * @return the page index (0-based)
  73. */
  74. public int getPageIndex() {
  75. if (this.pageIndex >= 0) {
  76. return this.pageIndex;
  77. } else {
  78. return 0;
  79. }
  80. }
  81. /**
  82. * Returns the absolute coordinates of the target location on the page.
  83. * <p>
  84. * This function will always return a valid value for safety. Use
  85. * {@link #isComplete()} to check if the link is actually complete.
  86. *
  87. * @return the target location (coordinates in millipoints)
  88. */
  89. public Point getTargetLocation() {
  90. if (this.targetLocation == null) {
  91. return new Point(0, 0);
  92. } else {
  93. return this.targetLocation;
  94. }
  95. }
  96. /**
  97. * Sets the absolute coordinates of the target location on the page.
  98. * @param location the location (coordinates in millipoints)
  99. */
  100. public void setTargetLocation(Point location) {
  101. this.targetLocation = location;
  102. }
  103. private boolean isCompleteExceptTargetLocation() {
  104. return (getPageIndex() >= 0);
  105. }
  106. /** {@inheritDoc} */
  107. public boolean isComplete() {
  108. return this.isCompleteExceptTargetLocation() && (this.targetLocation != null);
  109. }
  110. /** {@inheritDoc} */
  111. public boolean isSame(AbstractAction other) {
  112. if (other == null) {
  113. throw new NullPointerException("other must not be null");
  114. }
  115. if (!(other instanceof GoToXYAction)) {
  116. return false;
  117. }
  118. GoToXYAction otherAction = (GoToXYAction)other;
  119. if (this.pageIndex != otherAction.pageIndex) {
  120. return false;
  121. }
  122. if (this.targetLocation == null || otherAction.targetLocation == null) {
  123. return false;
  124. }
  125. if (!getTargetLocation().equals(otherAction.getTargetLocation())) {
  126. return false;
  127. }
  128. return true;
  129. }
  130. /** {@inheritDoc} */
  131. public void toSAX(ContentHandler handler) throws SAXException {
  132. AttributesImpl atts = new AttributesImpl();
  133. if (this.isCompleteExceptTargetLocation()) {
  134. final Point reportedTargetLocation = this.getTargetLocation();
  135. atts.addAttribute("", "id", "id", XMLUtil.CDATA, getID());
  136. atts.addAttribute("", "page-index", "page-index",
  137. XMLUtil.CDATA, Integer.toString(pageIndex));
  138. if (pageIndexRelative != null) {
  139. int pageIndexRelativeInt = pageIndexRelative.getPageIndexRelative();
  140. if (pageIndexRelativeInt < 0) {
  141. atts.addAttribute("", "page-index-relative", "page-index-relative",
  142. XMLUtil.CDATA, Integer.toString(pageIndexRelativeInt));
  143. }
  144. }
  145. atts.addAttribute("", "x", "x", XMLUtil.CDATA,
  146. Integer.toString(reportedTargetLocation.x));
  147. atts.addAttribute("", "y", "y", XMLUtil.CDATA,
  148. Integer.toString(reportedTargetLocation.y));
  149. } else {
  150. atts.addAttribute("", "idref", "idref", XMLUtil.CDATA, getID());
  151. }
  152. handler.startElement(GOTO_XY.getNamespaceURI(),
  153. GOTO_XY.getLocalName(), GOTO_XY.getQName(), atts);
  154. handler.endElement(GOTO_XY.getNamespaceURI(),
  155. GOTO_XY.getLocalName(), GOTO_XY.getQName());
  156. }
  157. /** {@inheritDoc} */
  158. public String toString() {
  159. return "GoToXY: ID=" + getID()
  160. + ", page=" + getPageIndex()
  161. + ", loc=" + getTargetLocation() + ", "
  162. + (isComplete() ? "complete" : "INCOMPLETE");
  163. }
  164. }