[graphl-cvs] graphl/src/org/mediavirus/graphl/view AbstractFacet.java Facet.java GraphFacetRegistry
Status: Pre-Alpha
Brought to you by:
flo1
|
From: Flo L. <fl...@us...> - 2006-06-30 14:32:15
|
Update of /cvsroot/graphl/graphl/src/org/mediavirus/graphl/view In directory sc8-pr-cvs2.sourceforge.net:/tmp/cvs-serv11488/src/org/mediavirus/graphl/view Modified Files: AbstractFacet.java Facet.java GraphFacetRegistry.java Log Message: - migrated from jxpath to jaxen, which gives a much cleaner implementation of custom axes Index: Facet.java =================================================================== RCS file: /cvsroot/graphl/graphl/src/org/mediavirus/graphl/view/Facet.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Facet.java 8 Jun 2006 13:14:59 -0000 1.6 --- Facet.java 30 Jun 2006 14:31:38 -0000 1.7 *************** *** 90,92 **** --- 90,95 ---- */ public void refresh(); + + public String getStringProperty(String name, GraphElement element); + } \ No newline at end of file Index: GraphFacetRegistry.java =================================================================== RCS file: /cvsroot/graphl/graphl/src/org/mediavirus/graphl/view/GraphFacetRegistry.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** GraphFacetRegistry.java 8 Jun 2006 13:14:59 -0000 1.9 --- GraphFacetRegistry.java 30 Jun 2006 14:31:38 -0000 1.10 *************** *** 223,227 **** try { if (type != null) { ! Class facetClass = Class.forName(type); if (facetClass != null) { Object o = facetClass.newInstance(); --- 223,228 ---- try { if (type != null) { ! // du to xpath, the class name is wrapped in '' ! Class facetClass = Class.forName(type.replace("'","")); if (facetClass != null) { Object o = facetClass.newInstance(); *************** *** 238,242 **** } catch (Exception e) { ! System.err.println("Problem instantiating facet: " + e.getMessage()); } return null; --- 239,243 ---- } catch (Exception e) { ! System.err.println("Problem instantiating facet: " + e.toString()); } return null; Index: AbstractFacet.java =================================================================== RCS file: /cvsroot/graphl/graphl/src/org/mediavirus/graphl/view/AbstractFacet.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** AbstractFacet.java 8 Jun 2006 13:14:59 -0000 1.9 --- AbstractFacet.java 30 Jun 2006 14:31:38 -0000 1.10 *************** *** 5,9 **** --- 5,12 ---- package org.mediavirus.graphl.view; + import java.awt.Color; import java.awt.Dimension; + import java.awt.Font; + import java.awt.Image; import java.awt.Stroke; import java.beans.BeanInfo; *************** *** 13,24 **** --- 16,39 ---- import java.beans.PropertyChangeListener; import java.beans.SimpleBeanInfo; + import java.io.IOException; + import java.net.MalformedURLException; + import java.net.URL; import java.util.ArrayList; import java.util.Collection; + import java.util.HashMap; + import java.util.Iterator; import java.util.List; + import java.util.Map; import javax.swing.JComponent; + + import org.jaxen.JaxenException; + import org.jaxen.XPath; import org.mediavirus.graphl.graph.GraphElement; import org.mediavirus.graphl.graph.Node; import org.mediavirus.graphl.gui.StrokePropertyEditor; + import org.mediavirus.graphl.jaxen.GraphlXPath; + import org.mediavirus.graphl.vocabulary.NS; + import org.mediavirus.util.ParseUtils; import com.l2fprod.common.propertysheet.Property; *************** *** 33,36 **** --- 48,54 ---- protected List<GraphElement> assignedElements = new ArrayList<GraphElement>(); + + protected Map<String, XPath> propertyExpressions = new HashMap<String, XPath>(); + protected Map<String, Map<GraphElement, Object>> expressionCache = new HashMap<String, Map<GraphElement, Object>>(); /* *************** *** 58,67 **** public void setConfigurationNode(Node node) { ! // do nothing per default. ! ! // TODO (2) either implement a generic property mechanism here, or implement this method in all subclasses ! // (it should then be removed here). } public JComponent getVisualController() { --- 76,195 ---- public void setConfigurationNode(Node node) { ! // copy xpath properties from config node ! for (Iterator propertyIterator = node.getProperties().keySet().iterator(); propertyIterator.hasNext();) { ! String propName = (String) propertyIterator.next(); ! //todo: only properties form a known list of valid property names should be copied ! //hack: just ignore id for now ! if (!propName.equals(NS.rdf + "id")) { ! try { ! XPath expr = new GraphlXPath(node.getProperty(propName)); ! propertyExpressions.put(propName, expr); ! expressionCache.put(propName, new HashMap<GraphElement, Object>()); ! } ! catch (JaxenException ijexned) { ! System.err.println("Error parsing XPath (" + node.getProperty(propName) + ") in property " + propName + " : " + ijexned.toString()); ! } ! } ! } } + public String getStringProperty(String name, GraphElement element) { + Object value = getProperty(name, element); + if (value instanceof String) return (String) value; + else return null; + } + + public Integer getIntProperty(String name, GraphElement element) { + Object value = getProperty(name, element); + if (value instanceof Double) return new Integer(((Double)value).intValue()); + else return null; + } + + public Double getDoubleProperty(String name, GraphElement element) { + Object value = getProperty(name, element); + if (value instanceof Double) return (Double)value; + else return null; + } + + public boolean getBoolProperty(String name, GraphElement element) { + Integer value = getIntProperty(name, element); + if (value != null && value > 0) return true; + else return false; + } + + public Color getColorProperty(String name, GraphElement element) { + Object value = getProperty(name, element); + if (value instanceof String) { + return ParseUtils.parseColor((String)value); + } + else return null; + } + + public Stroke getStrokeProperty(String name, GraphElement element) { + Object value = getProperty(name, element); + if (value instanceof String) { + return ParseUtils.parseStroke((String)value); + } + else return null; + } + + public Font getFontProperty(String name, GraphElement element) { + Object value = getProperty(name, element); + if (value instanceof String) { + return ParseUtils.parseFont((String)value); + } + else return null; + } + + public Image getImageProperty(String name, GraphElement element, URL baseURL) { + Object value = getProperty(name, element); + if (value instanceof Image) { + return (Image)value; + } + if (value instanceof String) { + String href = (String) value; + Image img = null; + try { + URL url = new URL(baseURL, href); + img = (Image)url.getContent(new Class[]{Image.class}); + } + catch (Exception ex) { + //img = null; + System.err.println("Error getting image from: " + href + " : " + ex.toString()); + } + if (img != null) { + expressionCache.get(name).put(element, img); + } + else { + // put something in cache to cancel future executions + expressionCache.get(name).put(element, new Object()); + } + return img; + } + else return null; + } + + + public Object getProperty(String name, GraphElement element) { + Object value = null; + Map<GraphElement, Object> cache = expressionCache.get(name); + // if cache doesn't exist, the property also has not been set + if (cache != null) { + value = cache.get(element); + if (value == null) { + XPath expr = propertyExpressions.get(name); + if (expr != null) { + try { + value = expr.evaluate(element); + } + catch (JaxenException spe) { + System.err.println("Error evaluating XPath (" + expr.toString() + ") on element " + element.getId()); + } + } + cache.put(element, value); + } + } + return value; + } public JComponent getVisualController() { |