Search in sources :

Example 11 with ISwingFoundSet

use of com.servoy.j2db.dataprocessing.ISwingFoundSet in project servoy-client by Servoy.

the class CellAdapter method valueChanged.

/*
	 * @see JSModificationListener#valueChanged(ModificationEvent)
	 */
public void valueChanged(ModificationEvent e) {
    if (adjusting)
        return;
    try {
        adjusting = true;
        // ignore globals in a cell adapter, will be handled by the row manager
        if (!table.isEditing() && ScopesUtils.isVariableScope(e.getName())) {
            // test if it is a related
            if (dataProviderID != null && dataProviderID.indexOf('.') != -1) {
                TableModel parent = table.getModel();
                if (parent instanceof ISwingFoundSet) {
                    // it could be based on that global so fire a table event.
                    ((ISwingFoundSet) parent).fireTableModelEvent(0, parent.getRowCount() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE);
                }
            }
            return;
        }
        // refresh value
        IRecord s = e.getRecord();
        if (s == null) {
            TableModel tm = table.getModel();
            if (tm instanceof IFoundSetInternal) {
                IFoundSetInternal fs = (IFoundSetInternal) tm;
                int selRow = fs.getSelectedIndex();
                if (selRow != -1) {
                    s = fs.getRecord(selRow);
                }
            }
        }
        if (s != null) {
            Object obj = e.getValue();
            if (e.getName().equals(dataProviderID)) {
                // make sure the change is seen and pushed to display by jtable
                fireModificationEvent(s);
            } else {
                obj = dal.getValueObject(s, dataProviderID);
                if (obj == Scriptable.NOT_FOUND) {
                    obj = null;
                }
            }
            if (s == currentEditingState && table.getEditorComponent() == editor && editor instanceof IDisplayData) {
                convertAndSetValue(((IDisplayData) editor), obj);
            }
        }
    } finally {
        adjusting = false;
    }
}
Also used : IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) IRecord(com.servoy.j2db.dataprocessing.IRecord) EventObject(java.util.EventObject) IDisplayData(com.servoy.j2db.dataprocessing.IDisplayData) TableModel(javax.swing.table.TableModel) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet) Point(java.awt.Point)

Example 12 with ISwingFoundSet

use of com.servoy.j2db.dataprocessing.ISwingFoundSet in project servoy-client by Servoy.

the class CellAdapter method getBgColor.

private Color getBgColor(JTable jtable, boolean isSelected, int row, boolean isEdited) {
    Color bgColor = null;
    IRecordInternal state = ((IFoundSetInternal) jtable.getModel()).getRecord(row);
    boolean specialStateCase = (state instanceof PrototypeState || state instanceof FindState || state == null || state.getRawData() == null);
    if (/* !(renderer instanceof JButton) && */
    !specialStateCase) {
        ISwingFoundSet foundset = (ISwingFoundSet) jtable.getModel();
        bgColor = (Color) getStyleAttributeForRow(jtable, isSelected, row, ISupportRowStyling.ATTRIBUTE.BGCOLOR);
        String strRowBGColorProvider = null;
        List<Object> rowBGColorArgs = null;
        if (jtable instanceof IView) {
            strRowBGColorProvider = ((IView) jtable).getRowBGColorScript();
            rowBGColorArgs = ((IView) jtable).getRowBGColorArgs();
        }
        // $NON-NLS-1$
        if (strRowBGColorProvider == null)
            strRowBGColorProvider = "servoy_row_bgcolor";
        boolean isRowBGColorCalculation = state.getRawData().containsCalculation(strRowBGColorProvider);
        if (// $NON-NLS-1$
        !isRowBGColorCalculation && strRowBGColorProvider.equals("servoy_row_bgcolor")) {
            // $NON-NLS-1$
            strRowBGColorProvider = "";
        }
        if (// $NON-NLS-1$
        strRowBGColorProvider != null && !"".equals(strRowBGColorProvider)) {
            Object bg_color = null;
            // TODO this should be done better....
            Record.VALIDATE_CALCS.set(Boolean.FALSE);
            try {
                String type = (editor instanceof IScriptableProvider && ((IScriptableProvider) editor).getScriptObject() instanceof IRuntimeComponent) ? ((IRuntimeComponent) ((IScriptableProvider) editor).getScriptObject()).getElementType() : null;
                String nm = (editor instanceof IDisplayData) ? ((IDisplayData) editor).getDataProviderID() : null;
                if (isRowBGColorCalculation) {
                    bg_color = foundset.getCalculationValue(state, strRowBGColorProvider, Utils.arrayMerge((new Object[] { new Integer(row), new Boolean(isSelected), type, nm, new Boolean(isEdited) }), Utils.parseJSExpressions(rowBGColorArgs)), null);
                } else {
                    try {
                        FormController currentForm = dal.getFormController();
                        bg_color = currentForm.executeFunction(strRowBGColorProvider, Utils.arrayMerge((new Object[] { new Integer(row), new Boolean(isSelected), type, nm, currentForm.getName(), state, new Boolean(isEdited) }), Utils.parseJSExpressions(rowBGColorArgs)), false, null, true, null);
                    } catch (Exception ex) {
                        Debug.error(ex);
                    }
                }
            } finally {
                Record.VALIDATE_CALCS.set(null);
            }
            if (bg_color != null && !(bg_color.toString().trim().length() == 0) && !(bg_color instanceof Undefined)) {
                bgColor = PersistHelper.createColor(bg_color.toString());
            }
        }
    }
    return bgColor;
}
Also used : FormController(com.servoy.j2db.FormController) Undefined(org.mozilla.javascript.Undefined) IView(com.servoy.j2db.IView) IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) Color(java.awt.Color) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet) ServoyException(com.servoy.j2db.util.ServoyException) ParseException(java.text.ParseException) ApplicationException(com.servoy.j2db.ApplicationException) FindState(com.servoy.j2db.dataprocessing.FindState) EventObject(java.util.EventObject) IRuntimeComponent(com.servoy.j2db.ui.runtime.IRuntimeComponent) IDisplayData(com.servoy.j2db.dataprocessing.IDisplayData) PrototypeState(com.servoy.j2db.dataprocessing.PrototypeState) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider)

