Search in sources :

Example 1 with IStylePropertyChangesRecorder

use of com.servoy.j2db.ui.IStylePropertyChangesRecorder in project servoy-client by Servoy.

the class ComponentFactory method createGraphicalComponent.

private static IComponent createGraphicalComponent(IApplication application, Form form, GraphicalComponent label, IScriptExecuter el, IDataProviderLookup dataProviderLookup) {
    int style_halign = -1;
    int style_valign = -1;
    int textTransform = 0;
    int mediaid = 0;
    Pair<IStyleSheet, IStyleRule> styleInfo = getStyleForBasicComponent(application, label, form);
    if (styleInfo != null) {
        IStyleSheet ss = styleInfo.getLeft();
        IStyleRule s = styleInfo.getRight();
        if (ss != null && s != null) {
            style_valign = ss.getVAlign(s);
            style_halign = ss.getHAlign(s);
            boolean parseMedia = true;
            // anything else then then the css through the templategenerator is used. (See TemplateGenerator.createGraphicalComponentHTML)
            if (application.getApplicationType() == IApplication.WEB_CLIENT) {
                parseMedia = s.getValue(CSS.Attribute.BACKGROUND_REPEAT.toString()) == null && s.getValue(CSS.Attribute.BACKGROUND_POSITION.toString()) == null;
            }
            if (parseMedia) {
                Object mediaUrl = s.getValue(CSS.Attribute.BACKGROUND_IMAGE.toString());
                if (mediaUrl != null && mediaUrl.toString() != null) {
                    String mediaUrlString = mediaUrl.toString();
                    int start = mediaUrlString.indexOf(MediaURLStreamHandler.MEDIA_URL_DEF);
                    if (start != -1) {
                        String name = mediaUrlString.substring(start + MediaURLStreamHandler.MEDIA_URL_DEF.length());
                        if (name.endsWith("')") || name.endsWith("\")"))
                            name = name.substring(0, name.length() - 2);
                        if (name.endsWith(")"))
                            name = name.substring(0, name.length() - 1);
                        Media media = application.getFlattenedSolution().getMedia(name);
                        if (media != null) {
                            mediaid = media.getID();
                        }
                    }
                }
            }
            String transform = s.getValue(CSS.Attribute.TEXT_TRANSFORM.toString());
            if (transform != null) {
                if ("uppercase".equals(transform)) {
                    textTransform = ILabel.UPPERCASE;
                } else if ("lowercase".equals(transform)) {
                    textTransform = ILabel.LOWERCASE;
                } else if ("capitalize".equals(transform)) {
                    textTransform = ILabel.CAPITALIZE;
                }
            }
        }
    }
    ILabel l;
    AbstractRuntimeLabel<? extends ILabel> scriptable;
    IStylePropertyChangesRecorder jsChangeRecorder = application.getItemFactory().createChangesRecorder();
    if (ComponentFactory.isButton(label)) {
        IButton button;
        if (label.getDataProviderID() == null && !label.getDisplaysTags()) {
            scriptable = new RuntimeScriptButton(jsChangeRecorder, application);
            button = application.getItemFactory().createScriptButton((RuntimeScriptButton) scriptable, getWebID(form, label));
        } else {
            scriptable = new RuntimeDataButton(jsChangeRecorder, application);
            button = application.getItemFactory().createDataButton((RuntimeDataButton) scriptable, getWebID(form, label));
            IDataProvider dp = null;
            try {
                dp = dataProviderLookup == null ? null : dataProviderLookup.getDataProvider(label.getDataProviderID());
            } catch (RepositoryException e) {
                Debug.error(e);
            }
            ((IDisplayData) button).setDataProviderID(dp == null ? label.getDataProviderID() : dp.getDataProviderID());
            ((IDisplayTagText) button).setTagText(application.getI18NMessageIfPrefixed(label.getText()));
            ((IDisplayData) button).setNeedEntireState(label.getDisplaysTags());
        }
        ((AbstractRuntimeButton<IButton>) scriptable).setComponent(button, label);
        button.setMediaOption(label.getMediaOptions());
        if (label.getRolloverImageMediaID() > 0) {
            try {
                button.setRolloverIcon(label.getRolloverImageMediaID());
                button.setRolloverEnabled(true);
            } catch (Exception ex) {
                Debug.error(ex);
            }
        }
        l = button;
    } else {
        if (label.getDataProviderID() == null && !label.getDisplaysTags()) {
            scriptable = new RuntimeScriptLabel(jsChangeRecorder, application);
            l = application.getItemFactory().createScriptLabel((RuntimeScriptLabel) scriptable, getWebID(form, label), (label.getOnActionMethodID() > 0));
        } else {
            scriptable = new RuntimeDataLabel(jsChangeRecorder, application);
            l = application.getItemFactory().createDataLabel((RuntimeDataLabel) scriptable, getWebID(form, label), (label.getOnActionMethodID() > 0));
            IDataProvider dp = null;
            try {
                dp = dataProviderLookup == null ? null : dataProviderLookup.getDataProvider(label.getDataProviderID());
            } catch (RepositoryException e) {
                Debug.error(e);
            }
            ((IDisplayData) l).setDataProviderID(dp == null ? label.getDataProviderID() : dp.getDataProviderID());
            ((IDisplayTagText) l).setTagText(application.getI18NMessageIfPrefixed(label.getText()));
            ((IDisplayData) l).setNeedEntireState(label.getDisplaysTags());
        }
        ((AbstractHTMLSubmitRuntimeLabel<ILabel>) scriptable).setComponent(l, label);
        l.setMediaOption(label.getMediaOptions());
        if (label.getRolloverImageMediaID() > 0) {
            try {
                l.setRolloverIcon(label.getRolloverImageMediaID());
            } catch (Exception ex) {
                Debug.error(ex);
            }
        }
    }
    String mnemonic = application.getI18NMessageIfPrefixed(label.getMnemonic());
    if (mnemonic != null && mnemonic.length() > 0) {
        l.setDisplayedMnemonic(mnemonic.charAt(0));
    }
    l.setTextTransform(textTransform);
    if (el != null && (label.getOnActionMethodID() > 0 || label.getOnDoubleClickMethodID() > 0 || label.getOnRightClickMethodID() > 0)) {
        l.addScriptExecuter(el);
        if (label.getOnActionMethodID() > 0)
            l.setActionCommand(Integer.toString(label.getOnActionMethodID()), Utils.parseJSExpressions(label.getFlattenedMethodArguments("onActionMethodID")));
        if (label.getOnDoubleClickMethodID() > 0)
            l.setDoubleClickCommand(Integer.toString(label.getOnDoubleClickMethodID()), Utils.parseJSExpressions(label.getFlattenedMethodArguments("onDoubleClickMethodID")));
        if (label.getOnRightClickMethodID() > 0)
            l.setRightClickCommand(Integer.toString(label.getOnRightClickMethodID()), Utils.parseJSExpressions(label.getFlattenedMethodArguments("onRightClickMethodID")));
    }
    if (label.getLabelFor() == null || (form.getView() != FormController.TABLE_VIEW && form.getView() != FormController.LOCKED_TABLE_VIEW)) {
        int onRenderMethodID = label.getOnRenderMethodID();
        AbstractBase onRenderPersist = label;
        if (onRenderMethodID <= 0) {
            onRenderMethodID = form.getOnRenderMethodID();
            onRenderPersist = form;
        }
        if (onRenderMethodID > 0) {
            RenderEventExecutor renderEventExecutor = scriptable.getRenderEventExecutor();
            renderEventExecutor.setRenderCallback(Integer.toString(onRenderMethodID), Utils.parseJSExpressions(onRenderPersist.getFlattenedMethodArguments("onRenderMethodID")));
            IForm rendererForm = application.getFormManager().getForm(form.getName());
            IScriptExecuter rendererScriptExecuter = rendererForm instanceof FormController ? ((FormController) rendererForm).getScriptExecuter() : null;
            renderEventExecutor.setRenderScriptExecuter(rendererScriptExecuter);
        }
    }
    l.setRotation(label.getRotation());
    l.setFocusPainted(label.getShowFocus());
    l.setCursor(Cursor.getPredefinedCursor(label.getRolloverCursor()));
    try {
        int halign = label.getHorizontalAlignment();
        if (halign != -1) {
            l.setHorizontalAlignment(halign);
        } else if (style_halign != -1) {
            l.setHorizontalAlignment(style_halign);
        }
    } catch (RuntimeException e) {
    // just ignore...Debug.error(e);
    }
    int valign = label.getVerticalAlignment();
    if (valign != -1) {
        l.setVerticalAlignment(valign);
    } else if (style_valign != -1) {
        l.setVerticalAlignment(style_valign);
    }
    try {
        if (!label.getDisplaysTags()) {
            l.setText(application.getI18NMessageIfPrefixed(label.getText()));
        }
    } catch (RuntimeException e1) {
    // ignore
    }
    l.setToolTipText(application.getI18NMessageIfPrefixed(label.getToolTipText()));
    if (label.getImageMediaID() > 0) {
        try {
            l.setMediaIcon(label.getImageMediaID());
        } catch (Exception e) {
            Debug.error(e);
        }
    } else if (mediaid > 0) {
        try {
            l.setMediaIcon(mediaid);
        } catch (Exception e) {
            Debug.error(e);
        }
    }
    if (label.getDataProviderID() != null) {
        scriptable.setComponentFormat(ComponentFormat.getComponentFormat(label.getFormat(), label.getDataProviderID(), dataProviderLookup, application));
    }
    applyBasicComponentProperties(application, l, label, styleInfo);
    Border border = null;
    Insets insets = null;
    if (label.getBorderType() != null) {
        border = ComponentFactoryHelper.createBorder(label.getBorderType());
    }
    if (label.getMargin() != null) {
        insets = label.getMargin();
    }
    if (styleInfo != null && (border == null || insets == null)) {
        IStyleSheet ss = styleInfo.getLeft();
        IStyleRule s = styleInfo.getRight();
        if (ss != null && s != null) {
            if (border == null && ss.hasBorder(s)) {
                border = ss.getBorder(s);
            }
            if (insets == null && ss.hasMargin(s)) {
                insets = ss.getMargin(s);
            }
        }
    }
    if (border != null && insets != null) {
        l.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right)));
    } else if (border == null && insets != null && l instanceof IButton) {
        ((IButton) l).setMargin(insets);
    } else // supports setMargin, then fake the margins through empty border. (issue 166391)
    if (border == null && insets != null) {
        l.setBorder(BorderFactory.createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right));
    }
    if (l instanceof IAnchoredComponent) {
        ((IAnchoredComponent) l).setAnchors(label.getAnchors());
    }
    return l;
}
Also used : Insets(java.awt.Insets) ILabel(com.servoy.j2db.ui.ILabel) RuntimeDataLabel(com.servoy.j2db.ui.scripting.RuntimeDataLabel) IForm(com.servoy.j2db.IForm) IDataProvider(com.servoy.j2db.persistence.IDataProvider) RuntimeScriptButton(com.servoy.j2db.ui.scripting.RuntimeScriptButton) FormController(com.servoy.j2db.FormController) IStyleSheet(com.servoy.j2db.util.IStyleSheet) IScriptExecuter(com.servoy.j2db.IScriptExecuter) AbstractHTMLSubmitRuntimeLabel(com.servoy.j2db.ui.scripting.AbstractHTMLSubmitRuntimeLabel) RuntimeDataButton(com.servoy.j2db.ui.scripting.RuntimeDataButton) Media(com.servoy.j2db.persistence.Media) AbstractBase(com.servoy.j2db.persistence.AbstractBase) RuntimeScriptLabel(com.servoy.j2db.ui.scripting.RuntimeScriptLabel) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JSONException(org.json.JSONException) IOException(java.io.IOException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IAnchoredComponent(com.servoy.j2db.ui.IAnchoredComponent) IButton(com.servoy.j2db.ui.IButton) IStyleRule(com.servoy.j2db.util.IStyleRule) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IDisplayData(com.servoy.j2db.dataprocessing.IDisplayData) RenderEventExecutor(com.servoy.j2db.ui.RenderEventExecutor) AbstractRuntimeButton(com.servoy.j2db.ui.scripting.AbstractRuntimeButton) IStylePropertyChangesRecorder(com.servoy.j2db.ui.IStylePropertyChangesRecorder) Border(javax.swing.border.Border) TitledBorder(javax.swing.border.TitledBorder) IDisplayTagText(com.servoy.j2db.ui.IDisplayTagText)

Example 2 with IStylePropertyChangesRecorder

use of com.servoy.j2db.ui.IStylePropertyChangesRecorder in project servoy-client by Servoy.

the class ScrollResponseHeaderContainer method applyStyleOnComponent.

private void applyStyleOnComponent(Component comp, Object bgColor, Object fgColor, Object compFont, Object listItemBorder) {
    if (comp instanceof IScriptableProvider) {
        IScriptable s = ((IScriptableProvider) comp).getScriptObject();
        if (s instanceof IRuntimeComponent) {
            IRuntimeComponent sbm = (IRuntimeComponent) s;
            RenderableWrapper sbmRW = null;
            if (s instanceof ISupportOnRenderCallback) {
                IScriptRenderMethods sr = ((ISupportOnRenderCallback) s).getRenderable();
                if (sr instanceof RenderableWrapper)
                    sbmRW = (RenderableWrapper) sr;
            }
            if (bgColor != null) {
                if (sbmRW != null)
                    sbmRW.clearProperty(RenderableWrapper.PROPERTY_BGCOLOR);
                String oldColor = sbm.getBgcolor();
                sbm.setBgcolor(bgColor.toString());
                if (sbm instanceof AbstractRuntimeBaseComponent && ((AbstractRuntimeBaseComponent) sbm).getComponent() instanceof WebDataLookupField) {
                    ((WebDataLookupField) ((AbstractRuntimeBaseComponent) sbm).getComponent()).setListColor(PersistHelper.createColor(oldColor));
                }
                if (sbm.isTransparent()) {
                    // apply the bg color even if transparent by clearing the transparent flag in the property changes map
                    if (comp instanceof IProviderStylePropertyChanges && ((IProviderStylePropertyChanges) comp).getStylePropertyChanges() instanceof IStylePropertyChangesRecorder) {
                        ((IStylePropertyChangesRecorder) (((IProviderStylePropertyChanges) comp).getStylePropertyChanges())).setTransparent(false);
                    }
                }
            } else {
                if (sbmRW != null && !Utils.equalObjects(sbmRW.getOnRenderSetProperties().get(RenderableWrapper.PROPERTY_BGCOLOR), sbm.getBgcolor()))
                    sbmRW.clearProperty(RenderableWrapper.PROPERTY_BGCOLOR);
                sbm.setBgcolor(sbm.getBgcolor());
                setParentBGcolor(comp, "");
            }
            if (fgColor != null) {
                if (sbmRW != null)
                    sbmRW.clearProperty(RenderableWrapper.PROPERTY_FGCOLOR);
                sbm.setFgcolor(fgColor.toString());
            } else {
                if (sbmRW != null && !Utils.equalObjects(sbmRW.getOnRenderSetProperties().get(RenderableWrapper.PROPERTY_FGCOLOR), sbm.getFgcolor())) {
                    sbmRW.clearProperty(RenderableWrapper.PROPERTY_FGCOLOR);
                }
                sbm.setFgcolor(sbm.getFgcolor());
            }
            if (compFont != null) {
                if (sbmRW != null && !Utils.equalObjects(sbmRW.getOnRenderSetProperties().get(RenderableWrapper.PROPERTY_FONT), sbm.getFont()))
                    sbmRW.clearProperty(RenderableWrapper.PROPERTY_FONT);
                sbm.setFont(compFont.toString());
            } else {
                if (sbmRW != null)
                    sbmRW.clearProperty(RenderableWrapper.PROPERTY_FONT);
                sbm.setFont(sbm.getFont());
            }
            if (listItemBorder != null) {
                // TODO left / right part of this list item border should only be applied on first / last components in the row (for table view)
                // like it is done in servoy.js when client side styling is used
                String newBorder = listItemBorder.toString();
                Border currentBorder = ComponentFactoryHelper.createBorder(sbm.getBorder());
                Border marginBorder = null;
                if (currentBorder instanceof EmptyBorder) {
                    marginBorder = currentBorder;
                } else if (currentBorder instanceof CompoundBorder && ((CompoundBorder) currentBorder).getInsideBorder() instanceof EmptyBorder) {
                    marginBorder = ((CompoundBorder) currentBorder).getInsideBorder();
                }
                if (marginBorder != null) {
                    newBorder = ComponentFactoryHelper.createBorderString(BorderFactory.createCompoundBorder(ComponentFactoryHelper.createBorder(newBorder), marginBorder));
                }
                if (sbmRW != null)
                    sbmRW.clearProperty(RenderableWrapper.PROPERTY_BORDER);
                sbm.setBorder(newBorder);
                // reset size so the web size will be recalculated based on the new border
                sbm.setSize(sbm.getWidth(), sbm.getHeight());
            } else {
                if (sbmRW != null && !Utils.equalObjects(sbmRW.getOnRenderSetProperties().get(RenderableWrapper.PROPERTY_BORDER), sbm.getBorder()))
                    sbmRW.clearProperty(RenderableWrapper.PROPERTY_BORDER);
                sbm.setBorder(sbm.getBorder());
            }
        }
    }
}
Also used : ISupportOnRenderCallback(com.servoy.j2db.ui.ISupportOnRenderCallback) IScriptable(com.servoy.j2db.scripting.IScriptable) IScriptRenderMethods(com.servoy.j2db.ui.IScriptRenderMethods) AbstractRuntimeBaseComponent(com.servoy.j2db.ui.scripting.AbstractRuntimeBaseComponent) IRuntimeComponent(com.servoy.j2db.ui.runtime.IRuntimeComponent) RenderableWrapper(com.servoy.j2db.ui.RenderableWrapper) IProviderStylePropertyChanges(com.servoy.j2db.ui.IProviderStylePropertyChanges) CompoundBorder(javax.swing.border.CompoundBorder) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider) EmptyBorder(javax.swing.border.EmptyBorder) IStylePropertyChangesRecorder(com.servoy.j2db.ui.IStylePropertyChangesRecorder) Border(javax.swing.border.Border) CompoundBorder(javax.swing.border.CompoundBorder) TitledBorder(javax.swing.border.TitledBorder) EmptyBorder(javax.swing.border.EmptyBorder)

