Search in sources :

Example 6 with ITagResolver

use of com.servoy.base.util.ITagResolver in project servoy-client by Servoy.

the class WebTabPanel method getTagResolver.

/**
 * @param parentState
 * @return
 */
private ITagResolver getTagResolver(IRecordInternal parentState) {
    ITagResolver resolver;
    WebForm webForm = findParent(WebForm.class);
    if (webForm != null) {
        resolver = webForm.getController().getTagResolver();
    } else {
        resolver = TagResolver.createResolver(parentState);
    }
    return resolver;
}
Also used : ITagResolver(com.servoy.base.util.ITagResolver) WebForm(com.servoy.j2db.server.headlessclient.WebForm)

Example 7 with ITagResolver

use of com.servoy.base.util.ITagResolver in project servoy-client by Servoy.

the class JSUtils method js_stringReplaceTags.

/**
 * Returns the text with %%tags%% replaced, based on provided record or foundset or form.
 *
 * @sample
 * //Next line places a string in variable x, whereby the tag(%%TAG%%) is filled with the value of the database column 'company_name' of the selected record.
 * var x = utils.stringReplaceTags("The companyName of the selected record is %%company_name%% ", foundset)
 * //var otherExample = utils.stringReplaceTags("The amount of the related order line %%amount%% ", order_to_orderdetails);
 * //var recordExample = utils.stringReplaceTags("The amount of the related order line %%amount%% ", order_to_orderdetails.getRecord(i);
 * //Next line places a string in variable y, whereby the tag(%%TAG%%) is filled with the value of the form variable 'x' of the form named 'main'.
 * //var y = utils.stringReplaceTags("The value of form variable is %%x%% ", forms.main);
 * //The next sample shows the use of a javascript object
 * //var obj = new Object();//create a javascript object
 * //obj['x'] = 'test';//assign an named value
 * //var y = utils.stringReplaceTags("The value of object variable is %%x%% ", obj);//use the named value in a tag
 * @param text the text tags to work with
 * @param scriptable the javascript object or foundset,record,form to be used to fill in the tags
 * @return the text with replaced tags
 */
public String js_stringReplaceTags(String text, Object scriptable) {
    if (text != null) {
        ITagResolver tagResolver = null;
        Properties settings = null;
        if (scriptable instanceof FoundSet) {
            IRecordInternal record = ((FoundSet) scriptable).getRecord(((FoundSet) scriptable).getSelectedIndex());
            if (record != null) {
                settings = record.getParentFoundSet().getFoundSetManager().getApplication().getSettings();
                tagResolver = TagResolver.createResolver(record);
            }
        } else if (scriptable instanceof IRecordInternal) {
            IRecordInternal record = (IRecordInternal) scriptable;
            settings = record.getParentFoundSet().getFoundSetManager().getApplication().getSettings();
            tagResolver = TagResolver.createResolver(record);
        } else if (scriptable instanceof BasicFormController) {
            final BasicFormController fc = (BasicFormController) scriptable;
            IFoundSetInternal fs = fc.getFoundSet();
            final ITagResolver defaultTagResolver = (fs != null) ? TagResolver.createResolver(fs.getRecord(fs.getSelectedIndex())) : null;
            settings = fc.getApplication().getSettings();
            tagResolver = new ITagResolver() {

                public String getStringValue(String name) {
                    Object value = fc.getFormScope().get(name);
                    if (value == null || value == Scriptable.NOT_FOUND) {
                        value = defaultTagResolver != null ? defaultTagResolver.getStringValue(name) : null;
                    }
                    // $NON-NLS-1$
                    return value != null ? value.toString() : "";
                }
            };
        } else if (scriptable instanceof Scriptable) {
            Scriptable scriptObject = (Scriptable) scriptable;
            settings = Settings.getInstance();
            tagResolver = TagResolver.createResolver(scriptObject, application);
        }
        if (tagResolver != null && settings != null) {
            return Text.processTags(TagResolver.formatObject(text, application), tagResolver);
        }
        // $NON-NLS-1$
        return "";
    } else {
        // $NON-NLS-1$
        return "";
    }
}
Also used : IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) ITagResolver(com.servoy.base.util.ITagResolver) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) IJSFoundSet(com.servoy.base.scripting.api.IJSFoundSet) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) Properties(java.util.Properties) Scriptable(org.mozilla.javascript.Scriptable) BasicFormController(com.servoy.j2db.BasicFormController)

