Search in sources :

Example 1 with DynamicComboBoxPropertyDescriptor

use of com.twinsoft.convertigo.eclipse.property_editors.DynamicComboBoxPropertyDescriptor in project convertigo by convertigo.

the class DatabaseObjectTreeObject method findPropertyDescriptor.

private PropertyDescriptor findPropertyDescriptor(final String name, String displayName, java.beans.PropertyDescriptor databaseObjectPropertyDescriptor, Object value, final Class<?> pec, boolean isExtractionRule, boolean isMasked) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    PropertyDescriptor propertyDescriptor = null;
    // Primitive types
    if (pec == null) {
        if (value instanceof Boolean) {
            String[] values = new String[] { "true", "false" };
            propertyDescriptor = new DynamicComboBoxPropertyDescriptor(name, displayName, values, this, name);
        } else if (value instanceof Number) {
            propertyDescriptor = new TextPropertyDescriptor(name, displayName);
        // propertyDescriptor.setValidator(new CompilableValueValidator(new NumberValidator(value.getClass())));
        }
    } else // Complex types
    {
        if (PropertyWithValidatorEditor.class.isAssignableFrom(pec)) {
            propertyDescriptor = new TextPropertyDescriptor(name, displayName);
            propertyDescriptor.setValidator(getValidator(name));
        } else if (PropertyWithTagsEditor.class.isAssignableFrom(pec)) {
            String[] tags;
            if (PropertyWithDynamicTagsEditor.class.isAssignableFrom(pec)) {
                Method getTags = pec.getMethod("getTags", new Class[] { DatabaseObjectTreeObject.class, String.class });
                tags = (String[]) getTags.invoke(null, new Object[] { this, name });
                propertyDescriptor = new DynamicComboBoxPropertyDescriptor(name, displayName, getTags, this, name);
            } else if (PropertyWithTagsEditorAdvance.class.isAssignableFrom(pec)) {
                Method getTags = pec.getDeclaredMethod("getTags", new Class[] { DatabaseObjectTreeObject.class, String.class });
                tags = (String[]) getTags.invoke(null, new Object[] { this, name });
                propertyDescriptor = new DynamicComboBoxPropertyDescriptor(name, displayName, tags, this, name);
            } else {
                Method getTags = pec.getDeclaredMethod("getTags", new Class[] { DatabaseObjectTreeObject.class });
                tags = (String[]) getTags.invoke(null, new Object[] { this });
                propertyDescriptor = new DynamicComboBoxPropertyDescriptor(name, displayName, tags, this, name);
            }
        } else if (StringComboBoxPropertyDescriptor.class.isAssignableFrom(pec)) {
            Method getTags = pec.getDeclaredMethod("getTags", new Class[] { DatabaseObjectTreeObject.class, String.class });
            String[] tags = (String[]) getTags.invoke(null, new Object[] { this, name });
            propertyDescriptor = new StringComboBoxPropertyDescriptor(name, displayName, tags, false);
        } else if (com.twinsoft.convertigo.eclipse.property_editors.MobileSmartSourcePropertyDescriptor.class.isAssignableFrom(pec)) {
            Method getTags = pec.getDeclaredMethod("getTags", new Class[] { DatabaseObjectTreeObject.class, String.class });
            String[] tags = (String[]) getTags.invoke(null, new Object[] { this, name });
            propertyDescriptor = new com.twinsoft.convertigo.eclipse.property_editors.MobileSmartSourcePropertyDescriptor(name, displayName, tags, false);
            ((com.twinsoft.convertigo.eclipse.property_editors.MobileSmartSourcePropertyDescriptor) propertyDescriptor).databaseObjectTreeObject = this;
        } else if (com.twinsoft.convertigo.eclipse.property_editors.NgxSmartSourcePropertyDescriptor.class.isAssignableFrom(pec)) {
            Method getTags = pec.getDeclaredMethod("getTags", new Class[] { DatabaseObjectTreeObject.class, String.class });
            String[] tags = (String[]) getTags.invoke(null, new Object[] { this, name });
            propertyDescriptor = new com.twinsoft.convertigo.eclipse.property_editors.NgxSmartSourcePropertyDescriptor(name, displayName, tags, false);
            ((com.twinsoft.convertigo.eclipse.property_editors.NgxSmartSourcePropertyDescriptor) propertyDescriptor).databaseObjectTreeObject = this;
        } else if (PropertyWithDynamicInfoEditor.class.isAssignableFrom(pec)) {
            Method getInfo = pec.getMethod("getInfo", new Class[] { DatabaseObjectTreeObject.class, String.class });
            propertyDescriptor = new DynamicInfoPropertyDescriptor(name, displayName, getInfo, this, name);
        } else if (AbstractDialogCellEditor.class.isAssignableFrom(pec)) {
            propertyDescriptor = new PropertyDescriptor(name, displayName) {

                @Override
                public CellEditor createPropertyEditor(Composite parent) {
                    try {
                        Constructor<?> constructor = pec.getConstructor(new Class[] { Composite.class });
                        AbstractDialogCellEditor editor = (AbstractDialogCellEditor) constructor.newInstance(new Object[] { parent });
                        editor.propertyDescriptor = this;
                        editor.databaseObjectTreeObject = DatabaseObjectTreeObject.this;
                        if (getValidator() != null) {
                            editor.setValidator(getValidator());
                        }
                        return editor;
                    } catch (Exception e) {
                        ConvertigoPlugin.logException(e, "Unexpected exception");
                        return null;
                    }
                }
            };
        } else if (Enum.class.isAssignableFrom(pec)) {
            String[] tags = EnumUtils.toStrings(pec);
            propertyDescriptor = new DynamicComboBoxPropertyDescriptor(name, displayName, tags, this, name);
        }
    }
    // Special cases
    if (propertyDescriptor == null) {
        // editor for scriptable properties
        Object scriptable = databaseObjectPropertyDescriptor.getValue("scriptable");
        if ((scriptable != null) && (scriptable.equals(Boolean.TRUE)))
            propertyDescriptor = new ScriptablePropertyDescriptor(name, displayName);
        // editor for nillable properties
        if (propertyDescriptor == null) {
            Object nillable = databaseObjectPropertyDescriptor.getValue("nillable");
            if ((nillable != null) && (nillable.equals(Boolean.TRUE))) {
                int style = isMasked ? SWT.PASSWORD : SWT.NONE;
                if (value instanceof String) {
                    propertyDescriptor = new DataOrNullPropertyDescriptor(name, displayName, StringOrNullEditor.class, style);
                } else if (value instanceof XMLVector) {
                    propertyDescriptor = new DataOrNullPropertyDescriptor(name, displayName, ArrayOrNullEditor.class, style);
                }
            }
        }
        // editor for disabled properties
        Object disable = databaseObjectPropertyDescriptor.getValue("disable");
        if ((disable != null) && (disable.equals(Boolean.TRUE)))
            propertyDescriptor = new InfoPropertyDescriptor(name, displayName);
    }
    // Default case
    if (propertyDescriptor == null) {
        propertyDescriptor = new TextPropertyDescriptor(name, displayName);
    // propertyDescriptor.setValidator(new CompilableValueValidator(getValidator(name)));
    }
    if (propertyDescriptor != null) {
        ICellEditorValidator validator = getValidator(name);
        if (validator != null) {
            propertyDescriptor.setValidator(validator);
        }
        final ILabelProvider labelProvider = propertyDescriptor.getLabelProvider();
        propertyDescriptor.setLabelProvider(new ILabelProvider() {

            @Override
            public void removeListener(ILabelProviderListener listener) {
                labelProvider.removeListener(listener);
            }

            @Override
            public boolean isLabelProperty(Object element, String property) {
                return labelProvider.isLabelProperty(element, property);
            }

            @Override
            public void dispose() {
                labelProvider.dispose();
            }

            @Override
            public void addListener(ILabelProviderListener listener) {
                labelProvider.addListener(listener);
            }

            @Override
            public String getText(Object element) {
                String text = labelProvider.getText(element);
                try {
                    String compiled = Engine.theApp.databaseObjectsManager.getCompiledValue(text);
                    if (!text.equals(compiled)) {
                        text += "  => " + compiled;
                    }
                } catch (UndefinedSymbolsException e) {
                    text += "  /!\\ undefined symbol /!\\";
                }
                return text;
            }

            @Override
            public Image getImage(Object element) {
                return labelProvider.getImage(element);
            }
        });
        String beanDescription = databaseObjectPropertyDescriptor.getShortDescription();
        int id = beanDescription.indexOf("|");
        if (id != -1) {
            beanDescription = beanDescription.substring(0, id);
        }
        if (isExtractionRule) {
            propertyDescriptor.setCategory(databaseObjectPropertyDescriptor.isExpert() ? "Selection" : "Configuration");
        } else {
            Object categoryValue = databaseObjectPropertyDescriptor.getValue("category");
            String category = categoryValue == null ? "Base properties" : String.valueOf(categoryValue);
            propertyDescriptor.setCategory(databaseObjectPropertyDescriptor.isExpert() ? "Expert" : category);
        }
        beanDescription = cleanDescription(beanDescription);
        propertyDescriptor.setDescription(beanDescription);
    }
    return propertyDescriptor;
}
Also used : PropertyWithTagsEditor(com.twinsoft.convertigo.eclipse.property_editors.PropertyWithTagsEditor) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) CellEditor(org.eclipse.jface.viewers.CellEditor) AbstractDialogCellEditor(com.twinsoft.convertigo.eclipse.property_editors.AbstractDialogCellEditor) ComboBoxCellEditor(org.eclipse.jface.viewers.ComboBoxCellEditor) InfoPropertyDescriptor(com.twinsoft.convertigo.eclipse.views.projectexplorer.InfoPropertyDescriptor) DynamicInfoPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DynamicInfoPropertyDescriptor) Image(org.eclipse.swt.graphics.Image) AbstractDialogCellEditor(com.twinsoft.convertigo.eclipse.property_editors.AbstractDialogCellEditor) DynamicComboBoxPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DynamicComboBoxPropertyDescriptor) DynamicInfoPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DynamicInfoPropertyDescriptor) InfoPropertyDescriptor(com.twinsoft.convertigo.eclipse.views.projectexplorer.InfoPropertyDescriptor) DynamicComboBoxPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DynamicComboBoxPropertyDescriptor) PropertyDescriptor(org.eclipse.ui.views.properties.PropertyDescriptor) StringComboBoxPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.StringComboBoxPropertyDescriptor) TextPropertyDescriptor(org.eclipse.ui.views.properties.TextPropertyDescriptor) ScriptablePropertyDescriptor(com.twinsoft.convertigo.eclipse.views.projectexplorer.ScriptablePropertyDescriptor) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor) DynamicInfoPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DynamicInfoPropertyDescriptor) DataOrNullPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DataOrNullPropertyDescriptor) Composite(org.eclipse.swt.widgets.Composite) PropertyWithDynamicInfoEditor(com.twinsoft.convertigo.eclipse.property_editors.PropertyWithDynamicInfoEditor) Constructor(java.lang.reflect.Constructor) UndefinedSymbolsException(com.twinsoft.convertigo.engine.UndefinedSymbolsException) PropertyWithDynamicTagsEditor(com.twinsoft.convertigo.eclipse.property_editors.PropertyWithDynamicTagsEditor) Method(java.lang.reflect.Method) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConvertigoException(com.twinsoft.convertigo.engine.ConvertigoException) UndefinedSymbolsException(com.twinsoft.convertigo.engine.UndefinedSymbolsException) ILabelProviderListener(org.eclipse.jface.viewers.ILabelProviderListener) StringOrNullEditor(com.twinsoft.convertigo.eclipse.property_editors.StringOrNullEditor) ICellEditorValidator(org.eclipse.jface.viewers.ICellEditorValidator) StringComboBoxPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.StringComboBoxPropertyDescriptor) TextPropertyDescriptor(org.eclipse.ui.views.properties.TextPropertyDescriptor) ScreenClass(com.twinsoft.convertigo.beans.core.ScreenClass) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) DataOrNullPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DataOrNullPropertyDescriptor) ScriptablePropertyDescriptor(com.twinsoft.convertigo.eclipse.views.projectexplorer.ScriptablePropertyDescriptor)

