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