Example 8 with ITagResolver

use of com.servoy.base.util.ITagResolver in project servoy-client by Servoy.

the class CellAdapter method getTableCellRendererComponent.

/*
	 * @see TableCellRenderer#getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int)
	 */
public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, final int row, final int column) {
    if (renderer == null || !isVisible(renderer) || !(jtable.getModel() instanceof IFoundSetInternal)) {
        return empty;
    }
    final ISwingFoundSet foundset = (ISwingFoundSet) jtable.getModel();
    if (foundset != tableViewFoundset) {
        // foundset changed
        this.tableViewFoundset = foundset;
        rowAndDataprovider.clear();
    }
    final IRecordInternal state;
    try {
        state = foundset.getRecord(row);
    } catch (RuntimeException re) {
        // $NON-NLS-1$
        Debug.error("Error getting row ", re);
        return empty;
    }
    RenderEventExecutor renderEventExecutor = null;
    IScriptRenderMethods renderable = null;
    if (renderer instanceof IScriptableProvider) {
        IScriptable scriptable = ((IScriptableProvider) renderer).getScriptObject();
        if (scriptable instanceof ISupportOnRenderCallback) {
            renderEventExecutor = ((ISupportOnRenderCallback) scriptable).getRenderEventExecutor();
            renderable = ((ISupportOnRenderCallback) scriptable).getRenderable();
        }
    }
    Color bgColor = getBgColor(jtable, isSelected, row, false);
    if (bgColor != null && renderer instanceof JComponent)
        ((JComponent) renderer).setOpaque(true);
    Color fgColor = getFgColor(jtable, isSelected, row);
    Font font = getFont(jtable, isSelected, row);
    // so that getLocation and getWidth in scripting on tableviews do work.
    if (editor != null && editor.getParent() == null) {
        Rectangle cellRect = jtable.getCellRect(row, column, false);
        editor.setLocation(cellRect.x, cellRect.y);
        editor.setSize(cellRect.width, cellRect.height);
    }
    boolean isRenderWithOnRender = renderEventExecutor != null && renderEventExecutor.hasRenderCallback() && renderable instanceof RenderableWrapper;
    Color renderBgColor = null;
    if (isSelected) {
        if (!isRenderWithOnRender || renderEventExecutor.isDifferentRenderState(state, row, isSelected)) {
            Color tableSelectionColor = jtable.getSelectionForeground();
            if (bgColor != null) {
                tableSelectionColor = adjustColorDifference(bgColor, tableSelectionColor);
            }
            ((RenderableWrapper) renderable).clearProperty(RenderableWrapper.PROPERTY_FGCOLOR);
            renderer.setForeground(fgColor != null ? fgColor : tableSelectionColor);
            ((RenderableWrapper) renderable).clearProperty(RenderableWrapper.PROPERTY_BGCOLOR);
            renderBgColor = (bgColor != null ? bgColor : jtable.getSelectionBackground());
            renderer.setBackground(renderBgColor);
            if (font != null) {
                ((RenderableWrapper) renderable).clearProperty(RenderableWrapper.PROPERTY_FONT);
                renderer.setFont(font);
            }
        } else if (isRenderWithOnRender && foundset.getSize() == 1) {
            // if the foundset contains a single record, we need to force trigger onRender
            // because renderEventExecutor refers already to the changed render state
            renderEventExecutor.setRenderStateChanged();
        }
    } else {
        if (isRenderWithOnRender) {
            if (renderEventExecutor.isDifferentRenderState(state, row, isSelected)) {
                Color newBGColor = bgColor != null ? bgColor : componentBgColor;
                if (newBGColor != null) {
                    ((RenderableWrapper) renderable).clearProperty(RenderableWrapper.PROPERTY_BGCOLOR);
                    renderBgColor = newBGColor;
                    renderer.setBackground(renderBgColor);
                }
                Color newFGColor = fgColor != null ? fgColor : componentFgColor;
                if (newFGColor != null) {
                    ((RenderableWrapper) renderable).clearProperty(RenderableWrapper.PROPERTY_FGCOLOR);
                    renderer.setForeground(newFGColor);
                } else if (newBGColor != null) {
                    renderer.setForeground(adjustColorDifference(newBGColor, jtable.getForeground()));
                }
                Font newFont = font != null ? font : componentFont;
                if (newFont != null) {
                    ((RenderableWrapper) renderable).clearProperty(RenderableWrapper.PROPERTY_FONT);
                    renderer.setFont(newFont);
                }
            }
        } else {
            // now get the editors background. if we don't do that then scripting doesn't show up
            Color background = editor.getBackground();
            if (background != null && !background.equals(lastEditorBgColor)) {
                unselectedBackground = background;
                lastEditorBgColor = background;
            }
            Font editorFont = editor.getFont();
            if (editorFont != null && !editorFont.equals(lastEditorFont)) {
                unselectedFont = editorFont;
            }
            if (editor instanceof IDisplayData && ((IDisplayData) editor).isValueValid() || !(editor instanceof IDisplayData)) {
                Color foreground = editor.getForeground();
                if (foreground != null && !foreground.equals(lastEditorFgColor)) {
                    unselectedForeground = foreground;
                }
                Color currentForeground = (fgColor != null ? fgColor : (unselectedForeground != null) ? unselectedForeground : jtable.getForeground());
                renderer.setForeground(currentForeground);
            }
            Color currentColor = (bgColor != null ? bgColor : (unselectedBackground != null) ? unselectedBackground : jtable.getBackground());
            renderer.setBackground(currentColor);
            Font currentFont = (font != null ? font : (unselectedFont != null) ? unselectedFont : jtable.getFont());
            renderer.setFont(currentFont);
        }
    }
    if (renderer instanceof JComponent) {
        applyRowBorder((JComponent) renderer, jtable, isSelected, row, hasFocus);
    }
    // $NON-NLS-1$
    boolean printing = Utils.getAsBoolean(application.getRuntimeProperties().get("isPrinting"));
    if (renderEventExecutor != null && renderEventExecutor.hasRenderCallback()) {
        renderEventExecutor.setRenderState(state, row, isSelected, true);
    }
    if (renderer instanceof IDisplayRelatedData) {
        IDisplayRelatedData drd = (IDisplayRelatedData) renderer;
        String relationName = drd.getSelectedRelationName();
        if (state != null) {
            if (relationName != null) {
                if (!printing && !state.isRelatedFoundSetLoaded(relationName, null)) {
                    IApplication app = dal.getApplication();
                    ((IDisplayData) renderer).setValueObject(null);
                    // $NON-NLS-1$ //$NON-NLS-2$
                    String key = row + "_" + relationName + "_" + null;
                    if (!rowAndDataprovider.contains(key)) {
                        rowAndDataprovider.add(key);
                        Runnable r = new ASynchonizedCellLoad(app, jtable, foundset, row, jtable.convertColumnIndexToModel(column), relationName, drd.getDefaultSort(), null);
                        app.getScheduledExecutor().execute(r);
                    }
                    return renderer.isVisible() ? renderer : empty;
                }
            }
            drd.setRecord(state, true);
        }
    }
    if (renderer instanceof IDisplayData) {
        if (state != null) {
            Object data = null;
            if (dataProviderID != null) {
                int index = -1;
                if (!printing && (index = dataProviderID.indexOf('.')) > 0) {
                    if (!ScopesUtils.isVariableScope(dataProviderID)) {
                        String partName = dataProviderID.substring(0, index);
                        final String restName = dataProviderID.substring(index + 1);
                        String relationName = partName;
                        if (relationName != null && !state.isRelatedFoundSetLoaded(relationName, restName)) {
                            IApplication app = dal.getApplication();
                            ((IDisplayData) renderer).setValueObject(null);
                            // $NON-NLS-1$ //$NON-NLS-2$
                            String key = row + "_" + relationName + "_" + restName;
                            if (!rowAndDataprovider.contains(key)) {
                                rowAndDataprovider.add(key);
                                List<SortColumn> defaultPKSortColumns = null;
                                try {
                                    defaultPKSortColumns = app.getFoundSetManager().getDefaultPKSortColumns(app.getFlattenedSolution().getRelation(relationName).getForeignDataSource());
                                } catch (ServoyException e) {
                                    Debug.error(e);
                                }
                                Runnable r = new ASynchonizedCellLoad(app, jtable, foundset, row, jtable.convertColumnIndexToModel(column), relationName, defaultPKSortColumns, restName);
                                app.getScheduledExecutor().execute(r);
                            }
                            return renderer.isVisible() ? renderer : empty;
                        }
                        IFoundSetInternal rfs = state.getRelatedFoundSet(relationName);
                        if (rfs != null) {
                            int selected = rfs.getSelectedIndex();
                            // should still go through record 0
                            if (selected == -1 && rfs.getSize() > 0) {
                                selected = 0;
                            }
                            final IRecordInternal relState = rfs.getRecord(selected);
                            if (testCalc(restName, relState, row, jtable.convertColumnIndexToModel(column), foundset))
                                return renderer;
                        }
                    }
                }
                if (!((IDisplayData) renderer).needEntireState() && !printing && testCalc(dataProviderID, state, row, jtable.convertColumnIndexToModel(column), foundset)) {
                    return renderer;
                }
                try {
                    data = dal.getValueObject(state, dataProviderID);
                } catch (IllegalArgumentException iae) {
                    Debug.error(iae);
                    // $NON-NLS-1$
                    data = "<conversion error>";
                }
            }
            ((IDisplayData) renderer).setTagResolver(new ITagResolver() {

                public String getStringValue(String nm) {
                    return TagResolver.formatObject(dal.getValueObject(state, nm), dal.getApplication());
                }
            });
            if (data instanceof DbIdentValue) {
                data = ((DbIdentValue) data).getPkValue();
            }
            convertAndSetValue(((IDisplayData) renderer), data);
        }
    }
    if (renderer instanceof IServoyAwareBean && state != null) {
        ((IServoyAwareBean) renderer).setSelectedRecord(new ServoyBeanState(state, dal.getFormScope()));
    }
    if (!(renderer instanceof IDisplayData) && !(renderer instanceof IDisplayRelatedData) && renderEventExecutor != null && renderEventExecutor.hasRenderCallback()) {
        renderEventExecutor.fireOnRender(false);
    }
    // if the bgcolor is not changed during onRender
    if (isRenderWithOnRender && renderBgColor != null && ((RenderableWrapper) renderable).getProperty(RenderableWrapper.PROPERTY_BGCOLOR) == null) {
        renderer.setBackground(renderBgColor);
    }
    return renderer.isVisible() ? renderer : empty;
}
Also used : IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) Rectangle(java.awt.Rectangle) ISupportOnRenderCallback(com.servoy.j2db.ui.ISupportOnRenderCallback) SortColumn(com.servoy.j2db.dataprocessing.SortColumn) Font(java.awt.Font) ServoyException(com.servoy.j2db.util.ServoyException) IScriptRenderMethods(com.servoy.j2db.ui.IScriptRenderMethods) IDisplayRelatedData(com.servoy.j2db.dataprocessing.IDisplayRelatedData) IServoyAwareBean(com.servoy.j2db.dataui.IServoyAwareBean) DbIdentValue(com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue) RenderableWrapper(com.servoy.j2db.ui.RenderableWrapper) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider) IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) ITagResolver(com.servoy.base.util.ITagResolver) Color(java.awt.Color) JComponent(javax.swing.JComponent) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet) IScriptable(com.servoy.j2db.scripting.IScriptable) Point(java.awt.Point) IApplication(com.servoy.j2db.IApplication) ServoyBeanState(com.servoy.j2db.component.ServoyBeanState) RenderEventExecutor(com.servoy.j2db.ui.RenderEventExecutor) IDisplayData(com.servoy.j2db.dataprocessing.IDisplayData) EventObject(java.util.EventObject)

Example 9 with ITagResolver

use of com.servoy.base.util.ITagResolver in project servoy-client by Servoy.

the class SpecialTabPanel method showFoundSet.

protected void showFoundSet(FormLookupPanel flp, IRecordInternal parentState, List<SortColumn> sort) {
    deregisterSelectionListeners();
    if (!flp.isReady())
        return;
    try {
        FormController fp = flp.getFormPanel();
        if (fp != null && flp.getRelationName() != null) {
            IFoundSetInternal relatedFoundSet = parentState == null ? null : parentState.getRelatedFoundSet(flp.getRelationName(), sort);
            registerSelectionListeners(parentState, flp.getRelationName());
            fp.loadData(relatedFoundSet, null);
        }
        ITagResolver resolver = getTagResolver(parentState);
        // refresh tab text
        int i = enclosingComponent.getTabIndex(flp);
        String element = i < originalTabText.size() ? originalTabText.get(i) : null;
        if (element != null) {
            enclosingComponent.setTitleAt(i, Text.processTags(element, resolver));
        }
        String tooltip = i < originalTabTooltip.size() ? originalTabTooltip.get(i) : null;
        if (tooltip != null) {
            enclosingComponent.setToolTipTextAt(i, Text.processTags(tooltip, resolver));
        }
    } catch (RuntimeException re) {
        application.handleException("Error setting the foundset of the relation " + flp.getRelationName() + " on the tab with form " + flp.getFormName(), re);
        throw re;
    }
}
Also used : FormController(com.servoy.j2db.FormController) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) ITagResolver(com.servoy.base.util.ITagResolver)

