View Javadoc

1   package org.lcsim.geometry.compact;
2   
3   import java.awt.Color;
4   
5   import org.jdom.DataConversionException;
6   import org.jdom.Element;
7   
8   /**
9    * This class stores visualization settings for Subdetector objects.
10   * 
11   * @author Jeremy McCormick <jeremym@slac.stanford.edu>
12   * @version $Id: VisAttributes.java,v 1.5 2010/12/03 01:21:14 jeremy Exp $
13   *
14   */
15  public class VisAttributes
16  {
17      // Visualization attributes.
18      private float[] rgba = new float[4];
19      private Color color = null;
20      private String name = null;
21      private String linestyle = "unbroken";
22      private String drawingstyle = "wireframe";
23      private boolean visible = true;
24      private boolean showdaughters = true;
25  
26      /**
27       * Constructor with name and defaults.
28       * @param name The name of the visualization settings.
29       */
30      public VisAttributes( String name )
31      {
32          this.name = name;
33      }
34  
35      /**
36       * Constructor that takes an XML element in the compact format.
37       * @param node The XML node.
38       */
39      protected VisAttributes( Element node )
40      {
41          try
42          {
43              this.name = node.getAttributeValue( "name" );
44  
45              // Create a Java Color from the input RGBA values.
46              if ( node.getAttribute( "r" ) != null )
47              {
48                  this.rgba[ 0 ] = (float) node.getAttribute( "r" ).getDoubleValue();
49              }
50  
51              if ( node.getAttribute( "g" ) != null )
52              {
53                  this.rgba[ 1 ] = (float) node.getAttribute( "g" ).getDoubleValue();
54              }
55  
56              if ( node.getAttribute( "b" ) != null )
57              {
58                  this.rgba[ 2 ] = (float) node.getAttribute( "b" ).getDoubleValue();
59              }
60  
61              if ( node.getAttribute( "alpha" ) != null )
62              {
63                  this.rgba[ 3 ] = (float) node.getAttribute( "alpha" ).getDoubleValue();
64              }
65  
66              // Create the Java Color from the RGBA input values.
67              this.color = new Color( rgba[ 0 ], rgba[ 1 ], rgba[ 2 ], rgba[ 3 ] );
68  
69              if ( node.getAttribute( "lineStyle" ) != null )
70              {
71                  this.linestyle = node.getAttributeValue( "lineStyle" );
72              }
73  
74              if ( node.getAttribute( "showDaughters" ) != null )
75              {
76                  this.showdaughters = node.getAttribute( "showDaughters" )
77                          .getBooleanValue();
78              }
79  
80              if ( node.getAttribute( "visible" ) != null )
81              {
82                  this.visible = node.getAttribute( "visible" ).getBooleanValue();
83              }
84  
85              if ( node.getAttribute( "drawingStyle" ) != null )
86              {
87                  this.drawingstyle = node.getAttributeValue( "drawingStyle" );
88              }
89          }
90          catch ( DataConversionException e )
91          {
92              throw new RuntimeException( e );
93          }
94      }
95  
96      /**
97       * Set visibility.
98       * @param visible True to make visible; false to make invisible.
99       */
100     public void setVisible( boolean visible )
101     {
102         this.visible = visible;
103     }
104 
105     /**
106      * Set colors between 0.0 and 1.0.
107      * @param r Red color component value.
108      * @param g Green color component value.
109      * @param b Blue color component value.
110      * @param a Alpha component value.
111      */
112     public final void setColor( float r, float g, float b, float a )
113     {
114         rgba[ 0 ] = r;
115         rgba[ 1 ] = g;
116         rgba[ 2 ] = b;
117         rgba[ 3 ] = a;
118 
119         color = new Color( r, g, b, a );
120     }
121 
122     /**
123      * Set the color from an {@see java.awt.Color} object.
124      * @param color The java Color.
125      */
126     public final void setColor( Color color )
127     {
128         this.color = color;
129 
130         rgba[ 0 ] = color.getRed();
131         rgba[ 1 ] = color.getGreen();
132         rgba[ 2 ] = color.getBlue();
133         rgba[ 3 ] = color.getAlpha();
134     }
135 
136     /**
137      * Set whether daughters are visible.
138      * @param b True to show daughters; false to hide.
139      */
140     public final void setShowDaughters( boolean b )
141     {
142         showdaughters = b;
143     }
144 
145     /**
146      * Set the drawing style.
147      * @param drawingstyle
148      */
149     public final void setDrawingStyle( String drawingstyle )
150     {
151         this.drawingstyle = drawingstyle;
152     }
153 
154     /**
155      * Set the lineStyle of these attributes.
156      * @param s The lineStyle.
157      */
158     public final void setLineStyle( String s )
159     {
160         this.linestyle = s;
161     }
162 
163     /**
164      * Get the <code>Color</code> of these settings.
165      * @return The Color.
166      */
167     public final Color getColor()
168     {
169         return color;
170     }
171 
172     /**
173      * Get whether visibility is on or off. 
174      * @return True if visible; false if not.
175      */
176     public final boolean getVisible()
177     {
178         return visible;
179     }
180     
181     /**
182      * Get whether visibility is on or off. 
183      * @return True if visible; false if not.
184      */
185     public final boolean isVisible()
186     {
187         return visible;
188     }
189 
190     /**
191      * Get showDaughters setting.
192      * @return showDaughters setting.
193      */
194     public final boolean getShowDaughters()
195     {
196         return showdaughters;
197     }
198 
199     /**
200      * Get the drawing style.  
201      * Possible settings same as Geant4.
202      * @return The drawing style.
203      */
204     public final String getDrawingStyle()
205     {
206         return drawingstyle;
207     }
208 
209     /**
210      * Get the line style.  
211      * Possible settings same as Geant4.
212      * @return The line style.
213      */
214     public final String getLineStyle()
215     {
216         return linestyle;
217     }
218 
219     /**
220      * Get the name of these visualization settings.
221      * @return The vis name.
222      */
223     public final String getName()
224     {
225         return name;
226     }
227 
228     /**
229      * Get the RGBA values as a float array.
230      * @return The RGBA values.
231      */
232     public final float[] getRGBA()
233     {
234         return rgba;
235     }
236 }