package org.csstudio.utility.adlparser.fileParser.widgets;

import java.util.ArrayList;

import org.csstudio.utility.adlparser.internationalization.Messages;
import org.csstudio.utility.adlparser.fileParser.ADLResource;
import org.csstudio.utility.adlparser.fileParser.FileLine;
import org.csstudio.utility.adlparser.fileParser.ADLWidget;
import org.csstudio.utility.adlparser.fileParser.WrongADLFormatException;
import org.csstudio.utility.adlparser.fileParser.widgetParts.ADLChildren;
import org.csstudio.utility.adlparser.fileParser.widgetParts.ADLDynamicAttribute;
import org.csstudio.utility.adlparser.fileParser.widgetParts.ADLObject;

/**
 * 
 * @author hammonds
 *
 */
public class Composite extends ADLAbstractWidget {
	private String _compositeFile = new String();
	private boolean _hasCompositeFile = false;
	private ADLChildren _adlChildren = null;
//	private ArrayList<ADLWidget> _children;
	
	public Composite(ADLWidget adlWidget) {
		super(adlWidget);
		name = new String("composite");
		descriptor = null;
		try {
			for (ADLWidget childWidget : adlWidget.getObjects()) {
	        	if (childWidget.getType().equals("object")){
	        		_adlObject = new ADLObject(childWidget);
	        		if (_adlObject != null){
	        			_hasObject = true;
	        		}
	        	}
	        	else if (childWidget.getType().equals("children")){
	        		_adlChildren = new ADLChildren(childWidget);
	        	}
	        	else if (childWidget.getType().equals("dynamic attribute")){
	        		_adlDynamicAttribute = new ADLDynamicAttribute(childWidget);
	        		if (_adlDynamicAttribute != null){
	        			_hasDynamicAttribute = true;
	        		}
	        	}
	        }
	        for (FileLine fileLine : adlWidget.getBody()) {
	            String obj = fileLine.getLine();
	            String[] row = obj.trim().split("="); //$NON-NLS-1$
	            if(row.length<2){
	                throw new WrongADLFormatException(Messages.Label_WrongADLFormatException_Parameter_Begin+obj+Messages.Label_WrongADLFormatException_Parameter_End);
	            }
	            if(FileLine.argEquals(row[0], "composite file")){ //$NON-NLS-1$
	                set_compositeFile(FileLine.getTrimmedValue(row[1]));
	                if (!(_compositeFile.equals(""))){
	                	_hasCompositeFile = false;
	                }
	            	_hasCompositeFile = true;
	            }
	        }
		}
		catch (WrongADLFormatException ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * @param _compositeFile the _compositeFile to set
	 */
	private void set_compositeFile(String _compositeFile) {
		this._compositeFile = _compositeFile;
	}

	/**
	 * @return the _compositeFile
	 */
	public String get_compositeFile() {
		return _compositeFile;
	}

	/**
	 * 
	 * @return
	 */
	public boolean hasCompositeFile(){
		return _hasCompositeFile;
	}

	/**
	 * 
	 * @return
	 */
	public ArrayList<ADLWidget> getChildWidgets() {
		if (_adlChildren != null){
			return _adlChildren.getAdlChildrens();
		}
		else return null;
	}

	@Override
	public Object[] getChildren() {
		ArrayList<Object> ret = new ArrayList<Object>();
		if (_adlObject != null ) ret.add( _adlObject);
		if (_adlDynamicAttribute != null) ret.add( _adlDynamicAttribute);
		if (_adlChildren != null ) ret.add(_adlChildren);
		if (!_compositeFile.equals("")) ret.add(new ADLResource(ADLResource.COMPOSITE_FILE, _compositeFile));
		return ret.toArray();
	}
}

@@ -2222,18 +2222,16 @@
      if 
-(!
 (_compos
@@ -2249,17 +2249,16 @@
 als(%22%22))
-)
 %7B%0A%09     

Fix logic that determines if this widget has a composite file defined.