]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/impl/Diagram.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / diagram / impl / Diagram.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.diagram.impl;
13
14 import java.util.Collection;
15
16 import org.simantics.g2d.diagram.DiagramClass;
17 import org.simantics.g2d.diagram.DiagramHints;
18 import org.simantics.g2d.diagram.IDiagram;
19 import org.simantics.g2d.element.IElement;
20 import org.simantics.g2d.element.impl.Element;
21 import org.simantics.utils.datastructures.hints.HintContext;
22
23 /**
24  * @author Toni Kalajainen
25  */
26 public class Diagram extends AbstractDiagram implements IDiagram {
27
28     public static final IDiagram EMPTY_DIAGRAM = spawnNew(DiagramClass.DEFAULT);
29
30     /**
31      * Spawn a new diagram to the world
32      * @param clazz
33      * @return
34      */
35     public static IDiagram spawnNew(DiagramClass clazz)
36     {
37         Diagram d = new Diagram(clazz);
38         AbstractDiagram.fireCreated(d);
39         return d;
40     }
41
42     /**
43      * Instantiate new diagram for purpose of loading from file...
44      * @param clazz
45      * @return
46      */
47     static Diagram instantiate(DiagramClass clazz)
48     {
49         Diagram d = new Diagram(clazz);
50         AbstractDiagram.fireCreated(d);
51         return d;
52     }
53
54     /**
55      * Creates an identical clone of a diagram.
56      * @param src
57      * @return
58      */
59     public static IDiagram clone(IDiagram src)
60     {
61         // Clone diagram
62         DiagramClass clazz = src.getDiagramClass();
63         Diagram d = new Diagram(clazz);
64         // Clone variables
65         d.setHints(src.getHints());
66         // Clone elements
67         for (IElement e : src.getElements())
68             d.addElement( Element.clone(e) );
69
70         AbstractDiagram.fireLoaded(d);
71         return d;
72     }
73
74     /**
75      * Creates an identical clone of a diagram.
76      * @param src
77      * @return
78      */
79     public static IDiagram clone(IDiagram src, Collection<IElement> elements)
80     {
81         // Clone diagram
82         DiagramClass clazz = src.getDiagramClass();
83         Diagram d = new Diagram(clazz);
84         // Clone variables
85         d.setHints(src.getHints());
86         // Clone elements
87         for (IElement e : elements) {
88             d.addElement( Element.clone(e) );
89         }
90         AbstractDiagram.fireLoaded(d);
91
92         return d;
93     }
94
95     Diagram(DiagramClass clazz)
96     {
97         super(clazz, new HintContext());
98     }
99
100     @Override
101     public String toString() {
102         String name = getHint(DiagramHints.KEY_TEXT);
103         if (name==null) name = super.toString();
104         return name;
105     }
106
107 }