Example usage for com.badlogic.gdx.utils.reflect Field getName

List of usage examples for com.badlogic.gdx.utils.reflect Field getName

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils.reflect Field getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the field.

Usage

From source file:com.mygdx.game.ui.FloatSettingsMenu.java

License:Apache License

public FloatSettingsMenu(String buttonText, Skin skin, Class<?> floatSettingsClass) {
    final Table innerTable = new Table();

    Field[] fields = ClassReflection.getFields(floatSettingsClass);
    for (final Field field : fields) {
        final Label fieldName = new Label(field.getName(), skin);
        float fieldValueFloat = 0;
        try {/*  w w w . j a  v a  2  s .  co  m*/
            fieldValueFloat = (Float) field.get(field);
        } catch (Exception e) {
            Gdx.app.debug(TAG, "Cannot parse float value for " + field.getName());
        }
        final TextField fieldValue = new TextField(String.valueOf(fieldValueFloat), skin);
        innerTable.add(fieldName).fillX();
        innerTable.row();
        innerTable.add(fieldValue).fillX();
        innerTable.row();
        fieldValue.addListener(new InputListener() {
            @Override
            public boolean keyTyped(InputEvent event, char character) {
                String userInput = fieldValue.getText();
                float newFieldValue;
                try {
                    newFieldValue = Float.parseFloat(userInput);
                } catch (NumberFormatException e) {
                    return true;
                }
                try {
                    field.set(field, (Float) newFieldValue);
                } catch (ReflectionException e) {
                    Gdx.app.debug(TAG, "Cannot set value for " + field.getName());
                }
                return true;
            }
        });
    }
    innerTable.setVisible(false);
    final TextButton button = new TextButton(buttonText, skin);
    button.addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            innerTable.setVisible(!innerTable.isVisible());
            return true;
        }
    });
    add(innerTable).fillX();
    row();
    add(button).fillX();
    row();
}

From source file:es.eucm.ead.editor.view.controllers.ClassOptionsController.java

License:Open Source License

public ClassOptionsController(Controller controller, Skin skin, Class<T> reflectedClass, String i18nPrefix,
        Array<String> ignoreFields) {
    super(controller, skin);
    this.clazz = reflectedClass;
    i18nPrefix(i18nPrefix + ClassReflection.getSimpleName(clazz).toLowerCase());

    Class clazz = reflectedClass;
    while (clazz != null) {
        for (java.lang.reflect.Field field : clazz.getDeclaredFields()) {
            String fieldName = field.getName();
            if (ignoreFields != null && ignoreFields.contains(fieldName, false)) {
                continue;
            }//from ww  w.  ja  v  a  2  s .co m
            if (field.isAnnotationPresent(Search.class)) {
                Search search = field.getAnnotation(Search.class);
                try {
                    FuzzyIndex index = controller.getIndex(ClassReflection.forName(search.index()));
                    search(fieldName, index);
                } catch (ReflectionException e) {
                    Gdx.app.error("ClassOptionsController", "No class for " + search.index());
                }
            } else if (field.isAnnotationPresent(Text.class)) {
                Text text = field.getAnnotation(Text.class);
                text(fieldName, text.lines());
            } else if (field.isAnnotationPresent(Fixed.class)) {
                fixed(fieldName);
            } else if (field.isAnnotationPresent(File.class)) {
                File file = field.getAnnotation(File.class);
                file(fieldName).folder(file.folder()).mustExist(file.mustExist());
            } else if (field.getType() == Integer.class || field.getType() == int.class) {
                this.intNumber(fieldName);
            } else if (field.getType() == Float.class || field.getType() == float.class) {
                floatNumber(fieldName);
            } else if (field.getType() == String.class) {
                string(fieldName);
            } else if (field.getType() == Boolean.class || field.getType() == boolean.class) {
                bool(fieldName);
            } else if (field.getType().isEnum()) {
                Map<String, Object> values = new LinkedHashMap<String, Object>();
                for (Object o : field.getType().getEnumConstants()) {
                    values.put(o.toString(), o);
                }
                select(fieldName, values);
            }
        }
        clazz = clazz.getSuperclass();
    }
}

From source file:es.eucm.ead.editor.view.controllers.ClassOptionsController.java

License:Open Source License

/**
 * Reads the object values and updates all options accordingly
 *///www . j av  a 2  s .c om
public void read(T object) {
    if (this.object != null) {
        controller.getModel().removeListener(this.object, fieldListener);
    }

    this.object = object;
    controller.getModel().addFieldListener(this.object, fieldListener);

    try {
        Class clazz = this.clazz;
        while (clazz != null) {
            for (Field field : ClassReflection.getDeclaredFields(clazz)) {
                field.setAccessible(true);
                String fieldName = field.getName();
                Object value = field.get(object);
                setValue(fieldName, value);
            }
            clazz = clazz.getSuperclass();
        }
    } catch (ReflectionException e) {
        Gdx.app.error("ClassOptionsController", "Error", e);
    }
}

From source file:es.eucm.ead.editor.control.repo.RepoRequestFactory.java

License:Open Source License

private String appendParameters(String serviceUrl, Class clazz, RepoRequest request) {
    String url = serviceUrl;//from   w w  w  .  j av a2 s.  com
    for (Field field : ClassReflection.getDeclaredFields(clazz)) {
        field.setAccessible(true);
        try {
            String name = field.getName();
            String value = field.get(request) == null ? null : "" + field.get(request);
            if (value == null) {
                continue;
            }
            try {
                value = URLEncoder.encode(value, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                Gdx.app.debug(LOG_TAG,
                        "Error occurred while encoding parameter " + name + " with value " + value);
            }
            if (url.endsWith("?")) {
                url += name + "=" + value;
            } else {
                url += "&" + name + "=" + value;
            }
        } catch (ReflectionException e) {
            e.printStackTrace();
        }
    }

    if (clazz.getSuperclass() != null && clazz != RepoRequest.class) {
        return appendParameters(url, clazz.getSuperclass(), request);
    }

    return url;
}

From source file:com.mygdx.game.ui.BooleanSettingsMenu.java

License:Apache License

public BooleanSettingsMenu(String buttonText, Skin skin, Class<?> booleanSettingsClass) {
    final Table innerTable = new Table();

    Field[] fields = ClassReflection.getDeclaredFields(booleanSettingsClass);
    for (final Field field : fields) {
        boolean fieldValueBoolean = false;
        try {//ww w.  ja va  2s . c om
            fieldValueBoolean = (Boolean) field.get(field);
        } catch (Exception e) {
            Gdx.app.debug(TAG, "Cannot parse value for boolean " + field.getName());
        }
        final CheckBox checkBox = new CheckBox(field.getName(), skin);
        checkBox.setChecked(fieldValueBoolean);
        innerTable.add(checkBox).pad(1).align(Align.left);
        innerTable.row();
        checkBox.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                try {
                    field.set(field, (Boolean) checkBox.isChecked());
                } catch (ReflectionException e) {
                    Gdx.app.debug(TAG, "Cannot set value for " + field.getName());
                }
            }
        });
    }
    innerTable.setVisible(false);
    final TextButton button = new TextButton(buttonText, skin);
    button.addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            innerTable.setVisible(!innerTable.isVisible());
            return true;
        }
    });
    add(innerTable).fillX();
    row();
    add(button).fillX();
    row();
}

From source file:com.algodal.gdxscreen.utils.GdxPrefs.java

License:Apache License

private void put(Field field) {
    field.setAccessible(true); //allow private access, important unless the POJO has only public fields
    if (field.getType().equals(Integer.class))
        prefs.putInteger(field.getName(),
                debug.assertNoException("get integer", getValue(field, Integer.class)));
    if (field.getType().equals(Short.class))
        prefs.putInteger(field.getName(), debug.assertNoException("get integer", getValue(field, Short.class)));
    if (field.getType().equals(int.class))
        prefs.putInteger(field.getName(), debug.assertNoException("get integer", getValue(field, int.class)));
    if (field.getType().equals(short.class))
        prefs.putInteger(field.getName(), debug.assertNoException("get integer", getValue(field, short.class)));
    else if (field.getType().equals(Long.class))
        prefs.putLong(field.getName(), debug.assertNoException("get long", getValue(field, Long.class)));
    else if (field.getType().equals(long.class))
        prefs.putLong(field.getName(), debug.assertNoException("get long", getValue(field, long.class)));
    else if (field.getType().equals(Boolean.class))
        prefs.putBoolean(field.getName(),
                debug.assertNoException("get boolean", getValue(field, Boolean.class)));
    else if (field.getType().equals(boolean.class))
        prefs.putBoolean(field.getName(),
                debug.assertNoException("get boolean", getValue(field, boolean.class)));
    else if (field.getType().equals(Float.class) || field.getType().equals(Double.class)
            || field.getType().equals(float.class) || field.getType().equals(double.class))
        prefs.putFloat(field.getName(), debug.assertNoException("get float", getValue(field, Float.class)));
    else if (field.getType().equals(float.class))
        prefs.putFloat(field.getName(), debug.assertNoException("get float", getValue(field, float.class)));
    else if (field.getType().equals(String.class))
        prefs.putString(field.getName(), debug.assertNoException("get string", getValue(field, String.class)));
    if (field.getType().equals(Character.class) || field.getType().equals(char.class)
            || field.getType().equals(Byte.class) || field.getType().equals(byte.class))
        prefs.putString(field.getName(),
                debug.assertNoException("get string", getStringValueFromCharacter(field)));
}

