/*******************************************************************************
 * Copyright (c) 2006, 2009 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.cda.ui.providers;

import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.service.AbstractProvider;
import org.eclipse.gmf.runtime.common.core.service.IOperation;
import org.eclipse.gmf.runtime.common.ui.services.parser.GetParserOperation;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParserProvider;
import org.eclipse.gmf.runtime.emf.ui.services.parser.ParserHintAdapter;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Property;
import org.openhealthtools.mdht.uml.cda.resources.util.CDAProfileUtil;
import org.openhealthtools.mdht.uml.cda.ui.parsers.PropertyParser;

/**
 * This class provides custom edit parsers for HL7 class diagrams.
 * It applies only to models with the HDF profile applied.
 */
public class CustomUMLParserProvider extends AbstractProvider 
implements IParserProvider {

	public CustomUMLParserProvider() {
		super();
	}

	public IParser getParser(IAdaptable hint) {
		IParser parser = null;
		String vid = (String) hint.getAdapter(String.class);
		if (vid != null) {
			parser = getParser(vid);
		}
		if (parser == null) {
			View view = (View) hint.getAdapter(View.class);
			if (view != null) {
				parser = getParser(view.getType());
			}
		}
		return parser;
	}

	public boolean provides(IOperation operation) {
		if (operation instanceof GetParserOperation) {
			IAdaptable hint = ((GetParserOperation) operation).getHint();
			if (hint instanceof ParserHintAdapter) {
				// only allow Property elements in models with HDF profile applied
				//TODO check user preference setting
				EObject eObject = (EObject)hint.getAdapter(EObject.class);
				if (eObject instanceof Property) {
					Profile cdaProfile = CDAProfileUtil.getCDAProfile(eObject.eResource().getResourceSet());
					if (cdaProfile != null) {
						List<Profile> appliedProfiles = ((Property)eObject).getNearestPackage().getAllAppliedProfiles();
						if (appliedProfiles.contains(cdaProfile)) {
							return getParser(hint) != null;
						}
					}
				}
			}
		}
		return false;
	}

	protected IParser getCustomPropertyParser() {
		return new PropertyParser();
	}

	/**
	 * getCustomPropertyParser
	 * 
	 * getCustomPropertyParser currently returns parser regardless of the visualid being passed in.  
	 * TODO Need to Determine proper visual id support for both rsm and papyrus 
	 * @param visualID
	 * @return
	 */
	protected IParser getParser(String visualID) {		
		return getCustomPropertyParser();
	}
}
@@ -2344,19 +2344,19 @@
 ls with 
-HDF
+CDA
  profile
@@ -2982,268 +2982,8 @@
 %0A%0D%0A%09
-/**%0D%0A%09 * getCustomPropertyParser%0D%0A%09 * %0D%0A%09 * getCustomPropertyParser currently returns parser regardless of the visualid being passed in.  %0D%0A%09 * TODO Need to Determine proper visual id support for both rsm and papyrus %0D%0A%09 * @param visualID%0D%0A%09 * @return%0D%0A%09 */%0D%0A%09
 prot
@@ -3013,16 +3013,45 @@
 (String 
+visualID) %7B%0D%0A%09%09if (%22%22.equals(
 visualID
@@ -3055,14 +3055,14 @@
 lID)
+)
  %7B
-%09%09
 %0D%0A
+%09
 %09%09re
@@ -3090,17 +3090,40 @@
 yParser();%0D%0A
+%09%09%7D%0D%0A%0D%0A%09%09return null;%0D%0A
 %09%7D%0D%0A%7D

Revert change to getParser() method (changed for Papyrus integration) because it causes custom parser to be used on all association end properties.