All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openxma.xmadsl.GuidesignExtension Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
package org.openxma.xmadsl;

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

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.openxma.dsl.common.Strings;
import org.openxma.dsl.core.model.SimpleType;
import org.openxma.dsl.core.model.TrueLiteral;
import org.openxma.dsl.core.model.ValidatorReference;
import org.openxma.dsl.generator.helper.Util;
import org.openxma.dsl.generator.helper.ValidatorExtension;
import org.openxma.dsl.generator.helper.WidgetExtension;
import org.openxma.dsl.pom.model.CenterProperty;
import org.openxma.dsl.pom.model.Component;
import org.openxma.dsl.pom.model.DropDownMenu;
import org.openxma.dsl.pom.model.HeightProperty;
import org.openxma.dsl.pom.model.ImageProperty;
import org.openxma.dsl.pom.model.Menu;
import org.openxma.dsl.pom.model.MenuItem;
import org.openxma.dsl.pom.model.ModalityProperty;
import org.openxma.dsl.pom.model.ObjectProperty;
import org.openxma.dsl.pom.model.Page;
import org.openxma.dsl.pom.model.PageProperty;
import org.openxma.dsl.pom.model.ResizeableProperty;
import org.openxma.dsl.pom.model.StatusBarProperty;
import org.openxma.dsl.pom.model.TabAlignment;
import org.openxma.dsl.pom.model.TabMenu;
import org.openxma.dsl.pom.model.TabPage;
import org.openxma.dsl.pom.model.TitleButton;
import org.openxma.dsl.pom.model.TitleButtonsProperty;
import org.openxma.dsl.pom.model.TreeMenu;
import org.openxma.dsl.pom.model.WidthProperty;
import org.openxma.dsl.pom.model.XmadslComposite;
import org.openxma.dsl.pom.model.XmadslPage;
import org.openxma.dsl.pom.validation.PomStringConstants;

import at.spardat.xma.guidesign.AppShell;
import at.spardat.xma.guidesign.BcdValidator;
import at.spardat.xma.guidesign.GuidesignFactory;
import at.spardat.xma.guidesign.HiddenWidget;
import at.spardat.xma.guidesign.NotebookPage;
import at.spardat.xma.guidesign.PageComposite;
import at.spardat.xma.guidesign.State;
import at.spardat.xma.guidesign.Validator;
import at.spardat.xma.guidesign.XMACompProperty;
import at.spardat.xma.guidesign.XMAComponent;
import at.spardat.xma.guidesign.XMAComposite;
import at.spardat.xma.guidesign.XMADialogPage;
import at.spardat.xma.guidesign.XMAPage;
import at.spardat.xma.guidesign.XMAWidget;
import at.spardat.xma.guidesign.flex.FlexFactory;
import at.spardat.xma.guidesign.flex.XMAApplicationContext;
import at.spardat.xma.guidesign.flex.type.CompositeMenuType;
import at.spardat.xma.guidesign.types.KeyboardKeyCode;
import at.spardat.xma.guidesign.types.MenuType;
import at.spardat.xma.guidesign.types.ModalityType;
import at.spardat.xma.guidesign.types.PropertyDirection;
import at.spardat.xma.guidesign.types.PropertyType;
import at.spardat.xma.guidesign.types.TypesFactory;

public class GuidesignExtension {
	
	public static final String GENERATED_PAGE_EXTENSION = "_generatedXMAPage";
	
	/**
	 * Find the XMAPage which belongs to a Xmadsl-Page. 
     *
	 * @param c
	 * @param p
	 * @return
	 */
	public static XMAPage findXMAPage(XMAComponent c, Page p) {
		String pageId = Strings.capitalize(LinkerUtil.getId(p));
		TreeIterator allContents = c.eAllContents();		
		while (allContents.hasNext()) {
			EObject obj = allContents.next();
			if (obj instanceof NotebookPage && ((NotebookPage)obj).getNamInstance().equals(pageId)) {
				return (XMAPage)obj;
			}
			if (obj instanceof XMAPage && ((XMAPage)obj).getNamClass().equals(pageId)) {
				return (XMAPage)obj;
			}
		}
		return null;
	}
	
