use of com.robotoworks.mechanoid.sharedprefs.sharedPreferencesModel.PreferenceType in project mechanoid by robotoworks.
the class SharedPreferencesGenerator method generate.
public CharSequence generate(final Model model) {
StringConcatenation _builder = new StringConcatenation();
PreferencesBlock sp = model.getPreferencesBlock();
_builder.newLineIfNotEmpty();
_builder.append("/*");
_builder.newLine();
_builder.append(" ");
_builder.append("* Generated by Robotoworks Mechanoid");
_builder.newLine();
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
_builder.append("package ");
String _packageName = model.getPackageName();
_builder.append(_packageName, "");
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("import android.annotation.TargetApi;");
_builder.newLine();
_builder.append("import android.content.Context;");
_builder.newLine();
_builder.append("import android.content.SharedPreferences;");
_builder.newLine();
_builder.append("import android.content.SharedPreferences.OnSharedPreferenceChangeListener;");
_builder.newLine();
_builder.append("import com.robotoworks.mechanoid.Mechanoid;");
_builder.newLine();
_builder.newLine();
_builder.append("public class ");
String _name = sp.getName();
String _pascalize = Strings.pascalize(_name);
_builder.append(_pascalize, "");
_builder.append("Preferences {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("public static final String PREFERENCES_NAME = \"");
String _name_1 = sp.getName();
String _pascalize_1 = Strings.pascalize(_name_1);
_builder.append(_pascalize_1, "\t");
_builder.append("Preferences\";");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t");
_builder.append("public interface Keys {");
_builder.newLine();
{
EList<Preference> _prefs = sp.getPrefs();
for (final Preference p : _prefs) {
_builder.append("\t\t");
_builder.append("String ");
String _name_2 = p.getName();
String _underscore = Strings.underscore(_name_2);
String _upperCase = _underscore.toUpperCase();
_builder.append(_upperCase, "\t\t");
_builder.append(" = \"");
String _name_3 = p.getName();
String _underscore_1 = Strings.underscore(_name_3);
String _upperCase_1 = _underscore_1.toUpperCase();
_builder.append(_upperCase_1, "\t\t");
_builder.append("\";");
_builder.newLineIfNotEmpty();
}
}
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("private final SharedPreferences mPreferences;");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("private static ");
String _name_4 = sp.getName();
String _pascalize_2 = Strings.pascalize(_name_4);
_builder.append(_pascalize_2, "\t");
_builder.append("Preferences sInstance;");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t");
_builder.append("public static ");
String _name_5 = sp.getName();
String _pascalize_3 = Strings.pascalize(_name_5);
_builder.append(_pascalize_3, "\t");
_builder.append("Preferences getInstance() {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("if(sInstance == null) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("sInstance = new ");
String _name_6 = sp.getName();
String _pascalize_4 = Strings.pascalize(_name_6);
_builder.append(_pascalize_4, "\t\t\t");
_builder.append("Preferences(Mechanoid.getApplicationContext());");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return sInstance;");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("private ");
String _name_7 = sp.getName();
String _pascalize_5 = Strings.pascalize(_name_7);
_builder.append(_pascalize_5, "\t");
_builder.append("Preferences(Context context) {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("mPreferences = context.getSharedPreferences(PREFERENCES_NAME, 0);");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
{
EList<Preference> _prefs_1 = sp.getPrefs();
for (final Preference p_1 : _prefs_1) {
_builder.append("\t");
_builder.append("public ");
PreferenceType _type = p_1.getType();
String _typeLiteral = Extensions.toTypeLiteral(_type);
_builder.append(_typeLiteral, "\t");
_builder.append(" get");
String _name_8 = p_1.getName();
String _pascalize_6 = Strings.pascalize(_name_8);
_builder.append(_pascalize_6, "\t");
_builder.append("() {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("return mPreferences.");
PreferenceType _type_1 = p_1.getType();
String _getMethodName = Extensions.toGetMethodName(_type_1);
_builder.append(_getMethodName, "\t\t");
_builder.append("(Keys.");
String _name_9 = p_1.getName();
String _underscore_2 = Strings.underscore(_name_9);
String _upperCase_2 = _underscore_2.toUpperCase();
_builder.append(_upperCase_2, "\t\t");
_builder.append(", ");
String _defaultValue = Extensions.toDefaultValue(p_1);
_builder.append(_defaultValue, "\t\t");
_builder.append(");");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
}
}
{
EList<Preference> _prefs_2 = sp.getPrefs();
for (final Preference p_2 : _prefs_2) {
_builder.append("\t");
_builder.append("/**");
_builder.newLine();
_builder.append("\t");
_builder.append(" ");
_builder.append("* Update this preference, if you need to change many preferences");
_builder.newLine();
_builder.append("\t");
_builder.append(" ");
_builder.append("* you should use edit() instead since calling update in succession");
_builder.newLine();
_builder.append("\t");
_builder.append(" ");
_builder.append("* can have a severe performance penalty");
_builder.newLine();
_builder.append("\t");
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
_builder.append("\t");
_builder.append("public void update");
String _name_10 = p_2.getName();
String _pascalize_7 = Strings.pascalize(_name_10);
_builder.append(_pascalize_7, "\t");
_builder.append("(");
PreferenceType _type_2 = p_2.getType();
String _typeLiteral_1 = Extensions.toTypeLiteral(_type_2);
_builder.append(_typeLiteral_1, "\t");
_builder.append(" value) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("mPreferences.edit()");
_builder.newLine();
_builder.append("\t");
_builder.append("\t\t");
_builder.append(".");
PreferenceType _type_3 = p_2.getType();
String _putMethodName = Extensions.toPutMethodName(_type_3);
_builder.append(_putMethodName, "\t\t\t");
_builder.append("(Keys.");
String _name_11 = p_2.getName();
String _underscore_3 = Strings.underscore(_name_11);
String _upperCase_3 = _underscore_3.toUpperCase();
_builder.append(_upperCase_3, "\t\t\t");
_builder.append(", value)");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t\t");
_builder.append(".commit();");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
}
}
_builder.append("\t");
_builder.append("public SharedPreferences getSharedPreferences() {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return mPreferences;");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("mPreferences.registerOnSharedPreferenceChangeListener(listener);");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("mPreferences.unregisterOnSharedPreferenceChangeListener(listener);");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("public ");
String _name_12 = sp.getName();
String _pascalize_8 = Strings.pascalize(_name_12);
_builder.append(_pascalize_8, "\t");
_builder.append("PreferencesEditor edit() {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("return new ");
String _name_13 = sp.getName();
String _pascalize_9 = Strings.pascalize(_name_13);
_builder.append(_pascalize_9, "\t\t");
_builder.append("PreferencesEditor(mPreferences.edit());");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("public class ");
String _name_14 = sp.getName();
String _pascalize_10 = Strings.pascalize(_name_14);
_builder.append(_pascalize_10, "\t");
_builder.append("PreferencesEditor {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("private SharedPreferences.Editor mEditor;");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("public ");
String _name_15 = sp.getName();
String _pascalize_11 = Strings.pascalize(_name_15);
_builder.append(_pascalize_11, "\t\t");
_builder.append("PreferencesEditor(SharedPreferences.Editor editor) {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("mEditor = editor;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("public boolean commit() {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("return mEditor.commit();");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("@TargetApi(9)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("public void apply() {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("mEditor.apply();");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("public ");
String _name_16 = sp.getName();
String _pascalize_12 = Strings.pascalize(_name_16);
_builder.append(_pascalize_12, "\t\t");
_builder.append("PreferencesEditor clear() {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("mEditor.clear();");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("return this;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
{
EList<Preference> _prefs_3 = sp.getPrefs();
for (final Preference p_3 : _prefs_3) {
_builder.append("\t\t");
_builder.append("public ");
String _name_17 = sp.getName();
String _pascalize_13 = Strings.pascalize(_name_17);
_builder.append(_pascalize_13, "\t\t");
_builder.append("PreferencesEditor put");
String _name_18 = p_3.getName();
String _pascalize_14 = Strings.pascalize(_name_18);
_builder.append(_pascalize_14, "\t\t");
_builder.append("(");
PreferenceType _type_4 = p_3.getType();
String _typeLiteral_2 = Extensions.toTypeLiteral(_type_4);
_builder.append(_typeLiteral_2, "\t\t");
_builder.append(" value) {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("mEditor.");
PreferenceType _type_5 = p_3.getType();
String _putMethodName_1 = Extensions.toPutMethodName(_type_5);
_builder.append(_putMethodName_1, "\t\t\t");
_builder.append("(Keys.");
String _name_19 = p_3.getName();
String _underscore_4 = Strings.underscore(_name_19);
String _upperCase_4 = _underscore_4.toUpperCase();
_builder.append(_upperCase_4, "\t\t\t");
_builder.append(", value);");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("\t");
_builder.append("return this;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
}
}
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder;
}
use of com.robotoworks.mechanoid.sharedprefs.sharedPreferencesModel.PreferenceType in project mechanoid by robotoworks.
the class PreferenceImpl method setType.
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public void setType(PreferenceType newType) {
PreferenceType oldType = type;
type = newType == null ? TYPE_EDEFAULT : newType;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, SharedPreferencesModelPackage.PREFERENCE__TYPE, oldType, type));
}
use of com.robotoworks.mechanoid.sharedprefs.sharedPreferencesModel.PreferenceType in project mechanoid by robotoworks.
the class SharedPreferencesModelJavaValidator method checkDefaultValue.
@Check
public void checkDefaultValue(Preference preference) {
PreferenceType type = preference.getType();
Literal defaultValue = preference.getDefaultValue();
if (defaultValue == null) {
return;
}
switch(type) {
case STRING:
if (!(defaultValue instanceof StringLiteral)) {
error("Type mismatch", SharedPreferencesModelPackage.Literals.PREFERENCE__DEFAULT_VALUE);
}
break;
case BOOLEAN:
if (!(defaultValue instanceof BooleanLiteral)) {
error("Type mismatch", SharedPreferencesModelPackage.Literals.PREFERENCE__DEFAULT_VALUE);
}
break;
case FLOAT:
case INTEGER:
case LONG:
if (!(defaultValue instanceof NumericLiteral)) {
error("Type mismatch", SharedPreferencesModelPackage.Literals.PREFERENCE__DEFAULT_VALUE);
}
break;
}
}
Aggregations