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

Quick Search    Search Deep

Source code: com/port80/util/attr/PointFactory.java


1   //
2   // Copyright(c) 2002, Chris Leung
3   //
4   
5   package com.port80.util.attr;
6   
7   import java.awt.geom.*;
8   
9   import com.port80.util.msg;
10  
11  
12  
13  /** Point attribute factory.
14   *
15   *  Since .dot file have coordinate system with origin at lower-left
16   *  corner instead of upper-left corner used in java.awt, coordinates
17   *  need to be converted when reading or writing to .dot file.
18   *  DotMode convert between the two systems.
19   *
20   */
21  public class PointFactory implements IAttrFactory {
22  
23      // Static fields ///////////////////////////////////////////////////////
24      //
25      private static final String NAME="PointFactory";
26      private static PointFactory instance=null;
27  
28      // Instance fields /////////////////////////////////////////////////////
29      //
30      private static boolean isDotMode=false;
31      private static double maxY=0.0;
32  
33      // State methods ///////////////////////////////////////////////////////
34      //
35      public static void dotModeOn(double maxy) {
36    isDotMode=true;
37    maxY=maxy;
38      }
39      public static void dotModeOff() { isDotMode=false;}
40  
41  
42      ////////////////////////////////////////////////////////////////////////
43  
44      /** @return The singleton instance of the factory. */
45      public static PointFactory getInstance() {
46    if(instance==null) instance=new PointFactory();
47    return instance;
48      }
49  
50      /** @return An instance of the specified shape. */
51      public static Point2D create(String stringvalue) {
52    if(instance==null) instance=new PointFactory();
53    return (Point2D)instance.createObject(stringvalue);
54      }
55  
56      private PointFactory() {}
57  
58      // IAttrFactory interface //////////////////////////////////////////////
59      //
60  
61      /** Create an instance of route from the String spec.
62       *
63       *  Current accepted .dot file string format:
64       *      float x,float y
65       */
66      public Object createObject(String str) {
67    int index=str.indexOf(',');
68    if(index<=0) {
69        msg.err(NAME+".createObject(): invalid input string: "+str);
70        return null;
71    }
72    double x=0;
73    double y=0;
74    try {
75        x=Double.parseDouble(str.substring(0,index));
76        y=Double.parseDouble(str.substring(index+1));
77    } catch(Exception e) { msg.err(NAME+".createObject(): invalid input string: "+str); }
78    if(isDotMode) return new Point2D.Double(x,maxY-y);
79    return new Point2D.Double(x,y);
80      }
81  
82      public boolean isValid(Object a) {
83        return (a instanceof Point2D);
84      }
85          
86      /** The String representation of an attribute value. */
87      public String toString(Object attr) {
88    Point2D pt=(Point2D)attr;
89    double x=pt.getX();
90    double y=pt.getY();
91    if(isDotMode) return ""+x+","+(maxY-y);
92    else return ""+x+","+y;
93    /*
94    Point2D pt=(Point2D)attr;
95    return ""+pt.getX()+","+pt.getY();
96    */
97      }
98  
99      /** The String representation of attribute type itself. */
100     public String toString() {
101   return NAME;
102     }
103 
104     /** Prompt user and present a user interface to obtain an
105      *  attribute value from user. */
106     public Object promptUser(String prompt) {
107   return null;
108     }
109 
110     ////////////////////////////////////////////////////////////////////////
111 
112 }
113