You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by hi...@apache.org on 2001/04/02 15:14:43 UTC
cvs commit: xml-batik/sources/org/apache/batik/swing/svg JSVGComponent.java
hillion 01/04/02 06:14:43
Modified: sources/org/apache/batik/apps/svgbrowser LocalHistory.java
sources/org/apache/batik/css AbstractViewCSS.java
sources/org/apache/batik/swing/gvt JGVTComponent.java
sources/org/apache/batik/swing/svg JSVGComponent.java
Log:
- <svg zoomAndPan="disable" ...> now disables the interactions with the
document.
- implemented a SoftReference cache for CSS computed styles.
Revision Changes Path
1.4 +4 -1 xml-batik/sources/org/apache/batik/apps/svgbrowser/LocalHistory.java
Index: LocalHistory.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/apps/svgbrowser/LocalHistory.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- LocalHistory.java 2001/03/27 15:23:25 1.3
+++ LocalHistory.java 2001/04/02 13:14:41 1.4
@@ -27,7 +27,7 @@
* browser frame.
*
* @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
- * @version $Id: LocalHistory.java,v 1.3 2001/03/27 15:23:25 hillion Exp $
+ * @version $Id: LocalHistory.java,v 1.4 2001/04/02 13:14:41 hillion Exp $
*/
public class LocalHistory {
@@ -141,6 +141,9 @@
* @param uri The URI of the document just loaded.
*/
public void update(String uri) {
+ if (currentURI < -1) {
+ currentURI = -1;
+ }
if (++currentURI < visitedURIs.size()) {
if (!visitedURIs.get(currentURI).equals(uri)) {
for (int i = currentURI + 1; i + index <= menu.getItemCount(); i++) {
1.11 +267 -23 xml-batik/sources/org/apache/batik/css/AbstractViewCSS.java
Index: AbstractViewCSS.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/css/AbstractViewCSS.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- AbstractViewCSS.java 2001/03/18 16:29:22 1.10
+++ AbstractViewCSS.java 2001/04/02 13:14:42 1.11
@@ -8,15 +8,17 @@
package org.apache.batik.css;
-import java.lang.ref.WeakReference;
+import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+
import org.apache.batik.css.sac.ExtendedSelector;
import org.apache.batik.css.value.ImmutableInherit;
import org.apache.batik.css.value.RelativeValueResolver;
+
import org.w3c.css.sac.SelectorList;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -41,9 +43,10 @@
* {@link org.w3c.dom.css.ViewCSS} interface.
*
* @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
- * @version $Id: AbstractViewCSS.java,v 1.10 2001/03/18 16:29:22 hillion Exp $
+ * @version $Id: AbstractViewCSS.java,v 1.11 2001/04/02 13:14:42 hillion Exp $
*/
public abstract class AbstractViewCSS implements ViewCSS {
+
/**
* The document of which this object is a view.
*/
@@ -51,13 +54,8 @@
/**
* The cached computed styles.
- */
- protected Map styles = new HashMap(11);
-
- /**
- * The cached stylesheets.
*/
- protected StyleSheetList styleSheets;
+ protected ComputedStyleCache styles = new ComputedStyleCache();
/**
* The media to use for cascading.
@@ -109,37 +107,35 @@
*/
public CSSOMReadOnlyStyleDeclaration getComputedStyleInternal(Element elt,
String pseudoElt) {
- Map m = (Map)styles.get(elt);
- if (m == null) {
- styles.put(elt, m = new HashMap(11));
- }
pseudoElt = (pseudoElt == null) ? "" : pseudoElt;
- CSSOMReadOnlyStyleDeclaration result =
- (CSSOMReadOnlyStyleDeclaration)m.get(pseudoElt);
+ CSSOMReadOnlyStyleDeclaration result = styles.get(elt, pseudoElt);
if (result == null) {
result = computeStyle(elt, pseudoElt);
- m.put(pseudoElt, result);
+ styles.put(elt, pseudoElt, result);
}
return result;
}
/**
- * Sets the computed style.
+ * Sets the computed style in the cache in a way it is not collectable.
*/
public void setComputedStyle(Element elt,
String pseudoElt,
- CSSStyleDeclaration sd) {
- Map m = (Map)styles.get(elt);
- if (m == null) {
- styles.put(elt, m = new HashMap(11));
- }
+ CSSOMReadOnlyStyleDeclaration sd) {
pseudoElt = (pseudoElt == null) ? "" : pseudoElt;
- ((CSSOMReadOnlyStyleDeclaration)sd).setContext(this, elt);
- m.put(pseudoElt, sd);
+ sd.setContext(this, elt);
+ styles.putPermanent(elt, pseudoElt, sd);
}
/**
+ * Disposes the style declarations explicitly setted in the cache.
+ */
+ public void dispose() {
+ styles.dispose();
+ }
+
+ /**
* Sets the media to use to compute the styles.
* @param mediaText The text representation of the media.
*/
@@ -532,5 +528,253 @@
}
}
return false;
+ }
+
+ /**
+ * To cache the computed styles.
+ */
+ protected static class ComputedStyleCache {
+
+ /**
+ * The table used to store the style.
+ */
+ protected Entry[] table;
+
+ /**
+ * The number of entries
+ */
+ protected int count;
+
+ /**
+ * Creates a new ComputedStyleCache.
+ */
+ public ComputedStyleCache() {
+ table = new Entry[11];
+ }
+
+ /**
+ * Caches the given computed style.
+ */
+ public void put(Element elt, String pe, CSSOMReadOnlyStyleDeclaration sd) {
+ update();
+
+ int hash = hashCode(elt, pe) & 0x7FFFFFFF;
+ int index = hash % table.length;
+
+ for (Entry e = table[index]; e != null; e = e.next) {
+ if ((e.hash == hash) && e.match(elt, pe)) {
+ e.computedStyleReference = new SoftReference(sd);
+ return;
+ }
+ }
+
+ // The key is not in the hash table
+ int len = table.length;
+ if (count++ >= (len * 3) >>> 2) {
+ rehash();
+ index = hash % table.length;
+ }
+
+ Entry e = new Entry(hash, elt, pe, new SoftReference(sd), table[index]);
+ table[index] = e;
+ }
+
+ /**
+ * Caches the given computed style without possibility of collection.
+ */
+ public void putPermanent(Element elt, String pe,
+ CSSOMReadOnlyStyleDeclaration sd) {
+ update();
+
+ int hash = hashCode(elt, pe) & 0x7FFFFFFF;
+ int index = hash % table.length;
+
+ for (Entry e = table[index]; e != null; e = e.next) {
+ if ((e.hash == hash) && e.match(elt, pe)) {
+ e.computedStyleReference = new StrongReference(sd);
+ return;
+ }
+ }
+
+ // The key is not in the hash table
+ int len = table.length;
+ if (count++ >= (len * 3) >>> 2) {
+ rehash();
+ index = hash % table.length;
+ }
+
+ Entry e = new Entry(hash, elt, pe, new StrongReference(sd), table[index]);
+ table[index] = e;
+ }
+
+ /**
+ * Returns the computed style mapped with the given element
+ * and pseudo-element, if any.
+ */
+ public CSSOMReadOnlyStyleDeclaration get(Element elt, String pe) {
+ update();
+
+ int hash = hashCode(elt, pe) & 0x7FFFFFFF;
+ int index = hash % table.length;
+
+ for (Entry e = table[index]; e != null; e = e.next) {
+ if ((e.hash == hash) && e.match(elt, pe)) {
+ return (CSSOMReadOnlyStyleDeclaration)e.computedStyleReference.get();
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Rehash the table
+ */
+ protected void rehash () {
+ Entry[] oldTable = table;
+
+ table = new Entry[oldTable.length * 2 + 1];
+
+ for (int i = oldTable.length-1; i >= 0; i--) {
+ for (Entry old = oldTable[i]; old != null;) {
+ Entry e = old;
+ old = old.next;
+
+ int index = e.hash % table.length;
+ e.next = table[index];
+ table[index] = e;
+ }
+ }
+ }
+
+ /**
+ * Updates the table.
+ */
+ protected void update() {
+ for (int i = table.length - 1; i >= 0; --i) {
+ Entry e = table[i];
+ Entry p = null;
+ if (e != null) {
+ if (e.computedStyleReference.get() == null) {
+ table[i] = e.next;
+ count--;
+ }
+ p = e;
+ e = e.next;
+ }
+ while (e != null) {
+ if (e.computedStyleReference.get() == null) {
+ p.next = e.next;
+ count--;
+ }
+ p = e;
+ e = e.next;
+ }
+ }
+ }
+
+ /**
+ * Removes the permanently cached style declarations.
+ */
+ public void dispose() {
+ for (int i = table.length - 1; i >= 0; --i) {
+ Entry e = table[i];
+ Entry p = null;
+ if (e != null) {
+ if (e.computedStyleReference instanceof StrongReference) {
+ table[i] = e.next;
+ count--;
+ }
+ p = e;
+ e = e.next;
+ }
+ while (e != null) {
+ if (e.computedStyleReference instanceof StrongReference) {
+ p.next = e.next;
+ count--;
+ }
+ p = e;
+ e = e.next;
+ }
+ }
+ }
+
+ /**
+ * Computes a hash code for the given element and pseudo-element.
+ */
+ protected int hashCode(Element e, String pe) {
+ return e.hashCode() ^ pe.hashCode();
+ }
+
+ /**
+ * To store computed style with a strong reference.
+ */
+ protected static class StrongReference extends SoftReference {
+
+ /**
+ * A strong reference.
+ */
+ protected Object reference;
+
+ /**
+ * Creates a new strong reference.
+ */
+ public StrongReference(Object o) {
+ super(o);
+ reference = o;
+ }
+ }
+
+ /**
+ * To manage collisions in the table.
+ */
+ protected static class Entry {
+
+ /**
+ * The hash code
+ */
+ public int hash;
+
+ /**
+ * The element.
+ */
+ public Element element;
+
+ /**
+ * The pseudo-element.
+ */
+ public String pseudoElement;
+
+ /**
+ * The computed style.
+ */
+ public SoftReference computedStyleReference;
+
+ /**
+ * The next entry.
+ */
+ public Entry next;
+
+ /**
+ * Creates a new entry.
+ */
+ public Entry(int h, Element e, String pe, SoftReference sd, Entry n) {
+ hash = h;
+ element = e;
+ pseudoElement = pe;
+ computedStyleReference = sd;
+ next = n;
+ }
+
+ /**
+ * Whether this entry match the given keys.
+ */
+ public boolean match(Element e, String pe) {
+ if (e == element) {
+ if (pe.equals(pseudoElement)) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
}
}
1.5 +9 -2 xml-batik/sources/org/apache/batik/swing/gvt/JGVTComponent.java
Index: JGVTComponent.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/swing/gvt/JGVTComponent.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- JGVTComponent.java 2001/03/26 15:46:20 1.4
+++ JGVTComponent.java 2001/04/02 13:14:42 1.5
@@ -52,7 +52,7 @@
* This class represents a component which can display a GVT tree.
*
* @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
- * @version $Id: JGVTComponent.java,v 1.4 2001/03/26 15:46:20 hillion Exp $
+ * @version $Id: JGVTComponent.java,v 1.5 2001/04/02 13:14:42 hillion Exp $
*/
public class JGVTComponent extends JComponent {
@@ -169,6 +169,11 @@
protected boolean suspendInteractions;
/**
+ * Whether to inconditionally disable interactions.
+ */
+ protected boolean disableInteractions;
+
+ /**
* Creates a new JGVTComponent.
*/
public JGVTComponent() {
@@ -767,7 +772,9 @@
* Selects an interactor, given an input event.
*/
protected void selectInteractor(InputEvent ie) {
- if (!suspendInteractions && interactor == null) {
+ if (!disableInteractions &&
+ !suspendInteractions &&
+ interactor == null) {
Iterator it = interactors.iterator();
while (it.hasNext()) {
Interactor i = (Interactor)it.next();
1.7 +9 -1 xml-batik/sources/org/apache/batik/swing/svg/JSVGComponent.java
Index: JSVGComponent.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/swing/svg/JSVGComponent.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- JSVGComponent.java 2001/03/30 15:13:14 1.6
+++ JSVGComponent.java 2001/04/02 13:14:42 1.7
@@ -42,6 +42,8 @@
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.gvt.JGVTComponent;
+import org.apache.batik.util.SVGConstants;
+
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGAElement;
@@ -57,7 +59,7 @@
* This class represents a Swing component which can display SVG.
*
* @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
- * @version $Id: JSVGComponent.java,v 1.6 2001/03/30 15:13:14 hillion Exp $
+ * @version $Id: JSVGComponent.java,v 1.7 2001/04/02 13:14:42 hillion Exp $
*/
public class JSVGComponent extends JGVTComponent {
@@ -186,6 +188,12 @@
throw new IllegalArgumentException("Invalid DOM implementation.");
}
svgDocument = doc;
+
+ Element root = doc.getDocumentElement();
+ String znp = root.getAttributeNS(null, SVGConstants.SVG_ZOOM_AND_PAN_ATTRIBUTE);
+ if (!znp.equals(SVGConstants.SVG_MAGNIFY_VALUE)) {
+ disableInteractions = true;
+ }
gvtTreeBuilder = new GVTTreeBuilder(doc, bridgeContext = createBridgeContext());
gvtTreeBuilder.setPriority(Thread.MIN_PRIORITY);
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org