Search in sources :

Example 1 with LFAwareSortableHeaderRenderer

use of com.servoy.j2db.gui.LFAwareSortableHeaderRenderer 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)

Aggregations

FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)1 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)1 SortColumn (com.servoy.j2db.dataprocessing.SortColumn)1 LFAwareSortableHeaderRenderer (com.servoy.j2db.gui.LFAwareSortableHeaderRenderer)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 Table (com.servoy.j2db.persistence.Table)1 ISupportValueList (com.servoy.j2db.ui.ISupportValueList)1 Component (java.awt.Component)1 ActionEvent (java.awt.event.ActionEvent)1 AbstractAction (javax.swing.AbstractAction)1 Timer (javax.swing.Timer)1 TableColumn (javax.swing.table.TableColumn)1 TableColumnModel (javax.swing.table.TableColumnModel)1