Example 2 with DynamicComboBoxPropertyDescriptor

use of com.twinsoft.convertigo.eclipse.property_editors.DynamicComboBoxPropertyDescriptor in project convertigo by convertigo.

the class DatabaseObjectTreeObject method getPropertyValue.

public Object getPropertyValue(Object id) {
    if (id == null)
        return null;
    DatabaseObject databaseObject = getObject();
    String propertyName = (String) id;
    if (propertyName.equals(P_TYPE))
        return databaseObjectBeanDescriptor.getDisplayName();
    else if (propertyName.equals(P_JAVA_CLASS))
        return databaseObject.getClass().getName();
    else if (propertyName.equals(P_NAME))
        return databaseObject.getName();
    else if (propertyName.equals(P_QNAME))
        return databaseObject.getQName();
    else if (propertyName.equals(P_PRIORITY))
        return Long.toString(databaseObject.priority);
    else if (propertyName.equals(P_DEPTH)) {
        if (databaseObject instanceof ScreenClass)
            return Integer.toString(((ScreenClass) databaseObject).getDepth());
        else
            return org.apache.commons.lang3.StringUtils.countMatches(databaseObject.getQName(), '.');
    } else if (propertyName.equals(P_EXPORTED)) {
        return databaseObject.getProject().getInfoForProperty("exported");
    } else if (propertyName.equals(P_MIN_VERSION)) {
        return databaseObject.getProject().getMinVersion();
    } else {
        try {
            java.beans.PropertyDescriptor databaseObjectPropertyDescriptor = getPropertyDescriptor(propertyName);
            if (databaseObjectPropertyDescriptor == null) {
                return null;
            }
            Class<?> pec = databaseObjectPropertyDescriptor.getPropertyEditorClass();
            Method getter = databaseObjectPropertyDescriptor.getReadMethod();
            Object compilablePropertySourceValue = databaseObject.getCompilablePropertySourceValue(propertyName);
            Object value;
            if (compilablePropertySourceValue == null) {
                Object[] args = {};
                value = getter.invoke(databaseObject, args);
            } else {
                value = compilablePropertySourceValue;
            }
            boolean done = false;
            if (value instanceof String) {
                PropertyDescriptor propertyDescriptor = findPropertyDescriptor(id);
                if (propertyDescriptor instanceof DynamicComboBoxPropertyDescriptor) {
                    DynamicComboBoxPropertyDescriptor pd = (DynamicComboBoxPropertyDescriptor) propertyDescriptor;
                    value = pd.setValue((String) value);
                    done = true;
                }
            }
            if (done) {
            // do nothing
            } else if (value instanceof Boolean) {
                value = ((Boolean) value).booleanValue() ? Integer.valueOf(0) : Integer.valueOf(1);
            } else if ((pec != null) && (PropertyWithTagsEditor.class.isAssignableFrom(pec) || Enum.class.isAssignableFrom(pec))) {
                if (!(value instanceof Integer)) {
                    if (PropertyWithTagsEditorAdvance.class.isAssignableFrom(pec)) {
                        Method getTags = pec.getMethod("getTags", new Class[] { DatabaseObjectTreeObject.class, String.class });
                        String[] tags = (String[]) getTags.invoke(null, new Object[] { this, propertyName });
                        int i;
                        for (i = 0; i < tags.length; i++) {
                            if (tags[i].equals(value)) {
                                value = Integer.valueOf(i);
                                break;
                            }
                        }
                        // if we did not find our string in the tag list set value to index 0
                        if (i == tags.length) {
                            value = Integer.valueOf(0);
                            String message = "Incorrect property \"" + propertyName + "\" value for the object \"" + databaseObject.getName() + "\".";
                            ConvertigoPlugin.logWarning(message);
                        }
                    } else if (Enum.class.isAssignableFrom(pec)) {
                        value = Integer.valueOf(((Enum<?>) value).ordinal());
                    } else if (StringComboBoxPropertyDescriptor.class.isAssignableFrom(pec)) {
                    // nothing to do: value is a string
                    }
                }
                if ((EmulatorTechnologyEditor.class.equals(pec))) {
                    Method getEmulatorClassNames = pec.getDeclaredMethod("getEmulatorClassNames", new Class[] { DatabaseObjectTreeObject.class });
                    String[] emulatorClassNames = (String[]) getEmulatorClassNames.invoke(null, new Object[] { this });
                    for (int i = 0; i < emulatorClassNames.length; i++) {
                        if (emulatorClassNames[i].equals(value)) {
                            value = Integer.valueOf(i);
                            break;
                        }
                    }
                }
            // else simply return the combo index
            } else if (value instanceof Number) {
                value = ((Number) value).toString();
            }
            // Get property's nillable value
            if (Boolean.TRUE.equals(databaseObjectPropertyDescriptor.getValue("nillable"))) {
                try {
                    Boolean isNull = ((INillableProperty) databaseObject).isNullProperty(propertyName);
                    PropertyDescriptor pd = findPropertyDescriptor(propertyName);
                    if ((pd != null) && (pd instanceof DataOrNullPropertyDescriptor)) {
                        ((DataOrNullPropertyDescriptor) pd).setNullProperty(isNull);
                        if (isNull) {
                            // Overrides value by fake one used by property editor
                            if (value instanceof String)
                                value = "<value is null>";
                            if (value instanceof XMLVector) {
                                XMLVector<Object> xmlv = new XMLVector<Object>();
                                xmlv.add("null");
                                value = xmlv;
                            }
                        }
                    }
                } catch (Exception e) {
                    String message = "Error while trying to retrieve 'isNull' attribute of property \"" + propertyName + "\" for the object \"" + databaseObject.getName() + "\".";
                    ConvertigoPlugin.logException(e, message);
                }
            }
            // Check for property normalized value if needed
            if (Boolean.TRUE.equals(databaseObjectPropertyDescriptor.getValue(DatabaseObject.PROPERTY_XMLNAME))) {
                // Ignore compilable property source value
                if (compilablePropertySourceValue == null) {
                    if (value instanceof String) {
                        if (!XMLUtils.checkName(value.toString())) {
                            String message = "Property \"" + propertyName + "\" value for the object \"" + databaseObject.getName() + "\" isn't XML compliant.";
                            ConvertigoPlugin.logError(message, Boolean.TRUE);
                        }
                    }
                }
            }
            return value;
        } catch (Exception e) {
            String message = "Error while trying to retrieve property \"" + propertyName + "\" value for the object \"" + databaseObject.getName() + "\".";
            ConvertigoPlugin.logException(e, message);
            return null;
        }
    }
}
Also used : XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) InfoPropertyDescriptor(com.twinsoft.convertigo.eclipse.views.projectexplorer.InfoPropertyDescriptor) DynamicComboBoxPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DynamicComboBoxPropertyDescriptor) PropertyDescriptor(org.eclipse.ui.views.properties.PropertyDescriptor) StringComboBoxPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.StringComboBoxPropertyDescriptor) TextPropertyDescriptor(org.eclipse.ui.views.properties.TextPropertyDescriptor) ScriptablePropertyDescriptor(com.twinsoft.convertigo.eclipse.views.projectexplorer.ScriptablePropertyDescriptor) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor) DynamicInfoPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DynamicInfoPropertyDescriptor) DataOrNullPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DataOrNullPropertyDescriptor) ScreenClass(com.twinsoft.convertigo.beans.core.ScreenClass) Method(java.lang.reflect.Method) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConvertigoException(com.twinsoft.convertigo.engine.ConvertigoException) UndefinedSymbolsException(com.twinsoft.convertigo.engine.UndefinedSymbolsException) DynamicComboBoxPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DynamicComboBoxPropertyDescriptor) StringComboBoxPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.StringComboBoxPropertyDescriptor) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) PropertyWithTagsEditorAdvance(com.twinsoft.convertigo.eclipse.property_editors.PropertyWithTagsEditorAdvance) ScreenClass(com.twinsoft.convertigo.beans.core.ScreenClass) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) DataOrNullPropertyDescriptor(com.twinsoft.convertigo.eclipse.property_editors.DataOrNullPropertyDescriptor)

