1
28 package demo.view.entityrelationship.painters;
29
30 import y.view.Arrow;
31 import y.view.EdgeRealizer;
32 import y.view.GenericNodeRealizer;
33 import y.view.LineType;
34 import y.view.NodeLabel;
35 import y.view.NodeRealizer;
36 import y.view.PolyLineEdgeRealizer;
37 import y.view.ShadowNodePainter;
38 import y.view.SmartNodeLabelModel;
39 import y.view.YLabel;
40
41 import java.awt.Color;
42 import java.awt.Font;
43 import java.util.Map;
44
45
52 public class ErdRealizerFactory {
53
58 public static final String BIG_ENTITY = "com.yworks.entityRelationship.big_entity";
59
60
65 public static final String SMALL_ENTITY = "com.yworks.entityRelationship.small_entity";
66
67
72 public static final String ATTRIBUTE = "com.yworks.entityRelationship.attribute";
73
74
79 public static final String RELATIONSHIP = "com.yworks.entityRelationship.relationship";
80
81
85 public static final String LABEL_NAME = "com.yworks.entityRelationship.label.name";
86
90 public static final String LABEL_ATTRIBUTES = "com.yworks.entityRelationship.label.attributes";
91
92
98 public static final String DOUBLE_BORDER = "com.yworks.entityRelationship.doubleBorder";
99
100 private static final Color PRIMARY_COLOR = new Color(232, 238, 247, 255);
102 private static final Color SECONDARY_COLOR = new Color(183, 201, 227, 255);
103
104
105
106 static {
107
108 registerNodePainter(BIG_ENTITY, new ErdNodePainter());
110
111 registerNodePainter(SMALL_ENTITY, new ErdSmallEntityNodePainter());
113
114 registerNodePainter(ATTRIBUTE, new ErdAttributeNodePainter());
116
117 registerNodePainter(RELATIONSHIP, new ErdRelationshipNodePainter());
119
120 registerLabelConfigurations();
122
123 }
124
125
132 public static NodeRealizer createBigEntity(){
133
134 GenericNodeRealizer erdRealizer = new GenericNodeRealizer(BIG_ENTITY);
135 erdRealizer.setSize(90,100);
136 erdRealizer.setFillColor(PRIMARY_COLOR);
137 erdRealizer.setFillColor2(SECONDARY_COLOR);
138
139 NodeLabel nameLabel = erdRealizer.getLabel();
140 nameLabel.setConfiguration(LABEL_NAME);
141 nameLabel.setBackgroundColor(SECONDARY_COLOR);
142 nameLabel.setPosition(NodeLabel.TOP);
143 nameLabel.setText("Entity Name");
144
145 NodeLabel attributesLabel = erdRealizer.createNodeLabel();
146 erdRealizer.addLabel(attributesLabel);
147 attributesLabel.setAlignment(NodeLabel.ALIGN_LEFT);
148 ErdAttributesNodeLabelModel model = new ErdAttributesNodeLabelModel();
149 attributesLabel.setLabelModel(model, model.getDefaultParameter());
150 attributesLabel.setConfiguration(LABEL_ATTRIBUTES);
151 attributesLabel.setText("Attribute 1\nAttribute 2\nAttribute 3");
152
153 return erdRealizer;
154 }
155
156
163 public static GenericNodeRealizer createSmallEntity(String label) {
164 final GenericNodeRealizer smallEntity = new GenericNodeRealizer(SMALL_ENTITY);
165 configure(smallEntity);
166 smallEntity.setLabelText(label);
167
168 return smallEntity;
169 }
170
171
179 public static GenericNodeRealizer createWeakSmallEntity(String label){
180
181 final GenericNodeRealizer smallEntity = new GenericNodeRealizer(SMALL_ENTITY);
182 configure(smallEntity);
183 smallEntity.setStyleProperty(DOUBLE_BORDER, Boolean.TRUE);
184 smallEntity.setLabelText(label);
185
186 return smallEntity;
187 }
188
189
195 public static GenericNodeRealizer createAttribute(String label){
196
197 final GenericNodeRealizer attribute = new GenericNodeRealizer(ATTRIBUTE);
198 configure(attribute);
199 attribute.setLabelText(label);
200
201 return attribute;
202 }
203
204
212 public static GenericNodeRealizer createMultiValuedAttribute(String label){
213
214 final GenericNodeRealizer attribute = new GenericNodeRealizer(ATTRIBUTE);
215 configure(attribute);
216 attribute.setStyleProperty(DOUBLE_BORDER, Boolean.TRUE);
217 attribute.setLabelText(label);
218
219 return attribute;
220 }
221
222
230 public static GenericNodeRealizer createPrimaryKeyAttribute(String label){
231
232 final GenericNodeRealizer attribute = new GenericNodeRealizer(ATTRIBUTE);
233 configure(attribute);
234 attribute.getLabel().setFontStyle(Font.BOLD);
235 attribute.getLabel().setUnderlinedTextEnabled(true);
236 attribute.setLabelText(label);
237
238 return attribute;
239 }
240
241
249 public static GenericNodeRealizer createDerivedAttribute(String label){
250
251 final GenericNodeRealizer attribute = new GenericNodeRealizer(ATTRIBUTE);
252 configure(attribute);
253 attribute.setLineType(LineType.DASHED_1);
254 attribute.setLabelText(label);
255
256 return attribute;
257 }
258
259
266 public static GenericNodeRealizer createRelationship(String label){
267
268 final GenericNodeRealizer relation = new GenericNodeRealizer(RELATIONSHIP);
269 configure(relation);
270 relation.setLabelText(label);
271
272 return relation;
273 }
274
275
283 public static GenericNodeRealizer createWeakRelationship(String label){
284
285 final GenericNodeRealizer relation = new GenericNodeRealizer(RELATIONSHIP);
286 configure(relation);
287 relation.setStyleProperty(DOUBLE_BORDER, Boolean.TRUE);
288 relation.setLabelText(label);
289
290 return relation;
291 }
292
293
299 public static boolean isBigEntityRealizer(NodeRealizer realizer){
300
301 if(realizer instanceof GenericNodeRealizer){
302 if(BIG_ENTITY.equals(((GenericNodeRealizer) realizer).getConfiguration())){
303 return true;
304 }
305 }
306
307 return false;
308 }
309
310
317 public static boolean isSmallEntityRealizer(NodeRealizer realizer){
318
319 if(realizer instanceof GenericNodeRealizer){
320 if(SMALL_ENTITY.equals(((GenericNodeRealizer) realizer).getConfiguration())){
321 return true;
322 }
323 }
324
325 return false;
326
327 }
328
329
338 public static boolean isAttributeRealizer(NodeRealizer realizer){
339 if(realizer instanceof GenericNodeRealizer){
340 if(ATTRIBUTE.equals(((GenericNodeRealizer) realizer).getConfiguration())){
341 return true;
342 }
343 }
344
345 return false;
346
347 }
348
349
356 public static boolean isRelationshipRealizer(NodeRealizer realizer){
357 if(realizer instanceof GenericNodeRealizer){
358 if(RELATIONSHIP.equals(((GenericNodeRealizer) realizer).getConfiguration())){
359 return true;
360 }
361 }
362
363 return false;
364
365 }
366
367
371 private static void configure(GenericNodeRealizer realizer) {
372 realizer.setFillColor(PRIMARY_COLOR);
373 realizer.setFillColor2(SECONDARY_COLOR);
374 realizer.setSize(80, 40);
375 NodeLabel label = realizer.getLabel();
376 SmartNodeLabelModel model = new SmartNodeLabelModel();
377 label.setLabelModel(model, model.getDefaultParameter());
378 }
379
380
386 public static EdgeRealizer createRelation(Arrow arrow){
387
388 final PolyLineEdgeRealizer pler = new PolyLineEdgeRealizer();
389 pler.setSourceArrow(arrow);
390
391 return pler;
392 }
393
394
400 public static EdgeRealizer createRelation(Arrow sourceArrow, Arrow targetArrow){
401
402 final PolyLineEdgeRealizer pler = new PolyLineEdgeRealizer();
403 pler.setSourceArrow(sourceArrow);
404 pler.setTargetArrow(targetArrow);
405
406 return pler;
407 }
408
409
414 private static void registerNodePainter(String configName, GenericNodeRealizer.Painter impl) {
415
416 GenericNodeRealizer.Factory factory = GenericNodeRealizer.getFactory();
417 Map implementationsMap = factory.createDefaultConfigurationMap();
418 implementationsMap.put(GenericNodeRealizer.Painter.class, new ShadowNodePainter(impl));
419 implementationsMap.put(GenericNodeRealizer.ContainsTest.class, impl);
420 factory.addConfiguration(configName, implementationsMap);
421
422 }
423
424
428 private static void registerLabelConfigurations() {
429
430 final YLabel.Factory lf = NodeLabel.getFactory();
431 final Map lnc = lf.createDefaultConfigurationMap();
432 lnc.put(YLabel.Painter.class, new ErdNameLabelPainter());
433 lf.addConfiguration(LABEL_NAME, lnc);
434
435 final Map lac = lf.createDefaultConfigurationMap();
436 final ErdAttributesLabelConfiguration eac = new ErdAttributesLabelConfiguration();
437 lac.put(YLabel.Layout.class, eac);
438 lac.put(YLabel.Painter.class, eac);
439 lf.addConfiguration(LABEL_ATTRIBUTES, lac);
440
441 }
442
443 }
444