	public static XMAPage findOrCreateXmaPage(XMAComponent xmaComponent, Page page) {
		XMAPage xmaPage = findXMAPage(xmaComponent,page);
		if (xmaPage != null) {
			return xmaPage;
		} else {
			return createXMAPage(xmaComponent,page);
		}
	}
	
	public static XMAPage createXMAPage(XMAComponent xmaComponent, Page page) {
		
		if (page instanceof XmadslPage == false) {
			throw new RuntimeException("Can only create a XMAPage for a XmadslPage");
		}
		
		XmadslPage xmadslPage = (XmadslPage)page;
		
		// Do not create a abstract XmadslPage
		if (xmadslPage.isAbstract()) {
			return null;
		}
		
		boolean hasMenus = xmadslPage.getMenus() != null && xmadslPage.getMenus().size() > 0; 
		XMADialogPage xmaPage = hasMenus 
		                      ? GuidesignFactory.eINSTANCE.createAppShell()
		                      : GuidesignFactory.eINSTANCE.createXMADialogPage();
		xmaPage.setYnGenerated(true);
		
		// Set the names and the label
		xmaPage.setNamClass(Strings.capitalize(xmadslPage.getName()));		
		xmaPage.setNamInstance(Strings.uncapitalize(xmadslPage.getName()+GENERATED_PAGE_EXTENSION));
		if (xmadslPage.getLabelText() != null && xmadslPage.getLabelText().getString() != null && xmadslPage.getLabelText().getString().length()>0) {
			xmaPage.setLabelName(xmadslPage.getLabelText().getString());
		} else {
			xmaPage.setLabelName(xmadslPage.getName());
		}
		xmaPage.setComponent(xmaComponent);
		
		// Build the help uri
		StringBuffer uri = new StringBuffer(32);
		uri.append("help/");
		uri.append(xmaComponent.getNamPackageAsPath());
		uri.append("/");
		uri.append(xmadslPage.getName());
		uri.append(".html");
		xmaPage.setUriHelp(uri.toString());			
		
		// Create the PageComposite
		PageComposite pageComposite = GuidesignFactory.eINSTANCE.createPageComposite();
		pageComposite.setNamInstance(xmaPage.getNamClass()+"Comp");
		pageComposite.setYnGenerated(true);		
		xmaPage.setComposite(pageComposite);	
		
		// Create init State
		State state = GuidesignFactory.eINSTANCE.createState();
		state.setNamState("init");
		xmaPage.getState().add(state);
		
		// On default all titlebuttons are set to true (can be overwritten by 
		// the titlebuttons-property)
		xmaPage.setYnClose(true);		
		xmaPage.setYnMin(true);		
		xmaPage.setYnMax(true);				
		
		// Set page properties
		List pageProperties = xmadslPage.getPageProperty();
		for (PageProperty prop : pageProperties) {
			if (prop instanceof WidthProperty) {
				xmaPage.setQntWidth(((WidthProperty)prop).getWidth());		
			} else if (prop instanceof HeightProperty) {
				xmaPage.setQntHeight(((HeightProperty)prop).getHeight());		
			} else if (prop instanceof TitleButtonsProperty) {
				xmaPage.setYnClose(false);		
				xmaPage.setYnMin(false);		
				xmaPage.setYnMax(false);						
				TitleButtonsProperty titleButtons = (TitleButtonsProperty)prop;
				for (TitleButton tb : titleButtons.getButton()) {
					if (tb.getValue() == TitleButton.MINIMIZE_VALUE) {
						xmaPage.setYnMin(true);						
					} else if (tb.getValue() == TitleButton.MAXIMIZE_VALUE) {
						xmaPage.setYnMax(true);
					} else if (tb.getValue() == TitleButton.CLOSE_VALUE) {
						xmaPage.setYnClose(true);
					}
				}
			} else if (prop instanceof ResizeableProperty) {
				ResizeableProperty resizeableProperty = (ResizeableProperty)prop;
				xmaPage.setYnResize(resizeableProperty.isResizeable())	;			
            } else if (prop instanceof StatusBarProperty) {
                StatusBarProperty statusBarProperty = (StatusBarProperty)prop;
                xmaPage.setYnStatusBar(statusBarProperty.isStatusBar());
            } else if (prop instanceof CenterProperty) {
                CenterProperty centerProperty = (CenterProperty)prop;
                xmaPage.setYnCenter(centerProperty.isCenter());
			} else if (prop instanceof ModalityProperty) {
				ModalityProperty mp = (ModalityProperty)prop;
				if (PomStringConstants.APPLICATION_MODAL.equals(mp.getModality())) {
					xmaPage.setCodModality(ModalityType.APPLICATION_MODAL_LITERAL);
				} else if (PomStringConstants.PRIMARY_MODAL.equals(mp.getModality())) {
					xmaPage.setCodModality(ModalityType.PRIMARY_MODAL_LITERAL);
				} else if (PomStringConstants.MODELESS.equals(mp.getModality())) {
					xmaPage.setCodModality(ModalityType.MODELESS_LITERAL);
				} else {
					throw new RuntimeException("Not supported modality-value: "+mp.getModality());
				}
			} else if (prop instanceof ImageProperty) {
				ImageProperty ip = (ImageProperty)prop;
				String imageUri = null;				
				if (ip.getImageUri() != null) {
					imageUri = ip.getImageUri();
				} else if (ip.getImageUriReference() != null) {
					imageUri = ip.getImageUriReference().getSource();
				}
				if (imageUri != null) {
					xmaPage.setUriImage(imageUri);
				}								
			}
		}
		
		xmaComponent.getPage().add(xmaPage);
		return xmaPage;
	}
	
