Search in sources :

Example 1 with SortColumn

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

the class FormPreviewPanel method process.

// build the chain and fill the renderers,returns number of pages
public int process() throws Exception {
    // clear
    root = null;
    // set size of this panel
    orgWidth = new Dimension((int) (currentPageFormat.getWidth() * (1 / factor)), (int) (currentPageFormat.getHeight() * (1 / factor)));
    applySize();
    part_panels = createPartPanels();
    Form form = controllerBeingPreviewed.getForm();
    // otherwise you cannot print multiple columns   (int) (application.getPageFormat().getImageableWidth()*(1/factor));
    int w = form.getWidth();
    try {
        // $NON-NLS-1$
        application.getRuntimeProperties().put("isPrinting", Boolean.TRUE);
        Map componentsUsingSliding = application.getDataRenderFactory().completeRenderers(application, form, controllerBeingPreviewed.getScriptExecuter(), part_panels, w, true, null, null);
        PropertyCopy.copyExistingPrintableProperties(application, controllerBeingPreviewed, part_panels);
        Iterator<DataRenderer> panels = part_panels.values().iterator();
        while (panels.hasNext()) {
            DataRenderer panel = panels.next();
            panel.setComponentsUsingSliding(componentsUsingSliding);
            DataRendererFactory.addSpringsBetweenComponents(application, panel);
        }
        // $NON-NLS-1$
        Debug.trace("usesSliding " + (componentsUsingSliding.size() != 0));
    } finally {
        // $NON-NLS-1$
        application.getRuntimeProperties().put("isPrinting", null);
    }
    // create list
    renderParent = application.getPrintingRendererParent();
    plist = new PageList(application, this, renderParent);
    PartNode node = null;
    // create the chain based on the sort,LAST node must be the body part (is virtal added if not present)
    Part body = null;
    FormController fp = ((FormManager) application.getFormManager()).leaseFormPanel(controllerBeingPreviewed.getName());
    if (fp != null && !fp.isShowingData()) {
        // List lst = fp.getFormModel().getLastSearchColumns();
        if (fp.wantEmptyFoundSet()) {
            if (fp.getFormModel() != null)
                fp.getFormModel().clear();
        } else {
            fp.loadAllRecords();
        }
    // fp.getFormModel().sort(lst);
    }
    List<SortColumn> sortColumns = ((FoundSet) formData).getLastSortColumns();
    if (formData.getSize() != 0) {
        if (sortColumns != null) {
            Set<String> consumed = new HashSet<String>();
            for (int i = 0; i < sortColumns.size(); i++) {
                SortColumn sc = sortColumns.get(i);
                Iterator<Part> it = part_panels.keySet().iterator();
                while (it.hasNext()) {
                    Part part = it.next();
                    DataRenderer dr = part_panels.get(part);
                    if (part.getPartType() == Part.BODY) {
                        body = part;
                        continue;
                    }
                    if (part.getPartType() != Part.LEADING_SUBSUMMARY && part.getPartType() != Part.TRAILING_SUBSUMMARY) {
                        IRecordInternal state = new PageNumberState(formData, plist);
                        plist.setNonRepeatingPart(part.getPartType(), new DataRendererDefinition(this, renderParent, part, dr, state));
                        continue;
                    }
                    boolean match = false;
                    int inlineCount = 0;
                    List<SortColumn> partSortColumns = new ArrayList<SortColumn>();
                    SortColumn lastMatch = sc;
                    String groupByDataproviders = part.getGroupbyDataProviderIDs() != null ? part.getGroupbyDataProviderIDs() : "";
                    // $NON-NLS-1$ //$NON-NLS-2$
                    StringTokenizer tk = new StringTokenizer("" + groupByDataproviders.toLowerCase(), ", ");
                    int tokenCount = tk.countTokens();
                    String[] ids = new String[tokenCount];
                    for (; inlineCount < tokenCount; inlineCount++) {
                        String id = tk.nextToken();
                        ids[inlineCount] = id;
                        if (lastMatch.getDataProviderID().equals(id)) {
                            partSortColumns.add(lastMatch);
                            if ((i + inlineCount + 1) < sortColumns.size()) {
                                lastMatch = sortColumns.get(i + inlineCount + 1);
                                if (part.getPartType() == Part.LEADING_SUBSUMMARY && consumed.contains(lastMatch)) {
                                    break;
                                }
                            } else {
                                break;
                            }
                        } else {
                            break;
                        }
                    }
                    if (// did all match?
                    tokenCount > 0 && partSortColumns.size() == tokenCount) {
                        match = true;
                        if (part.getPartType() == Part.LEADING_SUBSUMMARY) {
                            for (String element : ids) {
                                consumed.add(element);
                            }
                        }
                    }
                    if (match) {
                        SortColumn[] array = new SortColumn[partSortColumns.size()];
                        partSortColumns.toArray(array);
                        if (// create root
                        root == null) {
                            root = new PartNode(this, part, dr, renderParent, array);
                            node = root;
                        } else {
                            if (!tryToPlaceInExistingNodes(part, dr, array)) {
                                PartNode newNode = new PartNode(this, part, dr, renderParent, array);
                                node.setChild(newNode);
                                node = newNode;
                            }
                        }
                    }
                }
            }
            PartNode newNode = null;
            if (body == null) {
                // a virtual body (when no body is placed in the parts)
                newNode = new PartNode(this, null, null, renderParent, null);
            } else {
                // the body
                newNode = new PartNode(this, body, part_panels.get(body), renderParent, null);
            }
            if (node != null) {
                node.setChild(newNode);
            } else {
                root = newNode;
            }
        } else // no sort...
        {
            if (// search for body
            body == null) {
                Iterator<Part> it = part_panels.keySet().iterator();
                while (it.hasNext()) {
                    Part part = it.next();
                    DataRenderer dr = part_panels.get(part);
                    IRecordInternal state = new PageNumberState(formData, plist);
                    if (part.getPartType() == Part.BODY) {
                        body = part;
                        continue;
                    }
                    if (part.getPartType() != Part.LEADING_SUBSUMMARY && part.getPartType() != Part.TRAILING_SUBSUMMARY) {
                        plist.setNonRepeatingPart(part.getPartType(), new DataRendererDefinition(this, renderParent, part, dr, state));
                        continue;
                    }
                }
            }
            if (body == null) {
                // a virtual body (when no body is placed in the parts)
                root = new PartNode(this, null, null, renderParent, null);
            } else // if (body != null)
            {
                // the body
                root = new PartNode(this, body, part_panels.get(body), renderParent, null);
            }
        }
    }
    try {
        // $NON-NLS-1$
        application.getRuntimeProperties().put("isPrinting", Boolean.TRUE);
        long t1 = System.currentTimeMillis();
        // fill the renderers with data
        if (root != null) {
            // dump chain
            // $NON-NLS-1$
            Debug.trace("Root " + root);
            QuerySelect sqlString = ((FoundSet) formData).getQuerySelectForReading();
            Table table = formData.getSQLSheet().getTable();
            FoundSet fs = (FoundSet) ((FoundSetManager) application.getFoundSetManager()).getNewFoundSet(table, null, sortColumns);
            fs.browseAll(sqlString);
            long t3 = System.currentTimeMillis();
            List<DataRendererDefinition> childRetval = root.process(this, fs, table, sqlString);
            long t4 = System.currentTimeMillis();
            if (Debug.tracing()) {
                // $NON-NLS-1$ //$NON-NLS-2$
                Debug.trace("Database queries took " + ((t4 - t3) / 1000f) + " second");
            }
            if (childRetval != null) {
                for (int i = 0; i < childRetval.size(); i++) {
                    plist.addPanel(childRetval.get(i));
                }
            }
        }
        plist.finish();
        long t2 = System.currentTimeMillis();
        int pageCount = plist.getNumberOfPages();
        // dump
        if (Debug.tracing()) {
            Debug.trace(plist);
            // $NON-NLS-1$ //$NON-NLS-2$
            Debug.trace("Generated " + pageCount / ((t2 - t1) / 1000f) + " printable pages per second");
        }
    } finally {
        // $NON-NLS-1$
        application.getRuntimeProperties().put("isPrinting", null);
    }
    renderParent.removeAll();
    return plist.getNumberOfPages();
}
Also used : Form(com.servoy.j2db.persistence.Form) DataRenderer(com.servoy.j2db.smart.dataui.DataRenderer) ArrayList(java.util.ArrayList) SortColumn(com.servoy.j2db.dataprocessing.SortColumn) FormManager(com.servoy.j2db.FormManager) HashSet(java.util.HashSet) FormController(com.servoy.j2db.FormController) Table(com.servoy.j2db.persistence.Table) IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) Dimension(java.awt.Dimension) QuerySelect(com.servoy.j2db.query.QuerySelect) StringTokenizer(java.util.StringTokenizer) Part(com.servoy.j2db.persistence.Part) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with SortColumn

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

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

