Source code: com/port80/graph/impl/StrokeFactory.java
1 //
2 // Copyright(c) 2002, Chris Leung
3 //
4
5 package com.port80.graph.impl;
6
7 import java.awt.BasicStroke;
8 import java.awt.Color;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.Map;
12
13 import com.port80.util.msg;
14 import com.port80.util.attr.IAttrFactory;
15
16 /** Style attribute factory.
17 *
18 */
19 public class StrokeFactory implements IAttrFactory {
20
21 ////////////////////////////////////////////////////////////////////////
22
23 private static final String NAME = "StrokeFactory";
24 private static StrokeFactory instance = null;
25
26 public static final int STYLE_SOLID = 0;
27 public static final int STYLE_DASHED = 1;
28 public static final int STYLE_DOTTED = 2;
29 /** FILLED is equivalent to SOLID. Fill is solely determined by
30 * fillcolor!=null and can be combined with other style
31 * eg. DOTTED with fill.*/
32 public static final int STYLE_FILLED = 3;
33 public static final int STYLE_HIDE = 4;
34
35 private static final float DEF_LINEWIDTH = 1f;
36 private static final float DEF_BUSWIDTH = 3f;
37
38 private static String[] nameTable = { "solid", "dashed", "dotted", "filled", "hide", "none" };
39 private static Map table;
40 static {
41 table = new HashMap();
42 table.put("none", new GraphStroke("none", null,0f));
43 table.put("hide", new GraphStroke("hide", null,0f));
44 stockSolidStroke("solid", DEF_LINEWIDTH);
45 stockSolidStroke("filled", DEF_LINEWIDTH);
46 stockDashedStroke("dashed", DEF_LINEWIDTH);
47 stockDottedStroke("dotted", DEF_LINEWIDTH);
48 stockBusStroke("bus", DEF_BUSWIDTH);
49 }
50
51 ////////////////////////////////////////////////////////////////////////
52
53 public static StrokeFactory getInstance() {
54 if (instance == null)
55 instance = new StrokeFactory();
56 return instance;
57 }
58 public static GraphStroke create(String stringvalue) {
59 if (instance == null)
60 instance = new StrokeFactory();
61 return (GraphStroke) instance.createObject(stringvalue);
62 }
63 private StrokeFactory() {}
64
65 ////////////////////////////////////////////////////////////////////////
66
67 /** Convert String representation of an attribute value to the
68 * appropriate object for storing in AttrTable.
69 *
70 * Accepted format for Stroke:
71 * stylename[,linewidth_scale]
72 *
73 * where stylename=solid|dashed|dotted|hide and linewidth_scale
74 * is a float number.
75 */
76 public Object createObject(String stringvalue) {
77 if (stringvalue == null || stringvalue.length() == 0)
78 stringvalue = "solid";
79 Object ret = table.get(stringvalue);
80 if (ret != null)
81 return ret;
82 // Custom.
83 int index = stringvalue.indexOf(',');
84 String name;
85 float scale=1.0f;
86 if (index > 0) {
87 name = stringvalue.substring(0, index);
88 scale = msg.parseFloat(stringvalue.substring(index + 1), -1f);
89 if (scale < 0)
90 scale = 1.0f;
91 } else {
92 name = stringvalue;
93 }
94 if (name.equals("solid") || name.equals("filled"))
95 return stockSolidStroke(stringvalue, DEF_LINEWIDTH * scale);
96 if (name.equals("dashed"))
97 return stockDashedStroke(stringvalue, DEF_LINEWIDTH * scale);
98 if (name.equals("dotted"))
99 return stockDottedStroke(stringvalue, DEF_LINEWIDTH * scale);
100 if (name.equals("bus"))
101 return stockBusStroke(stringvalue, DEF_BUSWIDTH * scale);
102 msg.err(
103 NAME
104 + ".create(String): invalid spec: "
105 + stringvalue
106 + "\n\t Expected format: typename,linewidth_scale");
107 return table.get("solid");
108 }
109
110 public boolean isValid(Object a) {
111 return (a instanceof GraphStroke);
112 }
113
114 /** The String representation of an attribute value. */
115 public String toString(Object attr) {
116 for (Iterator it = table.keySet().iterator(); it.hasNext();) {
117 String key = (String) it.next();
118 if (table.get(key) == attr)
119 return key;
120 }
121 msg.err("StrokeFactory.toString(Object): attribute object not found in factory table.");
122 return null;
123 }
124
125 /** The String representation of attribute type itself. */
126 public String toString() {
127 return NAME;
128 }
129
130 /** Prompt user and present a user interface to obtain an
131 * attribute value from user. */
132 public Object promptUser(String prompt) {
133 return null;
134 }
135
136 ////////////////////////////////////////////////////////////////////////
137 public static GraphStroke stockSolidStroke(String name, float linewidth) {
138 BasicStroke stroke = new BasicStroke(linewidth);
139 GraphStroke ret = new GraphStroke(name, stroke,linewidth);
140 table.put(name, ret);
141 return ret;
142 }
143 public static GraphStroke stockDashedStroke(String name, float linewidth) {
144 BasicStroke stroke =
145 new BasicStroke(
146 linewidth,
147 BasicStroke.CAP_BUTT,
148 BasicStroke.JOIN_MITER,
149 10.0f,
150 new float[] { 7f,3f},
151 0f);
152 GraphStroke ret = new GraphStroke(name, stroke, linewidth);
153 table.put(name, ret);
154 return ret;
155 }
156 public static GraphStroke stockDottedStroke(String name, float linewidth) {
157 if(false) msg.println(NAME+".stockDottedStroke(): name="+name+", linewidth="+linewidth);
158 BasicStroke stroke =
159 new BasicStroke(
160 linewidth,
161 BasicStroke.CAP_BUTT,
162 BasicStroke.JOIN_MITER,
163 10.0f,
164 new float[] { 2f, 3f },
165 0f);
166 GraphStroke ret = new GraphStroke(name, stroke, linewidth);
167 table.put(name, ret);
168 return ret;
169 }
170 public static GraphStroke stockBusStroke(String name, float linewidth) {
171 BasicStroke stroke = new BasicStroke(linewidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
172 GraphStroke ret = new GraphStroke(name, stroke, linewidth);
173 table.put(name, ret);
174 return ret;
175 }
176
177 ////////////////////////////////////////////////////////////////////////
178
179 }