Search in sources :

Example 1 with FoundSetManager

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

the class SessionClient method createFoundSetManager.

@Override
protected void createFoundSetManager() {
    foundSetManager = new FoundSetManager(this, getFoundSetManagerConfig(), new SwingFoundSetFactory());
    foundSetManager.init();
}
Also used : FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) SwingFoundSetFactory(com.servoy.j2db.dataprocessing.SwingFoundSetFactory)

Example 2 with FoundSetManager

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

the class BasicFormController method setModel.

protected boolean setModel(IFoundSetInternal newModel) throws ServoyException {
    if (newModel == formModel || adjustingModel) {
        // same or adjusting do nothing
        return true;
    }
    ITable formTable = application.getFoundSetManager().getTable(form.getDataSource());
    if (newModel != null && ((formTable == null && newModel.getTable() != null) || (formTable != null && !formTable.equals(newModel.getTable())))) {
        throw new IllegalArgumentException(application.getI18NMessage("servoy.formPanel.error.wrongFoundsetTable", new Object[] { // $NON-NLS-1$
        newModel.getTable() == null ? "NONE" : newModel.getTable().getName(), // $NON-NLS-1$
        form.getTableName() }));
    }
    try {
        IView view = getViewComponent();
        if (view != null && view.isEditing()) {
            // TODO if save fails don't set the newModel??
            int stopped = application.getFoundSetManager().getEditRecordList().stopEditing(false);
            if (stopped != ISaveConstants.STOPPED && stopped != ISaveConstants.AUTO_SAVE_BLOCKED) {
                return false;
            }
        }
        adjustingModel = true;
        if (formModel != null) {
            try {
                ((ISwingFoundSet) formModel).getSelectionModel().removeListSelectionListener(this);
                ((ISwingFoundSet) formModel).getSelectionModel().removeFormController(this);
                ((ISwingFoundSet) formModel).removeTableModelListener(this);
                // to make sure all data is gc'ed
                if (formModel instanceof FoundSet)
                    ((FoundSet) formModel).flushAllCachedItems();
            } catch (Exception ex) {
                Debug.error(ex);
            }
        }
        setFormModelInternal(newModel == null ? ((FoundSetManager) application.getFoundSetManager()).getEmptyFoundSet(this) : newModel);
        if (formScope != null) {
            // $NON-NLS-1$
            formScope.putWithoutFireChange("foundset", formModel);
            if (formScope.getPrototype() == null) {
                formScope.setPrototype(new SelectedRecordScope(this, formTable == null ? null : application.getScriptEngine().getTableScope(formTable)));
            }
        }
        if (isFormVisible) {
            ((ISwingFoundSet) formModel).getSelectionModel().addListSelectionListener(this);
            ((ISwingFoundSet) formModel).getSelectionModel().addFormController(this);
            ((ISwingFoundSet) formModel).addTableModelListener(this);
            if (// it may not yet exist
            view != null) {
                view.setModel(formModel);
            }
            // this was former a call to aggregateChange, but now does now unwanted parent traverse...
            int[] idx = null;
            if (getView() == RECORD_VIEW || getView() == LOCKED_RECORD_VIEW) {
                int selIdx = formModel.getSelectedIndex();
                if (selIdx != -1)
                    idx = new int[] { selIdx };
            } else {
                idx = formModel.getSelectedIndexes();
            }
            if (idx == null || idx.length == 0) {
                refreshAllPartRenderers(new IRecordInternal[] { formModel.getPrototypeState() });
            } else {
                IRecordInternal[] row = new IRecordInternal[idx.length];
                for (int i = 0; i < idx.length; i++) row[i] = formModel.getRecord(idx[i]);
                refreshAllPartRenderers(row);
            }
        }
    } finally {
        adjustingModel = false;
    }
    return true;
}
Also used : FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) IJSFoundSet(com.servoy.base.scripting.api.IJSFoundSet) IFoundSet(com.servoy.j2db.dataprocessing.IFoundSet) RelatedFoundSet(com.servoy.j2db.dataprocessing.RelatedFoundSet) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) ViewFoundSet(com.servoy.j2db.dataprocessing.ViewFoundSet) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) SelectedRecordScope(com.servoy.j2db.scripting.SelectedRecordScope) ITable(com.servoy.j2db.persistence.ITable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject)

Example 3 with FoundSetManager

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

the class ColumnSortListener method mouseClicked.

/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
	 */
@Override
public void mouseClicked(final MouseEvent e) {
    if (!table.isEnabled())
        return;
    if (e.getButton() == MouseEvent.BUTTON1) {
        if (fc != null)
            fc.setLastKeyModifiers(e.getModifiers());
        TableColumnModel colModel = table.getColumnModel();
        if (colModel == null) {
            return;
        }
        int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
        if (columnModelIndex < 0) {
            return;
        }
        final TableColumn column = colModel.getColumn(columnModelIndex);
        if (column == null) {
            return;
        }
        int modelIndex = column.getModelIndex();
        if (modelIndex < 0) {
            return;
        }
        if (table.getModel() instanceof IFoundSetInternal && application.getFoundSetManager().getEditRecordList().stopIfEditing((IFoundSetInternal) table.getModel()) != ISaveConstants.STOPPED) {
            return;
        }
        Integer columnIndex = Integer.valueOf(columnModelIndex);
        if (lastColumnIndex.containsKey(columnIndex)) {
            lastSortAsc = !lastColumnIndex.get(columnIndex).booleanValue();
        } else {
            lastSortAsc = true;
            if (fc != null && column instanceof CellAdapter && ((CellAdapter) column).getDataProviderID() != null && e.getClickCount() <= 1 && table.getModel() instanceof IFoundSetInternal) {
                IFoundSetInternal foundset = (IFoundSetInternal) table.getModel();
                List<SortColumn> sortCols = foundset.getSortColumns();
                if (sortCols != null && sortCols.size() > 0) {
                    CellAdapter ca = (CellAdapter) column;
                    List<String> sortingProviders = null;
                    Component renderer = ca.getRenderer();
                    if (renderer instanceof ISupportValueList && ((ISupportValueList) renderer).getValueList() != null) {
                        try {
                            sortingProviders = DBValueList.getShowDataproviders(((ISupportValueList) renderer).getValueList().getValueList(), (Table) foundset.getTable(), ca.getDataProviderID(), application.getFoundSetManager());
                        } catch (RepositoryException ex) {
                            Debug.error(ex);
                        }
                    }
                    if (sortingProviders == null) {
                        // no related sort, use sort on dataProviderID instead
                        sortingProviders = Collections.singletonList(ca.getDataProviderID());
                    }
                    for (String sortingProvider : sortingProviders) {
                        SortColumn existingSc;
                        try {
                            FoundSetManager fsm = (FoundSetManager) foundset.getFoundSetManager();
                            existingSc = fsm.getSortColumn(foundset.getTable(), sortingProvider, false);
                        } catch (Exception ex) {
                            Debug.error(ex);
                            continue;
                        }
                        for (SortColumn sc : sortCols) {
                            if (sc.equalsIgnoreSortorder(existingSc)) {
                                lastSortAsc = sc.getSortOrder() == SortColumn.DESCENDING;
                            }
                        }
                    }
                }
            }
        }
        if (!e.isShiftDown()) {
            // clear previous data
            lastColumnIndex.clear();
        }
        lastColumnIndex.put(columnIndex, Boolean.valueOf(lastSortAsc));
        if (column instanceof CellAdapter && table.getModel() instanceof IFoundSetInternal) {
            try {
                if (sortTimer != null) {
                    sortTimer.stop();
                }
                sortTimer = new Timer(300, new AbstractAction() {

                    public void actionPerformed(ActionEvent event) {
                        try {
                            String dataProviderID = ((CellAdapter) column).getDataProviderID();
                            int labelForOnActionMethodId = 0;
                            if (((CellAdapter) column).getHeaderRenderer() instanceof LFAwareSortableHeaderRenderer) {
                                labelForOnActionMethodId = ((LFAwareSortableHeaderRenderer) ((CellAdapter) column).getHeaderRenderer()).getOnActionMethodID();
                            }
                            if (fc != null && labelForOnActionMethodId > 0) {
                                LFAwareSortableHeaderRenderer renderer = (LFAwareSortableHeaderRenderer) (((CellAdapter) column).getHeaderRenderer());
                                fc.executeFunction(String.valueOf(labelForOnActionMethodId), Utils.arrayMerge((new Object[] { getJavaScriptEvent(e, JSEvent.EventType.action, renderer.getName()) }), Utils.parseJSExpressions(renderer.getFlattenedMethodArguments("onActionMethodID"))), true, null, false, // $NON-NLS-1$//$NON-NLS-2$
                                "onActionMethodID");
                            } else if (fc != null && fc.getForm().getOnSortCmdMethodID() > 0) {
                                // Also execute the on sort command on none data providers (like a label) then they can do there own sort.
                                fc.executeFunction(String.valueOf(fc.getForm().getOnSortCmdMethodID()), Utils.arrayMerge((new Object[] { dataProviderID, Boolean.valueOf(lastSortAsc), getJavaScriptEvent(e, JSEvent.EventType.none, null) }), Utils.parseJSExpressions(fc.getForm().getFlattenedMethodArguments("onSortCmdMethodID"))), true, null, false, // $NON-NLS-1$//$NON-NLS-2$
                                "onSortCmdMethodID");
                            } else if (dataProviderID != null && fc.getForm().getOnSortCmdMethodID() != -1) {
                                List<String> sortingProviders = null;
                                IFoundSetInternal model = (IFoundSetInternal) table.getModel();
                                Component renderer = ((CellAdapter) column).getRenderer();
                                if (renderer instanceof ISupportValueList && ((ISupportValueList) renderer).getValueList() != null) {
                                    try {
                                        sortingProviders = DBValueList.getShowDataproviders(((ISupportValueList) renderer).getValueList().getValueList(), (Table) model.getTable(), dataProviderID, application.getFoundSetManager());
                                    } catch (RepositoryException ex) {
                                        Debug.error(ex);
                                    }
                                }
                                if (sortingProviders == null) {
                                    // no related sort, use sort on dataProviderID instead
                                    sortingProviders = Collections.singletonList(dataProviderID);
                                }
                                try {
                                    List<SortColumn> list = e.isShiftDown() ? model.getSortColumns() : new ArrayList<SortColumn>();
                                    for (String sortingProvider : sortingProviders) {
                                        FoundSetManager fsm = ((FoundSetManager) model.getFoundSetManager());
                                        SortColumn sc = fsm.getSortColumn(model.getTable(), sortingProvider, false);
                                        if (sc != null && sc.getColumn().getDataProviderType() != IColumnTypes.MEDIA) {
                                            for (SortColumn oldColumn : list) {
                                                if (oldColumn.equalsIgnoreSortorder(sc)) {
                                                    sc = oldColumn;
                                                    break;
                                                }
                                            }
                                            if (!list.contains(sc))
                                                list.add(sc);
                                            sc.setSortOrder(lastSortAsc ? SortColumn.ASCENDING : SortColumn.DESCENDING);
                                        }
                                        model.sort(list, false);
                                    }
                                } catch (Exception ex) {
                                    Debug.error(ex);
                                }
                            }
                        } finally {
                            sortTimer.stop();
                        }
                    }
                });
                sortTimer.start();
            } catch (Exception ex) {
                Debug.error(ex);
            }
        }
    }
}
Also used : LFAwareSortableHeaderRenderer(com.servoy.j2db.gui.LFAwareSortableHeaderRenderer) FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) Table(com.servoy.j2db.persistence.Table) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) ActionEvent(java.awt.event.ActionEvent) ISupportValueList(com.servoy.j2db.ui.ISupportValueList) TableColumnModel(javax.swing.table.TableColumnModel) RepositoryException(com.servoy.j2db.persistence.RepositoryException) SortColumn(com.servoy.j2db.dataprocessing.SortColumn) TableColumn(javax.swing.table.TableColumn) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Timer(javax.swing.Timer) Component(java.awt.Component) AbstractAction(javax.swing.AbstractAction)

Example 4 with FoundSetManager

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

the class JSCalculation method setVariableType.

@JSSetter
public void setVariableType(int type) {
    if (isStored())
        throw new RuntimeException("Can't alter variable type of the stored calculation " + scriptCalculation.getName());
    checkModification();
    scriptCalculation.setTypeAndCheck(type, application);
    TableScope tableScope;
    try {
        tableScope = (TableScope) application.getScriptEngine().getTableScope(scriptCalculation.getTable());
        if (tableScope != null) {
            tableScope.put(scriptCalculation, scriptCalculation);
            ((FoundSetManager) application.getFoundSetManager()).flushSQLSheet(scriptCalculation.getTable().getDataSource());
        }
    } catch (RepositoryException e) {
        Debug.error(e);
    }
}
Also used : FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) TableScope(com.servoy.j2db.scripting.TableScope) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JSSetter(org.mozilla.javascript.annotations.JSSetter)

Example 5 with FoundSetManager

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

the class JSDataSourceNode method newMethod.

/**
 * Creates a new foundset method with the specified code.
 *
 * @sample
 * var method = solutionModel.getDataSourceNode("db:/example_data/orders").newMethod("function doubleSize() { return 2*getSize(); }");
 *
 * application.output('Doubled orders for this customer: '+customers_to_orders.doubleSize())
 *
 * @param code the specified code for the foundset method
 *
 * @return a JSMethod object
 */