the class SwingForm method showSortDialog.

public void showSortDialog(IApplication app, String options) {
    ISmartClientApplication application = (ISmartClientApplication) app;
    try {
        Table t = (Table) formController.getTable();
        if (t != null) {
            List<SortColumn> sortColumns = null;
            if ((options == null || options.length() == 0) && formController.getFormModel() instanceof FoundSet) {
                sortColumns = ((FoundSet) formController.getFormModel()).getLastSortColumns();
            } else {
                sortColumns = ((FoundSetManager) application.getFoundSetManager()).getSortColumns(t, options);
            }
            Window window = SwingUtilities.getWindowAncestor(this);
            if (window == null)
                window = application.getMainApplicationFrame();
            // $NON-NLS-1$
            SortDialog nfd = (SortDialog) application.getWindow("SortDialog");
            if (nfd == null || nfd.getOwner() != window) {
                if (window instanceof Frame) {
                    nfd = new SortDialog((Frame) window, application);
                } else if (window instanceof Dialog) {
                    nfd = new SortDialog((Dialog) window, application);
                }
                // $NON-NLS-1$
                application.registerWindow("SortDialog", nfd);
            }
            List<SortColumn> list = nfd.showDialog(t, sortColumns);
            if (list != null)
                formController.sort(list, false);
        }
    } catch (Exception ex) {
        // $NON-NLS-1$
        application.reportError(Messages.getString("servoy.formPanel.error.sortRecordsDialog"), ex);
    }
}
Also used : Window(java.awt.Window) ISmartClientApplication(com.servoy.j2db.ISmartClientApplication) JFrame(javax.swing.JFrame) Frame(java.awt.Frame) Table(com.servoy.j2db.persistence.Table) JTable(javax.swing.JTable) Dialog(java.awt.Dialog) FormDialog(com.servoy.j2db.gui.FormDialog) JDialog(javax.swing.JDialog) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) SortColumn(com.servoy.j2db.dataprocessing.SortColumn)

Example 4 with SortColumn

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

the class SortModel method right.

private void right() {
    flagChanged();
    Object o = dpe.getValue();
    if (o != null) {
        int currentSize = model.getRowCount();
        if (o instanceof Column) {
            if (model.addRow(new SortColumn((Column) o)))
                table.setRowSelectionInterval(currentSize, currentSize);
        } else if (o instanceof ColumnWrapper) {
            if (model.addRow(new SortColumn((ColumnWrapper) o)))
                table.setRowSelectionInterval(currentSize, currentSize);
        } else {
            boolean clear = false;
            IDataProvider[] array = (IDataProvider[]) o;
            for (IDataProvider element : array) {
                boolean added = false;
                if (element instanceof ColumnWrapper) {
                    added = model.addRow(new SortColumn((ColumnWrapper) element));
                } else if (element instanceof Column) {
                    added = model.addRow(new SortColumn((Column) element));
                }
                if (added) {
                    if (!clear) {
                        table.clearSelection();
                        clear = true;
                    }
                    table.addRowSelectionInterval(currentSize, currentSize);
                    currentSize++;
                }
            }
        }
    }
}
Also used : IColumn(com.servoy.j2db.persistence.IColumn) SortColumn(com.servoy.j2db.dataprocessing.SortColumn) Column(com.servoy.j2db.persistence.Column) ColumnWrapper(com.servoy.j2db.persistence.ColumnWrapper) SortColumn(com.servoy.j2db.dataprocessing.SortColumn) IDataProvider(com.servoy.j2db.persistence.IDataProvider)

Example 5 with SortColumn

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

the class SortModel method down.

public void down(int index) {
    if (index >= 0 && index < rows.size() - 1) {
        SortColumn obj = rows.get(index);
        rows.remove(index);
        rows.add(index + 1, obj);
    }
    fireTableDataChanged();
}
Also used : SortColumn(com.servoy.j2db.dataprocessing.SortColumn)

Aggregations

SortColumn (com.servoy.j2db.dataprocessing.SortColumn)13 Table (com.servoy.j2db.persistence.Table)6 FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)5 RepositoryException (com.servoy.j2db.persistence.RepositoryException)5 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)4 FormController (com.servoy.j2db.FormController)3 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)3 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)3 Column (com.servoy.j2db.persistence.Column)3 Form (com.servoy.j2db.persistence.Form)3 GraphicalComponent (com.servoy.j2db.persistence.GraphicalComponent)3 ISupportValueList (com.servoy.j2db.ui.ISupportValueList)3 FormManager (com.servoy.j2db.FormManager)2 IApplication (com.servoy.j2db.IApplication)2 IDisplayData (com.servoy.j2db.dataprocessing.IDisplayData)2 ISwingFoundSet (com.servoy.j2db.dataprocessing.ISwingFoundSet)2 IColumn (com.servoy.j2db.persistence.IColumn)2 IDataProvider (com.servoy.j2db.persistence.IDataProvider)2 IPersist (com.servoy.j2db.persistence.IPersist)2 IComponent (com.servoy.j2db.ui.IComponent)2