Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/pqt/autorib/rib/RIBAttributeRec.java


1   //AutoRIB
2   // Copyright © 1998 - 2002, P W Quint
3   //
4   // Contact: autorib00@aol.com
5   //
6   // This library is free software; you can redistribute it and/or
7   // modify it under the terms of the GNU General Public
8   // License as published by the Free Software Foundation; either
9   // version 2 of the License, or (at your option) any later version.
10  //
11  // This library is distributed in the hope that it will be useful,
12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  // General Public License for more details.
15  //
16  // You should have received a copy of the GNU General Public
17  // License along with this library; if not, write to the Free Software
18  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  
20  package org.pqt.autorib.rib;
21  import java.util.*;
22  import java.io.*;
23  import org.pqt.autorib.tokenizer.*;
24  import org.pqt.autorib.globals.*;
25  import org.pqt.autorib.util.*;
26  
27  
28  /**Stores various attribute information for an
29   * object (color, shaders etc)
30   * it uses two types of special content = one for Declares and one for
31   * implementation dependent Attributes. Declare statements are, of course,
32   * not strictly attributes, but are treated as such in AutoRIB. Note also
33   * that Attribute "identifier" "name" is treated in a special way - it is stored
34   * in the name variable and must be written out separately.
35   *@version $Header: /home/CVSRepository/autoribnew/org/pqt/autorib/rib/RIBAttributeRec.java,v 1.5 2003/12/02 17:10:42 remote Exp $
36   */
37  public class RIBAttributeRec extends RIBStateRec {
38      
39      /** the name of the object, as set by Attribute
40       * "identifier" "name" in the RIB
41       */
42      public String name = "";
43      
44      /** another RIBAttribute rec which is searched for attributes
45       * if they are not found in the present AttributeRec*/
46      private RIBAttributeRec ancestor = null;
47      
48      /** default constructor
49       */
50      public RIBAttributeRec() {
51          super(2);
52      }
53      
54      /** Create an attribute record that inherits attributes
55       * from the given record. It does not inherit / copy
56       * across Declares.
57       * @param a the attribute record to inherit from
58       */
59      public RIBAttributeRec(RIBAttributeRec a) {
60          super((RIBStateRec) a, false);
61          newSpecialContent[1] = (Vector) a.newSpecialContent[1].clone();
62          name = a.name;
63      }
64      
65      /** add an attribute to the store of attributes,
66       * return true if this was indeed an attribute
67       * @param rq the request to try storing
68       * @return true if the request was an attribute
69       */
70      public boolean put(RIBRequest rq) {
71          boolean isPut = true;
72          boolean isName = false;
73          switch (rq.requestID) {
74              case RIBGlobals.ATMOSPHERE:
75              case RIBGlobals.COLOR:
76              case RIBGlobals.OPACITY:
77              case RIBGlobals.SURFACE:
78              case RIBGlobals.INTERIOR:
79              case RIBGlobals.EXTERIOR:
80              case RIBGlobals.DISPLACEMENT:
81              case RIBGlobals.TEXTURECOORDINATES:
82              case RIBGlobals.SHADINGRATE:
83              case RIBGlobals.SHADINGINTERPOLATION:
84              case RIBGlobals.MATTE:
85              case RIBGlobals.BOUND:
86              case RIBGlobals.DETAIL:
87              case RIBGlobals.DETAILRANGE:
88              case RIBGlobals.GEOMETRICAPPROXIMATION:
89              case RIBGlobals.ORIENTATION:
90              case RIBGlobals.REVERSEORIENTATION:
91              case RIBGlobals.SIDES:
92              case RIBGlobals.BASIS:
93              case RIBGlobals.TRIMCURVE:
94                  newContent.remove(new Integer(rq.requestID));
95                  newContent.put(new Integer(rq.requestID),
96                  rq);
97                  break;
98              case RIBGlobals.DECLARE:
99                  newSpecialContent[0].addElement(rq);
100                 break;
101             case RIBGlobals.ATTRIBUTE:
102                 Token t;
103                 RIBStandard rbs = (RIBStandard) rq;
104                 if (rbs.content.size() == 3) {
105                     t = (Token) rbs.content.elementAt(0);
106                     if ((t.tokenType == Token.STRING) &&
107                     (t.stringVal.equals("identifier"))) {
108                         t = (Token) rbs.content.elementAt(1);
109                         if ((t.tokenType == Token.STRING) &&
110                         (t.stringVal.equals("name"))) {
111                             t = (Token) rbs.content.elementAt(2);
112                             name = t.getAsString();
113                             isName = true;
114                         }
115                     }
116                 }
117                 if (!isName)
118                     newSpecialContent[1].addElement(rq);
119                 break;
120             default:
121                 //deal with any other attributes we may have missed
122                 if (RIBGlobals.arraySearch(rq.requestID, RIBGlobals.attributes)) {
123                     newContent.remove(new Integer(rq.requestID));
124                     newContent.put(new Integer(rq.requestID),
125                     rq);
126                     break;
127                 }
128                 else
129                     isPut = false;
130                 break;
131         }
132         return isPut;
133     }
134     
135     
136     /** get the color from the Attribute record
137      * @return the color
138      */
139     public Color getColor() {
140         RIBColor rqc = (RIBColor) get(RIBGlobals.COLOR); //inheritance handled by get
141         return rqc.color;
142     }
143     
144     /** a static method converting a string description
145      * of a shader into an integer tag, as defined in
146      * RIBGlobals
147      * @param type a string with the shader type e.g. "displacement"
148      * @return an integer representing the shader type
149      */
150     public static int shaderType(String type) {
151         int rid = -1;
152         if (type.equals("surface"))
153             rid = RIBGlobals.SURFACE;
154         else if (type.equals("displacement"))
155             rid = RIBGlobals.DISPLACEMENT;
156         else if (type.equals("exterior"))
157             rid = RIBGlobals.EXTERIOR;
158         else if (type.equals("interior"))
159             rid = RIBGlobals.INTERIOR;
160         else if (type.equals("atmosphere"))
161             rid = RIBGlobals.ATMOSPHERE;
162         return rid;
163     }
164     
165     /** retrieve a shader of the given type, returning
166      * null if a shader of this type has not been
167      * defined
168      * @param type the type of shader to return, as a string e.g "displacement"
169      * @return a shader, or null
170      */
171     public RIBShader getShader(String type) {
172         int rid = shaderType(type);
173         return rid < 0 ? null : (RIBShader) get(rid); //inheritance handled by get
174     }
175     
176     /** standard clone
177      * @return a clone of this object
178      */
179     public Object clone() {
180         RIBAttributeRec rar = (RIBAttributeRec) super.clone();
181         rar.name = name;
182         return rar;
183     }
184     
185     /** Set a RIBAttributeRec that is searched for attributes if the given attribute
186      *  is not found in the current RIBAttributeRec. Thus a call to getColor for instance
187      *  will search the RIBAttributeRec that this rec inherits from if no Color attribute
188      *  has been set in this rec. Note that the RIBAttributeRec given as a parameter
189      *  is not cloned.
190      * @param inheritsFrom the RIBAttributeRec to inherit from*/
191     public void setInheritance(RIBAttributeRec inheritsFrom) {
192         this.inheritsFrom = inheritsFrom;
193     }
194     
195 }
196 
197 
198