/*******************************************************************************
 * Copyright (c) 2009, 2012 David A Carlson.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     David A Carlson (XMLmodeling.com) - initial API and implementation
 *     
 * $Id$
 *******************************************************************************/
package org.openhealthtools.mdht.uml.common.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Property;

public class PropertyList {

	private Class clazz;

	private List<Property> attributes;

	private List<Property> associationEnds;

	private boolean isOmitAssociations = false;

	private boolean isAllSuperclasses = true;

	private boolean isSorted = true;

	public PropertyList(Class clazz) {
		this.clazz = clazz;
	}

	public List<Property> getAttributes() {
		if (attributes == null) {
			fillPropertyLists();
		}

		return attributes;
	}

	public List<Property> getAssociationEnds() {
		if (associationEnds == null) {
			fillPropertyLists();
		}

		return associationEnds;
	}

	public Property getForName(String name) {
		if (name != null) {
			for (Property property : getAttributes()) {
				if (name.equals(property.getName())) {
					return property;
				}
			}
			for (Property property : getAssociationEnds()) {
				if (name.equals(property.getName())) {
					return property;
				}
			}
		}

		return null;
	}

	private void fillPropertyLists() {
		attributes = new ArrayList<Property>();
		associationEnds = new ArrayList<Property>();

		addProperties(clazz);

		if (isSorted) {
			Collections.sort(attributes, new NamedElementComparator());
			Collections.sort(associationEnds, new NamedElementComparator());
		}
	}

	private void addProperties(Class aClass) {
		for (Property property : aClass.getOwnedAttributes()) {
			if (property.getAssociation() != null) {
				if (isOmitAssociations) {
					continue;
				}
			}

			// skip implicit redefinitions
			if (getForName(property.getName()) == null) {
				if (property.getAssociation() == null) {
					attributes.add(property);
				} else {
					associationEnds.add(property);
				}
			}

			// TODO skip explicit redefinitions

		}

		if (isAllSuperclasses) {
			for (Classifier general : aClass.getGenerals()) {
				if (general instanceof Class) {
					addProperties((Class) general);
				}
			}
		}
	}

}

@@ -1088,19 +1088,20 @@
 orted = 
-tru
+fals
 e;%0D%0A%0D%0A%09p
@@ -2165,29 +2165,29 @@
 s) %7B%0D%0A%09%09
-for (
+List%3C
 Property
  propert
@@ -2178,28 +2178,60 @@
 Property
- p
+%3E ownedAttributes = new ArrayList%3CP
 roperty
- : 
+%3E(
 aClass.g
@@ -2246,24 +2246,113 @@
 ttributes())
+;%0D%0A%09%09Collections.reverse(ownedAttributes);%0D%0A%0D%0A%09%09for (Property property : ownedAttributes)
  %7B%0D%0A%09%09%09if (p
@@ -2597,24 +2597,27 @@
 ributes.add(
+0, 
 property);%0D%0A
@@ -2655,16 +2655,19 @@
 nds.add(
+0, 
 property

Change sort order of properties in subclass editor. Instead of alpha, group in order or superclass hierarchy.