Method getSetMethod(Class<?> targetClass, Class<?> fieldType, String fieldName) { fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); String methodname = "set" + fieldName; Method method = null; try { method = targetClass.getMethod(methodname, new Class<?>[] {fieldType}); } catch (Exception e) { playerObjects.getLogFile().WriteLine(Formatting.exceptionToStackTrace(e)); } return method; }
Method getGetMethod(Class<?> targetClass, Class<?> fieldType, String fieldName) { fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); String methodname = "get" + fieldName; if (fieldType == boolean.class || fieldType == Boolean.class) { methodname = "is" + fieldName; } Method method = null; try { method = targetClass.getMethod(methodname, new Class<?>[0]); } catch (Exception e) { playerObjects.getLogFile().WriteLine(Formatting.exceptionToStackTrace(e)); } return method; }
void buildConfigPanel() { try { Config config = playerObjects.getConfig(); for (Field field : config.getClass().getDeclaredFields()) { Annotation excludeAnnotation = field.getAnnotation(ReflectionHelper.Exclude.class); if (excludeAnnotation == null) { // so, this field is not excluded Class<?> fieldType = field.getType(); Method getMethod = getGetMethod(config.getClass(), field.getType(), field.getName()); if (getMethod != null) { Object value = getMethod.invoke(config); if (fieldType == String.class) { addTextBox(field.getName(), (String) value); } if (fieldType == boolean.class || fieldType == Boolean.class) { addBooleanComponent(field.getName(), (Boolean) value); } if (fieldType == float.class || fieldType == Float.class) { addTextBox(field.getName(), "" + value); } if (fieldType == int.class || fieldType == Integer.class) { addTextBox(field.getName(), "" + value); } } else { playerObjects .getLogFile() .WriteLine("No get accessor method for config field " + field.getName()); } } } } catch (Exception e) { playerObjects.getLogFile().WriteLine(Formatting.exceptionToStackTrace(e)); } configGridLayout.setRows(configGridLayout.getRows() + 2); configRevertButton = new JButton("Revert"); configReloadButton = new JButton("Reload"); configApplyButton = new JButton("Apply"); configSaveButton = new JButton("Save"); configRevertButton.addActionListener(new ConfigRevert()); configReloadButton.addActionListener(new ConfigReload()); configApplyButton.addActionListener(new ConfigApply()); configSaveButton.addActionListener(new ConfigSave()); configPanel.add(configRevertButton); configPanel.add(configReloadButton); configPanel.add(configApplyButton); configPanel.add(configSaveButton); }
void revertConfig() { try { debug("reverting config panel"); Config config = playerObjects.getConfig(); for (Field field : config.getClass().getDeclaredFields()) { Annotation excludeAnnotation = field.getAnnotation(ReflectionHelper.Exclude.class); if (excludeAnnotation == null) { // so, this field is not excluded debug("field " + field.getName()); Class<?> fieldType = field.getType(); Method getMethod = getGetMethod(config.getClass(), field.getType(), field.getName()); if (getMethod != null) { debug(" ... found accessor method"); Object value = getMethod.invoke(config); String fieldname = field.getName(); Component component = componentByName.get(fieldname); if (component != null) { debug(" ... found component"); if (fieldType == String.class) { ((JTextField) component).setText((String) value); } if (fieldType == boolean.class || fieldType == Boolean.class) { ((JCheckBox) component).setSelected((Boolean) value); } if (fieldType == float.class || fieldType == Float.class) { ((JTextField) component).setText("" + value); } if (fieldType == int.class || fieldType == Integer.class) { ((JTextField) component).setText("" + value); } } } else { playerObjects .getLogFile() .WriteLine("No get accessor method for config field " + field.getName()); } } } } catch (Exception e) { playerObjects.getLogFile().WriteLine(Formatting.exceptionToStackTrace(e)); } }
void applyConfig() { debug("applying config from panel"); Config config = playerObjects.getConfig(); for (Field field : config.getClass().getDeclaredFields()) { Annotation excludeAnnotation = field.getAnnotation(ReflectionHelper.Exclude.class); if (excludeAnnotation == null) { // so, this field is not excluded debug("field " + field.getName()); Class<?> fieldType = field.getType(); Method setMethod = getSetMethod(config.getClass(), field.getType(), field.getName()); if (setMethod != null) { debug(" ... found accessor method"); String fieldname = field.getName(); Component component = componentByName.get(fieldname); if (component != null) { debug(" ... found component"); Object value = null; if (fieldType == String.class) { value = ((JTextField) component).getText(); } if (fieldType == boolean.class || fieldType == Boolean.class) { value = ((JCheckBox) component).isSelected(); } if (fieldType == float.class || fieldType == Float.class) { String stringvalue = (String) ((JTextField) component).getText(); try { value = Float.parseFloat(stringvalue); } catch (Exception e) { } } if (fieldType == int.class || fieldType == Integer.class) { String stringvalue = (String) ((JTextField) component).getText(); try { value = Integer.parseInt(stringvalue); } catch (Exception e) { } } if (value != null) { try { setMethod.invoke(config, value); } catch (Exception e) { playerObjects.getLogFile().WriteLine(Formatting.exceptionToStackTrace(e)); } } } if (fieldType == boolean.class || fieldType == Boolean.class) { // addBooleanComponent( field.getName(), (Boolean)value ); } } else { playerObjects .getLogFile() .WriteLine("No get accessor method for config field " + field.getName()); } } } revertConfig(); // in case some parses and stuff didn't work, so // user can see what is actually being read. playerObjects .getMainUI() .showInfo( "Config updated. Note that most changes require an AI restart. You can click on 'reloadAI' in 'Actions' tab to do so."); playerObjects.getConfig().configUpdated(); }