org.solovyev.android.menu.AMenuBuilder Maven / Gradle / Ivy
package org.solovyev.android.menu;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.ActivityDestroyerController;
import org.solovyev.android.DialogOnActivityDestroyedListener;
/**
* User: serso
* Date: 12/19/11
* Time: 10:54 AM
*/
public class AMenuBuilder, D>{
@NotNull
private final Context context;
@NotNull
private final AlertDialog.Builder menuBuilder;
@NotNull
private final AMenu menu;
@NotNull
public static , D> AMenuBuilder newInstance(@NotNull Context context, @NotNull Class enumClass) {
return new AMenuBuilder(context, EnumMenu.newInstance(enumClass));
}
@NotNull
public static , D> AMenuBuilder newInstance(@NotNull Context context, @NotNull AMenu menu) {
return new AMenuBuilder(context, menu);
}
private AMenuBuilder(@NotNull Context context, @NotNull AMenu menu) {
this.context = context;
this.menuBuilder = new AlertDialog.Builder(context);
this.menu = menu;
}
@NotNull
public AlertDialog.Builder getMenuBuilder() {
return menuBuilder;
}
@NotNull
public AlertDialog create(@NotNull final D data) {
menuBuilder.setItems(menu.getMenuCaptions(context), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
final LabeledMenuItem menuItem = menu.itemAt(item);
if (menuItem != null) {
menuItem.onClick(data, context);
}
}
});
final AlertDialog result = menuBuilder.create();
if ( context instanceof Activity) {
ActivityDestroyerController.getInstance().put((Activity)context, new DialogOnActivityDestroyedListener(result));
}
return result;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy