[Arakhnę-Dev] [431] * The labels of the properties are read from the android strings.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 431
Author:   galland
Date:     2013-04-27 14:59:58 +0200 (Sat, 27 Apr 2013)
Log Message:
-----------
* The labels of the properties are read from the android strings.

Modified Paths:
--------------
    trunk/ui/ui-android/src/main/java/org/arakhne/afc/ui/android/property/PropertyEditorView.java

Modified: trunk/ui/ui-android/src/main/java/org/arakhne/afc/ui/android/property/PropertyEditorView.java
===================================================================
--- trunk/ui/ui-android/src/main/java/org/arakhne/afc/ui/android/property/PropertyEditorView.java	2013-04-27 12:59:06 UTC (rev 430)
+++ trunk/ui/ui-android/src/main/java/org/arakhne/afc/ui/android/property/PropertyEditorView.java	2013-04-27 12:59:58 UTC (rev 431)
@@ -20,16 +20,37 @@
  */
 package org.arakhne.afc.ui.android.property;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
+import org.arakhne.afc.ui.android.R;
 import org.arakhne.afc.util.PropertyOwner;
 
 import android.content.Context;
+import android.content.res.Resources;
+import android.util.Log;
 import android.view.View;
 import android.widget.LinearLayout;
 
-/** Abstract implementation of a property editor inside a view. 
+/** Abstract implementation of a property editor inside a view.
+ * <p>
+ * This view create a set of fields in which the properties of
+ * an object can be edited. This implementation does not make
+ * any asssumption on the layout of the fields on the screen.
+ * <p>
+ * The labels of the properties, which are displayed on the screen,
+ * are retreived from the resources of the Android application.
+ * Let say the property <code>myProp</code>. This view tries
+ * to retreive the entry <code>string/property_label_myProp</code>
+ * in the resources. Because each entry in the resources is prefixed
+ * by a package name, a list of package names to consider must be provided.
+ * This list is given to the constructor of this view.
+ * In addition to this list, this view search in the Android application
+ * package (see {@link Context#getPackageName()}) and in the package of
+ * this ApkLib (see {@link R}).   
  * 
  * @author $Author: galland$
  * @version $Name$ $Revision$ $Date$
@@ -38,14 +59,38 @@
  */
 public abstract class PropertyEditorView extends LinearLayout {
 
+	private final List<String> packageNames;
 	private Collection<? extends PropertyOwner> editedObjects = null;
 	private boolean isEditable = true;
 	
-	/**
+	/** Create a view to edit properties with only the android
+	 * application package and this ApkLib package in the list
+	 * of package names.
+	 * 
 	 * @param context
 	 */
 	protected PropertyEditorView(Context context) {
+		this(context, new ArrayList<String>(2));
+	}
+
+	/** Create a view to edit properties with a list of package names.
+	 * <p>
+	 * The list of package names is used to retreive the labels of the
+	 * properties in the Android resources.
+	 * <p>
+	 * The application package is automatically added at the first position
+	 * of the given list of package names. The package of this apklib
+	 * is added at the end of the list.
+	 * 
+	 * @param context
+	 * @param packageNames is the list of the package names from which 
+	 * the label resources may be retreived.
+	 */
+	protected PropertyEditorView(Context context, List<String> packageNames) {
 		super(context);
+		this.packageNames = packageNames;
+		this.packageNames.add(R.class.getPackage().getName()); // Add the package of this apklib
+		this.packageNames.add(0, context.getPackageName()); // Force the use of the application package
 		View top = onCreateTopContentView();
 		if (top!=null) {
 			addView(top);
@@ -58,6 +103,35 @@
 				android.view.ViewGroup.LayoutParams.MATCH_PARENT);
 		setLayoutParams(layoutParams);
 	}
+	
+	/** Replies the label 
+	 * for the property with the given name.
+	 * <p>
+	 * This function search for a string in the Android resources
+	 * with the name <code>"property_"+propertyName</code>.
+	 * 
+	 * @param propertyName is the name of the property.
+	 * @return the label or <code>null</code>.
+	 */
+	protected String getPropertyLabel(String propertyName) {
+		Resources r = getResources();
+		String key = ":string/property_label_"+propertyName; //$NON-NLS-1$
+		int id = 0;
+		Iterator<String> packages = this.packageNames.iterator();
+		while (packages.hasNext() && id==0) {
+			id = r.getIdentifier(packages.next()+key, null, null);
+		}
+		String label = null;
+		if (id!=0) {
+			label = r.getString(id);
+		}
+		if (label==null || label.isEmpty()) {
+			Log.d(getContext().getApplicationInfo().className,
+					"Unable to retreive the resource"+key); //$NON-NLS-1$
+			return "?"+key; //$NON-NLS-1$
+		}
+		return label;
+	}
 
 	/** Replies if the fragment enables to edit the properties.
 	 * 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/