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

Quick Search    Search Deep

Source code: jpicedt/format/pstricks/PstricksUtilities.java


1   /*
2    PstricksUtilities.java - March 1, 2002 - jPicEdt 1.3.2, a picture editor for LaTeX.
3    Copyright (C) 1999-2002 Sylvain Reynal
4   
5    Département de Physique
6    Ecole Nationale Supérieure de l'Electronique et de ses Applications (ENSEA)
7    6, avenue du Ponceau
8    F-95014 CERGY CEDEX
9   
10   Tel : +33 130 736 245
11   Fax : +33 130 736 667
12   e-mail : reynal@ensea.fr
13   jPicEdt web page : http://www.jpicedt.org/
14    
15   This program is free software; you can redistribute it and/or
16   modify it under the terms of the GNU General Public License
17   as published by the Free Software Foundation; either version 2
18   of the License, or any later version.
19    
20   This program is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   GNU General Public License for more details.
24    
25   You should have received a copy of the GNU General Public License
26   along with this program; if not, write to the Free Software
27   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28   */
29   
30  package jpicedt.format.pstricks;
31  
32  import jpicedt.graphic.model.Element;
33  import jpicedt.graphic.model.Arrow;
34  import jpicedt.graphic.model.PicObjectConstants;
35  
36  import java.awt.*;
37  import java.util.*;
38  import java.io.*;
39  
40  /**
41   * Collection of static methods for the PsTricks format
42   * @author $Author: reynal $
43   * @version $Id: PstricksUtilities.java,v 1.2 2002/08/05 16:44:10 reynal Exp $ 
44   * @since jPicEdt 1.3.2
45   */
46  public class PstricksUtilities implements PstricksConstants, PicObjectConstants {
47    
48    /**
49     * @return PsTricks's colour name from Color object, 
50     * a null String if none matches (in which case the caller might define a new colour by using a \\newrgbcolor of a \\newgray...)
51     */
52    public static String getPsTricksColorName(Color c){
53      for(int i=0; i<PST_COLORS.length; i++){
54        if (PST_COLORS[i] == c) return PST_COLORNAMES[i];
55      }
56      return null;
57    }
58  
59    /**
60     * @return A Color object corresponding to the given PsTricks's colour name ; 
61     * a null Color if none matches (in which case the caller might look up a table of user-defined colours to see if one matches)
62     */
63    public static Color getPsTricksColor(String name){
64      for(int i=0; i<PST_COLORNAMES.length; i++){
65        if (PST_COLORNAMES[i].equals(name)) return PST_COLORS[i];
66      }
67      return null;
68    }
69  
70    
71    /**
72     * @return a string representing the given Arrow in the PsTricks syntax, e.g. ">" or "(" (or "" for NONE)
73     * @param rightArrow if TRUE, returns the String representation of this Arrow if it's a right-arrow ; 
74     *        otherwise, returns the left-arrow string.
75     */
76    public static String toPstricksString(Arrow arrow, boolean rightArrow){
77      // lookup arrow in the array of predefined arrows :
78      int j=0; // default = Arrow.NONE
79      for (int i=0; i<PST_ARROWS.length; i++){
80        if (arrow == PST_ARROWS[i]){
81          j = i;
82          break;
83        }
84      }
85      if (rightArrow) return PST_ARROWS_RIGHT[j]; else return PST_ARROWS_LEFT[j]; 
86    }
87    
88    /**
89     * @return PsTricks's arrow string for the given Element (e.g. "{<->}") with parenthesis included.
90     */
91    public static StringBuffer createPstricksStringFromArrows(Element obj){
92  
93      StringBuffer buf = new StringBuffer(5);
94      Arrow leftArrow = (Arrow)obj.getAttribute(LEFT_ARROW);
95      Arrow rightArrow = (Arrow)obj.getAttribute(RIGHT_ARROW);
96      buf.append("{");
97      buf.append(toPstricksString(leftArrow,false)); // e.g. "<" for ARROW_HEAD
98      buf.append("-");
99      buf.append(toPstricksString(rightArrow,true)); // e.g. ">" for ARROW_HEAD (this way, we really get "<->" instead of ">->" which was wrong !)
100     buf.append("}");
101     return buf;
102   }
103 
104 
105   /**
106    * @return an array containing two Arrows corresponding to the given PsTricks string
107    * (e.g. "-" or "<-" or ">>->" ...), left arrow first.
108    */
109   public static Arrow[] createArrowsFromPstricksString(String str){
110 
111     Arrow[] tab = new Arrow[2];
112     tab[0] = tab[1] = Arrow.NONE; // security
113     StringTokenizer tokenizer = new StringTokenizer(str, "-");
114     String tokenLeft="";
115     String tokenRight="";
116     tab[0] = tab[1] = Arrow.NONE; // security
117     if (tokenizer.hasMoreTokens()){ // this is not a "-" string !
118       if (str.startsWith("-")) { // e.g. "->"
119         tokenLeft = "";
120         tokenRight = tokenizer.nextToken();
121       }
122       else { // e.g. "<-" or "<->"
123         tokenLeft = tokenizer.nextToken();
124         if (tokenizer.hasMoreTokens()){ // e.g. "<->"
125           tokenRight = tokenizer.nextToken();
126         }
127         else tokenRight = "";
128       }
129     }
130 
131     for(int i=0; i<PST_ARROWS.length; i++){ // [pending] "do-while" loop is faster here.
132       if (tokenLeft.equals(PST_ARROWS_LEFT[i])) {
133         tab[0]=PST_ARROWS[i];
134         break;
135       }
136     }
137     for(int i=0; i<PST_ARROWS.length; i++){ // [pending] "do-while" loop is faster here.
138       if (tokenRight.equals(PST_ARROWS_RIGHT[i])) {
139         tab[1]=PST_ARROWS[i];
140         break;
141       }
142     }
143     return tab;
144   }
145 
146 
147   /**
148    * tester
149    */
150   public static void main(String arg[]){
151 
152     //  System.out.println("arg=" + arg[0]);
153     Arrow[] arrows;
154     String str = "<->";
155     while(true){
156       try {
157         System.out.println("Input string ?");
158         BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
159         str = r.readLine();
160         System.out.println("OK !");
161       }
162       catch(Exception e){e.printStackTrace();}
163 
164       arrows = createArrowsFromPstricksString(str);
165       System.out.println("Left Arrow = (" + arrows[0] + ")");
166       System.out.println("Right Arrow = (" + arrows[1] + ")");
167     }
168   }
169 
170 
171 
172   
173   
174 }
175 
176