    public static void addMenusToPage(Page page, XMAPage xmaPage) {
        if ( xmaPage instanceof AppShell && page instanceof XmadslPage) {
            XmadslPage xmadslPage = (XmadslPage)page;
            addMenus((AppShell)xmaPage,xmadslPage.getMenus());
        }
    }
	
	private static void addMenus(AppShell appShell, EList menus) {
	    appShell.setCodMenuType(MenuType.MULTI_MENU_LITERAL);
	    int dropDownCnt = 0, treeCnt = 0, tabCnt = 0;
	    for (Menu srcMenu : menus) {
            if ( srcMenu instanceof DropDownMenu ) {
                if ( ++dropDownCnt > 1 )
                    throw new RuntimeException("Page " + appShell.getLabelName() + " can have only one dropdown menu!");
                at.spardat.xma.guidesign.flex.Menu destMenu = FlexFactory.eINSTANCE.createMenu();
                transformMenu(srcMenu,destMenu);
                appShell.setMenu(destMenu);
            }
            else if ( srcMenu instanceof TabMenu || srcMenu instanceof TreeMenu ) {
                at.spardat.xma.guidesign.flex.CompositeMenu destMenu = FlexFactory.eINSTANCE.createCompositeMenu();
                transformMenu(srcMenu,destMenu);
                XmadslComposite srcPanel = null;
                if ( srcMenu instanceof TabMenu ) {
                    if ( ++tabCnt > 1 )
                        throw new RuntimeException("Page " + appShell.getLabelName() + " can have only one tab menu!");
                    TabAlignment alignment = ((TabMenu)srcMenu).getAlignment(); 
                    if ( alignment != null && alignment == TabAlignment.RIGHT )
                        destMenu.setType(CompositeMenuType.TAB_RIGHT_ALIGNED_LITERAL);
                    else
                        destMenu.setType(CompositeMenuType.TAB_LITERAL);
                    srcPanel = ((TabMenu) srcMenu).getPanel();
                }
                else {
                    if ( ++treeCnt > 1 )
                        throw new RuntimeException("Page " + appShell.getLabelName() + " can have only one tree menu!");
                    destMenu.setType(CompositeMenuType.TREE_LITERAL);
                    srcPanel = ((TreeMenu) srcMenu).getPanel();
                }
                XMAWidget widget = Util.getWidgetForName(appShell,srcPanel.getName());
                destMenu.setXMAComposite((XMAComposite) widget);
                appShell.getCompositeMenu().add(destMenu);
            }
        }
    }

