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.entityrelationship;
15  
16  import demo.view.entityrelationship.painters.ErdRealizerFactory;
17  import demo.view.flowchart.FlowchartPalette;
18  import y.view.Arrow;
19  import y.view.EdgeRealizer;
20  import y.view.GenericNodeRealizer;
21  import y.view.Graph2D;
22  import y.view.Graph2DView;
23  import y.view.NodeRealizer;
24  
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  /**
30   * This is a component, which provides templates for entity relationship diagram (ERD)
31   * nodes and edges that can be dragged into a Graph2DView.
32   */
33  public class EntityRelationshipPalette extends FlowchartPalette {
34  
35    private Map arrowNames;
36    private Map nodeNames;
37  
38    /**
39     * Creates a new <code>EntityRelationshipPalette</code> with a pre-configured list of
40     * node and edge realizers.
41     * @param view a view of the graph the palette is assigned to
42     */
43    public EntityRelationshipPalette(final Graph2DView view) {
44      super(view);
45  
46      arrowNames = new HashMap();
47      arrowNames.put(Arrow.NONE, "Unspecified");
48      arrowNames.put(Arrow.CROWS_FOOT_ONE, "(1)");
49      arrowNames.put(Arrow.CROWS_FOOT_MANY, "(N)");
50      arrowNames.put(Arrow.CROWS_FOOT_ONE_OPTIONAL, "(0,1)");
51      arrowNames.put(Arrow.CROWS_FOOT_ONE_MANDATORY, "(1,1)");
52      arrowNames.put(Arrow.CROWS_FOOT_MANY_OPTIONAL, "(0,N)");
53      arrowNames.put(Arrow.CROWS_FOOT_MANY_MANDATORY, "(1,N)");
54    }
55  
56    /**
57     * Initializes default realizers for the specified view's associated graph.
58     * @param view The respective Graph2DView.
59     */
60    protected void initializeDefaultRealizers(Graph2DView view) {
61      Graph2D graph2D = view.getGraph2D();
62      graph2D.setDefaultNodeRealizer(ErdRealizerFactory.createBigEntity());
63      graph2D.setDefaultEdgeRealizer(ErdRealizerFactory.createRelation(Arrow.NONE));
64    }
65  
66    /**
67     * Adds default ERD templates to the palette list.
68     * @param realizers The list of all template realizers
69     */
70    protected void addDefaultTemplates(final List realizers) {
71  
72      //create node templates
73      final NodeRealizer bigEntity = ErdRealizerFactory.createBigEntity();
74      final GenericNodeRealizer smallEntity = ErdRealizerFactory.createSmallEntity("Entity Name");
75      final GenericNodeRealizer weakSmallEntity = ErdRealizerFactory.createWeakSmallEntity("Entity Name");
76      final GenericNodeRealizer attribute = ErdRealizerFactory.createAttribute("Attribute");
77      final GenericNodeRealizer multiValuedAttribute = ErdRealizerFactory.createMultiValuedAttribute("Attribute");
78      final GenericNodeRealizer primaryKeyAttribute = ErdRealizerFactory.createPrimaryKeyAttribute("Attribute");
79      final GenericNodeRealizer derivedAttribute = ErdRealizerFactory.createDerivedAttribute("Attribute");
80      final GenericNodeRealizer relationship = ErdRealizerFactory.createRelationship("Relation");
81      final GenericNodeRealizer weakRelationship = ErdRealizerFactory.createWeakRelationship("Relation");
82  
83      //add node templates to list
84      realizers.add(bigEntity);
85      realizers.add(smallEntity);
86      realizers.add(weakSmallEntity);
87      realizers.add(attribute);
88      realizers.add(multiValuedAttribute);
89      realizers.add(primaryKeyAttribute);
90      realizers.add(derivedAttribute);
91      realizers.add(relationship);
92      realizers.add(weakRelationship);
93  
94      //register tooltips for node templates
95      nodeNames = new HashMap();
96      nodeNames.put(bigEntity, "Entity with Attributes");
97      nodeNames.put(smallEntity, "Entity");
98      nodeNames.put(weakSmallEntity, "Weak Entity");
99      nodeNames.put(attribute, "Attribute");
100     nodeNames.put(multiValuedAttribute, "Multi-Valued Attribute");
101     nodeNames.put(primaryKeyAttribute, "Primary Key");
102     nodeNames.put(derivedAttribute, "Derived Attribute");
103     nodeNames.put(relationship, "Relationship");
104     nodeNames.put(weakRelationship, "Weak Relationship");
105 
106     //add edge templates to list
107     realizers.add(ErdRealizerFactory.createRelation(Arrow.NONE));
108     realizers.add(ErdRealizerFactory.createRelation(Arrow.CROWS_FOOT_ONE));
109     realizers.add(ErdRealizerFactory.createRelation(Arrow.CROWS_FOOT_MANY));
110     realizers.add(ErdRealizerFactory.createRelation(Arrow.CROWS_FOOT_ONE_OPTIONAL));
111     realizers.add(ErdRealizerFactory.createRelation(Arrow.CROWS_FOOT_ONE_MANDATORY));
112     realizers.add(ErdRealizerFactory.createRelation(Arrow.CROWS_FOOT_MANY_OPTIONAL));
113     realizers.add(ErdRealizerFactory.createRelation(Arrow.CROWS_FOOT_MANY_MANDATORY));
114   }
115 
116   /**
117    * Creates text for a tooltip that appears if you move the mouse over an
118    * edge icon in the palette.
119    * @param realizer the realizer of the selected edge template
120    * @return a string that shows the text assigned to the edge template
121    */
122   protected String createEdgeToolTipText(EdgeRealizer realizer){
123     return (String) arrowNames.get(realizer.getSourceArrow());
124   }
125 
126   /**
127    * Creates text for a tooltip that appears if you move the mouse over a
128    * node icon in the palette.
129    * @param realizer the realizer of the selected node template
130    * @return a string that shows the text assigned to the node template
131    */
132   protected String createNodeToolTipText(NodeRealizer realizer){
133     return (String) nodeNames.get(realizer);
134   }
135 
136 }
137