1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.flowchart.painters;
15  
16  import y.view.EdgeLabel;
17  import y.view.GenericNodeRealizer;
18  import y.view.NodeRealizer;
19  import y.view.EdgeRealizer;
20  import y.view.PolyLineEdgeRealizer;
21  import y.view.Arrow;
22  import y.view.ShadowNodePainter;
23  
24  
25  import java.awt.Color;
26  import java.util.Map;
27  
28  /**
29   * This is a factory for elements conforming to the Flowchart diagrams.
30   * <p> Realizers for the different kinds of flowchart elements can be created (see e.g. {@link #createCard()},
31   * {@link #createData()}, {@link #createProcess()}...).</p>
32   */
33  public class FlowchartRealizerFactory implements FlowchartRealizerConstants{
34  
35    static {
36  
37      // Process
38      register(FLOWCHART_PROCESS_CONFIG_NAME, new FlowchartProcessPainter());
39  
40      // DirectDataPainter
41      register(FLOWCHART_DIRECT_DATA_CONFIG_NAME, new FlowchartDirectDataPainter());
42  
43      // DataBasePainter
44      register(FLOWCHART_DATABASE_CONFIG_NAME, new FlowchartDataBasePainter());
45  
46      // DecisionPainter
47      register(FLOWCHART_DECISION_CONFIG_NAME, new FlowchartDecisionPainter());
48  
49      // DocumentPainter
50      register(FLOWCHART_DOCUMENT_CONFIG_NAME, new FlowchartDocumentPainter());
51  
52      // DataPainter
53      register(FLOWCHART_DATA_CONFIG_NAME, new FlowchartDataPainter());
54  
55      // Start1
56      register(FLOWCHART_START1_CONFIG_NAME, new FlowchartStart1Painter());
57  
58      // Start2
59      register(FLOWCHART_START2_CONFIG_NAME, new FlowchartStart2Painter());
60  
61      // predefinedProcess
62      register(FLOWCHART_PREDEFINED_PROCESS_CONFIG_NAME, new FlowchartPredefinedProcessPainter());
63  
64      // Stored Data
65      register(FLOWCHART_STORED_DATA_CONFIG_NAME, new FlowchartStoredDataPainter());
66  
67      // Internal storage
68      register(FLOWCHART_INTERNAL_STORAGE_CONFIG_NAME, new FlowchartInternalStoragePainter());
69  
70      // SequentialData
71      register(FLOWCHART_SEQUENTIAL_DATA_CONFIG_NAME, new FlowchartSequentialDataPainter());
72  
73      // ManualInput
74      register(FLOWCHART_MANUAL_INPUT_CONFIG_NAME, new FlowchartManualInputPainter());
75  
76      // Card
77      register(FLOWCHART_CARD_CONFIG_NAME, new FlowchartCardPainter());
78  
79      // Paper tape
80      register(FLOWCHART_PAPER_TYPE_CONFIG_NAME, new FlowchartPaperTapePainter());
81  
82      // Cloud
83      register(FLOWCHART_CLOUD_TYPE_CONFIG_NAME, new FlowchartCloudPainter());
84  
85      // Delay
86      register(FLOWCHART_DELAY_CONFIG_NAME, new FlowchartDelayPainter());
87  
88      // Display
89      register(FLOWCHART_DISPLAY_CONFIG_NAME, new FlowchartDisplayPainter());
90  
91      // Manual operation
92      register(FLOWCHART_MANUAL_OPERATION_CONFIG_NAME, new FlowchartManualOperationPainter());
93  
94      // Preparation
95      register(FLOWCHART_PREPARATION_CONFIG_NAME, new FlowchartPreparationPainter());
96  
97      // Loop limit
98      register(FLOWCHART_LOOP_LIMIT_CONFIG_NAME, new FlowchartLoopLimitPainter());
99  
100     // Loop limit end
101     register(FLOWCHART_LOOP_LIMIT_END_CONFIG_NAME, new FlowchartLoopLimitPainter(false));
102 
103     // Terminator
104     register(FLOWCHART_TERMINATOR_CONFIG_NAME, new FlowchartTerminatorPainter());
105 
106     // On page reference
107     register(FLOWCHART_ON_PAGE_REFERENCE_CONFIG_NAME, new FlowchartOnPageReferencePainter());
108 
109     // Off page reference
110     register(FLOWCHART_OFF_PAGE_REFERENCE_CONFIG_NAME, new FlowchartOffPageReferencePainter());
111 
112     // Annotation
113     final GenericNodeRealizer.Painter painterImpl = new FlowchartAnnotationPainter();
114     GenericNodeRealizer.Factory factory = GenericNodeRealizer.getFactory();
115     Map implementationsMap = factory.createDefaultConfigurationMap();
116     implementationsMap.put(GenericNodeRealizer.Painter.class, new ShadowNodePainter(painterImpl));
117     implementationsMap.put(GenericNodeRealizer.ContainsTest.class, painterImpl);
118     implementationsMap.put(GenericNodeRealizer.LayerHandler.class, new FlowchartAnnotationLayerHandler());
119     factory.addConfiguration(FLOWCHART_ANNOTATION_CONFIG_NAME, implementationsMap);
120 
121     // User Message
122     register(FLOWCHART_USER_MESSAGE_CONFIG_NAME, new FlowchartMessagePainter(true));
123 
124     // Network Message
125     register(FLOWCHART_NETWORK_MESSAGE_CONFIG_NAME, new FlowchartMessagePainter(false));
126   }
127 
128   private FlowchartRealizerFactory() {
129   }
130 
131 
132   /**
133    * Creates a node realizer, that represents a "Direct Data" symbol. The realizer is not bound to a node.
134    * @return a flowchart "Direct Data" node realizer.
135    */
136   public static NodeRealizer createDirectData() {
137     return createConfigured(FLOWCHART_DIRECT_DATA_CONFIG_NAME);
138   }
139 
140   /**
141    * Creates a node realizer, that represents a "Data Base" symbol. The realizer is not bound to a node.
142    * @return a flowchart "Data Base" node realizer.
143    */
144   public static NodeRealizer createDataBase() {
145     GenericNodeRealizer nodeRealizer = createConfigured(FLOWCHART_DATABASE_CONFIG_NAME);
146     nodeRealizer.setSize(60,40);
147     return nodeRealizer;
148   }
149 
150   /**
151    * Creates a node realizer, that represents a "Process" symbol. The realizer is not bound to a node.
152    * @return a flowchart "Process" node realizer.
153    */
154   public static NodeRealizer createProcess() {
155     return createConfigured(FLOWCHART_PROCESS_CONFIG_NAME);
156   }
157 
158   /**
159    * Creates a node realizer, that represents a "Decision" symbol. The realizer is not bound to a node.
160    * @return a flowchart "Decision" node realizer.
161    */
162   public static NodeRealizer createDecision() {
163     return createConfigured(FLOWCHART_DECISION_CONFIG_NAME);
164   }
165 
166   /**
167    * Creates a node realizer, that represents a "Document" symbol. The realizer is not bound to a node.
168    * Creates a flowchart "Document" node realizer. The realizer is not bound to a node.
169    * @return a flowchart "Document" node realizer.
170    */
171   public static NodeRealizer createDocument() {
172     return createConfigured(FLOWCHART_DOCUMENT_CONFIG_NAME);
173   }
174 
175   /**
176    * Creates a node realizer, that represents a "Data" symbol. The realizer is not bound to a node.
177    * @return a flowchart "Data" node realizer.
178    */
179   public static NodeRealizer createData() {
180     return createConfigured(FLOWCHART_DATA_CONFIG_NAME);
181   }
182 
183   /**
184    * Creates a node realizer, that represents a "Start1" symbol. The realizer is not bound to a node.
185    * @return a flowchart "Start1" node realizer.
186    */
187   public static NodeRealizer createStart1() {
188     return createConfigured(FLOWCHART_START1_CONFIG_NAME);
189   }
190 
191   /**
192    *  Creates a node realizer, that represents a "Start2" symbol. The realizer is not bound to a node.
193    * @return a flowchart "Start2" node realizer.
194    */
195   public static NodeRealizer createStart2() {
196     return createConfigured(FLOWCHART_START2_CONFIG_NAME);
197   }
198 
199   /**
200    * Creates a node realizer, that represents a "Predefined Process" symbol. The realizer is not bound to a node.
201    * @return a flowchart "Predefined Process" node realizer.
202    */
203   public static NodeRealizer createPredefinedProcess() {
204     return createConfigured(FLOWCHART_PREDEFINED_PROCESS_CONFIG_NAME);
205   }
206 
207   /**
208    * Creates a node realizer, that represents a "Stored Data" symbol. The realizer is not bound to a node.
209    * @return a flowchart "Stored Data" node realizer.
210    */
211   public static NodeRealizer createStoredData() {
212     return createConfigured(FLOWCHART_STORED_DATA_CONFIG_NAME);
213   }
214 
215   /**
216    * Creates a node realizer, that represents an "Internal Storage" symbol. The realizer is not bound to a node.
217    * @return a flowchart "Internal Storage" node realizer.
218    */
219   public static NodeRealizer createInternalStorage() {
220     return createConfigured(FLOWCHART_INTERNAL_STORAGE_CONFIG_NAME);
221   }
222 
223   /**
224    * Creates a node realizer, that represents a "Sequential Data" symbol. The realizer is not bound to a node.
225    * @return a flowchart "Sequential Data" node realizer.
226    */
227   public static NodeRealizer createSequentialData() {
228     return createConfigured(FLOWCHART_SEQUENTIAL_DATA_CONFIG_NAME);
229   }
230 
231   /**
232    * Creates a node realizer, that represents a "Manual Input" symbol. The realizer is not bound to a node.
233    * @return a flowchart "Manual Input" node realizer.
234    */
235   public static NodeRealizer createManualInput() {
236     return createConfigured(FLOWCHART_MANUAL_INPUT_CONFIG_NAME);
237   }
238 
239   /**
240    * Creates a node realizer, that represents a "Card" symbol. The realizer is not bound to a node.
241    * @return a flowchart "Card" node realizer.
242    */
243   public static NodeRealizer createCard() {
244     return createConfigured(FLOWCHART_CARD_CONFIG_NAME);
245   }
246 
247   /**
248    * Creates a node realizer, that represents a "Paper Tape" symbol. The realizer is not bound to a node.
249    * @return a flowchart "Paper Tape" node realizer.
250    */
251   public static NodeRealizer createPaperTape() {
252     return createConfigured(FLOWCHART_PAPER_TYPE_CONFIG_NAME);
253   }
254 
255   /**
256    * Creates a node realizer, that represents a "Cloud" symbol. The realizer is not bound to a node.
257    * @return a flowchart "Cloud" node realizer.
258    */
259   public static NodeRealizer createCloud() {
260     GenericNodeRealizer nodeRealizer = createConfigured(FLOWCHART_CLOUD_TYPE_CONFIG_NAME);
261     nodeRealizer.setSize(80,50);
262     return nodeRealizer;
263   }
264 
265   /**
266    * Creates a node realizer, that represents a "Delay" symbol. The realizer is not bound to a node.
267    * @return a flowchart "Delay" node realizer.
268    */
269   public static NodeRealizer createDelay() {
270     return createConfigured(FLOWCHART_DELAY_CONFIG_NAME);
271   }
272 
273   /**
274    * Creates a node realizer, that represents a "Display" symbol. The realizer is not bound to a node.
275    * @return a flowchart "Display" node realizer.
276    */
277   public static NodeRealizer createDisplay() {
278     return createConfigured(FLOWCHART_DISPLAY_CONFIG_NAME);
279   }
280 
281   /**
282    * Creates a node realizer, that represents a "Manual Operation" symbol. The realizer is not bound to a node.
283    * @return a flowchart "Manual Operation" node realizer.
284    */
285   public static NodeRealizer createManualOperation() {
286     return createConfigured(FLOWCHART_MANUAL_OPERATION_CONFIG_NAME);
287   }
288 
289   /**
290    * Creates a node realizer, that represents a "Preparation" symbol. The realizer is not bound to a node.
291    * @return a flowchart "Preparation" node realizer.
292    */
293   public static NodeRealizer createPreparation() {
294     return createConfigured(FLOWCHART_PREPARATION_CONFIG_NAME);
295   }
296 
297   /**
298    * Creates a node realizer, that represents a "Loop Limit" symbol. The realizer is not bound to a node.
299    * @return a flowchart "Loop Limit" node realizer.
300    */
301   public static NodeRealizer createLoopLimit() {
302     return createConfigured(FLOWCHART_LOOP_LIMIT_CONFIG_NAME);
303   }
304 
305   /**
306    * Creates a node realizer, that represents a "Loop Limit End" symbol. The realizer is not bound to a node.
307    * @return a flowchart "Loop Limit End" node realizer
308    */
309   public static NodeRealizer createLoopLimitEnd() {
310     return createConfigured(FLOWCHART_LOOP_LIMIT_END_CONFIG_NAME);
311   }
312 
313   /**
314    * Creates a node realizer, that represents a "Terminator" symbol. The realizer is not bound to a node.
315    * @return a flowchart "Terminator" node realizer.
316    */
317   public static NodeRealizer createTerminator() {
318     return createConfigured(FLOWCHART_TERMINATOR_CONFIG_NAME);
319   }
320 
321   /**
322    * Creates a node realizer, that represents a "On Page Reference" symbol. The realizer is not bound to a node.
323    * @return a flowchart "On Page Reference" node realizer.
324    */
325   public static NodeRealizer createOnPageReference() {
326     return createConfigured(FLOWCHART_ON_PAGE_REFERENCE_CONFIG_NAME);
327   }
328 
329   /**
330    * Creates a node realizer, that represents a "Off Page Reference" symbol. The realizer is not bound to a node.
331    * @return a flowchart "Off Page Reference" node realizer.
332    */
333   public static NodeRealizer createOffPageReference() {
334     return createConfigured(FLOWCHART_OFF_PAGE_REFERENCE_CONFIG_NAME);
335   }
336 
337   /**
338    * Creates a node realizer, that represents an "Annotation" symbol. The realizer is not bound to a node.
339    * @param orientation The orientation of the bracket. Possible values:
340    * <ul>
341    * <li> {@link FlowchartRealizerConstants#PROPERTY_ORIENTATION_VALUE_AUTO} </li>
342    * <li> {@link FlowchartRealizerConstants#PROPERTY_ORIENTATION_VALUE_LEFT} </li>
343    * <li> {@link FlowchartRealizerConstants#PROPERTY_ORIENTATION_VALUE_RIGHT} </li>
344    * <li> {@link FlowchartRealizerConstants#PROPERTY_ORIENTATION_VALUE_TOP} </li>
345    * <li> {@link FlowchartRealizerConstants#PROPERTY_ORIENTATION_VALUE_DOWN} </li>
346    * </ul>
347    * @return a flowchart "Annotation" node realizer.
348    */
349   public static NodeRealizer createAnnotation(byte orientation) {
350     GenericNodeRealizer nodeRealizer = createConfigured(FLOWCHART_ANNOTATION_CONFIG_NAME);
351     nodeRealizer.setStyleProperty(PROPERTY_ORIENTATION, new Byte(orientation));
352     return nodeRealizer;
353   }
354 
355   /**
356    * Creates a node realizer, that represents a "User Message" symbol. The realizer is not bound to a node.
357    * @return a flowchart "User Message" node realizer.
358    */
359   public static NodeRealizer createUserMessage(){
360     return createConfigured(FLOWCHART_USER_MESSAGE_CONFIG_NAME);
361   }
362 
363   /**
364    * Creates a node realizer, that represents a "Network Message" symbol. The realizer is not bound to a node.
365    * @return a flowchart "Network Message" node realizer.
366    */
367   public static NodeRealizer createNetworkMessage(){
368     return createConfigured(FLOWCHART_NETWORK_MESSAGE_CONFIG_NAME);
369   }
370 
371   /**
372    * Creates an edge realizer, that represents a default connection between two flowchart nodes. The realizer is not bound to an edge.
373    * @return a flowchart "Default Connection" edge realizer.
374    */
375   public static EdgeRealizer createDefaultConnection() {
376     final PolyLineEdgeRealizer pel = new PolyLineEdgeRealizer();
377     pel.setSmoothedBends(true);
378     pel.setTargetArrow(Arrow.STANDARD);
379     pel.getLabel().setModel(EdgeLabel.SIX_POS);
380     pel.getLabel().setPosition(EdgeLabel.STAIL);
381     return pel;
382   }
383 
384   /**
385    * Creates an edge realizer, that represents a connection between two flowchart nodes. The connection is labeled as "No". The realizer is not bound to an edge.
386    * @return a flowchart "No-Connection" edge realizer.
387    */
388   public static EdgeRealizer createNoConnection() {
389     final PolyLineEdgeRealizer pel = new PolyLineEdgeRealizer();
390     pel.setSmoothedBends(true);
391     pel.setTargetArrow(Arrow.STANDARD);
392     pel.setLabelText("No");
393     pel.getLabel().setModel(EdgeLabel.SIX_POS);
394     pel.getLabel().setPosition(EdgeLabel.STAIL);
395     return pel;
396   }
397 
398   /**
399    * Creates an edge realizer, that represents a connection between two flowchart nodes. The connection is labeled as "Yes". The realizer is not bound to an edge.
400    * @return a flowchart "Yes-Connection" edge realizer.
401    */
402   public static EdgeRealizer createYesConnection() {
403     final PolyLineEdgeRealizer pel = new PolyLineEdgeRealizer();
404     pel.setSmoothedBends(true);
405     pel.setTargetArrow(Arrow.STANDARD);
406     pel.setLabelText("Yes");
407     pel.getLabel().setModel(EdgeLabel.SIX_POS);
408     pel.getLabel().setPosition(EdgeLabel.STAIL);
409     return pel;
410   }
411 
412   /**
413    * This method registers a {@link y.view.GenericNodeRealizer.Painter painter} implementation in the configuration map of the {@link y.view.GenericNodeRealizer}
414    * @param configName The name of the configuration
415    * @param impl The {@link y.view.GenericNodeRealizer.Painter painter} implementation
416    */
417   private static void register(final String configName, final GenericNodeRealizer.Painter impl) {
418     GenericNodeRealizer.Factory factory = GenericNodeRealizer.getFactory();
419     Map implementationsMap = factory.createDefaultConfigurationMap();
420     implementationsMap.put(GenericNodeRealizer.Painter.class, new ShadowNodePainter(impl));
421     implementationsMap.put(GenericNodeRealizer.ContainsTest.class, impl);
422     factory.addConfiguration(configName, implementationsMap);
423   }
424 
425   /**
426    * Creates a node realizer by given configuration name.
427    * @param configName The configuration name.
428    * @return A GenericNodeRealizer
429    */
430   private static GenericNodeRealizer createConfigured(String configName) {
431     GenericNodeRealizer nodeRealizer = new GenericNodeRealizer();
432     nodeRealizer.setConfiguration(configName);
433     nodeRealizer.setFillColor2(new Color(183, 201, 227));
434     nodeRealizer.setFillColor(new Color(232, 238, 247));
435     nodeRealizer.setSize(80, 40);
436     return nodeRealizer;
437   }
438 }
439