View Javadoc

1   // Copyright 2003-2005, FreeHEP.
2   package hep.wired.heprep.graphicspanel;
3   
4   import java.awt.*;
5   import java.io.*;
6   
7   import org.freehep.graphics2d.VectorGraphics;
8   import hep.graphics.heprep.HepRepInstance;
9   import hep.graphics.heprep.HepRepIterator;
10  import hep.graphics.heprep.HepRepAttributeAdapter;
11  import hep.graphics.heprep.HepRepDefaults;
12  import hep.graphics.heprep.xml.XMLHepRepReader;
13  import hep.graphics.heprep.util.HepRepFont;
14  
15  import hep.wired.heprep.services.Attributes;
16  import hep.wired.heprep.services.GraphicsMode;
17  
18  /***
19   * Cache for subscribed attributes
20   *
21   * @author Mark Donszelmann
22   * @version $Id: AttributeCache.java 2181 2005-08-03 19:11:28Z duns $
23   */
24  
25  public class AttributeCache implements Attributes {
26  
27      private static final boolean visibility0;
28      private static final boolean pickable0;
29      private static final String drawAs0;
30      private static final Color color0;
31      private static final double lineWidth0;
32      private static final int lineHasArrow0;
33      private static final Color frameColor0;
34      private static final double frameWidth0;
35      private static final Color fillColor0;
36      private static final boolean fill0;
37      private static final int markSymbol0;
38      private static final double markSize0;
39  
40      private boolean visibility; 
41      private boolean pickable;
42      private String drawAs;
43      private Color color;
44      private double lineWidth;
45      private int lineHasArrow;
46      private Color frameColor;
47      private double frameWidth;
48      private Color fillColor;
49      private boolean fill;
50      private int markSymbol;
51      private double markSize;
52  
53      private Color overrideColor;
54      private Color overrideFrameColor;
55      private Color overrideFillColor;
56  
57      private VectorGraphics graphics;
58      private HepRepGraphicsMode mode;
59  
60      static {
61          boolean error = false;
62          try {
63              XMLHepRepReader.readDefaults();
64          } catch (IOException e) {
65              System.out.println("AttributeCache: could not read heprep defaults from XML file.");
66              error = true;
67          }
68          
69          if (error) {
70              visibility0   = true;
71              pickable0     = true; 
72              drawAs0       = "point";
73              color0        = Color.WHITE;
74              lineWidth0    = 1.0;
75              lineHasArrow0 = ARROW_NONE;
76              frameColor0   = Color.BLACK;
77              frameWidth0   = 1.0;
78              fillColor0    = Color.WHITE;
79              fill0         = false;
80              markSymbol0   = VectorGraphics.SYMBOL_BOX;
81              markSize0     = 1.0;
82          } else {
83              visibility0   = HepRepDefaults.getAttValue("visibility").getBoolean();
84              pickable0     = HepRepDefaults.getAttValue("ispickable").getBoolean();
85              drawAs0       = HepRepDefaults.getAttValue("drawas").getString();
86              color0        = HepRepDefaults.getAttValue("color").getColor();
87              lineWidth0    = HepRepDefaults.getAttValue("linewidth").getDouble();
88              lineHasArrow0 = getArrowSymbol(HepRepDefaults.getAttValue("linehasarrow").getString().toLowerCase());
89              frameColor0   = HepRepDefaults.getAttValue("framecolor").getColor();
90              frameWidth0   = HepRepDefaults.getAttValue("framewidth").getDouble();
91              fillColor0    = HepRepDefaults.getAttValue("fillcolor").getColor();
92              fill0         = HepRepDefaults.getAttValue("fill").getBoolean();
93              markSymbol0   = VectorGraphics.getSymbol(HepRepDefaults.getAttValue("markname").getString().toLowerCase());
94              markSize0     = HepRepDefaults.getAttValue("marksize").getDouble();
95          }
96      }        
97  
98      /***
99       * Creates a cache for subscribed attributes.
100      */
101     public AttributeCache(VectorGraphics graphics, HepRepGraphicsMode mode, HepRepIterator iterator) {
102         visibility    = visibility0;
103         pickable      = pickable0;
104         drawAs        = drawAs0;
105         color         = color0;
106         lineWidth     = lineWidth0;
107         lineHasArrow  = lineHasArrow0;
108         frameColor    = frameColor0;
109         frameWidth    = frameWidth0;
110         fillColor     = fillColor0;
111         fill          = fill0;
112         markSymbol    = markSymbol0;
113         markSize      = markSize0;
114         
115         overrideColor = null;
116         overrideFrameColor = null;
117         overrideFillColor = null;
118         
119         this.graphics = graphics;
120         this.mode = mode;
121         subscribeTo(iterator);
122     }
123 
124     public boolean isVisible() {
125         return visibility;
126     }
127 
128     public boolean isPickable() {
129         return pickable;
130     }
131 
132     public String getDrawAs() {
133         return drawAs;
134     }
135     
136     public Color getColor() {
137         return overrideColor != null ? overrideColor : color;
138     }
139 
140     public double getLineWidth() {
141         return lineWidth;
142     }    
143     
144     public int hasLineArrow() {
145         return lineHasArrow;
146     }
147     
148     public Color getFrameColor() {
149         return overrideFrameColor != null ? overrideFrameColor : frameColor;
150     }
151     
152     public double getFrameWidth() {
153         return frameWidth;
154     }
155     
156     public Color getFillColor() {
157         return overrideFillColor != null ? overrideFillColor : fillColor;
158     }
159     
160     public boolean isFilled() {
161         return fill;
162     }
163     
164     public int getMarkSymbol() {
165         return markSymbol;
166     }
167     
168     public double getMarkSize() {
169         return markSize;
170     }
171     
172     public void overrideColor(Color color) {
173         overrideColor = color;
174         graphics.setColor(getColor());
175     }
176     
177     public void overrideFrameColor(Color color) {
178         overrideFrameColor = color;
179     }
180     
181     public void overrideFillColor(Color color) {
182         overrideFillColor = color;
183     }
184     
185     private void subscribeTo(HepRepIterator iterator) {
186         iterator.addHepRepAttributeListener("visibility", new HepRepAttributeAdapter() {       
187             public void setAttribute(HepRepInstance instance, String name, boolean value, int showLabel) {
188                 visibility = value;
189             }
190             public void removeAttribute(HepRepInstance instance, String name) {
191                 visibility = visibility0;
192             }
193         });
194         
195         iterator.addHepRepAttributeListener("isPickable", new HepRepAttributeAdapter() {       
196             public void setAttribute(HepRepInstance instance, String name, boolean value, int showLabel) {
197                 pickable = value;
198             }
199             public void removeAttribute(HepRepInstance instance, String name) {
200                 pickable = pickable0;
201             }
202         });
203         
204         iterator.addHepRepAttributeListener("drawas", new HepRepAttributeAdapter() {
205             public void setAttribute(HepRepInstance instance, String name, String value, String lowerCaseValue, int showLabel) {
206                 drawAs = lowerCaseValue;
207             }
208             public void removeAttribute(HepRepInstance instance, String name) {
209                 drawAs = drawAs0;
210             }
211         });
212         
213         iterator.addHepRepAttributeListener("color", new HepRepAttributeAdapter() {
214             public void setAttribute(HepRepInstance instance, String name, Color value, int showLabel) {
215                 color = value;
216                 graphics.setColor(color);                
217             }
218             public void removeAttribute(HepRepInstance instance, String name) {
219                 color = color0;
220                 graphics.setColor(color);                
221             }            
222         });
223         
224         iterator.addHepRepAttributeListener("framecolor", new HepRepAttributeAdapter() {
225             public void setAttribute(HepRepInstance instance, String name, Color value, int showLabel) {
226                 frameColor = value;
227             }
228             public void removeAttribute(HepRepInstance instance, String name) {
229                 frameColor = frameColor0;
230             }            
231         });
232         
233         iterator.addHepRepAttributeListener("linewidth", new HepRepAttributeAdapter() {       
234             public void setAttribute(HepRepInstance instance, String name, double value, int showLabel) {           
235                 lineWidth = mode.drawWideLines 
236                           ? value*mode.lineWidthMultiplier
237                           : 1.0;
238                 graphics.setLineWidth(lineWidth);
239             }
240             public void removeAttribute(HepRepInstance instance, String name) {
241                 lineWidth = lineWidth0;
242                 graphics.setLineWidth(lineWidth);
243             }
244         });
245 
246         iterator.addHepRepAttributeListener("linehasarrow", new HepRepAttributeAdapter() {       
247             public void setAttribute(HepRepInstance instance, String name, String value, String lowerCaseValue, int showLabel) {           
248                 lineHasArrow = getArrowSymbol(lowerCaseValue);
249             }
250             public void removeAttribute(HepRepInstance instance, String name) {
251                 lineHasArrow = lineHasArrow0;
252             }
253         });
254 
255         iterator.addHepRepAttributeListener("framewidth", new HepRepAttributeAdapter() {       
256             public void setAttribute(HepRepInstance instance, String name, double value, int showLabel) {
257                 frameWidth = value;
258             }
259             public void removeAttribute(HepRepInstance instance, String name) {
260                 frameWidth = frameWidth0;
261             }
262         });
263                
264         iterator.addHepRepAttributeListener("fillcolor", new HepRepAttributeAdapter() {
265             public void setAttribute(HepRepInstance instance, String name, Color value, int showLabel) {
266                 fillColor = value;
267             }
268             public void removeAttribute(HepRepInstance instance, String name) {
269                 fillColor = fillColor0;
270             }            
271         });
272         
273         iterator.addHepRepAttributeListener("fill", new HepRepAttributeAdapter() {       
274             public void setAttribute(HepRepInstance instance, String name, boolean value, int showLabel) {
275                 fill = mode.fillBoxes ? value : false;
276             }
277             public void removeAttribute(HepRepInstance instance, String name) {
278                 fill = fill0;
279             }
280         });
281         
282         iterator.addHepRepAttributeListener("markname", new HepRepAttributeAdapter() {       
283             public void setAttribute(HepRepInstance instance, String name, String value, String lowerCaseValue, int showLabel) {
284                 markSymbol = VectorGraphics.getSymbol(lowerCaseValue);
285             }
286             public void removeAttribute(HepRepInstance instance, String name) {
287                 markSymbol = markSymbol0;
288             }
289         });      
290 
291         iterator.addHepRepAttributeListener("marksize", new HepRepAttributeAdapter() {       
292             public void setAttribute(HepRepInstance instance, String name, double value, int showLabel) {
293                 markSize = value;
294             }
295             public void removeAttribute(HepRepInstance instance, String name) {
296                 markSize = markSize0;
297             }
298         });      
299     }
300 
301     private static int getArrowSymbol(String lowerCaseName) {
302         if (lowerCaseName.equals("none"))  return ARROW_NONE;
303         if (lowerCaseName.equals("start")) return ARROW_START;
304         if (lowerCaseName.equals("end"))   return ARROW_END;
305         return ARROW_BOTH;
306     }
307 }