summaryrefslogtreecommitdiff
path: root/src/plugins/android.codeutils/src/com/motorola/studio/android/generateviewbylayout/codegenerators/SaveStateCodeGenerator.java
blob: cdf05c4f5cf5e159fbcf38131234384cb2777433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * 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.motorola.studio.android.generateviewbylayout.codegenerators;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.BooleanLiteral;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.QualifiedType;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS;
import com.motorola.studio.android.generateviewbylayout.JavaModifierBasedOnLayout;
import com.motorola.studio.android.generateviewbylayout.model.CheckboxNode;
import com.motorola.studio.android.generateviewbylayout.model.CodeGeneratorDataBasedOnLayout;
import com.motorola.studio.android.generateviewbylayout.model.EditTextNode;
import com.motorola.studio.android.generateviewbylayout.model.LayoutNode;
import com.motorola.studio.android.generateviewbylayout.model.LayoutNode.ViewProperties;
import com.motorola.studio.android.generateviewbylayout.model.RadioButtonNode;
import com.motorola.studio.android.generateviewbylayout.model.SeekBarNode;
import com.motorola.studio.android.generateviewbylayout.model.SpinnerNode;

/**
 * Generate code to save/restore GUI state (e.g.: EditText, ComboBox, etc)
 */
public class SaveStateCodeGenerator extends AbstractLayoutCodeGenerator

{

    public static LayoutNode[] saveStateNodeTypes = new LayoutNode[]
    {
            new CheckboxNode(), new RadioButtonNode(), new EditTextNode(), new SpinnerNode(),
            new SeekBarNode()
    };

    /*
     * Constants (types, methods and variable names)
     */
    private static final String COMMIT = "commit";

    private static final String ANDROID_CONTENT_IMPORT = "android.content.*";

    private static final String MODE_PRIVATE = "MODE_PRIVATE";

    private static final String GET_PREFERENCES = "getPreferences";

    private static final String PREFERENCES = "preferences";

    private static final String EDITOR_CAPITAL_LETTER = "Editor";

    private static final String SHARED_PREFERENCES = "SharedPreferences";

    private static final String EDITOR = "editor";

    private static final String EDIT = "edit";

    private static final String TO_STRING = "toString";

    private static final String PROTECTED_VOID_ON_PAUSE = "protected void onPause()"; //$NON-NLS-1$

    private static final String PROTECTED_VOID_ON_RESUME = "protected void onResume()"; //$NON-NLS-1$

    private static final String ON_PAUSE = "onPause"; //$NON-NLS-1$

    private static final String ON_RESUME = "onResume"; //$NON-NLS-1$

