| [comment encoding = UTF-8 /] |
| [comment] |
| Copyright (c) 2010, 2012 Obeo |
| All rights reserved. This program and the accompanying materials |
| are made available under the terms of the Eclipse Public License 1.0 |
| |
| Any license can be applied to the files generated with this template |
| and Acceleo. |
| |
| author <a href="mailto:jonathan.musset@obeo.fr">Jonathan Musset</a> |
| [/comment] |
| [module edit('http://www.eclipse.org/acceleo/example/android')/] |
| |
| [template public edit(a : Activity)] |
| [file ('/src/android/activities/'.concat(a.name.concat('Edit.java')), false, 'Cp1252')] |
| /******************************************************************************* |
| * Copyright (c) 2006, 2007, 2008, 2009 Obeo. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Obeo - initial API and implementation |
| *******************************************************************************/ |
| package android.activities; |
| |
| import android.app.Activity; |
| import android.database.Cursor; |
| import android.os.Bundle; |
| import android.view.View; |
| import android.widget.Button; |
| import android.widget.EditText; |
| import android.widget.Spinner; |
| |
| /** |
| * [a.name.toUpperFirst()/]Activity. |
| * |
| * @author <a href="mailto:jonathan.musset@obeo.fr">Jonathan Musset</a> |
| */ |
| public class [a.name.toUpperFirst()/]Edit extends Activity { |
| |
| [for (w : Widget | widgets)] |
| [if (w.oclIsKindOf(Text))] |
| private EditText w[w.name.toUpperFirst()/]; |
| |
| [elseif (w.oclIsKindOf(Spinner))] |
| private Spinner w[w.name.toUpperFirst()/]; |
| |
| [elseif (w.oclIsKindOf(Button))] |
| private Button w[w.name.toUpperFirst()/]; |
| |
| [/if] |
| [/for] |
| |
| private Long rowId; |
| private [a.name.toUpperFirst()/]DbAdapter dbHelper; |
| |
| /** Called when the activity is first created. */ |
| @Override |
| public void onCreate(Bundle instanceState) { |
| super.onCreate(instanceState); |
| dbHelper = new [a.name.toUpperFirst()/]DbAdapter(this); |
| dbHelper.open(); |
| setContentView(R.layout.edit); |
| |
| rowId = instanceState != null ? instanceState.getLong([a.name.toUpperFirst()/]DbAdapter.KEY_ROWID) : null; |
| if (rowId == null) { |
| Bundle extras = getIntent().getExtras(); |
| rowId = extras != null ? extras.getLong([a.name.toUpperFirst()/]DbAdapter.KEY_ROWID) : null; |
| } |
| |
| initControls(); |
| |
| populateFields(); |
| } |
| |
| private void initControls() { |
| [for (w : Widget | widgets)] |
| [if (w.oclIsKindOf(Text))] |
| w[w.name.toUpperFirst()/] = (EditText) findViewById(R.id.w[w.name.toUpperFirst()/]); |
| |
| [elseif (w.oclIsKindOf(Spinner))] |
| w[w.name.toUpperFirst()/] = (Spinner) findViewById(R.id.w[w.name.toUpperFirst()/]); |
| |
| [elseif (w.oclIsKindOf(Button))] |
| w[w.name.toUpperFirst()/] = (Button) findViewById(R.id.w[w.name.toUpperFirst()/]); |
| w[w.name.toUpperFirst()/].setText("[w.name.toUpperFirst()/]"); |
| w[w.name.toUpperFirst()/].setOnClickListener(new Button.OnClickListener() { |
| public void onClick(View v) { |
| setResult(RESULT_OK); |
| finish(); |
| } |
| }); |
| |
| [/if] |
| [/for] |
| } |
| |
| private void populateFields() { |
| if (rowId != null) { |
| Cursor c = dbHelper.fetchNote(rowId); |
| startManagingCursor(c); |
| |
| [for (w : Widget | widgets)] |
| [if (w.oclIsKindOf(Text))] |
| w[w.name.toUpperFirst()/].setText(c.getString(c.getColumnIndexOrThrow([a.name.toUpperFirst()/]DbAdapter.KEY_[w.name.toUpper()/]))); |
| |
| [elseif (w.oclIsKindOf(Spinner))] |
| w[w.name.toUpperFirst()/].setSelection(c.getInt(c.getColumnIndexOrThrow([a.name.toUpperFirst()/]DbAdapter.KEY_[w.name.toUpper()/]))); |
| |
| [/if] |
| [/for] |
| } |
| } |
| |
| @Override |
| protected void onSaveInstanceState(Bundle outState) { |
| super.onSaveInstanceState(outState); |
| outState.putLong([a.name.toUpperFirst()/]DbAdapter.KEY_ROWID, rowId); |
| } |
| |
| @Override |
| protected void onPause() { |
| super.onPause(); |
| |
| [for (w : Widget | widgets)] |
| [if (w.oclIsKindOf(Text))] |
| String [w.name/] = w[w.name.toUpperFirst()/].getText().toString(); |
| |
| [elseif (w.oclIsKindOf(Spinner))] |
| int [w.name/] = w[w.name.toUpperFirst()/].getSelectedItemPosition(); |
| |
| [/if] |
| [/for] |
| |
| if (rowId == null) { |
| long id = dbHelper.create[a.name.toUpperFirst()/]([a.allWidgets()/]); |
| if (id > 0) { |
| rowId = id; |
| } |
| } else { |
| dbHelper.updateNote(rowId,[a.allWidgets()/]); |
| } |
| } |
| |
| @Override |
| protected void onResume() { |
| super.onResume(); |
| populateFields(); |
| } |
| |
| public String toString() { |
| StringBuilder result = new StringBuilder(); |
| |
| [for (w : Widget | widgets)] |
| [if (w.oclIsKindOf(Text))] |
| result.append(w[w.name.toUpperFirst()/].getText().toString()); |
| |
| [elseif (w.oclIsKindOf(Spinner))] |
| result.append(w[w.name.toUpperFirst()/].getSelectedItemPosition()); |
| |
| [elseif (w.oclIsKindOf(Button))] |
| // We don't display the "[w.name.toUpperFirst()/]" Button. |
| |
| [/if] |
| [/for] |
| |
| return result.toString(); |
| } |
| |
| } |
| [/file] |
| [/template] |
| |
| [template public allWidgets (a : Activity) ] |
| [a.widgets->select(not oclIsKindOf(widgets::Button)).oneWidget()->sep(',')/] |
| [/template] |
| |
| [template public oneWidget (widget : Widget)] |
| [widget.name/] |
| [/template] |
| |