View Javadoc

1   
2   package org.catacomb.graph.drawing;
3   
4   import org.catacomb.be.ReReferencable;
5   import org.catacomb.graph.gui.Painter;
6   import org.catacomb.report.E;
7   
8   
9   import java.awt.Color;
10  
11  
12  
13  public abstract class FixedDrawingComponent implements ReReferencable, Cloneable {
14  
15  
16      public String lineColor;
17      public String lineWidth;
18  
19  
20      protected Color p_color;
21      protected double p_width;
22      private int p_widthStyle;
23  
24  
25      public final static int PIXELS = 0;
26      public final static int RELATIVE = 1;
27  
28  
29      public String fillColor;
30  
31      private Color p_fill;
32  
33  
34      private int p_closure;
35      public final static int OPEN = 1;
36      public final static int CLOSED = 2;
37      public final static int FILLED = 3;
38  
39  
40  
41  
42      public FixedDrawingComponent() {
43          p_width=1.;
44          p_color = Color.black;
45      }
46  
47  
48      public FixedDrawingComponent copy() {
49          Object ret = null;
50          try {
51              ret = clone();
52          } catch (Exception ex) {
53              E.error("whats this " + ex);
54          }
55          return (FixedDrawingComponent)ret;
56      }
57  
58  
59      public boolean isWrapper() {
60          return false;
61      }
62  
63  
64      public void setColor(Color c) {
65          p_color = c;
66      }
67  
68  
69      public void setWidth(double d) {
70          p_width = d;
71          p_widthStyle = PIXELS;
72      }
73  
74  
75  
76      public Color getColor() {
77          return p_color;
78      }
79  
80      public void setLineColor(Color c) {
81          p_color = c;
82      }
83  
84  
85  
86      public void setLineWidth(double d) {
87          p_width = d;
88      }
89  
90      public double getWidth() {
91          return p_width;
92      }
93  
94      public boolean widthIsPixels() {
95          return (p_widthStyle == PIXELS);
96      }
97  
98      public void setFillColor(Color c) {
99          p_fill = c;
100         p_closure = FILLED;
101     }
102 
103     public Color getFillColor() {
104         return p_fill;
105     }
106 
107 
108     public abstract void instruct(Painter p, double offx, double offy, double scale);
109 
110 
111 
112     public void reReference() {
113 
114         p_widthStyle = PIXELS; // parseWidthStyle(lineWidth);
115 
116         p_width = parseWidth(lineWidth);
117 
118         p_color = parseColor(lineColor);
119 
120         if (fillColor == null) {
121             p_closure = CLOSED;
122         } else {
123             p_closure = FILLED;
124             p_fill = parseColor(fillColor);
125         }
126     }
127 
128 
129     public boolean isOpen() {
130         return (p_closure == OPEN);
131     }
132 
133 
134     public boolean isClosed() {
135         return (p_closure == CLOSED);
136     }
137 
138 
139     public boolean isFilled() {
140         return (p_closure == FILLED);
141     }
142 
143 
144     public void setClosed() {
145         p_closure = CLOSED;
146     }
147 
148     public void setOpen() {
149         p_closure = OPEN;
150     }
151 
152     public void setFilled() {
153         p_closure = FILLED;
154     }
155 
156 
157     public Color parseColor(String s) {
158         Color ret = Color.black;
159         if (s != null && s.startsWith("#") && s.length() == 7) {
160             String s1 = s.substring(1, s.length());
161             int icol = Integer.parseInt(s1, 16);
162             ret = new Color(icol);
163 
164         } else if (s == null || s.length() == 0) {
165 
166         } else {
167             E.error(" - cant read color " + s);
168         }
169         return ret;
170     }
171 
172 
173 
174 
175     public int parseWidthStyle(String win) {
176         String w = win;
177         int iret = PIXELS;
178         if (w == null) {
179             // leave as is;
180 
181         } else {
182             w = w.trim();
183             if (w.endsWith("px")) {
184                 iret = PIXELS;
185             } else {
186                 iret = RELATIVE;
187             }
188         }
189         return iret;
190     }
191 
192 
193     public double parseWidth(String win) {
194         String w = win;
195         double ret = 1.;
196         if (w != null) {
197             w = w.trim();
198 
199             if (w.endsWith("px")) {
200                 w = w.substring(0, w.length()-2);
201             }
202 
203             ret = (new Double(w)).doubleValue();
204         }
205         return ret;
206     }
207 
208 
209     public Shape makeShape() {
210         Shape shp = new Shape();
211 
212         shp.setFillColor(Color.black);
213         if (isClosed()) {
214             shp.setClosure(Shape.CLOSED);
215 
216         } else if (isOpen()) {
217             shp.setClosure(Shape.OPEN);
218 
219         } else if (isFilled()) {
220             shp.setClosure(Shape.FILLED);
221             shp.setFillColor(getFillColor());
222         }
223 
224         shp.setLineColor(p_color);
225         shp.setLineWidth(p_width);
226         applyToShape(shp);
227         shp.makePoints();
228         shp.syncArrays();
229         return shp;
230     }
231 
232 
233     public abstract void applyToShape(Shape shp);
234 
235 
236 }