Commit f77187cf authored by jerry73204's avatar jerry73204 Committed by Jeffrey I. Schiller

Add editorArgs DesignerProperty parameter

Add additional editorArgs parameter to DesignerProperty, and make
ChoicePropertyEditor accept editorArgs to obtain choice names.

Change-Id: I70f5421fe05f0061fe0e09a63e170ce26631caa1
parent f2119766
......@@ -331,9 +331,20 @@ class ComponentDatabase implements ComponentDatabaseInterface {
private void findComponentProperties(ComponentDefinition component, JSONArray propertiesArray) {
for (JSONValue propertyValue : propertiesArray.getElements()) {
Map<String, JSONValue> properties = propertyValue.asObject().getProperties();
// TODO Since older versions of extensions do not have the "editorArgs" key,
// we check if "editorArgs" exists before parsing as a workaround. We may
// need better approaches in future versions.
List<String> editorArgsList = new ArrayList<String>();
if (properties.containsKey("editorArgs")) {
for (JSONValue val : properties.get("editorArgs").asArray().getElements())
editorArgsList.add(val.asString().getString());
}
component.add(new PropertyDefinition(properties.get("name").asString().getString(),
properties.get("defaultValue").asString().getString(), properties.get("editorType")
.asString().getString()));
properties.get("defaultValue").asString().getString(),
properties.get("editorType").asString().getString(),
editorArgsList.toArray(new String[0])));
}
}
......
......@@ -1098,7 +1098,7 @@ public abstract class MockComponent extends Composite implements PropertyChangeL
}
for (PropertyDefinition property : newProperties) {
if (toBeAdded.contains(property.getName())) {
PropertyEditor propertyEditor = PropertiesUtil.createPropertyEditor(property.getEditorType(), property.getDefaultValue(), (YaFormEditor) editor);
PropertyEditor propertyEditor = PropertiesUtil.createPropertyEditor(property.getEditorType(), property.getDefaultValue(), (YaFormEditor) editor, property.getEditorArgs());
addProperty(property.getName(), property.getDefaultValue(), property.getCaption(), propertyEditor);
}
}
......
......@@ -42,6 +42,7 @@ import com.google.appinventor.client.editor.youngandroid.properties.YoungAndroid
import com.google.appinventor.client.editor.youngandroid.properties.YoungAndroidVerticalAlignmentChoicePropertyEditor;
import com.google.appinventor.client.properties.BadPropertyEditorException;
import com.google.appinventor.client.properties.Property;
import com.google.appinventor.client.widgets.properties.ChoicePropertyEditor;
import com.google.appinventor.client.widgets.properties.CountryChoicePropertyEditor;
import com.google.appinventor.client.widgets.properties.EditableProperties;
import com.google.appinventor.client.widgets.properties.EditableProperty;
......@@ -95,7 +96,7 @@ public class PropertiesUtil {
for (ComponentDatabaseInterface.PropertyDefinition property : propertyDefintions) {
mockComponent.addProperty(property.getName(), property.getDefaultValue(),
ComponentsTranslation.getPropertyName(property.getCaption()),
PropertiesUtil.createPropertyEditor(property.getEditorType(), property.getDefaultValue(), editor));
PropertiesUtil.createPropertyEditor(property.getEditorType(), property.getDefaultValue(), editor, property.getEditorArgs()));
/*OdeLog.log("Property Caption: " + property.getCaption() + ", "
+ TranslationComponentProperty.getName(property.getCaption()));*/
}
......@@ -170,7 +171,7 @@ public class PropertiesUtil {
/*
* Creates a new property editor.
*/
public static PropertyEditor createPropertyEditor(String editorType, String defaultValue, YaFormEditor editor) {
public static PropertyEditor createPropertyEditor(String editorType, String defaultValue, YaFormEditor editor, String[] editorArgs) {
if (editorType.equals(PropertyTypeConstants.PROPERTY_TYPE_HORIZONTAL_ALIGNMENT)) {
return new YoungAndroidHorizontalAlignmentChoicePropertyEditor();
} else if (editorType.equals(PropertyTypeConstants.PROPERTY_TYPE_VERTICAL_ALIGNMENT)) {
......@@ -208,6 +209,8 @@ public class PropertiesUtil {
return new YoungAndroidLegoEv3UltrasonicSensorModeChoicePropertyEditor();
} else if (editorType.equals(PropertyTypeConstants.PROPERTY_TYPE_LEGO_EV3_GYRO_SENSOR_MODE)) {
return new YoungAndroidLegoEv3GyroSensorModeChoicePropertyEditor();
} else if (editorType.equals(PropertyTypeConstants.PROPERTY_TYPE_CHOICES)) {
return new ChoicePropertyEditor(editorArgs);
} else if (editorType.equals(PropertyTypeConstants.PROPERTY_TYPE_LEGO_NXT_SENSOR_PORT)) {
return new YoungAndroidLegoNxtSensorPortChoicePropertyEditor();
} else if (editorType.equals(PropertyTypeConstants.PROPERTY_TYPE_LEGO_NXT_GENERATED_COLOR)) {
......
......@@ -98,6 +98,32 @@ public class ChoicePropertyEditor extends PropertyEditor {
initWidget(dropDownButton);
}
/**
* Creates a new instance of the property editor with choice names.
* Each choice name is treated as both the captain and the value of a choice.
*
* @param choiceNames array of choice names to choose from
*/
public ChoicePropertyEditor(String[] choiceNames) {
this.choices = new Choice[choiceNames.length];
for (int idx = 0; idx < choiceNames.length; idx += 1)
this.choices[idx] = new Choice(choiceNames[idx], choiceNames[idx]);
List<DropDownItem> items = Lists.newArrayList();
for(final Choice choice : choices) {
items.add(new DropDownItem("Choice Property Editor", choice.caption, new Command() {
@Override
public void execute() {
property.setValue(choice.value);
}
}));
}
dropDownButton = new DropDownButton("Choice Property Editor", choices[0].caption, items, false);
dropDownButton.setStylePrimaryName("ode-ChoicePropertyEditor");
initWidget(dropDownButton);
}
@Override
protected void updateValue() {
String propertyValue = property.getValue();
......
......@@ -159,16 +159,18 @@ public interface ComponentDatabaseInterface {
private final String defaultValue;
private final String caption;
private final String editorType;
private final String[] editorArgs;
public PropertyDefinition(String name, String defaultValue, String editorType) {
this(name, defaultValue, name, editorType);
public PropertyDefinition(String name, String defaultValue, String editorType, String[] editorArgs) {
this(name, defaultValue, name, editorType, editorArgs);
}
public PropertyDefinition(String name, String defaultValue, String caption, String editorType) {
public PropertyDefinition(String name, String defaultValue, String caption, String editorType, String[] editorArgs) {
this.name = name;
this.defaultValue = defaultValue;
this.caption = caption;
this.editorType = editorType;
this.editorArgs = editorArgs;
}
public String getName() {
......@@ -186,6 +188,10 @@ public interface ComponentDatabaseInterface {
public String getEditorType() {
return editorType;
}
public String[] getEditorArgs() {
return editorArgs;
}
}
/**
......
......@@ -36,4 +36,11 @@ public @interface DesignerProperty {
* @return default property value
*/
String defaultValue() default "";
/**
* Arguments passed to editor class.
*
* @return editor arguments
*/
String[] editorArgs() default {};
}
......@@ -149,6 +149,13 @@ public class PropertyTypeConstants {
*/
public static final String PROPERTY_TYPE_LEGO_EV3_GENERATED_COLOR = "lego_ev3_generated_color";
/**
* Choices.
* @see
* com.google.appinventor.client.widgets.properties.ChoicePropertyEditor
*/
public static final String PROPERTY_TYPE_CHOICES = "choices";
/**
* Floating-point values in the range of valid longitudes [-180, 180].
* @see com.google.appinventor.client.editor.youngandroid.properties.YoungAndroidFloatRangePropertyEditor
......
......@@ -140,7 +140,28 @@ public final class ComponentDescriptorGenerator extends ComponentProcessor {
sb.append(dp.editorType());
sb.append("\", \"defaultValue\": \"");
sb.append(dp.defaultValue().replace("\"", "\\\""));
sb.append("\"}");
sb.append("\", \"editorArgs\": ");
String[] editorArgs = dp.editorArgs();
for (int idx = 0; idx < editorArgs.length; idx += 1)
editorArgs[idx] = "\"" + editorArgs[idx].replace("\"", "\\\"") + "\"";
StringBuilder listLiteralBuilder = new StringBuilder();
listLiteralBuilder.append("[");
if (editorArgs.length > 0) {
listLiteralBuilder.append(editorArgs[0]);
for (int ind = 1; ind < editorArgs.length; ind += 1) {
listLiteralBuilder.append(", ");
listLiteralBuilder.append(editorArgs[ind]);
}
}
listLiteralBuilder.append("]");
sb.append(listLiteralBuilder.toString());
sb.append("}");
}
private void outputBlockProperty(String propertyName, Property prop, StringBuilder sb) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment