Skip to content

How to use FXPreview for Code Generation

chqu1012 edited this page Jun 7, 2019 · 1 revision

Introduction

FXPreview is mainly used for the representation of the code generation. The emf model is required as a prerequisite. This view reacts to all view selection.

How to use the FXPreview

  1. Extend a class of FXPreview, e.g. in demo we
public class ExtendedFXPreview extends FXPreview {
	
	@Override
	public void changed(ObservableValue<? extends Object> obs, Object oldValue, Object newValue) {
		if (newValue instanceof Person) {
			Person p = (Person) newValue;
			try {
				String code = IOUtils.toString(new FileInputStream(new File("templates/PersonTemplate.tpl")), "UTF-8");
				Field[] declaredFields = p.getClass().getDeclaredFields();
				for (Field field : declaredFields) {
					field.setAccessible(true);
					code = code.replaceAll("%"+field.getName().toUpperCase()+"%", field.get(p).toString());
				}
				setContent(code);
			} catch (IOException | IllegalArgumentException | IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}
}
  1. Add the preview declarative to the workbench, it is import that the registrateChangeListener for this view should be activated. <views _Id="de.dc.javafx.xcore.workbench.ui.demo.ui.ExtendedFXPreview" name="Extended FXPreview" viewClass="de.dc.javafx.xcore.workbench.ui.demo.ui.ExtendedFXPreview" registrateChangeListener="true"/>