use of com.servoy.j2db.dataprocessing.SortColumn 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;
}
use of com.servoy.j2db.dataprocessing.SortColumn in project servoy-client by Servoy.
the class ScrollResponseHeaderContainer method tableChanged.
/**
* @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent)
*/
public void tableChanged(TableModelEvent e) {
// as changed, as it will be faster on the client the component replace
if ((e.getType() == TableModelEvent.UPDATE) && (e.getFirstRow() == e.getLastRow()) && (nrUpdatedListItems < 20)) {
Component component = table.get(Integer.toString(e.getFirstRow()));
if (component instanceof ListItem) {
((ListItem) component).visitChildren(IProviderStylePropertyChanges.class, new IVisitor<Component>() {
public Object component(Component comp) {
if ((comp instanceof IDisplayData) || !(comp instanceof ILabel)) {
if (comp instanceof ISupportsNeedEntireState && ((ISupportsNeedEntireState) comp).needEntireState()) {
// it if has tags - mark as changed; it could be enhanced further by also know what was last-rendered on tags, not just simple dataproviders
((IProviderStylePropertyChanges) comp).getStylePropertyChanges().setChanged();
} else {
// try to mark cells as changed only if there was a real value change; otherwise there is no use to replace the whole row...
checkForValueChanges(comp);
}
}
// else labels/buttons that don't display data are not changed
return CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
}
});
nrUpdatedListItems++;
IModel<IRecordInternal> newModel = table.getListItemModel(table.getModel(), e.getFirstRow());
IModel oldModel = ((ListItem) component).getModel();
if (newModel != null && oldModel != null && newModel.getObject() != null && !newModel.getObject().equals(oldModel.getObject())) {
// refresh model if it changed
((ListItem) component).setModel(newModel);
}
}
} else {
if (!isRendering && (!isScrollMode() || !(scrollBehavior != null && scrollBehavior.isGettingRows()))) {
if (isScrollMode())
resetScrollParams();
getStylePropertyChanges().setValueChanged();
}
}
// This is just an heuristic for filtering out the sort event from all table changed events that are raised.
if (currentData != null && e.getColumn() == TableModelEvent.ALL_COLUMNS && e.getFirstRow() == 0 && elementToColumnHeader.size() > 0) {
List<SortColumn> sortCols = currentData.getSortColumns();
if (sortCols != null && sortCols.size() > 0) {
Map<String, Boolean> sortMap = new HashMap<String, Boolean>();
for (IPersist persist : elementToColumnHeader.keySet()) {
SortableCellViewHeader sortableCellViewHeader = (SortableCellViewHeader) elementToColumnHeader.get(persist);
sortableCellViewHeader.setResizeImage(R_ARROW_OFF);
}
for (SortColumn sc : sortCols) {
for (IPersist persist : elementToColumnHeader.keySet()) {
Component comp = elementToColumnIdentifierComponent.get(persist);
SortableCellViewHeader sortableCellViewHeader = (SortableCellViewHeader) elementToColumnHeader.get(persist);
if (comp instanceof IDisplayData && ((IDisplayData) comp).getDataProviderID() != null) {
IDisplayData dispComp = (IDisplayData) comp;
List<String> sortingProviders = null;
if (dispComp instanceof ISupportValueList && ((ISupportValueList) dispComp).getValueList() != null) {
try {
sortingProviders = DBValueList.getShowDataproviders(((ISupportValueList) dispComp).getValueList().getValueList(), (Table) currentData.getTable(), dispComp.getDataProviderID(), currentData.getFoundSetManager());
} catch (RepositoryException ex) {
Debug.error(ex);
}
}
if (sortingProviders == null) {
// no related sort, use sort on dataProviderID instead
sortingProviders = Collections.singletonList(dispComp.getDataProviderID());
}
for (String sortingProvider : sortingProviders) {
SortColumn existingSc;
try {
FoundSetManager fsm = (FoundSetManager) currentData.getFoundSetManager();
existingSc = fsm.getSortColumn(currentData.getTable(), sortingProvider, false);
} catch (RepositoryException ex) {
Debug.error(ex);
continue;
}
if (sc.equalsIgnoreSortorder(existingSc)) {
boolean descending = sc.getSortOrder() == SortColumn.DESCENDING;
sortableCellViewHeader.setResizeImage(descending ? R_ARROW_UP : R_ARROW_DOWN);
sortMap.put(sortableCellViewHeader.getId(), Boolean.valueOf(!descending));
}
}
}
}
}
headers.recordSort(sortMap);
}
}
MainPage mp = table.findParent(MainPage.class);
if (mp != null)
mp.triggerBrowserRequestIfNeeded();
}
use of com.servoy.j2db.dataprocessing.SortColumn in project servoy-client by Servoy.
the class SortableCellViewHeaderGroup method sort.
protected final void sort(final String name, final WebCellBasedView view, int modifiers) {
direction = Utils.getAsBoolean(sorted.get(name));
direction = !direction;
FormController fc = null;
try {
Iterator<IPersist> it = cellview.getAllObjects();
while (it.hasNext()) {
IPersist element = it.next();
if (element instanceof ISupportName && element instanceof ISupportDataProviderID) {
if (name.equals(ComponentFactory.getWebID(form, element))) {
IFoundSetInternal fs = ((FoundSetListWrapper) listView.getList()).getFoundSet();
if (fs != null) {
WebForm wf = listView.findParent(WebForm.class);
if (wf != null)
fc = wf.getController();
GraphicalComponent gc = (GraphicalComponent) view.labelsFor.get(((ISupportName) element).getName());
int labelForOnActionMethodId = 0;
if (gc != null) {
labelForOnActionMethodId = gc.getOnActionMethodID();
}
if (fc != null && labelForOnActionMethodId > 0) {
// execute on action
JSEvent event = new JSEvent();
event.setType(JSEvent.EventType.action);
event.setName(RepositoryHelper.getDisplayName(StaticContentSpecLoader.PROPERTY_ONSORTCMDMETHODID.getPropertyName(), Form.class));
event.setFormName(view.getDataAdapterList().getFormController().getName());
event.setModifiers(modifiers);
event.setElementName(gc.getName());
fc.executeFunction(String.valueOf(labelForOnActionMethodId), // $NON-NLS-1$
Utils.arrayMerge((new Object[] { event }), Utils.parseJSExpressions(gc.getFlattenedMethodArguments("onActionMethodID"))), true, null, false, // $NON-NLS-1$
"onActionMethodID");
}
String id = ((ISupportDataProviderID) element).getDataProviderID();
if (id != null) {
if (cellview instanceof Portal && !ScopesUtils.isVariableScope(id)) {
int idx = id.lastIndexOf('.');
if (idx > 0) {
id = id.substring(idx + 1);
}
}
IDataProvider dataProvider = null;
if (fc != null) {
dataProvider = fs.getFoundSetManager().getApplication().getFlattenedSolution().getDataproviderLookup(fs.getFoundSetManager(), fc.getForm()).getDataProvider(id);
}
if (!(fc != null && labelForOnActionMethodId > 0)) {
// in case there is no onAction definned
if (cellview instanceof Portal || fc == null || fc.getForm().getOnSortCmdMethodID() == 0) {
List<String> sortingProviders = null;
try {
sortingProviders = DBValueList.getShowDataproviders(fs.getFoundSetManager().getApplication().getFlattenedSolution().getValueList(((ISupportDataProviderID) element).getValuelistID()), (Table) fs.getTable(), dataProvider == null ? id : dataProvider.getDataProviderID(), fs.getFoundSetManager());
} catch (RepositoryException ex) {
Debug.error(ex);
}
if (sortingProviders == null) {
// no related sort, use sort on dataProviderID instead
sortingProviders = Collections.singletonList(dataProvider == null ? id : dataProvider.getDataProviderID());
}
List<SortColumn> list = (modifiers & Event.SHIFT_MASK) != 0 ? fs.getSortColumns() : new ArrayList<SortColumn>();
for (String sortingProvider : sortingProviders) {
FoundSetManager fsm = (FoundSetManager) fs.getFoundSetManager();
SortColumn sc = fsm.getSortColumn(fs.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(direction ? SortColumn.ASCENDING : SortColumn.DESCENDING);
}
fs.sort(list, false);
}
} else if (fc != null && fc.getForm().getOnSortCmdMethodID() != -1) {
JSEvent event = new JSEvent();
event.setType(JSEvent.EventType.none);
event.setName(RepositoryHelper.getDisplayName(StaticContentSpecLoader.PROPERTY_ONSORTCMDMETHODID.getPropertyName(), Form.class));
event.setFormName(view.getDataAdapterList().getFormController().getName());
event.setModifiers(modifiers);
fc.executeFunction(String.valueOf(fc.getForm().getOnSortCmdMethodID()), Utils.arrayMerge((new Object[] { dataProvider == null ? id : dataProvider.getDataProviderID(), Boolean.valueOf(direction), event }), // $NON-NLS-1$
Utils.parseJSExpressions(fc.getForm().getFlattenedMethodArguments("onSortCmdMethodID"))), true, null, false, // $NON-NLS-1$
"onSortCmdMethodID");
}
}
if ((modifiers & Event.SHIFT_MASK) == 0) {
sorted.clear();
}
sorted.put(name, new Boolean(direction));
listView.setCurrentPage(0);
}
}
break;
}
}
}
listView.modelChanged();
} catch (Exception e) {
if (fc != null) {
if (e instanceof ServoyException) {
((ServoyException) e).setContext(fc.toString());
} else {
ServoyException se = new ServoyException();
se.initCause(e);
se.setContext(fc.toString());
e = se;
}
}
Debug.error("error sorting foundset: " + sorted, e);
}
}
Aggregations