    /**
     * @param codeGeneratorData
     * @param onCreateDeclaration
     * @param typeDeclaration
     */
    public SaveStateCodeGenerator(CodeGeneratorDataBasedOnLayout codeGeneratorData,
            MethodDeclaration onCreateDeclaration, TypeDeclaration typeDeclaration)
    {
        super(codeGeneratorData, onCreateDeclaration, typeDeclaration);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void generateCode(IProgressMonitor monitor) throws JavaModelException
    {
        SubMonitor subMonitor = SubMonitor.convert(monitor);
        subMonitor.beginTask(CodeUtilsNLS.SaveStateCodeGenerator_AddingCodeSaveRestoreUIState,
                codeGeneratorData.getGuiItems().size());

        boolean alreadyFoundItemToSaveState = false;
        MethodDeclaration onPauseFoundMethod = null;
        MethodDeclaration onResumeFoundMethod = null;
        SimpleName editorVarName = null;
        SimpleName preferencesVarName = null;
        for (LayoutNode node : codeGeneratorData.getGUIItems(false))
        {
            if (canGenerateSaveStateCode(node) && node.getSaveState()
                    && (node.isAlreadyDeclaredInCode() || node.shouldInsertCode()))
            {
                if (!alreadyFoundItemToSaveState)
                {
                    onPauseFoundMethod = insertOnPause();

                    onResumeFoundMethod = insertOnResume();

                    String superMethodNameOnPause = ON_PAUSE;
                    insertSuperInvocation(onPauseFoundMethod, superMethodNameOnPause, null);

                    String superMethodNameOnResume = ON_RESUME;
                    insertSuperInvocation(onResumeFoundMethod, superMethodNameOnResume, null);

                    JavaModifierBasedOnLayout.IMPORT_LIST.add(ANDROID_CONTENT_IMPORT);

                    preferencesVarName = getPreferenceVariable(onPauseFoundMethod);

                    editorVarName = insertPreferencesEditor(onPauseFoundMethod, preferencesVarName);

                    getPreferenceVariable(onResumeFoundMethod);

                    //need to add method declaration and variables only once
                    alreadyFoundItemToSaveState = true;
                }
                String putMethodName = node.getProperty(ViewProperties.PreferenceSetMethod);
                String getUIStateMethodName = node.getProperty(ViewProperties.ViewStateGetMethod);
                String getter = node.getProperty(ViewProperties.PreferenceGetMethod);
                String setMethodName = node.getProperty(ViewProperties.ViewStateSetMethod);

                MethodInvocation getGUIStateInvocation = null;
                if (node.getNodeType().equals(LayoutNode.LayoutNodeViewType.EditText.name()))
                {
                    //Add code for save state: savedInstanceState.putString("MyEditTextId", edittext.getText());                    
                    getGUIStateInvocation = onPauseFoundMethod.getAST().newMethodInvocation();
                    String getGUIStateToStringName = TO_STRING; //$NON-NLS-1$

                    MethodInvocation editTextMethod =
                            addMethodToRetrieveUIState(onPauseFoundMethod, node,
                                    getUIStateMethodName);

                    SimpleName toStringName =
                            onPauseFoundMethod.getAST().newSimpleName(getGUIStateToStringName);
                    getGUIStateInvocation.setName(toStringName);
                    getGUIStateInvocation.setExpression(editTextMethod);

                    insertPutMethod(onPauseFoundMethod, node, putMethodName,
                            editorVarName.getIdentifier(), getGUIStateInvocation);

                    //Add code for restore state: edittext.setText(savedInstanceState.getString("MyEditTextId"));
                    MethodInvocation getBundleStateInvocation =
                            onResumeFoundMethod.getAST().newMethodInvocation();

                    SimpleName getName = onPauseFoundMethod.getAST().newSimpleName(getter);
                    getBundleStateInvocation.setName(getName);
                    SimpleName getExpr =
                            onPauseFoundMethod.getAST().newSimpleName(
                                    preferencesVarName.getIdentifier());
                    getBundleStateInvocation.setExpression(getExpr);
                    StringLiteral id = onPauseFoundMethod.getAST().newStringLiteral();
                    id.setLiteralValue(node.getNodeId());
                    getBundleStateInvocation.arguments().add(id);

                    StringLiteral defaultValue = onPauseFoundMethod.getAST().newStringLiteral();
                    defaultValue.setLiteralValue("");
                    getBundleStateInvocation.arguments().add(defaultValue);

                    insertSetMethod(onResumeFoundMethod, node, setMethodName,
                            getBundleStateInvocation);
                }
                else
                {
                    //Add code to save state: savedInstanceState.putBoolean("MyCheckboxId", checkbox.getEnabled());
                    //Add code to restore state: checkbox.setEnabled(savedInstanceState.getBoolean("MyCheckbox"));

                    insertSaveRestoreCode(onPauseFoundMethod, onResumeFoundMethod,
                            editorVarName.getIdentifier(), preferencesVarName.getIdentifier(),
                            node, putMethodName, setMethodName, getter, getUIStateMethodName);
                }

            }
            subMonitor.worked(1);
        }

        if (alreadyFoundItemToSaveState)
        {
            //Add code: editor.commit();
            invokeMethod(onPauseFoundMethod, EDITOR, COMMIT);
        }
    }

    /**
     * @param node layout node being analyzed
     * @return true if it is in the array of saveStateNodeTypes (the current supported view items to save/restore state), false otherwise
     */
    public static boolean canGenerateSaveStateCode(LayoutNode node)
    {
        boolean canSaveCode = false;

        if ((node != null) && (node.getNodeType() != null))
        {
            int i = 0;
            while (!canSaveCode && (i < saveStateNodeTypes.length))
            {
                if (node.getNodeType().equals(saveStateNodeTypes[i].getNodeType()))
                {
                    canSaveCode = true;
                }
                i++;
            }
        }

        return canSaveCode;
    }

    /**
     * Inserts SharedPreferences.Editor statement into onPause method
     * @param onPauseFoundMethod
     * @param preferencesVarName
     * @return
     */
    @SuppressWarnings("unchecked")
    public SimpleName insertPreferencesEditor(MethodDeclaration onPauseFoundMethod,
            SimpleName preferencesVarName)
    {
        SimpleName editorVarName = onPauseFoundMethod.getAST().newSimpleName(EDITOR);
        boolean alreadyAddedVariable = false;
        if (onPauseFoundMethod.getBody() != null)
        {
            outer: for (Object s : onPauseFoundMethod.getBody().statements())
            {
                if (s instanceof VariableDeclarationStatement)
                {
                    VariableDeclarationStatement variableDeclarationStatement =
                            (VariableDeclarationStatement) s;
                    if (variableDeclarationStatement.getType().toString()
                            .equals(SHARED_PREFERENCES + "." + EDITOR_CAPITAL_LETTER))
                    {
                        for (Object f : variableDeclarationStatement.fragments())
                        {
                            VariableDeclarationFragment frag = (VariableDeclarationFragment) f;
                            if (frag.getName().toString().equals(EDITOR))
                            {
                                alreadyAddedVariable = true;
                                break outer;
                            }
                        }
                    }
                }
            }
        }
        if (!alreadyAddedVariable)
        {
            VariableDeclarationFragment getEditorPreference =
                    onPauseFoundMethod.getAST().newVariableDeclarationFragment();
            MethodInvocation getEditorInvoke = onPauseFoundMethod.getAST().newMethodInvocation();
            SimpleName editInvokeName = onPauseFoundMethod.getAST().newSimpleName(EDIT);
            getEditorInvoke.setName(editInvokeName);
            SimpleName editInvokeVariable =
                    onPauseFoundMethod.getAST().newSimpleName(preferencesVarName.getIdentifier());
            getEditorInvoke.setExpression(editInvokeVariable);
            getEditorPreference.setInitializer(getEditorInvoke);

            getEditorPreference.setName(editorVarName);
            VariableDeclarationStatement getEditorVariableDeclarationStatement =
                    onPauseFoundMethod.getAST()
                            .newVariableDeclarationStatement(getEditorPreference);
            SimpleName sharedPreferencesName1 =
                    onPauseFoundMethod.getAST().newSimpleName(SHARED_PREFERENCES);
            SimpleType type = onPauseFoundMethod.getAST().newSimpleType(sharedPreferencesName1);
            SimpleName sharedPreferencesName2 =
                    onPauseFoundMethod.getAST().newSimpleName(EDITOR_CAPITAL_LETTER);
            QualifiedType editorPreferencesType =
                    onPauseFoundMethod.getAST().newQualifiedType(type, sharedPreferencesName2);
            getEditorVariableDeclarationStatement.setType(editorPreferencesType);
            onPauseFoundMethod.getBody().statements().add(getEditorVariableDeclarationStatement);
        }
        return editorVarName;
    }

    /**
     * Creates onResume method if it does not exist yet
     * @return {@link MethodDeclaration} for void onResume() method
     */
    @SuppressWarnings("unchecked")
    public MethodDeclaration insertOnResume()
    {
        //Add protected void onResume()
        ModifierKeyword keyword = ModifierKeyword.PUBLIC_KEYWORD;
        if (getCodeGeneratorData().getAssociatedType().equals(
                CodeGeneratorDataBasedOnLayout.TYPE.FRAGMENT))
        {
            keyword = ModifierKeyword.PUBLIC_KEYWORD;
        }
        else if (getCodeGeneratorData().getAssociatedType().equals(
                CodeGeneratorDataBasedOnLayout.TYPE.ACTIVITY))
        {
            keyword = ModifierKeyword.PROTECTED_KEYWORD;
        }
        MethodDeclaration onResumeMethodDeclaration =
                addMethodDeclaration(keyword, ON_RESUME, PrimitiveType.VOID,
                        new ArrayList<SingleVariableDeclaration>());
        Block onResumeMethodBlock = onCreateDeclaration.getAST().newBlock();
        MethodDeclaration onResumeFoundMethod =
                isMethodAlreadyDeclared(onResumeMethodDeclaration, PROTECTED_VOID_ON_RESUME);
        if (onResumeFoundMethod == null)
        {
            //add method onRestore if it does not exist yet
            onResumeMethodDeclaration.setBody(onResumeMethodBlock);
            typeDeclaration.bodyDeclarations().add(onResumeMethodDeclaration);
            onResumeFoundMethod = onResumeMethodDeclaration;
        }
        return onResumeFoundMethod;
    }

    /**
     * Creates onPause method if it does not exist yet
     * @return {@link MethodDeclaration} for void onPause() method
     */
    @SuppressWarnings("unchecked")
    public MethodDeclaration insertOnPause()
    {
        //Add protected void onPause() 
        ModifierKeyword keyword = ModifierKeyword.PUBLIC_KEYWORD;
        if (getCodeGeneratorData().getAssociatedType().equals(
                CodeGeneratorDataBasedOnLayout.TYPE.FRAGMENT))
        {
            keyword = ModifierKeyword.PUBLIC_KEYWORD;
        }
        else if (getCodeGeneratorData().getAssociatedType().equals(
                CodeGeneratorDataBasedOnLayout.TYPE.ACTIVITY))
        {
            keyword = ModifierKeyword.PROTECTED_KEYWORD;
        }
        MethodDeclaration onPauseMethodDeclaration =
                addMethodDeclaration(keyword, ON_PAUSE, PrimitiveType.VOID,
                        new ArrayList<SingleVariableDeclaration>());
        Block onPauseMethodBlock = onCreateDeclaration.getAST().newBlock();
        MethodDeclaration onPauseFoundMethod =
                isMethodAlreadyDeclared(onPauseMethodDeclaration, PROTECTED_VOID_ON_PAUSE);
        if (onPauseFoundMethod == null)
        {
            //add method onPause if it does not exist yet         
            onPauseMethodDeclaration.setBody(onPauseMethodBlock);
            typeDeclaration.bodyDeclarations().add(onPauseMethodDeclaration);
            onPauseFoundMethod = onPauseMethodDeclaration;
        }
        return onPauseFoundMethod;
    }

    /**
     * @param method
     * @return {@link SimpleName} for the variable with SharedPreferences type inside the method body
     */
    @SuppressWarnings("unchecked")
    public SimpleName getPreferenceVariable(MethodDeclaration method)
    {
        SimpleName preferencesVarName = method.getAST().newSimpleName(PREFERENCES);
        boolean alreadyAddedVariable = false;
        if (method.getBody() != null)
        {
            outer: for (Object s : method.getBody().statements())
            {
                if (s instanceof VariableDeclarationStatement)
                {
                    VariableDeclarationStatement variableDeclarationStatement =
                            (VariableDeclarationStatement) s;
                    if (variableDeclarationStatement.getType().toString()
                            .equals(SHARED_PREFERENCES))
                    {
                        for (Object f : variableDeclarationStatement.fragments())
                        {
                            VariableDeclarationFragment frag = (VariableDeclarationFragment) f;
                            if (frag.getName().toString().equals(PREFERENCES))
                            {
                                alreadyAddedVariable = true;
                                break outer;
                            }
                        }
                    }
                }
            }
        }
        if (!alreadyAddedVariable)
        {
            VariableDeclarationFragment getPreferencefragment =
                    method.getAST().newVariableDeclarationFragment();
            MethodInvocation invoke = method.getAST().newMethodInvocation();
            SimpleName invokeName = method.getAST().newSimpleName(GET_PREFERENCES);
            invoke.setName(invokeName);
            if (getCodeGeneratorData().getAssociatedType().equals(
                    CodeGeneratorDataBasedOnLayout.TYPE.ACTIVITY))
            {
                SimpleName invokeMode = method.getAST().newSimpleName(MODE_PRIVATE);
                invoke.arguments().add(invokeMode);
            }
            else if (getCodeGeneratorData().getAssociatedType().equals(
                    CodeGeneratorDataBasedOnLayout.TYPE.FRAGMENT))
            {
                SimpleName invokeMode = method.getAST().newSimpleName(MODE_PRIVATE);
                SimpleName activityRef = method.getAST().newSimpleName("Activity");
                QualifiedName qName = method.getAST().newQualifiedName(activityRef, invokeMode);

                MethodInvocation activityInvoke = method.getAST().newMethodInvocation();
                SimpleName activityMethodName = method.getAST().newSimpleName("getActivity");
                activityInvoke.setName(activityMethodName);
                invoke.setExpression(activityInvoke);

                invoke.arguments().add(qName);
            }

            getPreferencefragment.setInitializer(invoke);
            getPreferencefragment.setName(preferencesVarName);
            VariableDeclarationStatement variableDeclarationStatement =
                    method.getAST().newVariableDeclarationStatement(getPreferencefragment);
            SimpleName sharedPreferencesName = method.getAST().newSimpleName(SHARED_PREFERENCES);
            SimpleType sharedPreferencesType = method.getAST().newSimpleType(sharedPreferencesName);
            variableDeclarationStatement.setType(sharedPreferencesType);
            method.getBody().statements().add(variableDeclarationStatement);
        }
        return preferencesVarName;
    }

    /**
     * Add code to save state: $bundleNameOnSaveMethod.$putMethodName("$nodeId", $nodeId.$getUIStateMethodName());
     * Add code to restore state: $nodeId.$setMethodName($bundleNameOnRestoreMethod.$getBundleState("$nodeId"));
     */
    @SuppressWarnings("unchecked")
    public void insertSaveRestoreCode(MethodDeclaration onSaveInstanceStateFoundMethod,
            MethodDeclaration onRestoreInstanceFoundMethod, String bundleNameOnSaveMethod,
            String bundleNameOnRestoreMethod, LayoutNode node, String putMethodName,
            String setMethodName, String getBundleState, String getUIStateMethodName)
    {
        MethodInvocation getGUIStateInvocation;
        getGUIStateInvocation =
                addMethodToRetrieveUIState(onSaveInstanceStateFoundMethod, node,
                        getUIStateMethodName);

        insertPutMethod(onSaveInstanceStateFoundMethod, node, putMethodName,
                bundleNameOnSaveMethod, getGUIStateInvocation);

        MethodInvocation getBundleStateInvocation =
                onRestoreInstanceFoundMethod.getAST().newMethodInvocation();

        SimpleName getName = onSaveInstanceStateFoundMethod.getAST().newSimpleName(getBundleState);
        getBundleStateInvocation.setName(getName);
        SimpleName getExpr =
                onSaveInstanceStateFoundMethod.getAST().newSimpleName(bundleNameOnRestoreMethod);
        getBundleStateInvocation.setExpression(getExpr);
        StringLiteral id = onSaveInstanceStateFoundMethod.getAST().newStringLiteral();
        id.setLiteralValue(node.getNodeId());
        getBundleStateInvocation.arguments().add(id);

        String stateType = node.getProperty(ViewProperties.ViewStateValueType);

        if (stateType != null)
        {
            if (stateType.equals(Integer.class.toString()))
            {
                NumberLiteral defaultValue =
                        onSaveInstanceStateFoundMethod.getAST().newNumberLiteral();
                getBundleStateInvocation.arguments().add(defaultValue);
            }
            else if (stateType.equals(Boolean.class.toString()))
            {
                BooleanLiteral defaultValue =
                        onSaveInstanceStateFoundMethod.getAST().newBooleanLiteral(false);
                getBundleStateInvocation.arguments().add(defaultValue);
            }
        }

        insertSetMethod(onRestoreInstanceFoundMethod, node, setMethodName, getBundleStateInvocation);
    }

    /**
     * Insert method in the format $nodeId.$getUIStateMethodName()
     * @param onSaveInstanceStateFoundMethod
     * @param node
     * @param getUIStateMethodName
     * @return
     */
    public MethodInvocation addMethodToRetrieveUIState(
            MethodDeclaration onSaveInstanceStateFoundMethod, LayoutNode node,
            String getUIStateMethodName)
    {
        MethodInvocation retrieveUIStateMethod =
                onSaveInstanceStateFoundMethod.getAST().newMethodInvocation();
        SimpleName guiId = onSaveInstanceStateFoundMethod.getAST().newSimpleName(node.getNodeId());
        retrieveUIStateMethod.setExpression(guiId);
        SimpleName getText =
                onSaveInstanceStateFoundMethod.getAST().newSimpleName(getUIStateMethodName);
        retrieveUIStateMethod.setName(getText);
        return retrieveUIStateMethod;
    }

    /**
     * Add method in the format savedInstanceState.putXXXX($nodeid, $getGUIStateInvocation);
     * @param onSaveInstanceStateFoundMethod
     * @param node
     * @param methodName
     * @param bundleName
     * @param getGUIStateInvocation
     */
    @SuppressWarnings("unchecked")
    public void insertPutMethod(MethodDeclaration onSaveInstanceStateFoundMethod, LayoutNode node,
            String methodName, String bundleName, MethodInvocation getGUIStateInvocation)
    {
        MethodInvocation putMethod = onSaveInstanceStateFoundMethod.getAST().newMethodInvocation();
        SimpleName putMethodName =
                onSaveInstanceStateFoundMethod.getAST().newSimpleName(methodName);
        putMethod.setName(putMethodName);
        SimpleName bundle = onSaveInstanceStateFoundMethod.getAST().newSimpleName(bundleName);
        putMethod.setExpression(bundle);
        StringLiteral id = onSaveInstanceStateFoundMethod.getAST().newStringLiteral();
        id.setLiteralValue(node.getNodeId());
        putMethod.arguments().add(id);

        putMethod.arguments().add(getGUIStateInvocation);

        ExpressionStatement exprSt =
                onSaveInstanceStateFoundMethod.getAST().newExpressionStatement(putMethod);
        int commitPosition =
                findMethodInvocation(onSaveInstanceStateFoundMethod.getBody().statements(), bundle,
                        "commit");
        onSaveInstanceStateFoundMethod.getBody().statements().add(commitPosition, exprSt);
    }

    private int findMethodInvocation(List<?> statements, final SimpleName bundle,
            final String methodName)
    {
        int position = -1;
        int i = 0;
        while ((i < statements.size()) && (position == -1))
        {
            Statement statement = (Statement) statements.get(i);
            if (statement instanceof ExpressionStatement)
            {
                ExpressionStatement expressionSt = (ExpressionStatement) statement;
                Expression expression = expressionSt.getExpression();
                if (expression instanceof MethodInvocation)
                {
                    MethodInvocation method = (MethodInvocation) expression;
                    if (method.getName().getIdentifier().equals(methodName)
                            && (method.getExpression() instanceof SimpleName))
                    {
                        SimpleName name = (SimpleName) method.getExpression();
                        if (name.getIdentifier().equals(bundle.getIdentifier()))
                        {
                            position = i;
                        }

                    }

                }

            }
            i++;
        }
        if (position == -1)
        {
            position = i;
        }

        return position;
    }

    /**
     * Add method in the format $nodeId.setXXXXX($getBundleStateInvocation);
     * @param onRestoreInstanceStateFoundMethod
     * @param node
     * @param methodName
     * @param bundleName
     * @param getBundleStateInvocation
     */
    @SuppressWarnings("unchecked")
    public void insertSetMethod(MethodDeclaration onRestoreInstanceStateFoundMethod,
            LayoutNode node, String methodName, MethodInvocation getBundleStateInvocation)
    {
        MethodInvocation setMethod =
                onRestoreInstanceStateFoundMethod.getAST().newMethodInvocation();
        SimpleName setMethodName =
                onRestoreInstanceStateFoundMethod.getAST().newSimpleName(methodName);
        setMethod.setName(setMethodName);
        SimpleName id = onRestoreInstanceStateFoundMethod.getAST().newSimpleName(node.getNodeId());
        setMethod.setExpression(id);

        //add in the end of the method
        setMethod.arguments().add(getBundleStateInvocation);

        ExpressionStatement exprSt =
                onRestoreInstanceStateFoundMethod.getAST().newExpressionStatement(setMethod);
        onRestoreInstanceStateFoundMethod.getBody().statements().add(exprSt);
    }
}