use of com.robotoworks.mechanoid.sharedprefs.sharedPreferencesModel.PreferencesBlock 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.PreferencesBlock in project mechanoid by robotoworks.
the class SharedPreferencesModelGenerator method doGenerate.
public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
EList<EObject> _contents = resource.getContents();
EObject _head = IterableExtensions.<EObject>head(_contents);
Model model = ((Model) _head);
String _packageName = model.getPackageName();
PreferencesBlock _preferencesBlock = model.getPreferencesBlock();
String _name = _preferencesBlock.getName();
String _pascalize = Strings.pascalize(_name);
String _concat = _pascalize.concat("Preferences");
String _resolveFileName = Strings.resolveFileName(_packageName, _concat);
CharSequence _generate = this.mPreferencesGenerator.generate(model);
fsa.generateFile(_resolveFileName, _generate);
}
use of com.robotoworks.mechanoid.sharedprefs.sharedPreferencesModel.PreferencesBlock in project mechanoid by robotoworks.
the class ModelImpl method basicSetPreferencesBlock.
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public NotificationChain basicSetPreferencesBlock(PreferencesBlock newPreferencesBlock, NotificationChain msgs) {
PreferencesBlock oldPreferencesBlock = preferencesBlock;
preferencesBlock = newPreferencesBlock;
if (eNotificationRequired()) {
ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, SharedPreferencesModelPackage.MODEL__PREFERENCES_BLOCK, oldPreferencesBlock, newPreferencesBlock);
if (msgs == null)
msgs = notification;
else
msgs.add(notification);
}
return msgs;
}
use of com.robotoworks.mechanoid.sharedprefs.sharedPreferencesModel.PreferencesBlock in project mechanoid by robotoworks.
the class NewMechanoidPrefsFileWizard method createElementResource.
@Override
protected IResource createElementResource(IProgressMonitor monitor, IPath path) {
try {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.beginTask(Messages.NewMechanoidPrefsFileWizard_Progress_Message, 1);
URI newEmfResourceURI = URI.createURI(//$NON-NLS-1$
"platform:/resource" + path.toPortableString());
Resource emfResource = mResourceSet.createResource(newEmfResourceURI);
Model model = SharedPreferencesModelFactory.eINSTANCE.createModel();
model.setPackageName(mSelectedPackageName);
emfResource.getContents().add(model);
PreferencesBlock prefs = (PreferencesBlock) SharedPreferencesModelFactory.eINSTANCE.createPreferencesBlock();
prefs.setName(mSelectedElementName);
model.setPreferencesBlock(prefs);
emfResource.save(Collections.EMPTY_MAP);
IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(newEmfResourceURI.toPlatformString(true));
monitor.worked(1);
return resource;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Aggregations