use of com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue in project servoy-client by Servoy.
the class CellAdapter method getTableCellEditorComponent.
/*
* @see TableCellEditor#getTableCellEditorComponent(JTable, Object, boolean, int, int)
*/
public Component getTableCellEditorComponent(JTable jtable, Object value, boolean isSelected, int row, int column) {
if (editor == null || !isVisible(editor) || !(jtable.getModel() instanceof IFoundSetInternal)) {
return empty;
}
IRecordInternal newRec = ((IFoundSetInternal) jtable.getModel()).getRecord(row);
if (isSelected) {
Color bgColor = getBgColor(jtable, isSelected, row, true);
if (bgColor != null && editor instanceof JComponent)
((JComponent) editor).setOpaque(true);
if (bgColor == null) {
// unselected background is the default background color of the editor.
bgColor = unselectedBackground;
}
lastEditorBgColor = bgColor;
if (editor instanceof ISupportsDoubleBackground) {
((ISupportsDoubleBackground) editor).setBackground(bgColor, unselectedBackground);
} else {
editor.setBackground(bgColor);
}
Color fgColor = getFgColor(jtable, isSelected, row);
if (fgColor == null) {
// unselected foreground is the default foreground color of the editor.
fgColor = unselectedForeground;
}
lastEditorFgColor = fgColor;
if (editor instanceof ISupportsDoubleBackground) {
((ISupportsDoubleBackground) editor).setForeground(fgColor, unselectedForeground);
} else {
editor.setForeground(fgColor);
}
Font font = getFont(jtable, isSelected, row);
if (font == null) {
// unselected font is the default font of the editor.
font = unselectedFont;
}
lastEditorFont = font;
editor.setFont(font);
Border styleBorder = getBorder(jtable, isSelected, row);
if (styleBorder != null && editor instanceof JComponent) {
if (editor instanceof AbstractButton && !((AbstractButton) editor).isBorderPainted()) {
((AbstractButton) editor).setBorderPainted(true);
}
Border marginBorder = null;
if (noFocusBorder instanceof EmptyBorder) {
marginBorder = noFocusBorder;
} else if (noFocusBorder instanceof CompoundBorder && ((CompoundBorder) noFocusBorder).getInsideBorder() instanceof EmptyBorder) {
marginBorder = ((CompoundBorder) noFocusBorder).getInsideBorder();
}
// if we have margin set on the component, keep it along with the style border
if (marginBorder != null) {
styleBorder = new CompoundBorder(styleBorder, marginBorder);
}
((JComponent) editor).setBorder(styleBorder);
}
}
// try
// {
// if (currentEditingState != null && newRec != currentEditingState && currentEditingState.isEditing())
// {
// currentEditingState.stopEditing();
// }
// }
// catch (Exception e)
// {
// Debug.error(e);
// }
currentEditingState = newRec;
if (currentEditingState != null) {
if (editor instanceof IScriptableProvider) {
IScriptable scriptable = ((IScriptableProvider) editor).getScriptObject();
if (scriptable instanceof ISupportOnRenderCallback) {
RenderEventExecutor renderEventExecutor = ((ISupportOnRenderCallback) scriptable).getRenderEventExecutor();
if (renderEventExecutor != null && renderEventExecutor.hasRenderCallback()) {
renderEventExecutor.setRenderState(currentEditingState, row, isSelected, true);
}
}
}
// if not enabled or not editable do not start the edit
if (editor instanceof IDisplay && ((IDisplay) editor).isEnabled() && !((IDisplay) editor).isReadOnly()) {
DisplaysAdapter.startEdit(dal, (IDisplay) editor, currentEditingState);
}
if (editor instanceof IDisplayRelatedData) {
IDisplayRelatedData drd = (IDisplayRelatedData) editor;
IRecordInternal state = ((IFoundSetInternal) jtable.getModel()).getRecord(row);
if (state != null) {
drd.setRecord(state, true);
}
}
if (// && dataProviderID != null)
editor instanceof IDisplayData) {
try {
Object data = dal.getValueObject(currentEditingState, dataProviderID);
if (data instanceof DbIdentValue) {
data = ((DbIdentValue) data).getPkValue();
}
convertAndSetValue(((IDisplayData) editor), data);
} catch (IllegalArgumentException iae) {
Debug.error(iae);
}
}
if (editor instanceof IServoyAwareBean) {
((IServoyAwareBean) editor).setSelectedRecord(new ServoyBeanState(currentEditingState, dal.getFormScope()));
}
if (editor instanceof IScriptableProvider && !(editor instanceof IDisplayData) && !(editor instanceof IDisplayRelatedData)) {
IScriptable scriptable = ((IScriptableProvider) editor).getScriptObject();
if (scriptable instanceof ISupportOnRenderCallback) {
RenderEventExecutor renderEventExecutor = ((ISupportOnRenderCallback) scriptable).getRenderEventExecutor();
if (renderEventExecutor != null && renderEventExecutor.hasRenderCallback()) {
renderEventExecutor.fireOnRender(editor instanceof JTextComponent ? ((JTextComponent) editor).isEditable() : false);
}
}
}
}
return editor.isVisible() ? editor : empty;
}
use of com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue 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.ValueFactory.DbIdentValue in project servoy-client by Servoy.
the class RecordItemModel method getValue.
/**
* Gets the value for dataProviderID in the context of this record and the form determined using given component. The component is only used for getting the
* form, otherwise it does not affect the returned value.
*
* @param component the component used to determine the form (in case of global or form variables).
* @param dataProviderID the data provider id pointing to a data provider in the record, a form or a global variable.
* @return the value.
*/
public Object getValue(Component component, String dataProviderID) {
Object value = null;
WebForm webForm = component.findParent(WebForm.class);
if (webForm == null) {
// component.toString() may cause stackoverflow here
// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
Debug.error(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
"Component " + component.getClass() + " with dp: " + dataProviderID + " already removed from its parent form.", new RuntimeException());
return null;
}
FormScope fs = webForm.getController().getFormScope();
IRecord record = (IRecord) RecordItemModel.this.getObject();
if (ScopesUtils.isVariableScope(dataProviderID)) {
value = webForm.getController().getApplication().getScriptEngine().getSolutionScope().getScopesScope().get(null, dataProviderID);
} else if (record != null) {
value = record.getValue(dataProviderID);
}
if (value == Scriptable.NOT_FOUND && fs != null && fs.has(dataProviderID, fs)) {
value = fs.get(dataProviderID);
}
if (value instanceof DbIdentValue) {
value = ((DbIdentValue) value).getPkValue();
}
if (value == Scriptable.NOT_FOUND) {
value = null;
} else if (!(record instanceof FindState)) {
// use UI converter to convert from record value to UI value
value = ComponentFormat.applyUIConverterToObject(component, value, dataProviderID, webForm.getController().getApplication().getFoundSetManager());
}
return value;
}
use of com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue in project servoy-client by Servoy.
the class FoundsetTypeSabloValue method populateRowData.
protected void populateRowData(IRecordInternal record, Set<String> columnNames, JSONWriter w, DataConversion clientConversionInfo, IBrowserConverterContext browserConverterContext) throws JSONException {
Iterator<Entry<String, String>> it = dataproviders.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
String dataProvider = entry.getValue();
if (columnNames == null || columnNames.contains(dataProvider)) {
Object value = (dataProvider != null ? record.getValue(dataProvider) : null);
// if the given DP is invalid, then record.getValue(dataProvider) can return Rhino Scriptable.NOT_FOUND; we must handle that as that can't be sent to client conversion directly
if (value == Scriptable.NOT_FOUND)
value = null;
PropertyDescription pd = getDataProviderPropertyDescription(dataProvider);
// currently all that NGUtils.getDataProviderPropertyDescription can return is IConvertedProperty type or default types; so we don't need any special value pre-processing (like IWrapperType or IServoyAwareValue or others would need)
// if (pd != null)
// {
// if (pd.getType() instanceof IWrapperType< ? , ? >) value = ((IWrapperType)pd.getType()).wrap(value, null, new DataConverterContext(pd,
// webObject));
// }
clientConversionInfo.pushNode(entry.getKey());
if (value instanceof DbIdentValue) {
value = ((DbIdentValue) value).getPkValue();
}
FullValueToJSONConverter.INSTANCE.toJSONValue(w, entry.getKey(), value, pd, clientConversionInfo, browserConverterContext);
clientConversionInfo.popNode();
}
}
}
use of com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue in project servoy-client by Servoy.
the class Row method setValue.
// returns the oldvalue, or value if no change
public Object setValue(IRowChangeListener src, String dataProviderID, Object value) {
Object o = getRawValue(dataProviderID);
// this column is controlled by the database - so do not allow sets until the database chose a value
if (o instanceof DbIdentValue)
return o;
Object convertedValue = value;
SQLSheet sheet = parent.getSQLSheet();
int columnIndex = sheet.getColumnIndex(dataProviderID);
VariableInfo variableInfo = sheet.getCalculationOrColumnVariableInfo(dataProviderID, columnIndex);
if (// do not convert null to 0 incase of numbers, this means the calcs the value whould change each time //$NON-NLS-1$
convertedValue != null && !("".equals(convertedValue) && Column.mapToDefaultType(variableInfo.type) == IColumnTypes.TEXT)) {
convertedValue = sheet.convertObjectToValue(dataProviderID, convertedValue, parent.getFoundsetManager().getColumnConverterManager(), parent.getFoundsetManager().getColumnValidatorManager(), src);
} else if (parent.getFoundsetManager().isNullColumnValidatorEnabled() && // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
Settings.getInstance().getProperty("servoy.execute.column.validators.only.on.validate_and_save", "true").equals("false")) {
// check for not null constraint
Column c = null;
try {
c = sheet.getTable().getColumn(dataProviderID);
} catch (Exception e) {
Debug.error(e);
}
if (c != null && !c.getAllowNull()) {
// $NON-NLS-1$
throw new IllegalArgumentException(Messages.getString("servoy.record.error.validation", new Object[] { dataProviderID, convertedValue }));
}
}
boolean wasUNINITIALIZED = false;
if (o == UNINITIALIZED) {
o = null;
wasUNINITIALIZED = true;
}
boolean isCalculation = containsCalculation(dataProviderID);
// if we receive NULL from the db for Empty strings in Servoy calcs, return value
if (// $NON-NLS-1$
o == null && "".equals(convertedValue) && isCalculation) {
mustRecalculate(dataProviderID, false);
return convertedValue;
}
if (!Utils.equalObjects(o, convertedValue)) {
boolean mustStop = false;
if (columnIndex != -1 && columnIndex < columndata.length) {
mustStop = !parent.getFoundsetManager().getEditRecordList().isEditing();
if (// if not yet existInDB, leave startEdit to Foundset new/duplicateRecord code!
src != null && existInDB && !wasUNINITIALIZED) {
src.startEditing(false);
}
createOldValuesIfNeeded();
columndata[columnIndex] = convertedValue;
} else if (isCalculation) {
unstoredCalcCache.put(dataProviderID, convertedValue);
}
lastException = null;
// Reset the mustRecalculate here, before setValue fires events, so if it is an every time changing calculation it will not be calculated again and again
if (isCalculation) {
mustRecalculate(dataProviderID, false);
threadCalculationComplete(dataProviderID);
}
handleCalculationDependencies(sheet.getTable().getColumn(dataProviderID), dataProviderID);
FireCollector collector = FireCollector.getFireCollector();
try {
fireNotifyChange(dataProviderID, convertedValue, collector);
} finally {
collector.done();
}
if (src != null && mustStop && existInDB && !wasUNINITIALIZED) {
try {
src.stopEditing();
} catch (Exception e) {
Debug.error(e);
}
}
return o;
} else if (isCalculation) {
// Reset the mustRecalculate here, before setValue fires events, so if it is an every time changing calculation it will not be calculated again and again
mustRecalculate(dataProviderID, false);
}
// is same so return
return convertedValue;
}
Aggregations