/* * Copyright (C) 2014 Servoy BV * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.servoy.j2db.server.ngclient.property.types; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONWriter; import org.sablo.WebComponent; import org.sablo.specification.PropertyDescription; import org.sablo.specification.property.IBrowserConverterContext; import org.sablo.specification.property.IPropertyConverterForBrowser; import org.sablo.specification.property.types.DefaultPropertyType; import org.sablo.util.ValueReference; import org.sablo.websocket.utils.DataConversion; import org.sablo.websocket.utils.JSONUtils; import com.servoy.j2db.server.ngclient.ComponentFactory; import com.servoy.j2db.server.ngclient.FormElementContext; import com.servoy.j2db.server.ngclient.IWebFormUI; import com.servoy.j2db.server.ngclient.property.types.NGConversions.IFormElementToTemplateJSON; /** * @author jcompagner */ public class LabelForPropertyType extends DefaultPropertyType<String> implements IPropertyConverterForBrowser<String>, IFormElementToTemplateJSON<String, String>, ISupportTemplateValue<String> { public static final LabelForPropertyType INSTANCE = new LabelForPropertyType(); public static final String TYPE_NAME = "labelfor"; protected LabelForPropertyType() { } @Override public String getName() { return TYPE_NAME; } @Override public Object parseConfig(JSONObject json) { return json; } @Override public String fromJSON(Object newJSONValue, String previousSabloValue, PropertyDescription pd, IBrowserConverterContext dataConverterContext, ValueReference<Boolean> returnValueAdjustedIncommingValue) { return (String)newJSONValue; } @Override public JSONWriter toJSON(JSONWriter writer, String key, String sabloValue, PropertyDescription pd, DataConversion clientConversion, IBrowserConverterContext dataConverterContext) throws JSONException { JSONUtils.addKeyIfPresent(writer, key); if (sabloValue != null && dataConverterContext != null && dataConverterContext.getWebObject() instanceof WebComponent) { writer.value(ComponentFactory.getMarkupId( ((WebComponent)dataConverterContext.getWebObject()).findParent(IWebFormUI.class).getController().getName(), sabloValue)); } else { writer.value(null); } return writer; } @Override public JSONWriter toTemplateJSONValue(JSONWriter writer, String key, String formElementValue, PropertyDescription pd, DataConversion browserConversionMarkers, FormElementContext formElementContext) throws JSONException { JSONUtils.addKeyIfPresent(writer, key); writer.value(formElementValue); return writer; } @Override public boolean valueInTemplate(String object, PropertyDescription pd, FormElementContext formElementContext) { return false; } }