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

Quick Search    Search Deep

Source code: jpicedt/format/pstricks/parser/PSTColorExpression.java


1   /*  jPicEdt version 1.3.2, a picture editor for LaTeX.
2       Copyright (C) 1999-2002  Sylvain Reynal
3   
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8   
9       This program is distributed in the hope that it will be useful,
10      but WITHOUT ANY WARRANTY; without even the implied warranty of
11      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12      GNU General Public License for more details.
13  
14      You should have received a copy of the GNU General Public License
15      along with this program; if not, write to the Free Software
16      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  
18      Sylvain Reynal
19      Département de Physique
20      Ecole Nationale Supérieure de l'Electronique et de ses Applications (ENSEA)
21      6, avenue du Ponceau
22      95014 CERGY CEDEX
23      FRANCE
24  
25      Tel : 00 +33 130 736 245
26      Fax : 00 +33 130 736 667
27      e-mail : reynal@ensea.fr
28      jPicEdt web page : http://www.jpicedt.org
29  */
30  
31  package jpicedt.format.pstricks.parser;
32  
33  import jpicedt.graphic.io.parser.*;
34  import jpicedt.format.pstricks.PstricksUtilities;
35  import jpicedt.graphic.model.*;
36  
37  import java.awt.*;
38  import java.util.*;
39  
40  /**
41   * Used by PSTParametersExpression to parse statements involving colours, for instance
42   *  "linecolor=green" (predefined colour) or "fillcolor=MyGray" (user-defined colours).
43   * <p>
44   * If no pstricks's native colour matches the RHS, we fetch a HashMap from the Pool's hashtable 
45   * (value associated with key PstricksParser.KEY_USER_COLOURS) and look up the given colour ; 
46   * this HashMap may've been filled by UserDefinedColorExpression in the course of the parsing process.
47   * @author $Author: reynal $
48   * @version $Id: PSTColorExpression.java,v 1.2 2002/08/05 16:44:10 reynal Exp $ 
49   *
50   */
51  public class PSTColorExpression extends SequenceExpression {
52  
53      private Pool pool;
54      private PicAttributeName attribute;
55    private Pool.Key setKey;
56  
57      /**
58       * @param pl parser's pool
59       * @param tag LHS tag (e.g. "linecolor" or "fillcolor") for the StatementExpression
60       * @param attribute name of attribute to modify (must be a predefined PicAttributeName of type "Color")
61     * @param attributeSetKey used to fetch the attribute set in which parsed parameters are stored. 
62       */
63      public PSTColorExpression(Pool pl, String tag, PicAttributeName attributeName, Pool.Key attributeSetKey){
64  
65      super(false); // doesn't throw IncompleteSequenceException (which means that there might be nothing b/w [ and ])
66  
67      pool = pl;
68      attribute = attributeName;
69      this.setKey = attributeSetKey;
70  
71      add(new LiteralExpression(tag+"=")); // e.g. "linecolor"
72      add(new ParseColorValue());
73      }
74    
75    private class ParseColorValue extends WordExpression {
76      public ParseColorValue(){
77        // no postfix, swallows as many letters-or-digit as possible (since user-defined colours may contain digits)
78        super(null,false,true,true);
79      }
80      public void action(ParserEvent e){
81        if (DEBUG) System.out.println(e); 
82        String colourStr = (String)(e.getValue());
83        //System.out.println("!!!!!!!!!!!!!!!!!!!! FOUND COLOR : " + colourStr);
84        Color colour = PstricksUtilities.getPsTricksColor(colourStr);
85        if (colour == null) { // if not a native colour, look up hashtable
86          HashMap userColoursMap = (HashMap)pool.get(PstricksParser.KEY_USER_COLOURS);
87          colour = (Color)(userColoursMap.get(colourStr));
88          if (colour == null) // if nor a native colour, nor a user-defined colour, default to black...
89            colour = Color.black;
90        }
91        pool.setAttribute(setKey,attribute, colour);
92      }
93    }
94  } // class
95