From source file:com.ray3k.skincomposer.data.StyleData.java

License:Open Source License

private void newStyleProperties(Class clazz) {
    for (Field field : ClassReflection.getFields(clazz)) {
        StyleProperty styleProperty = new StyleProperty(field.getType(), field.getName(), true);
        properties.put(field.getName(), styleProperty);
    }//from  ww w  . j av  a  2  s  .  co  m
}

From source file:com.github.antag99.retinazer.WireCache.java

License:Open Source License

public WireCache(Engine engine, Class<?> type, WireResolver[] wireResolvers) {
    final List<Field> fields = new ArrayList<>();

    for (Class<?> current = type; current != Object.class; current = current.getSuperclass()) {
        for (Field field : ClassReflection.getDeclaredFields(current)) {
            if (field.getDeclaredAnnotation(Wire.class) == null) {
                continue;
            }/*  w w w.  j  ava2s .c o  m*/

            if (field.isStatic()) {
                throw new RetinazerException(
                        "Static fields can not be wired (" + current.getName() + "#" + field.getName() + ")");
            }

            if (field.isSynthetic()) {
                continue;
            }

            field.setAccessible(true);
            fields.add(field);
        }
    }

    this.engine = engine;
    this.fields = fields.toArray(new Field[fields.size()]);
    this.wireResolvers = wireResolvers;
}

From source file:com.algodal.gdxscreen.utils.GdxPrefs.java

License:Apache License

private void take(Field field) {
    field.setAccessible(true); //allow private access, important unless the POJO has only public fields
    if (field.getType().equals(Integer.class) || field.getType().equals(Short.class)
            || field.getType().equals(int.class) || field.getType().equals(short.class))
        setValue(field, prefs.getInteger(field.getName(), 0));
    else if (field.getType().equals(Long.class) || field.getType().equals(long.class))
        setValue(field, prefs.getLong(field.getName(), 0));
    else if (field.getType().equals(Boolean.class) || field.getType().equals(boolean.class))
        setValue(field, prefs.getBoolean(field.getName(), false));
    else if (field.getType().equals(Float.class) || field.getType().equals(Double.class)
            || field.getType().equals(float.class) || field.getType().equals(double.class))
        setValue(field, prefs.getFloat(field.getName(), 0.0f));
    else if (field.getType().equals(String.class))
        setValue(field, prefs.getString(field.getName(), null));
    if (field.getType().equals(Character.class) || field.getType().equals(char.class)
            || field.getType().equals(Byte.class) || field.getType().equals(byte.class))
        setStringValueFromCharacter(field, prefs.getString(field.getName(), null));
}

From source file:com.github.antag99.retinazer.WireCache.java

License:Open Source License

public void wire(Object object) {
    final WireResolver[] wireResolvers = this.wireResolvers;

    fields: for (final Field field : fields) {
        for (WireResolver wireResolver : wireResolvers) {
            try {
                if (wireResolver.wire(engine, object, field)) {
                    continue fields;
                }//from   ww w . j  ava 2  s  .c o  m
            } catch (Throwable ex) {
                throw Internal.sneakyThrow(ex);
            }
        }

        throw new RetinazerException(
                "Failed to wire field " + field.getName() + " of " + field.getDeclaringClass().getName()
                        + " of type " + field.getType().getName() + "; no resolver");
    }
}