    private static void transformMenu(Menu srcMenu, at.spardat.xma.guidesign.flex.Menu destMenu) {
        for ( MenuItem srcItem : srcMenu.getMenuItems() ) {
            at.spardat.xma.guidesign.flex.MenuItem destItem = transformMenuItem(srcItem);
            destMenu.getMenuItem().add(destItem);
        }
    }

    private static at.spardat.xma.guidesign.flex.MenuItem transformMenuItem(MenuItem srcItem) {
        at.spardat.xma.guidesign.flex.MenuItem destItem = FlexFactory.eINSTANCE.createMenuItem();
        destItem.setRscMnuLabel(srcItem.getLabel());
        destItem.setNamRscKeyLabel(srcItem.getName());
        if ( srcItem.getImage() != null ) 
            destItem.setUriImage(srcItem.getImage());
        if ( srcItem.getAccelerator() != null ) {
            KeyboardKeyCode keyCode = TypesFactory.eINSTANCE.createKeyboardKeyCode();
            boolean ok = keyCode.setFromDisplayText(srcItem.getAccelerator());
            if ( ! ok )
                throw new RuntimeException("Accelerator '" + srcItem.getAccelerator() + "' is not valid!");
            destItem.setAccelerator(keyCode);
        }
        for ( MenuItem subSrcItem : srcItem.getMenuItem() ) {
            at.spardat.xma.guidesign.flex.MenuItem subDestItem = transformMenuItem(subSrcItem);
            destItem.getMenuItem().add(subDestItem);
        }
        return destItem;
    }

    /**
	 * Get and if necessary create the applicationcontext
	 */	
	// TODO use in Extend
	public static XMAApplicationContext getXMAApplicationContext(XMAComponent c) {
		if (c.getApplicationContext() == null) {
			c.setApplicationContext(FlexFactory.eINSTANCE.createXMAApplicationContext());
		}		
		return c.getApplicationContext();
	}
	
	/**
	 * 
	 * @param component
	 * @param xmaComponent
	 */
	public static void extendComponentProperties(Component component, XMAComponent xmaComponent) {		
		for (ObjectProperty objProp : component.getComponentProperties()) {
			XMACompProperty compProperty = GuidesignFactory.eINSTANCE.createXMACompProperty();
			compProperty.setNamProperty(objProp.getName());
			if (objProp.isPublic()) {
				if (objProp.getReadOnly() instanceof TrueLiteral) {
					compProperty.setCodDirection(PropertyDirection.OUT_LITERAL);
				} else {				
					compProperty.setCodDirection(PropertyDirection.IN_OUT_LITERAL);
				}
			} else {
				compProperty.setCodDirection(PropertyDirection.INTERNAL_LITERAL);
			}			
			if (objProp.getRequired() instanceof TrueLiteral) {
				compProperty.setYnMandatory(true);
			}
			SimpleType type = objProp.getType();
			PropertyType propertyType = getPropertyType(type);
			compProperty.setCodType(propertyType);
			
			if (objProp.getDefaultValue() != null && objProp.getDefaultValue().length()>0) {
			    compProperty.setTxtDefaultValue(objProp.getDefaultValue());
			}

			compProperty.setTxtDescription(objProp.getName() + " - property");            
			WidgetExtension.setSourceModelElementProperty(compProperty, objProp);
			compProperty.setYnGenerated(true);
			xmaComponent.getProperty().add(compProperty);
		}
	}
	
