package org.hwo.ui.diagram; import java.awt.Color; import java.beans.MethodDescriptor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Vector; import org.hwo.ui.diagram.annotation.Plot; public class AnnotatedPlotProvider implements PlotProvider2{ private Class clazz; private GraphDefinition[] graphDefinitions; private int maxOrdinate; private Color[] colors; private Vector values; private int diagram; public AnnotatedPlotProvider(Class clazz){ this.setClazz(clazz); this.setValues(new Object[0]); this.diagram = 0; } public AnnotatedPlotProvider(Class clazz,int diagram){ this.diagram = diagram; this.setClazz(clazz); this.setValues(new Object[0]); } public void setClazz(Class clazz) { this.clazz = clazz; updateClazz(); } public Class getClazz() { return clazz; } public void setValues(Object[] values) { this.values = new Vector(); this.values.addAll(Arrays.asList(values)); } public void setValues(Vector v){ this.values = v; } public Vector getValues() { return values; } private void updateClazz(){ LinkedList gdl = new LinkedList(); for (Field f: this.clazz.getDeclaredFields()){ Plot plot = f.getAnnotation(Plot.class); if (plot != null){ if (plot.diagram() == this.diagram) gdl.add(new GraphDefinition(f)); } } for (Method m: this.clazz.getDeclaredMethods()){ Plot plot = m.getAnnotation(Plot.class); if (plot != null){ if (plot.diagram() == this.diagram) gdl.add(new GraphDefinition(m)); } } this.colors = new Color[ gdl.size() ]; for (GraphDefinition gd: gdl){ if (gd.getOrdinate() > maxOrdinate) maxOrdinate = gd.getOrdinate(); this.colors[ gdl.indexOf(gd) ] = gd.getColor(); } this.graphDefinitions = gdl.toArray(new GraphDefinition[0]); } public Color getColor(int graph){ return this.colors[graph]; } public void setColors(int graph,Color color) { this.colors[graph] = color; } @Override public int getNumGraphs() { return this.graphDefinitions.length; } @Override public Color[] getColors() { return this.colors; } public int getMaxOrdinate() { return maxOrdinate; } public int getLength(){ return this.values.size(); } public String getLabel(int graph){ return this.graphDefinitions[graph].getLabel(); } public Float getValue(int x,int graph){ Object o = this.values.get(x); if (o==null) return null; return this.graphDefinitions[graph].getValue(o); } public int getOrdinate(int graph){ return this.graphDefinitions[graph].getOrdinate(); } class GraphDefinition{ Method method; Field field; String label; int ordinate; Color color; public GraphDefinition(Method method){ this.method = method; this.setPlot(method.getAnnotation(Plot.class)); } public GraphDefinition(Field field){ this.field = field; this.field.setAccessible(true); this.setPlot(field.getAnnotation(Plot.class)); } private void setPlot(Plot plot){ this.label = plot.label(); this.ordinate = plot.ordinate(); this.color = new Color(plot.r(), plot.g(), plot.b()); } public int getOrdinate() { return ordinate; } public String getLabel() { return label; } public Color getColor() { return color; } public Float getValue(Object o){ try { if (field != null){ if (field.getType().equals(Integer.class)){ return ((Integer)field.get(o)).floatValue(); }; if (field.getType().equals(Double.class)){ return ((Double)field.get(o)).floatValue(); } if (field.getType().equals(Float.class)){ return (Float)field.get(o); } } if (method != null){ if (method.getReturnType().equals(Integer.class)){ return ((Integer)method.invoke(o, null)).floatValue(); } if (method.getReturnType().equals(Double.class)){ return ((Double)method.invoke(o, null)).floatValue(); } if (method.getReturnType().equals(Float.class)){ return ((Float)method.invoke(o, null)); } } } catch (IllegalAccessException illegalAccessException){ illegalAccessException.printStackTrace(); return null; } catch (InvocationTargetException invocationTargetException){ invocationTargetException.printStackTrace(); return null; } return null; } } @Override public Float getPosition(int x, int graph) { return (float)x; } @Override public Float getPositionMinimum() { return 0.0f; } @Override public Float getPositionMaximum() { return this.values.size()-1.0f; } private LinkedList plotProviderListeners = new LinkedList(); @Override public void addPlotProviderListener(PlotProviderListener listener) { plotProviderListeners.add(listener); } @Override public void removePlotProviderListener(PlotProviderListener listener) { plotProviderListeners.remove(listener); } private void fireFundamentalsChanged(){ for (PlotProviderListener l: plotProviderListeners) l.fundamentalsChanged(this); } }