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.uml;
29  
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * The class model holds the class details like its name, attributes and operations. This part will be stored when the
36   * diagram is saved. The class {@link UmlClassModelIOHandler} handles the (de)serialization.
37   *
38   * In addition it stores the visual state of the class: which section is currently open and which list item is currently
39   * selected. This part will not be (de)serialized. The {@link y.view.GenericNodeRealizer uml class realizer} stores the
40   * model in its {@link y.view.GenericNodeRealizer#getUserData() user data field}.
41   */
42  public class UmlClassModel implements Cloneable {
43  
44    /** Constant that is used to specify that the currently selected item belongs to the attribute list. */
45    public static final int LIST_ATTRIBUTES =  0;
46  
47    /** Constant that is used to specify that the currently selected item belongs to the operation list. */
48    public static final int LIST_OPERATIONS =  1;
49  
50    /** Constant that is used to notify that no list item is currently selected. */
51    public static final int LIST_INDEX_NONE =  -1;
52  
53    private String className;
54    private List attributes;
55    private List operations;
56  
57    private boolean sectionsVisible;
58    private boolean attributesVisible;
59    private boolean operationsVisible;
60    private int selectedList;
61    private int selectedListIndex;
62  
63    public UmlClassModel() {
64      this("Name", new ArrayList(), new ArrayList());
65    }
66  
67    public UmlClassModel(final String name, final List attributes, final List operations) {
68      this.attributes = attributes;
69      this.className = name;
70      this.operations = operations;
71  
72      sectionsVisible = true;
73      attributesVisible = true;
74      operationsVisible = true;
75      selectedList = LIST_ATTRIBUTES;
76      selectedListIndex = LIST_INDEX_NONE;
77    }
78  
79    public String getClassName() {
80      return className;
81    }
82  
83    public void setClassName(final String className) {
84      this.className = className;
85    }
86  
87    public List getAttributes() {
88      return attributes;
89    }
90  
91    public void setAttributes(final List attributes) {
92      this.attributes = attributes;
93    }
94  
95    public List getOperations() {
96      return operations;
97    }
98  
99    public void setOperations(final List operations) {
100     this.operations = operations;
101   }
102 
103   /**
104    * Checks whether or not the sections for attributes and operations are visible or if only the name of the class is
105    * visible.
106    */
107   public boolean areSectionsVisible() {
108     return sectionsVisible;
109   }
110 
111   /**
112    * Specifies whether or not the sections for attributes and operations are visible or if only the name of the class is
113    * visible.
114    */
115   public void setSectionsVisible(final boolean sectionsVisible) {
116     this.sectionsVisible = sectionsVisible;
117   }
118 
119   /**
120    * Checks whether or not the list of attributes is visible.
121    */
122   public boolean areAttributesVisible() {
123     return attributesVisible;
124   }
125 
126   /**
127    * Specifies whether or not the list of attributes is visible.
128    */
129   public void setAttributesVisible(final boolean attributesVisible) {
130     this.attributesVisible = attributesVisible;
131   }
132 
133   /**
134    * Checks whether or not the list of attributes is visible.
135    */
136   public boolean areOperationsVisible() {
137     return operationsVisible;
138   }
139 
140   /**
141    * Specifies whether or not the list of operations is visible.
142    */
143   public void setOperationsVisible(final boolean operationsVisible) {
144     this.operationsVisible = operationsVisible;
145   }
146 
147   /**
148    * Returns the list of the currently selected item. There are the following lists:
149    * <ul>
150    *   <li>{@link #LIST_ATTRIBUTES}</li>
151    *   <li>{@link #LIST_OPERATIONS}</li>
152    * </ul>
153    */
154   public int getSelectedList() {
155     return selectedList;
156   }
157 
158   /**
159    * Specifies the list of the currently selected item. There are the following lists:
160    * <ul>
161    *   <li>{@link #LIST_ATTRIBUTES}</li>
162    *   <li>{@link #LIST_OPERATIONS}</li>
163    * </ul>
164    */
165   public void setSelectedList(final int list) {
166     switch (list) {
167       case LIST_ATTRIBUTES:
168       case LIST_OPERATIONS:
169         selectedList = list;
170         return;
171       default:
172         throw new IllegalArgumentException("Unknown list" + list);
173     }
174   }
175 
176   /**
177    * Returns the list index of the currently selected list.
178    */
179   public int getSelectedListIndex() {
180     return selectedListIndex;
181   }
182 
183   /**
184    * Specifies the list index of the currently selected list.
185    */
186   public void setSelectedListIndex(final int listIndex) {
187     selectedListIndex = listIndex;
188   }
189 
190   public Object clone() throws CloneNotSupportedException {
191     final UmlClassModel clone = (UmlClassModel) super.clone();
192     clone.attributes = new ArrayList(attributes.size());
193     for (Iterator it = attributes.iterator(); it.hasNext(); ) {
194       final String attribute = (String) it.next();
195       clone.attributes.add(attribute);
196     }
197     clone.operations = new ArrayList(operations.size());
198     for (Iterator it = operations.iterator(); it.hasNext(); ) {
199       final String operation = (String) it.next();
200       clone.operations.add(operation);
201     }
202     return clone;
203   }
204 }
205