Example 3 with IStylePropertyChangesRecorder

use of com.servoy.j2db.ui.IStylePropertyChangesRecorder in project servoy-client by Servoy.

the class ComponentFactory method createField.

private static IComponent createField(IApplication application, Form form, Field field, IDataProviderLookup dataProviderLookup, IScriptExecuter el, boolean printing) {
    ValueList valuelist = application.getFlattenedSolution().getValueList(field.getValuelistID());
    ComponentFormat fieldFormat = ComponentFormat.getComponentFormat(field.getFormat(), field.getDataProviderID(), dataProviderLookup, application);
    IDataProvider dp = null;
    if (field.getDataProviderID() != null && dataProviderLookup != null) {
        try {
            dp = dataProviderLookup.getDataProvider(field.getDataProviderID());
        } catch (RepositoryException e) {
            Debug.error(e);
        }
    }
    // apply any style
    Insets style_margin = null;
    int style_halign = -1;
    boolean hasBorder = false;
    Pair<IStyleSheet, IStyleRule> styleInfo = getStyleForBasicComponent(application, field, form);
    if (styleInfo != null) {
        IStyleSheet ss = styleInfo.getLeft();
        IStyleRule s = styleInfo.getRight();
        if (ss != null && s != null) {
            style_margin = ss.getMargin(s);
            style_halign = ss.getHAlign(s);
            hasBorder = ss.hasBorder(s);
        }
    }
    IStylePropertyChangesRecorder jsChangeRecorder = application.getItemFactory().createChangesRecorder();
    IFieldComponent fl;
    AbstractRuntimeField<? extends IFieldComponent> scriptable;
    switch(field.getDisplayType()) {
        case Field.PASSWORD:
            {
                RuntimeDataPassword so;
                scriptable = so = new RuntimeDataPassword(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataPassword(so, getWebID(form, field));
                so.setComponent(fl, field);
            }
            break;
        case Field.RTF_AREA:
            {
                RuntimeRtfArea so;
                scriptable = so = new RuntimeRtfArea(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataTextEditor(so, getWebID(form, field), RTF_AREA, field.getEditable());
                so.setComponent(fl, field);
                if (fl instanceof IScrollPane) {
                    applyScrollBarsProperty((IScrollPane) fl, field);
                }
            }
            break;
        case Field.HTML_AREA:
            {
                RuntimeHTMLArea so;
                scriptable = so = new RuntimeHTMLArea(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataTextEditor(so, getWebID(form, field), HTML_AREA, field.getEditable());
                so.setComponent(fl, field);
                if (fl instanceof IScrollPane) {
                    applyScrollBarsProperty((IScrollPane) fl, field);
                }
            }
            break;
        case Field.TEXT_AREA:
            {
                RuntimeTextArea so;
                scriptable = so = new RuntimeTextArea(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataTextArea(so, getWebID(form, field));
                so.setComponent(fl, field);
                if (fl instanceof IScrollPane) {
                    applyScrollBarsProperty((IScrollPane) fl, field);
                }
            }
            break;
        case Field.CHECKS:
            {
                AbstractRuntimeValuelistComponent<IFieldComponent> so;
                if (valuelist != null) {
                    IValueList list = getRealValueList(application, valuelist, true, fieldFormat.dpType, fieldFormat.parsedFormat, field.getDataProviderID());
                    if (isSingleValue(valuelist)) {
                        scriptable = so = new RuntimeCheckbox(jsChangeRecorder, application);
                        fl = application.getItemFactory().createCheckBox((RuntimeCheckbox) so, getWebID(form, field), application.getI18NMessageIfPrefixed(field.getText()), list);
                    } else {
                        scriptable = so = new RuntimeCheckBoxChoice(jsChangeRecorder, application);
                        fl = application.getItemFactory().createDataChoice((RuntimeCheckBoxChoice) so, getWebID(form, field), list, false, fieldFormat == null || fieldFormat.dpType == IColumnTypes.TEXT);
                        if (fl instanceof IScrollPane) {
                            applyScrollBarsProperty((IScrollPane) fl, field);
                        }
                    }
                } else {
                    scriptable = so = new RuntimeCheckbox(jsChangeRecorder, application);
                    fl = application.getItemFactory().createCheckBox((RuntimeCheckbox) so, getWebID(form, field), application.getI18NMessageIfPrefixed(field.getText()), null);
                }
                so.setComponent(fl, field);
            }
            break;
        case Field.RADIOS:
            {
                AbstractRuntimeValuelistComponent<IFieldComponent> so;
                IValueList list = getRealValueList(application, valuelist, true, fieldFormat.dpType, fieldFormat.parsedFormat, field.getDataProviderID());
                if (isSingleValue(valuelist)) {
                    scriptable = so = new RuntimeRadioButton(jsChangeRecorder, application);
                    fl = application.getItemFactory().createRadioButton((RuntimeRadioButton) so, getWebID(form, field), application.getI18NMessageIfPrefixed(field.getText()), list);
                } else {
                    scriptable = so = new RuntimeRadioChoice(jsChangeRecorder, application);
                    fl = application.getItemFactory().createDataChoice((RuntimeRadioChoice) so, getWebID(form, field), list, true, false);
                    if (fl instanceof IScrollPane) {
                        applyScrollBarsProperty((IScrollPane) fl, field);
                    }
                }
                so.setComponent(fl, field);
            }
            break;
        case Field.COMBOBOX:
            {
                RuntimeDataCombobox so;
                scriptable = so = new RuntimeDataCombobox(jsChangeRecorder, application);
                IValueList list = getRealValueList(application, valuelist, true, fieldFormat.dpType, fieldFormat.parsedFormat, field.getDataProviderID());
                fl = application.getItemFactory().createDataComboBox(so, getWebID(form, field), list);
                so.setComponent(fl, field);
            }
            break;
        case Field.CALENDAR:
            {
                RuntimeDataCalendar so;
                scriptable = so = new RuntimeDataCalendar(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataCalendar(so, getWebID(form, field));
                so.setComponent(fl, field);
            }
            break;
        case Field.IMAGE_MEDIA:
            {
                RuntimeMediaField so;
                scriptable = so = new RuntimeMediaField(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataImgMediaField(so, getWebID(form, field));
                if (fl instanceof IScrollPane) {
                    applyScrollBarsProperty((IScrollPane) fl, field);
                }
                so.setComponent(fl, field);
            }
            break;
        case Field.TYPE_AHEAD:
            if (field.getValuelistID() > 0) {
                fl = createTypeAheadWithValueList(application, form, field, dataProviderLookup, fieldFormat.uiType, fieldFormat.parsedFormat, jsChangeRecorder);
                if (fl == null)
                    return null;
                scriptable = (AbstractRuntimeField<? extends IFieldComponent>) fl.getScriptObject();
                break;
            }
            if (// only allow plain columns
            dp != null && dp.getColumnWrapper() != null && dp.getColumnWrapper().getRelations() == null) {
                RuntimeDataLookupField so;
                scriptable = so = new RuntimeDataLookupField(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataLookupField(so, getWebID(form, field), form.getServerName(), form.getTableName(), dp == null ? field.getDataProviderID() : dp.getDataProviderID());
                so.setComponent(fl, field);
                break;
            } else {
                RuntimeDataField so;
                scriptable = so = new RuntimeDataField(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataField(so, getWebID(form, field));
                so.setComponent(fl, field);
                break;
            }
        // $FALL-THROUGH$
        case Field.LIST_BOX:
        case Field.MULTISELECT_LISTBOX:
            {
                boolean multiSelect = (field.getDisplayType() == Field.MULTISELECT_LISTBOX);
                RuntimeListBox so;
                scriptable = so = new RuntimeListBox(jsChangeRecorder, application, multiSelect);
                IValueList list = getRealValueList(application, valuelist, true, fieldFormat.dpType, fieldFormat.parsedFormat, field.getDataProviderID());
                fl = application.getItemFactory().createListBox(so, getWebID(form, field), list, multiSelect);
                so.setComponent(fl, field);
            }
            break;
        case Field.SPINNER:
            {
                RuntimeSpinner so;
                scriptable = so = new RuntimeSpinner(jsChangeRecorder, application);
                IValueList list = getRealValueList(application, valuelist, true, fieldFormat.dpType, fieldFormat.parsedFormat, field.getDataProviderID());
                fl = application.getItemFactory().createSpinner(so, getWebID(form, field), list);
                so.setComponent(fl, field);
                break;
            }
        // else treat as the default case: TEXT_FIELD
        default:
            // Field.TEXT_FIELD
            if (field.getValuelistID() > 0) {
                fl = createTypeAheadWithValueList(application, form, field, dataProviderLookup, fieldFormat.uiType, fieldFormat.parsedFormat, jsChangeRecorder);
                if (fl == null)
                    return null;
                scriptable = (AbstractRuntimeField<? extends IFieldComponent>) fl.getScriptObject();
            } else {
                RuntimeDataField so;
                scriptable = so = new RuntimeDataField(jsChangeRecorder, application);
                fl = application.getItemFactory().createDataField(so, getWebID(form, field));
                so.setComponent(fl, field);
            }
    }
    if (fl instanceof ISupportAsyncLoading) {
        ((ISupportAsyncLoading) fl).setAsyncLoadingEnabled(!printing);
    }
    fl.setSelectOnEnter(field.getSelectOnEnter());
    fl.setEditable(field.getEditable());
    try {
        int halign = field.getHorizontalAlignment();
        if (halign != -1) {
            fl.setHorizontalAlignment(halign);
        } else if (style_halign != -1) {
            fl.setHorizontalAlignment(style_halign);
        }
    } catch (RuntimeException e) {
    // just ignore...Debug.error(e);
    }
    fl.setToolTipText(application.getI18NMessageIfPrefixed(field.getToolTipText()));
    fl.setTitleText(application.getI18NMessageIfPrefixed(field.getText()));
    fl.setDataProviderID(dp == null ? field.getDataProviderID() : dp.getDataProviderID());
    if (field.getDataProviderID() != null && dataProviderLookup != null) {
        if (scriptable instanceof IFormatScriptComponent) {
            ((IFormatScriptComponent) scriptable).setComponentFormat(fieldFormat);
        }
        if (dp != null) {
            // if (valuelist != null && valuelist.getValueListType() != ValueList.CUSTOM_VALUES) type = valuelist.getDisplayValueType();
            int l = dp.getLength();
            int defaultType = Column.mapToDefaultType(fieldFormat.dpType);
            boolean skipMaxLength = false;
            if (valuelist != null) {
                // if the display values are different than the real values, then maxlength should be skipped
                IValueList vl = getRealValueList(application, valuelist, true, fieldFormat.dpType, fieldFormat.parsedFormat, dp.getDataProviderID());
                skipMaxLength = vl.hasRealValues();
            }
            if (l > 0 && (defaultType == IColumnTypes.TEXT || defaultType == IColumnTypes.MEDIA) && !skipMaxLength) {
                fl.setMaxLength(l);
            }
        }
    }
    // fl.setOpaque(!field.getTransparent());
    if (field.getDisplaysTags()) {
        fl.setNeedEntireState(true);
        if (field.getDataProviderID() == null && field.getText() != null && fl instanceof IDisplayTagText) {
            ((IDisplayTagText) fl).setTagText(field.getText());
        }
    }
    if (// el is an ActionListener
    el != null) {
        fl.addScriptExecuter(el);
        Object[] cmds = combineMethodsToCommands(form, form.getOnElementFocusGainedMethodID(), "onElementFocusGainedMethodID", field, field.getOnFocusGainedMethodID(), "onFocusGainedMethodID");
        if (cmds != null)
            fl.setEnterCmds((String[]) cmds[0], (Object[][]) cmds[1]);
        cmds = combineMethodsToCommands(form, form.getOnElementFocusLostMethodID(), "onElementFocusLostMethodID", field, field.getOnFocusLostMethodID(), "onFocusLostMethodID");
        if (cmds != null)
            fl.setLeaveCmds((String[]) cmds[0], (Object[][]) cmds[1]);
        if (field.getOnActionMethodID() > 0)
            fl.setActionCmd(Integer.toString(field.getOnActionMethodID()), Utils.parseJSExpressions(field.getFlattenedMethodArguments("onActionMethodID")));
        if (field.getOnDataChangeMethodID() > 0)
            fl.setChangeCmd(Integer.toString(field.getOnDataChangeMethodID()), Utils.parseJSExpressions(field.getFlattenedMethodArguments("onDataChangeMethodID")));
        if (field.getOnRightClickMethodID() > 0)
            fl.setRightClickCommand(Integer.toString(field.getOnRightClickMethodID()), Utils.parseJSExpressions(field.getFlattenedMethodArguments("onRightClickMethodID")));
    }
    int onRenderMethodID = field.getOnRenderMethodID();
    AbstractBase onRenderPersist = field;
    if (onRenderMethodID <= 0) {
        onRenderMethodID = form.getOnRenderMethodID();
        onRenderPersist = form;
    }
    if (onRenderMethodID > 0) {
        RenderEventExecutor renderEventExecutor = scriptable.getRenderEventExecutor();
        renderEventExecutor.setRenderCallback(Integer.toString(onRenderMethodID), Utils.parseJSExpressions(onRenderPersist.getFlattenedMethodArguments("onRenderMethodID")));
        IForm rendererForm = application.getFormManager().getForm(form.getName());
        IScriptExecuter rendererScriptExecuter = rendererForm instanceof FormController ? ((FormController) rendererForm).getScriptExecuter() : null;
        renderEventExecutor.setRenderScriptExecuter(rendererScriptExecuter);
    }
    applyBasicComponentProperties(application, fl, field, styleInfo);
    if (fl instanceof INullableAware) {
        INullableAware nullAware = (INullableAware) fl;
        boolean allowNull = true;
        // become 0 (because it is unchecked) so that the user does not need to check/uncheck it for save
        try {
            if (dataProviderLookup != null && dataProviderLookup.getTable() != null && field.getDataProviderID() != null) {
                String dataproviderId = dp == null ? field.getDataProviderID() : dp.getDataProviderID();
                if (dataProviderLookup.getTable().getColumn(dataproviderId) != null) {
                    allowNull = dataProviderLookup.getTable().getColumn(dataproviderId).getAllowNull();
                }
            }
        } catch (RepositoryException e) {
        // maybe this field is not linked to a table column... so leave it true
        }
        nullAware.setAllowNull(allowNull);
    }
    Insets m = field.getMargin();
    if (m == null)
        m = style_margin;
    if (m != null) {
        fl.setMargin(m);
        if (fl instanceof IMarginAwareBorder) {
            if (field.getBorderType() != null || hasBorder) {
                Border b = fl.getBorder();
                if (b != null) {
                    fl.setBorder(BorderFactory.createCompoundBorder(b, BorderFactory.createEmptyBorder(m.top, m.left, m.bottom, m.right)));
                }
            }
        }
    }
    if (fl.getScriptObject() instanceof HasRuntimePlaceholder) {
        ((HasRuntimePlaceholder) fl.getScriptObject()).setPlaceholderText(field.getPlaceholderText());
    }
    return fl;
}
Also used : RuntimeDataCombobox(com.servoy.j2db.ui.scripting.RuntimeDataCombobox) Insets(java.awt.Insets) IScrollPane(com.servoy.j2db.ui.IScrollPane) ValueList(com.servoy.j2db.persistence.ValueList) LookupValueList(com.servoy.j2db.dataprocessing.LookupValueList) CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) IValueList(com.servoy.j2db.dataprocessing.IValueList) RuntimeHTMLArea(com.servoy.j2db.ui.scripting.RuntimeHTMLArea) IForm(com.servoy.j2db.IForm) IDataProvider(com.servoy.j2db.persistence.IDataProvider) RuntimeMediaField(com.servoy.j2db.ui.scripting.RuntimeMediaField) RuntimeRtfArea(com.servoy.j2db.ui.scripting.RuntimeRtfArea) HasRuntimePlaceholder(com.servoy.j2db.ui.runtime.HasRuntimePlaceholder) RuntimeDataPassword(com.servoy.j2db.ui.scripting.RuntimeDataPassword) RuntimeDataCalendar(com.servoy.j2db.ui.scripting.RuntimeDataCalendar) IValueList(com.servoy.j2db.dataprocessing.IValueList) RuntimeDataField(com.servoy.j2db.ui.scripting.RuntimeDataField) FormController(com.servoy.j2db.FormController) RuntimeRadioButton(com.servoy.j2db.ui.scripting.RuntimeRadioButton) RuntimeDataLookupField(com.servoy.j2db.ui.scripting.RuntimeDataLookupField) IStyleSheet(com.servoy.j2db.util.IStyleSheet) RuntimeCheckbox(com.servoy.j2db.ui.scripting.RuntimeCheckbox) IScriptExecuter(com.servoy.j2db.IScriptExecuter) AbstractBase(com.servoy.j2db.persistence.AbstractBase) RepositoryException(com.servoy.j2db.persistence.RepositoryException) RuntimeRadioChoice(com.servoy.j2db.ui.scripting.RuntimeRadioChoice) RuntimeCheckBoxChoice(com.servoy.j2db.ui.scripting.RuntimeCheckBoxChoice) RuntimeTextArea(com.servoy.j2db.ui.scripting.RuntimeTextArea) AbstractRuntimeValuelistComponent(com.servoy.j2db.ui.scripting.AbstractRuntimeValuelistComponent) IStyleRule(com.servoy.j2db.util.IStyleRule) RuntimeListBox(com.servoy.j2db.ui.scripting.RuntimeListBox) RuntimeSpinner(com.servoy.j2db.ui.scripting.RuntimeSpinner) IFormatScriptComponent(com.servoy.j2db.ui.scripting.IFormatScriptComponent) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) RenderEventExecutor(com.servoy.j2db.ui.RenderEventExecutor) IFieldComponent(com.servoy.j2db.ui.IFieldComponent) IStylePropertyChangesRecorder(com.servoy.j2db.ui.IStylePropertyChangesRecorder) Border(javax.swing.border.Border) TitledBorder(javax.swing.border.TitledBorder) IDisplayTagText(com.servoy.j2db.ui.IDisplayTagText)

Aggregations

IStylePropertyChangesRecorder (com.servoy.j2db.ui.IStylePropertyChangesRecorder)3 Border (javax.swing.border.Border)3 TitledBorder (javax.swing.border.TitledBorder)3 FormController (com.servoy.j2db.FormController)2 IForm (com.servoy.j2db.IForm)2 IScriptExecuter (com.servoy.j2db.IScriptExecuter)2 AbstractBase (com.servoy.j2db.persistence.AbstractBase)2 IDataProvider (com.servoy.j2db.persistence.IDataProvider)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 IDisplayTagText (com.servoy.j2db.ui.IDisplayTagText)2 RenderEventExecutor (com.servoy.j2db.ui.RenderEventExecutor)2 CustomValueList (com.servoy.j2db.dataprocessing.CustomValueList)1 IDisplayData (com.servoy.j2db.dataprocessing.IDisplayData)1 IValueList (com.servoy.j2db.dataprocessing.IValueList)1 LookupValueList (com.servoy.j2db.dataprocessing.LookupValueList)1 Media (com.servoy.j2db.persistence.Media)1 ValueList (com.servoy.j2db.persistence.ValueList)1 IScriptable (com.servoy.j2db.scripting.IScriptable)1 IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)1 IAnchoredComponent (com.servoy.j2db.ui.IAnchoredComponent)1