PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/batik-1.7/sources/org/apache/batik/apps/svgbrowser/LocalHistory.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 258 lines | 144 code | 27 blank | 87 comment | 19 complexity | 917552ec5179e6cee04c2f9f16b2709e MD5 | raw file
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package org.apache.batik.apps.svgbrowser;
  16. import java.awt.event.ActionEvent;
  17. import java.awt.event.ActionListener;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import javax.swing.ButtonGroup;
  21. import javax.swing.JMenu;
  22. import javax.swing.JMenuBar;
  23. import javax.swing.JMenuItem;
  24. import javax.swing.JRadioButtonMenuItem;
  25. /**
  26. * This class represents an history of the files visited by a single
  27. * browser frame.
  28. *
  29. * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
  30. * @version $Id: LocalHistory.java 489226 2006-12-21 00:05:36Z cam $
  31. */
  32. public class LocalHistory {
  33. /**
  34. * The frame to manage.
  35. */
  36. protected JSVGViewerFrame svgFrame;
  37. /**
  38. * The menu which contains the history.
  39. */
  40. protected JMenu menu;
  41. /**
  42. * The index of the first history item in this menu.
  43. */
  44. protected int index;
  45. /**
  46. * The visited URIs.
  47. */
  48. protected List visitedURIs = new ArrayList();
  49. /**
  50. * The index of the current URI.
  51. */
  52. protected int currentURI = -1;
  53. /**
  54. * The button group for the menu items.
  55. */
  56. protected ButtonGroup group = new ButtonGroup();
  57. /**
  58. * The action listener.
  59. */
  60. protected ActionListener actionListener = new RadioListener();
  61. /**
  62. * The current state.
  63. */
  64. protected int state;
  65. // States
  66. protected static final int STABLE_STATE = 0;
  67. protected static final int BACK_PENDING_STATE = 1;
  68. protected static final int FORWARD_PENDING_STATE = 2;
  69. protected static final int RELOAD_PENDING_STATE = 3;
  70. /**
  71. * Creates a new local history.
  72. * @param mb The menubar used to display the history. It must
  73. * contain one '@@@' item used as marker to place the
  74. * history items.
  75. * @param svgFrame The frame to manage.
  76. */
  77. public LocalHistory(JMenuBar mb, JSVGViewerFrame svgFrame) {
  78. this.svgFrame = svgFrame;
  79. // Find the marker.
  80. int mc = mb.getMenuCount();
  81. for (int i = 0; i < mc; i++) {
  82. JMenu m = mb.getMenu(i);
  83. int ic = m.getItemCount();
  84. for (int j = 0; j < ic; j++) {
  85. JMenuItem mi = m.getItem(j);
  86. if (mi != null) {
  87. String s = mi.getText();
  88. if ("@@@".equals(s)) {
  89. menu = m;
  90. index = j;
  91. m.remove(j);
  92. return;
  93. }
  94. }
  95. }
  96. }
  97. throw new IllegalArgumentException("No '@@@' marker found");
  98. }
  99. /**
  100. * Goes back of one position in the history.
  101. * Assumes that <tt>canGoBack()</tt> is true.
  102. */
  103. public void back() {
  104. update();
  105. state = BACK_PENDING_STATE;
  106. currentURI -= 2;
  107. svgFrame.showSVGDocument((String)visitedURIs.get(currentURI + 1));
  108. }
  109. /**
  110. * Whether it is possible to go back.
  111. */
  112. public boolean canGoBack() {
  113. return currentURI > 0;
  114. }
  115. /**
  116. * Goes forward of one position in the history.
  117. * Assumes that <tt>canGoForward()</tt> is true.
  118. */
  119. public void forward() {
  120. update();
  121. state = FORWARD_PENDING_STATE;
  122. svgFrame.showSVGDocument((String)visitedURIs.get(currentURI + 1));
  123. }
  124. /**
  125. * Whether it is possible to go forward.
  126. */
  127. public boolean canGoForward() {
  128. return currentURI < visitedURIs.size() - 1;
  129. }
  130. /**
  131. * Reloads the current document.
  132. */
  133. public void reload() {
  134. update();
  135. state = RELOAD_PENDING_STATE;
  136. currentURI--;
  137. svgFrame.showSVGDocument((String)visitedURIs.get(currentURI + 1));
  138. }
  139. /**
  140. * Updates the history.
  141. * @param uri The URI of the document just loaded.
  142. */
  143. public void update(String uri) {
  144. if (currentURI < -1) {
  145. throw new IllegalStateException("Unexpected currentURI:" + currentURI );
  146. }
  147. state = STABLE_STATE;
  148. if (++currentURI < visitedURIs.size()) {
  149. if (!visitedURIs.get(currentURI).equals(uri)) {
  150. int len = menu.getItemCount();
  151. for (int i = len - 1; i >= index + currentURI + 1; i--) {
  152. JMenuItem mi = menu.getItem(i);
  153. group.remove(mi);
  154. menu.remove(i);
  155. }
  156. visitedURIs = visitedURIs.subList(0, currentURI + 1);
  157. }
  158. JMenuItem mi = menu.getItem(index + currentURI);
  159. group.remove(mi);
  160. menu.remove(index + currentURI);
  161. visitedURIs.set(currentURI, uri);
  162. } else {
  163. if (visitedURIs.size() >= 15) {
  164. visitedURIs.remove(0);
  165. JMenuItem mi = menu.getItem(index);
  166. group.remove(mi);
  167. menu.remove(index);
  168. currentURI--;
  169. }
  170. visitedURIs.add(uri);
  171. }
  172. // Computes the button text.
  173. String text = uri;
  174. int i = uri.lastIndexOf('/');
  175. if (i == -1) {
  176. i = uri.lastIndexOf('\\' );
  177. }
  178. if (i != -1) {
  179. text = uri.substring(i + 1);
  180. }
  181. JMenuItem mi = new JRadioButtonMenuItem(text);
  182. mi.setToolTipText(uri);
  183. mi.setActionCommand(uri);
  184. mi.addActionListener(actionListener);
  185. group.add(mi);
  186. mi.setSelected(true);
  187. menu.insert(mi, index + currentURI);
  188. }
  189. /**
  190. * Updates the state of this history.
  191. */
  192. protected void update() {
  193. switch (state) {
  194. case BACK_PENDING_STATE:
  195. currentURI += 2;
  196. break;
  197. case RELOAD_PENDING_STATE:
  198. currentURI++;
  199. break;
  200. case FORWARD_PENDING_STATE:
  201. // fall-through intended
  202. case STABLE_STATE:
  203. }
  204. }
  205. /**
  206. * To listen to the radio buttons.
  207. */
  208. protected class RadioListener
  209. implements ActionListener {
  210. protected RadioListener() {
  211. }
  212. public void actionPerformed( ActionEvent e ) {
  213. String uri = e.getActionCommand();
  214. currentURI = getItemIndex( (JMenuItem)e.getSource() ) - 1;
  215. svgFrame.showSVGDocument( uri );
  216. }
  217. public int getItemIndex( JMenuItem item ) {
  218. int ic = menu.getItemCount();
  219. for ( int i = index; i < ic; i++ ) {
  220. if ( menu.getItem( i ) == item ) {
  221. return i - index;
  222. }
  223. }
  224. throw new IllegalArgumentException("MenuItem is not from my menu!" );
  225. }
  226. }
  227. }