f@0: /*
f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0:
f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0:
f@0: This program is free software: you can redistribute it and/or modify
f@0: it under the terms of the GNU General Public License as published by
f@0: the Free Software Foundation, either version 3 of the License, or
f@0: (at your option) any later version.
f@0:
f@0: This program is distributed in the hope that it will be useful,
f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0: GNU General Public License for more details.
f@0:
f@0: You should have received a copy of the GNU General Public License
f@0: along with this program. If not, see .
f@0: */
f@0: package uk.ac.qmul.eecs.ccmi.diagrammodel;
f@0:
f@0: import java.util.ArrayList;
f@0: import java.util.Collections;
f@0: import java.util.LinkedHashMap;
f@0: import java.util.LinkedHashSet;
f@0: import java.util.List;
f@0: import java.util.Map;
f@0: import java.util.Set;
f@0:
f@0: /**
f@0: * This class represents the internal properties of a node. Internal properties can be seen as
f@0: * attributes that each single nodes owns and that, in a visual diagram, would normally be displayed inside
f@0: * or in the neighbourhood of the node itself. This is a very high abstraction of the concept of properties
f@0: * of a node as the user can define their own type of properties through the property type definition object
f@0: * passed as argument in the constructor.
f@0: * An example of properties is attributes and methods of a class diagram in the UML language or just the
f@0: * attributes of entities in an ER diagram.
f@0: *
f@0: */
f@0: public final class NodeProperties implements Cloneable {
f@0:
f@0: /**
f@0: * Creates a diagram element property data structure out of a property type specification. In a UML
f@0: * diagram the NodeProperties for a class node would be constructed passing as arguments a linked hash
f@0: * map containing the strings "attributes" and "properties" and the strings "public", "protected", "private",
f@0: * "static" as value for both the keys. "attributes" and "methods" would be the types of the NodeProperties,
f@0: * that is, all the values inserted in the object such as values would fall under either type. For example
f@0: * if a UML diagram class has the methods getX() and getY(), these would be inserted in the NodeProperties
f@0: * object with a type "methods".
f@0: *
f@0: * @param typeDefinition a linked Hash Map holding the properties types as keys
f@0: * and the modifiers type definition of each property as values
f@0: */
f@0:
f@0: public NodeProperties(LinkedHashMap> typeDefinition){
f@0: if(typeDefinition == null)
f@0: this.typeDefinition = EMPTY_PROPERTY_TYPE_DEFINITION;
f@0: else
f@0: this.typeDefinition = typeDefinition;
f@0: /* create the type collection out of the typeDefinition keyset */
f@0: types = Collections.unmodifiableList(new ArrayList(this.typeDefinition.keySet()));
f@0: properties = new LinkedHashMap();
f@0: for(String s : types){
f@0: Entry q = new Entry();
f@0: q.values = new ArrayList(); // property values to be filled by user + modifiers
f@0: q.indexes = new ArrayList>();
f@0: q.view = null;
f@0: Set modifierTypeDefinition = this.typeDefinition.get(s);
f@0: if(modifierTypeDefinition == null)
f@0: /* null modifiers for this property */
f@0: q.modifiers = new Modifiers(EMPTY_MODIFIER_TYPE_DEFINITION,q.indexes);
f@0: else if(modifierTypeDefinition.size() == 0)
f@0: /* null modifiers for this property */
f@0: q.modifiers = new Modifiers(EMPTY_MODIFIER_TYPE_DEFINITION,q.indexes);
f@0: else
f@0: q.modifiers = new Modifiers(modifierTypeDefinition, q.indexes);
f@0: properties.put(s, q);
f@0: }
f@0: }
f@0:
f@0: /**
f@0: * Returns the type definition argument of the constructor this object has been created through.
f@0: *
f@0: * @return the type definition
f@0: */
f@0: public Map> getTypeDefinition(){
f@0: return typeDefinition;
f@0: }
f@0:
f@0: /**
f@0: * Returns the types of properties.
f@0: *
f@0: * @return a list of strings holding types of properties
f@0: */
f@0: public List getTypes(){
f@0: return types;
f@0: }
f@0:
f@0: /**
f@0: * @param propertyType the property type we want to get the values of.
f@0: * @return an array of string with the different properties set by the user or
f@0: * {@code null} if the specified type does not exist.
f@0: */
f@0: public List getValues(String propertyType){
f@0: Entry e = properties.get(propertyType);
f@0: if(e == null)
f@0: throw new IllegalArgumentException(ILLEGAL_TYPE_MSG+propertyType);
f@0: return new ArrayList(e.values);
f@0: }
f@0:
f@0: /**
f@0: * Returns the view object associated with a property type in this NodeProperties instance. A view object is
f@0: * defined by the client of this class and it holds the information needed for a visual representation
f@0: * of the property.
f@0: *
f@0: * @param type the property type the returned view is associated with
f@0: * @return the View object or null if non has been set previously
f@0: */
f@0: public Object getView(String type){
f@0: Entry e = properties.get(type);
f@0: if(e == null)
f@0: throw new IllegalArgumentException(ILLEGAL_TYPE_MSG+type);
f@0: return e.view;
f@0: }
f@0:
f@0: /**
f@0: * Sets the View object for the a type of properties. The NodeProperties only works
f@0: * as a holder as for the view objects. The client code of this class has to define
f@0: * its own view.
f@0: *
f@0: * @param type the type of property the view is associated with.
f@0: * @param o an object defined by user
f@0: * @see #getView(String)
f@0: */
f@0: public void setView(String type, Object o){
f@0: Entry e = properties.get(type);
f@0: if(e == null)
f@0: throw new IllegalArgumentException(ILLEGAL_TYPE_MSG+type);
f@0: e.view = o;
f@0: }
f@0:
f@0: /**
f@0: * Returns a reference to the modifier object associated with a property type. Changes to the returned
f@0: * reference will affect the internal state of the NodeProperty object.
f@0: *
f@0: * @param propertyType a property type
f@0: * @return the modifiers of the specified property type or null if either
f@0: * the property type is null or it was not listed in the property type definition
f@0: * passed to the constructor
f@0: */
f@0: public Modifiers getModifiers(String propertyType){
f@0: Entry e = properties.get(propertyType);
f@0: if(e == null)
f@0: throw new IllegalArgumentException(ILLEGAL_TYPE_MSG+propertyType);
f@0: return e.modifiers;
f@0: }
f@0:
f@0: /**
f@0: * Adds a value of the type passed as argument.
f@0: *
f@0: * @param propertyType a property type defined in the property type definition passed as argument to the constructor
f@0: * @param propertyValue the value to add to the property type
f@0: */
f@0: public void addValue(String propertyType, String propertyValue){
f@0: Entry e = properties.get(propertyType);
f@0: /* no such property type exists */
f@0: if(e == null)
f@0: throw new IllegalArgumentException(ILLEGAL_TYPE_MSG+propertyType);
f@0: e.values.add(propertyValue);
f@0: e.indexes.add(new LinkedHashSet());
f@0: }
f@0:
f@0: /**
f@0: * Adds a value of the type passed as argument and sets the modifier indexes for it as for
f@0: * {@link Modifiers#setIndexes(int, Set)}.
f@0: *
f@0: * @param propertyType a property type
f@0: * @param propertyValue a property value
f@0: * @param modifierIndexes the modifier set of indexes
f@0: *
f@0: * @throws IllegalArgumentException if propertyType
f@0: * is not among the ones in the type definition passed as argument to the constructor
f@0: */
f@0: public void addValue(String propertyType, String propertyValue, Set modifierIndexes){
f@0: for(Integer i : modifierIndexes)
f@0: if((i < 0)||(i >= getModifiers(propertyType).getTypes().size()))
f@0: throw new ArrayIndexOutOfBoundsException("Index "+ i +
f@0: " corresponds to no Modifier type (modifierType size = "+
f@0: getModifiers(propertyType).getTypes().size()+")" );
f@0:
f@0: Entry e = properties.get(propertyType);
f@0: if(e == null)
f@0: throw new IllegalArgumentException(ILLEGAL_TYPE_MSG+propertyType);
f@0: e.values.add(propertyValue);
f@0: e.indexes.add(modifierIndexes);
f@0: }
f@0:
f@0: /**
f@0: * Removes the value at the specified index for the specified property type.
f@0: *
f@0: * @param propertyType a property type
f@0: * @param valueIndex the index of the value to remove
f@0: * @return the removed value
f@0: * @throws IllegalArgumentException if propertyType
f@0: * is not among the ones in the type definition passed as argument to the constructor
f@0: */
f@0: public String removeValue(String propertyType, int valueIndex){
f@0: Entry e = properties.get(propertyType);
f@0: if(e == null)
f@0: throw new IllegalArgumentException(ILLEGAL_TYPE_MSG+propertyType);
f@0: e.indexes.remove(valueIndex);
f@0: return e.values.remove(valueIndex);
f@0: }
f@0:
f@0: /**
f@0: * Sets the value of a property type at the specified index to a new value.
f@0: *
f@0: * @param propertyType a property type
f@0: * @param valueIndex the index of the value which must be replaced
f@0: * @param newValue the new value for the specified index
f@0: * @throws IllegalArgumentException if propertyType
f@0: * is not among the ones in the type definition passed as argument to the constructor
f@0: */
f@0: public void setValue(String propertyType, int valueIndex, String newValue){
f@0: Entry e = properties.get(propertyType);
f@0: if(e == null)
f@0: throw new IllegalArgumentException(ILLEGAL_TYPE_MSG+propertyType);
f@0: e.values.set(valueIndex, newValue);
f@0: }
f@0:
f@0: /**
f@0: * Removes all the values and modifiers for a specific type.
f@0: *
f@0: * @param propertyType the type whose property and modifiers must be removed
f@0: * @throws IllegalArgumentException if propertyType
f@0: * is not among the ones in the type definition passed as argument to the constructor
f@0: */
f@0: public void clear(String propertyType){
f@0: Entry e = properties.get(propertyType);
f@0: if(e == null)
f@0: throw new IllegalArgumentException(ILLEGAL_TYPE_MSG+propertyType);
f@0: e.values.clear();
f@0: e.indexes.clear();
f@0: }
f@0:
f@0: /**
f@0: * Removes all the values and modifiers of this object.
f@0: */
f@0: public void clear(){
f@0: for(String type : types){
f@0: clear(type);
f@0: }
f@0: }
f@0:
f@0: /**
f@0: * Returns true if this NodeProperties contains no values.
f@0: *
f@0: * @return true if this NodeProperties contains no values
f@0: */
f@0: public boolean isEmpty(){
f@0: boolean empty = true;
f@0: for(String type : types)
f@0: if(!properties.get(type).values.isEmpty()){
f@0: empty = false;
f@0: break;
f@0: }
f@0: return empty;
f@0: }
f@0:
f@0: /**
f@0: * Returns true if there are no values for the specified type in this NodeProperties instance.
f@0: *
f@0: * @param propertyType the property type to be checked for value presence
f@0: * @return true if there are no values for the specified type in this NodeProperties instance
f@0: */
f@0: public boolean typeIsEmpty(String propertyType){
f@0: Entry e = properties.get(propertyType);
f@0: if(e == null)
f@0: throw new IllegalArgumentException(ILLEGAL_TYPE_MSG+propertyType);
f@0: return e.values.isEmpty();
f@0: }
f@0:
f@0: /**
f@0: * true if this NodeProperties object has no types. This can happen if the constructor
f@0: * has been called with an empty or null property type definition.
f@0: *
f@0: * @return true if this NodeProperties object has no types
f@0: */
f@0: public boolean isNull(){
f@0: return types.isEmpty();
f@0: }
f@0:
f@0: /**
f@0: * Returns a string representation of types, value and modifiers of this property. Such a
f@0: * representation can be passed as argument to {@link #fill(String)} to populate a NodeProperties
f@0: * object. Such NodeProperties object though must have the same type and modifier definition of
f@0: * the object this method is called on.
f@0: *
f@0: * @return a string representation of the values and modifiers of this object
f@0: */
f@0: @Override
f@0: public String toString(){
f@0: StringBuilder builder = new StringBuilder();
f@0: for(String type : types){
f@0: builder.append(TYPE_ENTRY_SEPARATOR).append(type);
f@0: int propertyValueIndex = 0;
f@0: for(String value : properties.get(type).values){
f@0: builder.append(VALUE_ENTRY_SEPARATOR);
f@0: builder.append(value);
f@0: for(int modifierIndex : getModifiers(type).getIndexes(propertyValueIndex)){
f@0: builder.append(MODIFIER_ENTRY_SEPARATOR);
f@0: builder.append(modifierIndex);
f@0: }
f@0: propertyValueIndex++;
f@0: }
f@0: }
f@0: return builder.toString();
f@0: }
f@0:
f@0: /**
f@0: * Fills up the this property according to the string passed as arguments
f@0: * The string must be generated by calling toString on a NodeProperty instance
f@0: * holding the same property types as type, otherwise an IllegalArgumentException
f@0: * is likely to be thrown.
f@0: *
f@0: * @param s the string representation of the property values, such as the one returned
f@0: * by toString()
f@0: * @throws IllegalArgumentException if s contains property types which
f@0: * are not among the ones in the type definition passed as argument to the constructor
f@0: */
f@0: public void fill(String s){
f@0: /* clear up previous values */
f@0: clear();
f@0:
f@0: /* a property entry is a string with the type of the property followed by value *
f@0: * entries of that type. all value entries begin with the VALUE_SEPARATOR. *
f@0: * a value entry is a property value followed by its modifiers entries, a modifier entry *
f@0: * is a modifier beginning with the MODIFIER_SEPARATOR */
f@0: String propertyEntries[] = s.split(TYPE_ENTRY_SEPARATOR);
f@0: for(String propertyEntry : propertyEntries){
f@0: String[] typeEntries = propertyEntry.split(VALUE_ENTRY_SEPARATOR);
f@0: String type = typeEntries[0];
f@0: for(int i=1;i modifiers = new LinkedHashSet();
f@0: for(int j=1;j)properties.clone();
f@0: for(String key : p.properties.keySet()){
f@0: Entry old = p.properties.get(key);
f@0: Entry q = new Entry();
f@0: q.values = new ArrayList();
f@0: q.indexes = new ArrayList>();
f@0: q.view = old.view;
f@0: if(old.modifiers != null){
f@0: q.modifiers = new Modifiers(old.modifiers.modifierTypes, q.indexes);
f@0: for(String modifierType : q.modifiers.getTypes())
f@0: q.modifiers.setView(modifierType, old.modifiers.getView(modifierType));
f@0: }
f@0: p.properties.put(key, q);
f@0: }
f@0: return p;
f@0: }
f@0:
f@0: private class Entry{
f@0: List values;
f@0: List> indexes;
f@0: Modifiers modifiers;
f@0: Object view;
f@0: }
f@0:
f@0: /**
f@0: * A modifier is a label peculiar of a certain subset of properties. For example in
f@0: * a UML class diagram one or more methods can be labelled as public, private or protected.
f@0: * Had a NodeProperties instance been used to represent the methods of a class node, there would be then
f@0: * one modifier for each label: one for public, one for private, and one for protected. To each modifier
f@0: * a view-object can be associated, which describes how these labels would be rendered visually.
f@0: * Following on from the UML example, a view-object would be used with the protected modifier
f@0: * to hold the information that the properties which are assigned such a modifier must be prefixed
f@0: * with the '#' sign.
f@0: */
f@0: public class Modifiers{
f@0: private Modifiers(Set modifierTypes, List> indexes){
f@0: views = new LinkedHashMap();
f@0: indexesRef = indexes;
f@0: this.modifierTypes = Collections.unmodifiableList(new ArrayList(modifierTypes));
f@0: for(String modifierType : modifierTypes){
f@0: views.put(modifierType,null);
f@0: }
f@0: }
f@0:
f@0: /* only used by NodeProperties.clone() method */
f@0: private Modifiers(List modifierTypes, List> indexes){
f@0: views = new LinkedHashMap();
f@0: indexesRef = indexes;
f@0: this.modifierTypes = modifierTypes;
f@0: for(String modifierType : modifierTypes){
f@0: views.put(modifierType,null);
f@0: }
f@0: }
f@0:
f@0: /**
f@0: * Returns the list of modifier types, as per the type definition passed as argument to the NodeProperties
f@0: * constructor.
f@0: *
f@0: * @return a list of modifier types
f@0: * @see NodeProperties#NodeProperties(LinkedHashMap)
f@0: */
f@0: public List getTypes(){
f@0: return modifierTypes;
f@0: }
f@0:
f@0: /**
f@0: * Returns the view object associated with a modifier type in this Modifier instance. A view object is
f@0: * defined by the client of this class and it holds the information needed for a visual representation
f@0: * of the modifier.
f@0: *
f@0: * @param modifierType the property type the returned view is associated with
f@0: * @return the View object or null if non has been set previously
f@0: */
f@0: public Object getView(String modifierType){
f@0: return views.get(modifierType);
f@0: }
f@0:
f@0: /**
f@0: * Sets the View object for the a type of modifier. The NodeProperties only works
f@0: * as a holder as for the view objects. The client code of this class has to define
f@0: * its own view, which will then be used by the client itself to visualise the modifier
f@0: *
f@0: * @param modifierType the type of modifier the view is associated with.
f@0: * @param view an object defined by user defining how this property looks like
f@0: * @see #getView(String)
f@0: * @throws IllegalArgumentException if modifierType
f@0: * is not among the ones in the type definition passed as argument to the constructor
f@0: */
f@0: public void setView(String modifierType, Object view){
f@0: if(!views.containsKey(modifierType))
f@0: throw new IllegalArgumentException(ILLEGAL_TYPE_MSG+modifierType);
f@0: views.put(modifierType,view);
f@0: }
f@0:
f@0: /**
f@0: * Returns the modifier indexes for the specified property value. Each property type can be associated
f@0: * with one or more modifier type and each property value can be assigned one or more modifier. The
f@0: * integer set returned by this method tells the client code to which modifiers the property value at the index
f@0: * passed as argument is assigned. The returned indexes refer to the modifier list returned by {@link #getTypes()}
f@0: *
f@0: * @param propertyValueIndex the index of the property value for which the set of modifier indexes
f@0: * is being queried
f@0: * @return a set of indexes of the modifier types the property value at the index passed as argument
f@0: * is assigned to
f@0: */
f@0: public Set getIndexes(int propertyValueIndex){
f@0: Set set = indexesRef.get(propertyValueIndex);
f@0: return (new LinkedHashSet(set));
f@0: }
f@0:
f@0: /**
f@0: * Set the modifier indexes for the property value at the index passed as argument
f@0: *
f@0: * @param propertyValueIndex the index of the property value
f@0: * @param modifierIndexes a set of indexes which refer to the list returned by {@link #getTypes()}
f@0: * @throws ArrayIndexOutOfBoundsException if one or more of the indexes in modifierIndexes are lower than 0 or greater
f@0: * or equal to the size of the modifier type list returned by {@link #getTypes()}. The same exception is also thrown
f@0: * if propertyValueIndex is lower than 0 or greater or equal to the property values list returned by {@link NodeProperties#getValues(String)}
f@0: * passing as argument the same property type passed to {@link NodeProperties#getModifiers(String)} to obtain
f@0: * this Modifiers instance.
f@0: * @see #getIndexes(int)
f@0: */
f@0: public void setIndexes(int propertyValueIndex, Set modifierIndexes){
f@0: for(Integer i : modifierIndexes)
f@0: if((i < 0)||(i >= getTypes().size()))
f@0: throw new ArrayIndexOutOfBoundsException("Index "+ i + " corresponds to no Modifier Type (modifierType size = "+getTypes().size()+")" );
f@0:
f@0: Set m = indexesRef.get(propertyValueIndex);
f@0: m.clear();
f@0: m.addAll(modifierIndexes);
f@0: }
f@0:
f@0: /**
f@0: * Removes all the modifier indexes for a property value at the specified index.
f@0: *
f@0: * @param propertyValueIndex the index of the property value
f@0: */
f@0: public void clear(int propertyValueIndex){
f@0: Set m = indexesRef.get(propertyValueIndex);
f@0: m.clear();
f@0: }
f@0:
f@0: /**
f@0: * true if this Modifiers object has no types. This can happen if the constructor
f@0: * has been called with an empty or null modifier in the type definition passed as argument
f@0: * to the constructor of the NodeProperties object holding this Modifiers object.
f@0: *
f@0: * @return true if this Modifiers object has no types
f@0: */
f@0: public boolean isNull(){
f@0: return modifierTypes.isEmpty();
f@0: }
f@0:
f@0: private LinkedHashMap views;
f@0: private final List modifierTypes;
f@0: private List> indexesRef;
f@0: }
f@0:
f@0: /* for each property (key) I associate a Couple (value) made out of *
f@0: * a list of the property values plus a list of possible modifiers */
f@0: private LinkedHashMap properties;
f@0: private final List types;
f@0: private Map> typeDefinition;
f@0:
f@0: private static final LinkedHashMap> EMPTY_PROPERTY_TYPE_DEFINITION = new LinkedHashMap>();
f@0: private static final Set EMPTY_MODIFIER_TYPE_DEFINITION = Collections.emptySet();
f@0: private static final String ILLEGAL_TYPE_MSG = "argument must be in type definition list: ";
f@0: /**
f@0: * A special static instance of the class corresponding to the null NodeProperties. A null NodeProperties instance will have isNull() returning true,
f@0: * which means it has no property types associated.
f@0: * @see #isNull()
f@0: */
f@0: public static final NodeProperties NULL_PROPERTIES = new NodeProperties(EMPTY_PROPERTY_TYPE_DEFINITION);
f@0:
f@0: private final static String TYPE_ENTRY_SEPARATOR = "\n:";
f@0: private final static String VALUE_ENTRY_SEPARATOR = "\n;";
f@0: private final static String MODIFIER_ENTRY_SEPARATOR = "\n,";
f@0: }