Example 10 with ITagResolver

use of com.servoy.base.util.ITagResolver in project servoy-client by Servoy.

the class SpecialTabPanel method getTagResolver.

/**
 * @param parentState
 * @return
 */
private ITagResolver getTagResolver(IRecordInternal parentState) {
    ITagResolver resolver;
    Container parent = getParent();
    while (!(parent instanceof SwingForm) && parent != null) {
        parent = parent.getParent();
    }
    if (parent instanceof SwingForm) {
        resolver = ((SwingForm) parent).getController().getTagResolver();
    } else {
        resolver = TagResolver.createResolver(parentState);
    }
    return resolver;
}
Also used : Container(java.awt.Container) ITagResolver(com.servoy.base.util.ITagResolver) SwingForm(com.servoy.j2db.smart.SwingForm)

Aggregations

ITagResolver (com.servoy.base.util.ITagResolver)13 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)5 Point (java.awt.Point)5 FormController (com.servoy.j2db.FormController)3 IDisplayData (com.servoy.j2db.dataprocessing.IDisplayData)2 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)2 WebForm (com.servoy.j2db.server.headlessclient.WebForm)2 IJSFoundSet (com.servoy.base.scripting.api.IJSFoundSet)1 BasicFormController (com.servoy.j2db.BasicFormController)1 IApplication (com.servoy.j2db.IApplication)1 ServoyBeanState (com.servoy.j2db.component.ServoyBeanState)1 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)1 IDisplay (com.servoy.j2db.dataprocessing.IDisplay)1 IDisplayRelatedData (com.servoy.j2db.dataprocessing.IDisplayRelatedData)1 ISwingFoundSet (com.servoy.j2db.dataprocessing.ISwingFoundSet)1 SortColumn (com.servoy.j2db.dataprocessing.SortColumn)1 DbIdentValue (com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue)1 IServoyAwareBean (com.servoy.j2db.dataui.IServoyAwareBean)1 AggregateVariable (com.servoy.j2db.persistence.AggregateVariable)1 IDataProvider (com.servoy.j2db.persistence.IDataProvider)1