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.

GoToXYAction.java 5.8KB

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