@JSFunction
public JSMethod newMethod(String code) {
    try {
        FlattenedSolution fs = application.getFlattenedSolution();
        TableNode tablenode = fs.getSolutionCopyTableNode(dataSource);
        if (tablenode == null)
            throw new RuntimeException("Couldnt create method for datasource: " + dataSource);
        String name = JSMethod.parseName(code);
        ScriptMethod method = tablenode.createNewFoundsetMethod(new ScriptNameValidator(fs), name, null);
        method.setDeclaration(code);
        ((FoundSetManager) application.getFoundSetManager()).reloadFoundsetMethod(dataSource, method);
        return new JSMethod(this, method, application, true);
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) TableNode(com.servoy.j2db.persistence.TableNode) FlattenedSolution(com.servoy.j2db.FlattenedSolution) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) ScriptNameValidator(com.servoy.j2db.persistence.ScriptNameValidator) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Aggregations

FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)17 RepositoryException (com.servoy.j2db.persistence.RepositoryException)9 SortColumn (com.servoy.j2db.dataprocessing.SortColumn)5 Table (com.servoy.j2db.persistence.Table)4 ServoyException (com.servoy.j2db.util.ServoyException)4 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)3 GraphicalComponent (com.servoy.j2db.persistence.GraphicalComponent)3 IPersist (com.servoy.j2db.persistence.IPersist)3 ISupportValueList (com.servoy.j2db.ui.ISupportValueList)3 FlattenedSolution (com.servoy.j2db.FlattenedSolution)2 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)2 IDataServer (com.servoy.j2db.dataprocessing.IDataServer)2 IDataSet (com.servoy.j2db.dataprocessing.IDataSet)2 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)2 ISwingFoundSet (com.servoy.j2db.dataprocessing.ISwingFoundSet)2 SwingFoundSetFactory (com.servoy.j2db.dataprocessing.SwingFoundSetFactory)2 Form (com.servoy.j2db.persistence.Form)2 Relation (com.servoy.j2db.persistence.Relation)2 ScriptNameValidator (com.servoy.j2db.persistence.ScriptNameValidator)2 Solution (com.servoy.j2db.persistence.Solution)2