Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.kubatatami.judonetworking.observers;
  
  import  android.content.Context;
  import  android.util.Pair;
  import  android.view.LayoutInflater;
  import  android.view.View;
  import  android.view.ViewGroup;
  import  android.widget.TextView;
  
 
 import java.util.List;

Created with IntelliJ IDEA. User: jbogacki Date: 25.10.2013 Time: 10:15 To change this template use File | Settings | File Templates.
 
 public class ObserverAdapterHelper {
 
     protected Context context;
     protected LayoutInflater layoutInflater;
     private static final String splitter = "\\.";
 
     public ObserverAdapterHelper(Context context) {
         this. = context;
         this. = LayoutInflater.from(context);
     }
 
     protected static class DataSourceOrTarget {
         protected Context context;
         protected Field field;
         protected Method method;
         protected List<Fieldfields;
 
         public DataSourceOrTarget(Context contextList<FieldfieldsField field) {
             this. = context;
             this. = field;
             this. = fields;
         }
 
         public DataSourceOrTarget(Context contextList<FieldfieldsMethod method) {
             this. = context;
             this. = method;
             this. = fields;
         }
 
         public boolean isSource() {
             return  != null || !.getReturnType().equals(.);
         }
 
         protected Object findObject(Object itemthrows IllegalAccessException {
             for (Field field : ) {
                 item = field.get(item);
             }
             return item;
         }
 
         public String getValue(Object item) {
             try {
                 String result;
                 item = findObject(item);
                 if ( != null) {
                     result = .get(item).toString();
                 } else if (.getParameterTypes().length == 1) {
                     result = .invoke(item).toString();
                 } else {
                     result = .invoke(item).toString();
                 }
 
                 return result;
 
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }
         }
 
         public void setValue(Object item, View view) {
             try {
                 item = findObject(item);
                 .invoke(itemview);
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }
         }
 
     }
 
     public View getView(int layoutObject item, View convertView, ViewGroup parent) {
         return getView(layoutitemconvertViewparentnull);
     }
 
    public static boolean isInnerClass(Class<?> clazz) {
        return clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers());
    }
    public View getView(int layout, View convertView, ViewGroup parentClass<?> holderClass) {
        return getView(layoutnullconvertViewparentholderClass);
    }
    public View getView(int layout, View convertView, ViewGroup parent) {
        return getView(layoutnullconvertViewparentnull);
    }
    @SuppressWarnings("unchecked")
    public View getView(int layoutObject item, View convertView, ViewGroup parentClass<?> holderClass) {
        try {
            List<Pair<View, DataSourceOrTarget>> dataSources;
            if (convertView == null) {
                convertView = .inflate(layoutparentfalse);
                dataSources = new ArrayList<Pair<View, DataSourceOrTarget>>();
                if (item != null) {
                    findViewTag(convertViewdataSourcesitem.getClass());
                }
                convertView.setTag(layoutdataSources);
                if (holderClass != null) {
                    if (isInnerClass(holderClass)) {
                        throw new JudoException("Inner holder class must be static!");
                    }
                    Constructor<?> constructor = holderClass.getDeclaredConstructors()[0];
                    constructor.setAccessible(true);
                    Object holder = constructor.newInstance();
                    for (Field field : holderClass.getDeclaredFields()) {
                        HolderView viewById = field.getAnnotation(HolderView.class);
                        if (viewById != null) {
                            field.setAccessible(true);
                            field.set(holderconvertView.findViewById(viewById.value()));
                        }
                    }
                    convertView.setTag(holder);
                }
            } else {
                dataSources = (List<Pair<View, DataSourceOrTarget>>) convertView.getTag(layout);
            }
            for (Pair<View, DataSourceOrTargetpair : dataSources) {
                if (pair.second.isSource()) {
                    ((TextView) pair.first).setText(pair.second.getValue(item));
                } else {
                    pair.second.setValue(itempair.first);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return convertView;
    }
    private void findViewTag(View viewList<Pair<View, DataSourceOrTarget>> dataClass<?> itemClassthrows JudoException {
        if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;
            for (int i = 0; i < group.getChildCount(); i++) {
                View viewElem = group.getChildAt(i);
                findViewTag(viewElemdataitemClass);
            }
        } else {
            linkViewTag(viewdataitemClass);
        }
    }
    @SuppressWarnings("unchecked")
    private void linkViewTag(final View viewList<Pair<View, DataSourceOrTarget>> dataClass<?> itemClassthrows JudoException {
        if (view.getTag() != null && view.getTag() instanceof String) {
            String tag = (Stringview.getTag();
            if (tag.matches("\\[.*\\]")) {
                tag = tag.substring(2, tag.length() - 1);
                DataSourceOrTarget dataSourceOrTarget = getDataSource(tagitemClass);
                if (dataSourceOrTarget.isSource() && !(view instanceof TextView)) {
                    throw new JudoException("Method which returns value must be link with TextView");
                }
                data.add(new Pair<View, DataSourceOrTarget>(viewdataSourceOrTarget));
            }
        }
    }
    private DataSourceOrTarget getDataSource(String fieldNameClass<?> clazz) {
        int i = 0;
        Field field;
        List<Fieldfields = new ArrayList<Field>();
        String parts[] = fieldName.split();
        for (String part : parts) {
            i++;
            if (i != parts.length) {
                try {
                    field = getField(partclazz);
                    clazz = field.getType();
                    fields.add(field);
                } catch (NoSuchFieldException e) {
                    throw new RuntimeException(e);
                }
            } else {
                try {
                    field = getField(partclazz);
                    return new DataSourceOrTarget(fieldsfield);
                } catch (NoSuchFieldException e) {
                    try {
                        Method method = getMethod(partclazz);
                        return new DataSourceOrTarget(fieldsmethod);
                    } catch (NoSuchFieldException e1) {
                        throw new RuntimeException(e1);
                    }
                }
            }
        }
        return null;
    }
    private static Field getField(String fieldNameClass<?> objectClassthrows NoSuchFieldException {
        Field field = null;
        while (objectClass != null && field == null) {
            try {
                field = objectClass.getDeclaredField(fieldName);
            } catch (NoSuchFieldException e) {
                objectClass = objectClass.getSuperclass();
            }
        }
        if (field != null) {
            field.setAccessible(true);
        } else {
            throw new NoSuchFieldException(fieldName);
        }
        return field;
    }
    static Method getMethod(String fieldNameClass<?> objectClassthrows NoSuchFieldException {
        Method finalMethod = null;
        while (objectClass != null && finalMethod == null) {
            for (Method method : objectClass.getDeclaredMethods()) {
                if (method.getName().equals(fieldName)) {
                    Class<?>[] paramsType = method.getParameterTypes();
                    if (paramsType.length == 0) {
                        finalMethod = method;
                        break;
                    } else if (paramsType.length == 1) {
                        if (paramsType[0].equals(Context.class) || View.class.isAssignableFrom(paramsType[0])) {
                            finalMethod = method;
                            break;
                        }
                    }
                }
            }
            if (finalMethod == null) {
                objectClass = objectClass.getSuperclass();
            }
        }
        if (finalMethod == null) {
            throw new NoSuchFieldException(fieldName);
        }
        return finalMethod;
    }
New to GrepCode? Check out our FAQ X