menu builders, translations, small fix for history

This commit is contained in:
Sergey Solovyev
2011-12-19 11:52:09 +04:00
parent 03cc14a097
commit 866553725b
15 changed files with 143 additions and 86 deletions

View File

@@ -6,6 +6,7 @@
package org.solovyev.android.view;
import android.content.Context;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -14,10 +15,10 @@ import org.jetbrains.annotations.Nullable;
* Date: 12/18/11
* Time: 1:30 PM
*/
public interface AMenu<T extends AMenuItem> {
public interface AMenu<T extends AMenuItem<D>, D> {
@Nullable
T itemAt(int i);
@NotNull
CharSequence[] getMenuCaptions();
CharSequence[] getMenuCaptions(@NotNull final Context context);
}

View File

@@ -0,0 +1,60 @@
package org.solovyev.android.view;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 12/19/11
* Time: 10:54 AM
*/
public class AMenuBuilder<T extends AMenuItem<D>, D>{
@NotNull
private final Context context;
@NotNull
private final AlertDialog.Builder menuBuilder;
@NotNull
private final AMenu<T, D> menu;
@NotNull
public static <T extends Enum & AMenuItem<D>, D> AMenuBuilder<T, D> newInstance(@NotNull Context context, @NotNull Class<T> enumClass) {
return new AMenuBuilder<T, D>(context, EnumMenu.<T, D>newInstance(enumClass));
}
@NotNull
public static <T extends AMenuItem<D>, D> AMenuBuilder<T, D> newInstance(@NotNull Context context, @NotNull AMenu<T, D> menu) {
return new AMenuBuilder<T, D>(context, menu);
}
private AMenuBuilder(@NotNull Context context, @NotNull AMenu<T, D> 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 AMenuItem<D> menuItem = menu.itemAt(item);
if (menuItem != null) {
menuItem.doAction(data, context);
}
}
});
return menuBuilder.create();
}
}

View File

@@ -17,7 +17,7 @@ import org.jetbrains.annotations.NotNull;
public interface AMenuItem<T> {
@NotNull
String getCaption();
String getCaption(@NotNull Context context);
void doAction(@NotNull T data, @NotNull Context context);
}

View File

@@ -6,6 +6,7 @@
package org.solovyev.android.view;
import android.content.Context;
import org.jetbrains.annotations.NotNull;
/**
@@ -13,13 +14,18 @@ import org.jetbrains.annotations.NotNull;
* Date: 12/18/11
* Time: 1:34 PM
*/
public class EnumMenu<T extends Enum & AMenuItem> implements AMenu<T> {
public class EnumMenu<T extends Enum & AMenuItem<D>, D> implements AMenu<T, D> {
@NotNull
private final AMenu<T> menu;
private final AMenu<T, D> menu;
public EnumMenu(Class<T> enumClass) {
this.menu = new MenuImpl<T>(enumClass.getEnumConstants());
@NotNull
public static <T extends Enum & AMenuItem<D>, D> AMenu<T, D> newInstance(@NotNull Class<T> enumClass) {
return new EnumMenu<T, D>(enumClass);
}
private EnumMenu(Class<T> enumClass) {
this.menu = MenuImpl.newInstance(enumClass.getEnumConstants());
}
@Override
@@ -29,7 +35,7 @@ public class EnumMenu<T extends Enum & AMenuItem> implements AMenu<T> {
@NotNull
@Override
public CharSequence[] getMenuCaptions() {
return this.menu.getMenuCaptions();
public CharSequence[] getMenuCaptions(@NotNull final Context context) {
return this.menu.getMenuCaptions(context);
}
}

View File

@@ -6,6 +6,7 @@
package org.solovyev.android.view;
import android.content.Context;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.utils.CollectionsUtils;
@@ -18,15 +19,25 @@ import java.util.List;
* Date: 12/18/11
* Time: 1:31 PM
*/
public class MenuImpl<T extends AMenuItem> implements AMenu<T> {
public class MenuImpl<T extends AMenuItem<D>, D> implements AMenu<T, D> {
private final List<T> menuItems = new ArrayList<T>();
public MenuImpl(T... menuItems) {
@NotNull
public static <T extends AMenuItem<D>, D> AMenu<T, D> newInstance(T... menuItems) {
return new MenuImpl<T, D>(menuItems);
}
@NotNull
public static <T extends AMenuItem<D>, D> AMenu<T, D> newInstance(@NotNull List<T> menuItems) {
return new MenuImpl<T, D>(menuItems);
}
private MenuImpl(T... menuItems) {
this(CollectionsUtils.asList(menuItems));
}
public MenuImpl(@NotNull List<T> menuItems) {
private MenuImpl(@NotNull List<T> menuItems) {
this.menuItems.addAll(menuItems);
}
@@ -42,10 +53,10 @@ public class MenuImpl<T extends AMenuItem> implements AMenu<T> {
@Override
@NotNull
public CharSequence[] getMenuCaptions() {
public CharSequence[] getMenuCaptions(@NotNull final Context context) {
final CharSequence[] result = new CharSequence[this.menuItems.size()];
for (int i = 0; i < this.menuItems.size(); i++) {
result[i] = this.menuItems.get(i).getCaption();
result[i] = this.menuItems.get(i).getCaption(context);
}
return result;
}