1 18 package org.apache.batik.apps.svgbrowser; 19 20 import java.awt.event.ActionEvent ; 21 import java.awt.event.ActionListener ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 25 import javax.swing.ButtonGroup ; 26 import javax.swing.JMenu ; 27 import javax.swing.JMenuBar ; 28 import javax.swing.JMenuItem ; 29 import javax.swing.JRadioButtonMenuItem ; 30 31 38 public class LocalHistory { 39 42 protected JSVGViewerFrame svgFrame; 43 44 47 protected JMenu menu; 48 49 52 protected int index; 53 54 57 protected List visitedURIs = new ArrayList (); 58 59 62 protected int currentURI = -1; 63 64 67 protected ButtonGroup group = new ButtonGroup (); 68 69 72 protected ActionListener actionListener = new RadioListener(); 73 74 77 protected int state; 78 79 protected final static int STABLE_STATE = 0; 81 protected final static int BACK_PENDING_STATE = 1; 82 protected final static int FORWARD_PENDING_STATE = 2; 83 protected final static int RELOAD_PENDING_STATE = 3; 84 85 92 public LocalHistory(JMenuBar mb, JSVGViewerFrame svgFrame) { 93 this.svgFrame = svgFrame; 94 95 int mc = mb.getMenuCount(); 97 for (int i = 0; i < mc; i++) { 98 JMenu m = mb.getMenu(i); 99 int ic = m.getItemCount(); 100 for (int j = 0; j < ic; j++) { 101 JMenuItem mi = m.getItem(j); 102 if (mi != null) { 103 String s = mi.getText(); 104 if ("@@@".equals(s)) { 105 menu = m; 106 index = j; 107 m.remove(j); 108 return; 109 } 110 } 111 } 112 } 113 throw new IllegalArgumentException ("No '@@@' marker found"); 114 } 115 116 120 public void back() { 121 update(); 122 state = BACK_PENDING_STATE; 123 currentURI -= 2; 124 svgFrame.showSVGDocument((String )visitedURIs.get(currentURI + 1)); 125 } 126 127 130 public boolean canGoBack() { 131 return currentURI > 0; 132 } 133 134 138 public void forward() { 139 update(); 140 state = FORWARD_PENDING_STATE; 141 svgFrame.showSVGDocument((String )visitedURIs.get(currentURI + 1)); 142 } 143 144 147 public boolean canGoForward() { 148 return currentURI < visitedURIs.size() - 1; 149 } 150 151 154 public void reload() { 155 update(); 156 state = RELOAD_PENDING_STATE; 157 currentURI--; 158 svgFrame.showSVGDocument((String )visitedURIs.get(currentURI + 1)); 159 } 160 161 165 public void update(String uri) { 166 if (currentURI < -1) { 167 throw new InternalError (); 168 } 169 state = STABLE_STATE; 170 if (++currentURI < visitedURIs.size()) { 171 if (!visitedURIs.get(currentURI).equals(uri)) { 172 int len = menu.getItemCount(); 173 for (int i = len - 1; i >= index + currentURI + 1; i--) { 174 JMenuItem mi = menu.getItem(i); 175 group.remove(mi); 176 menu.remove(i); 177 } 178 visitedURIs = visitedURIs.subList(0, currentURI + 1); 179 } 180 JMenuItem mi = menu.getItem(index + currentURI); 181 group.remove(mi); 182 menu.remove(index + currentURI); 183 visitedURIs.set(currentURI, uri); 184 } else { 185 if (visitedURIs.size() >= 15) { 186 visitedURIs.remove(0); 187 JMenuItem mi = menu.getItem(index); 188 group.remove(mi); 189 menu.remove(index); 190 currentURI--; 191 } 192 visitedURIs.add(uri); 193 } 194 195 String text = uri; 197 int i = uri.lastIndexOf("/"); 198 if (i == -1) { 199 i = uri.lastIndexOf("\\"); 200 if (i != -1) { 201 text = uri.substring(i + 1); 202 } 203 } else { 204 text = uri.substring(i + 1); 205 } 206 207 JMenuItem mi = new JRadioButtonMenuItem (text); 208 mi.setActionCommand(uri); 209 mi.addActionListener(actionListener); 210 group.add(mi); 211 mi.setSelected(true); 212 menu.insert(mi, index + currentURI); 213 } 214 215 218 protected void update() { 219 switch (state) { 220 case BACK_PENDING_STATE: 221 currentURI += 2; 222 break; 223 case RELOAD_PENDING_STATE: 224 currentURI++; 225 case FORWARD_PENDING_STATE: 226 case STABLE_STATE: 227 } 228 } 229 230 233 protected class RadioListener implements ActionListener { 234 public RadioListener() {} 235 public void actionPerformed(ActionEvent e) { 236 String uri = e.getActionCommand(); 237 currentURI = getItemIndex((JMenuItem )e.getSource()) - 1; 238 svgFrame.showSVGDocument(uri); 239 } 240 public int getItemIndex(JMenuItem item) { 241 int ic = menu.getItemCount(); 242 for (int i = index; i < ic; i++) { 243 if (menu.getItem(i) == item) { 244 return i - index; 245 } 246 } 247 throw new InternalError (); 248 } 249 } 250 } 251 | Popular Tags |