Example 13 with ISwingFoundSet

use of com.servoy.j2db.dataprocessing.ISwingFoundSet 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 14 with ISwingFoundSet

use of com.servoy.j2db.dataprocessing.ISwingFoundSet in project servoy-client by Servoy.

the class SpecialTabPanel method registerSelectionListeners.

private void registerSelectionListeners(IRecordInternal parentState, String relationName) {
    // $NON-NLS-1$
    String[] parts = relationName.split("\\.");
    IRecordInternal currentRecord = parentState;
    for (int i = 0; currentRecord != null && i < parts.length - 1; i++) {
        IFoundSetInternal fs = currentRecord.getRelatedFoundSet(parts[i]);
        if (fs instanceof ISwingFoundSet) {
            related.add((ISwingFoundSet) fs);
            ((ISwingFoundSet) fs).getSelectionModel().addListSelectionListener(this);
        }
        currentRecord = (fs == null) ? null : fs.getRecord(fs.getSelectedIndex());
    }
}
Also used : IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet)

Example 15 with ISwingFoundSet

use of com.servoy.j2db.dataprocessing.ISwingFoundSet in project servoy-client by Servoy.

the class WebTabPanel method registerSelectionListeners.

private void registerSelectionListeners(IRecordInternal parentState, String relationName) {
    // $NON-NLS-1$
    String[] parts = relationName.split("\\.");
    IRecordInternal currentRecord = parentState;
    for (int i = 0; currentRecord != null && i < parts.length - 1; i++) {
        IFoundSetInternal fs = currentRecord.getRelatedFoundSet(parts[i]);
        if (fs instanceof ISwingFoundSet) {
            related.add((ISwingFoundSet) fs);
            ((ISwingFoundSet) fs).getSelectionModel().addListSelectionListener(this);
        }
        currentRecord = (fs == null) ? null : fs.getRecord(fs.getSelectedIndex());
    }
}
Also used : IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) Point(java.awt.Point) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet)

Aggregations

ISwingFoundSet (com.servoy.j2db.dataprocessing.ISwingFoundSet)16 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)11 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)10 Point (java.awt.Point)6 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)3 IDisplayData (com.servoy.j2db.dataprocessing.IDisplayData)3 ServoyException (com.servoy.j2db.util.ServoyException)3 EventObject (java.util.EventObject)3 IApplication (com.servoy.j2db.IApplication)2 FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)2 IFoundSet (com.servoy.j2db.dataprocessing.IFoundSet)2 SortColumn (com.servoy.j2db.dataprocessing.SortColumn)2 IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)2 IComponent (com.servoy.j2db.ui.IComponent)2 IFieldComponent (com.servoy.j2db.ui.IFieldComponent)2 IRuntimeComponent (com.servoy.j2db.ui.runtime.IRuntimeComponent)2 Color (java.awt.Color)2 ArrayList (java.util.ArrayList)2 Component (org.apache.wicket.Component)2 BaseQueryTable (com.servoy.base.query.BaseQueryTable)1