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