Aggregations

XMLVector (com.twinsoft.convertigo.beans.common.XMLVector)2 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)2 ScreenClass (com.twinsoft.convertigo.beans.core.ScreenClass)2 DataOrNullPropertyDescriptor (com.twinsoft.convertigo.eclipse.property_editors.DataOrNullPropertyDescriptor)2 DynamicComboBoxPropertyDescriptor (com.twinsoft.convertigo.eclipse.property_editors.DynamicComboBoxPropertyDescriptor)2 DynamicInfoPropertyDescriptor (com.twinsoft.convertigo.eclipse.property_editors.DynamicInfoPropertyDescriptor)2 StringComboBoxPropertyDescriptor (com.twinsoft.convertigo.eclipse.property_editors.StringComboBoxPropertyDescriptor)2 InfoPropertyDescriptor (com.twinsoft.convertigo.eclipse.views.projectexplorer.InfoPropertyDescriptor)2 ScriptablePropertyDescriptor (com.twinsoft.convertigo.eclipse.views.projectexplorer.ScriptablePropertyDescriptor)2 ConvertigoException (com.twinsoft.convertigo.engine.ConvertigoException)2 UndefinedSymbolsException (com.twinsoft.convertigo.engine.UndefinedSymbolsException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 CoreException (org.eclipse.core.runtime.CoreException)2 IPropertyDescriptor (org.eclipse.ui.views.properties.IPropertyDescriptor)2 PropertyDescriptor (org.eclipse.ui.views.properties.PropertyDescriptor)2 TextPropertyDescriptor (org.eclipse.ui.views.properties.TextPropertyDescriptor)2 AbstractDialogCellEditor (com.twinsoft.convertigo.eclipse.property_editors.AbstractDialogCellEditor)1 PropertyWithDynamicInfoEditor (com.twinsoft.convertigo.eclipse.property_editors.PropertyWithDynamicInfoEditor)1 PropertyWithDynamicTagsEditor (com.twinsoft.convertigo.eclipse.property_editors.PropertyWithDynamicTagsEditor)1