	public static void extendPageProperties(Page page, XMAPage xmaPage, XMAComponent xmaCompoenent) {
		
		EList properties = null;				
		if (page instanceof XmadslPage) {
			properties = ((XmadslPage)page).getPageProperties();
		} else if (page instanceof TabPage) {
			properties = ((TabPage)page).getPageProperties();
		}
		
		if (properties != null) {
			for (ObjectProperty prop : properties) {
				HiddenWidget hiddenWidget = GuidesignFactory.eINSTANCE.createHiddenWidget();
				hiddenWidget.setNamInstance(prop.getName());	
				hiddenWidget.setYnGenerated(true);				
				
				SimpleType type = prop.getType();								
				Validator validator = createAndSetHiddenWidgetBdValidator(hiddenWidget,type);
				
				/* For a ObjectProperty no type parameter can be specified in the model, therefore set defaults here. */
				if (validator instanceof BcdValidator) {
					BcdValidator bcdValidator = (BcdValidator)validator;					
					if (type.getName().equals("Integer")) {
						bcdValidator.setCntBeforeComma("10");						
						bcdValidator.setCntAfterComma("0");
					}
					if (type.getName().equals("Long")) {						
						bcdValidator.setCntAfterComma("0");
					}										
				}
				
				xmaPage.getComposite().getControls().add(hiddenWidget);
			}
		}
	}
	
	/**
	 * Create a simple BdValidator for a HiddenWidget. This should only be used in case
	 * there is no Attribute linked to this HiddenWidget. Otherwise a CompoundValidator
	 * has to be created. 
	 * 
	 * @param hiddenWidget
	 * @param simpleType
	 * @return
	 */
	public static Validator createAndSetHiddenWidgetBdValidator(HiddenWidget hiddenWidget, SimpleType simpleType) {
		ValidatorReference validatorReference = simpleType.getValidatorReference();
		ValidatorReference rootValidatorReference = validatorReference.getRootValidatorReference();
						
		Validator validator = ValidatorExtension.createXMAValidatorFromRootValidatorReference(rootValidatorReference,new ArrayList(),null,false);		
		hiddenWidget.setBdValidator(validator);
		
		return validator;
	}
	
	/**
	 * Only a subset of the predefined (in the platform) SimpleTypes are
	 * supported as property type. 
	 * In future there could be more types supported and then the type
	 * can be determined from the root validator like for attributes as well.
	 * 
	 * @param type
	 * @return
	 */
	private static PropertyType getPropertyType(SimpleType type) {		
		if (type.getName().equalsIgnoreCase("String")) {
			 return PropertyType.STRING_LITERAL;				
		} else if (type.getName().equalsIgnoreCase("Long")) {
			return PropertyType.LONG_LITERAL;				
		} else if (type.getName().equalsIgnoreCase("Integer")) {
			return PropertyType.INT_LITERAL;				
		} else if (type.getName().equalsIgnoreCase("Date")) {
			return PropertyType.DATE_LITERAL;				
		} else if (type.getName().equalsIgnoreCase("Boolean")) {
			return PropertyType.BOOLEAN_LITERAL;				
		} else if (type.getName().equalsIgnoreCase("Double")) {
			return PropertyType.DOUBLE_LITERAL;				
		} else if (type.getName().equalsIgnoreCase("Float")) {
			return PropertyType.FLOAT_LITERAL;				
		} else if (type.getName().equalsIgnoreCase("BigDecimal")) {
			return PropertyType.BIG_DECIMAL_LITERAL;				
		} else {			
			// Fallback to String (should never happen because of Check)
			return PropertyType.STRING_LITERAL;
		}
	}
	
		

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy