Skip to content

Commit

Permalink
Merge pull request #133 from eclipse/issues_132
Browse files Browse the repository at this point in the history
Issue 132 - Functional interface for ValueListener and Generics on IBeanController
  • Loading branch information
gerring authored Jun 12, 2017
2 parents 31df12e + f242b6b commit c08f66e
Show file tree
Hide file tree
Showing 24 changed files with 98 additions and 42 deletions.
14 changes: 7 additions & 7 deletions org.eclipse.richbeans.api/.classpath
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
11 changes: 8 additions & 3 deletions org.eclipse.richbeans.api/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
org.eclipse.jdt.core.compiler.source=1.8
2 changes: 1 addition & 1 deletion org.eclipse.richbeans.api/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Bundle-Name: Api
Bundle-SymbolicName: org.eclipse.richbeans.api
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: Diamond Light Source
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: org.eclipse.richbeans.api,
org.eclipse.richbeans.api.beans,
org.eclipse.richbeans.api.binding,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @author Matthew Gerring
*
*/
public interface IBeanController {
public interface IBeanController<T> {

/**
* Add a value listener to any UI objects which change in the UIObject.
Expand Down Expand Up @@ -59,7 +59,7 @@ public interface IBeanController {
* Get the current bean directly (no copy is made, use with caution)
* @return
*/
Object getBean();
T getBean();

/**
* Returns the original UI object to which we are linking with reflection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface IBeanService {
* @param bean
* @return
*/
public IBeanController createController(Object ui, Object bean);
public <T> IBeanController<T> createController(Object ui, T bean);


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @author Matthew Gerring
*
*/
@FunctionalInterface
public interface ValueListener extends EventListener {

/**
Expand All @@ -41,7 +42,9 @@ public interface ValueListener extends EventListener {
*
* @return name
*/
public String getValueListenerName();
default String getValueListenerName() {
return null;
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
*
* @author Colin Palmer
*/
class BeanController implements IBeanController {
class BeanController<T> implements IBeanController<T> {

private final Object ui;
private final Object bean;
private final T bean;

/**
* Create a new BeanController with the given UI and bean objects. Note the
* correct order of the arguments!
*/
public BeanController(Object ui, Object bean) {
public BeanController(Object ui, T bean) {
if (ui == null) {
throw new NullPointerException("UI object must not be null");
}
Expand All @@ -57,7 +57,7 @@ public Object getUI() {
return ui;
}

public Object getBean() {
public T getBean() {
return bean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public static BeanService getInstance() {
}

@Override
public IBeanController createController(Object ui, Object bean) {
return new BeanController(ui, bean);
public <T> IBeanController<T> createController(Object ui, T bean) {
return new BeanController<>(ui, bean);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@

public class ExamplePrintBeanValueListener extends ValueAdapter {

private IBeanController controller;
private IBeanController<?> controller;
private Control value;
private int textLimit = -1;

/**
* @param controller
* @param value A control with a setText(...) method.
*/
public ExamplePrintBeanValueListener(IBeanController controller, Control value) {
public ExamplePrintBeanValueListener(IBeanController<?> controller, Control value) {

// The name should be unique and is only hard-coded here for simplicity - see the javadoc for ValueAdapter.
this.name = "Example listener";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Shell createShell(Display display) throws Exception {
bean.setY(5);

// Connect the UI and bean
final IBeanController controller = BeanService.getInstance().createController(ui, bean);
final IBeanController<SimpleBean> controller = BeanService.getInstance().createController(ui, bean);
controller.addValueListener(new ExamplePrintBeanValueListener(controller, value));
controller.beanToUI();
controller.switchState(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Shell createShell(Display display) throws Exception {
bean.addItem(new ExampleItem("Fred 1", 1, 2));

// Connect the UI and bean
final IBeanController controller = BeanService.getInstance().createController(ui, bean);
final IBeanController<ExampleParameters> controller = BeanService.getInstance().createController(ui, bean);
controller.addValueListener(new ExamplePrintBeanValueListener(controller, value));
controller.beanToUI();
controller.switchState(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Shell createShell(Display display) throws Exception {
bean.addItem(new ExampleItem("Item 2", 2, 3, ItemChoice.POLAR));

// Connect the UI and bean
final IBeanController controller = BeanService.getInstance().createController(ui, bean);
final IBeanController<ExampleBean> controller = BeanService.getInstance().createController(ui, bean);
controller.addValueListener(new ExamplePrintBeanValueListener(controller, value));
controller.beanToUI();
controller.switchState(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Shell createShell(Display display) throws Exception {
bean.addItem(new ExampleItem("Item 3", 3, 4, ItemChoice.XY));

// Connect the UI and bean
final IBeanController controller = BeanService.getInstance().createController(ui, bean);
final IBeanController<ExampleBean> controller = BeanService.getInstance().createController(ui, bean);
controller.addValueListener(new ExamplePrintBeanValueListener(controller, value));
controller.beanToUI();
controller.switchState(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Shell createShell(Display display) throws Exception {
bean.setY("5");

// Connect the UI and bean
final IBeanController controller = BeanService.getInstance().createController(ui, bean);
final IBeanController<SimpleBean> controller = BeanService.getInstance().createController(ui, bean);
controller.beanToUI();
controller.switchState(true);
controller.addValueListener(new ValueAdapter("Example listener") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void main(String[] args) throws Exception {
bean.setY(5);

// Connect the UI and bean
final IBeanController controller = BeanService.getInstance().createController(ui, bean);
final IBeanController<DecoratorBean> controller = BeanService.getInstance().createController(ui, bean);
controller.addValueListener(new ExamplePrintBeanValueListener(controller, value));
controller.beanToUI();
controller.switchState(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void main(String[] args) throws Exception {
final ExampleBean bean = createSimpleBean();

// Connect the UI and bean
final IBeanController controller = BeanService.getInstance().createController(ui, bean);
final IBeanController<ExampleBean> controller = BeanService.getInstance().createController(ui, bean);
ExamplePrintBeanValueListener listener = new ExamplePrintBeanValueListener(controller, value);
listener.setTextLimit(300);
controller.addValueListener(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,25 @@ public String[] getExtensions() {
return extensions;
}

/**
* This method would be more accurately called getLabels()
* It is the labels for the file dialog
* @return
*/
public String[] getFiles() {
return files;
}

public void setFiles(String[] files) {
/**
* This method would be more accurately called setLabels(...)
* It is the labels for the file dialog
* @return
*/
public void setFiles(String... files) {
this.files = files;
}

public void setExtensions(String[] extensions) {
public void setExtensions(String... extensions) {
this.extensions = extensions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public void setLockEditing(boolean checked) {
}

public List<ISeriesItemDescriptor> getSeriesItems() {
if (tableViewer==null) return null;
SeriesContentProvider prov = (SeriesContentProvider)tableViewer.getContentProvider();
return prov.getSeriesItems();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@
*
*
*/
public abstract class BeanDialog extends Dialog {
public abstract class BeanDialog<T> extends Dialog {

private static final Logger logger = LoggerFactory.getLogger(BeanDialog.class);

protected IBeanController controller;
protected IBeanController<T> controller;


protected BeanDialog(Shell parentShell) {
Expand All @@ -82,11 +82,11 @@ public boolean close() {
return super.close();
}

public Object getBean() {
public T getBean() {
return controller.getBean();
}

public void setBean(Object bean) {
public void setBean(T bean) {
try {
IBeanService service = (IBeanService)Activator.getService(IBeanService.class);
this.controller = service.createController(this, bean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public abstract class NumberBox extends ButtonComposite implements BoundsProvide
private ActiveMode activeMode = ActiveMode.SET_VISIBLE_AND_ACTIVE;
private String boundsKey;

// FIXME Widgets should not have a reference to the controller.
// This breaks the model/view/controller design because the controller
// is referenced by the view to look up field values.
private IBeanController beanService;


Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
package org.eclipse.richbeans.widgets.selector;

import java.util.List;

/**
* A configurator may be set on a list editor to
* provide configuration of a bean when it is added.
* provide configuration of a bean when it is added.<p>
*
* The configurator is a functional interface allowing beans when created and added to be
* compared with existing values and changed. This allows checks to be done of the current
* state of added items and the new item to be added with sensible defaults. <p>
*
* For instance:<p>
* <code>
ListEditor<MyBean> myListEditor = new VerticalListEditor<>();
// myListEditor...
// MyBean bean, MyBean previous, List<MyBean> context
myListEditor.setBeanConfigurator((bean, previous, context)->contiguous(bean, previous));
</code><p>
Here the contiguous(...) method checks that the values of bean are contiguous with previous
and sets those values as the new bean is added.
*
* @author Matthew Gerring
*
Expand All @@ -18,5 +35,5 @@ public interface BeanConfigurator<T> {
* @param previous - the previous bean in the list of beans or null
* @param context - the parent bean which contains the list of bean which we are editing
*/
void configure(T bean, T previous, Object context);
void configure(T bean, T previous, List<T> context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,15 @@ public void addBean(final T bean) throws ClassCastException {
* @throws ClassCastException
* is bean is not an instance of beanTemplate
*/
@SuppressWarnings("unchecked")
public void addBean(final T bean, int index) throws ClassCastException {
if (!beanTemplate.getClass().isInstance(bean)) {
throw new ClassCastException("Bean passed to addBean is not an instance of beanTemplate.getClass()");
}
if (!getListEditorUI().isAddAllowed(this))
return;
try {
if (getBeanConfigurator()!=null) getBeanConfigurator().configure(bean, getBean(), getValue());
if (getBeanConfigurator()!=null) getBeanConfigurator().configure(bean, getBean(), (List<T>)getValue());
final BeanWrapper<T> wrapper = new BeanWrapper<>(bean);
wrapper.setName(getFreeName(wrapper, getTemplateName(), index));
if (index < 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,28 @@ public String toString() {
return super.toString();
}


/**
* The configurator is a functional interface allowing beans when created and added to be
* compared with existing values and changed. This allows checks to be done of the current
* state of added items and the new item to be added with sensible defaults.
*
* @return the current configurator
*/
public BeanConfigurator<T> getBeanConfigurator() {
return beanConfigurator;
}

public void setBeanConfigurator(BeanConfigurator<T> beanConfigurator) {
/**
* The configurator is a functional interface allowing beans when created and added to be
* compared with existing values and changed. This allows checks to be done of the current
* state of added items and the new item to be added with sensible defaults.
*
* @return the current configurator
*/
public BeanConfigurator<T> setBeanConfigurator(BeanConfigurator<T> beanConfigurator) {
BeanConfigurator<T> old = this.beanConfigurator;
this.beanConfigurator = beanConfigurator;
return old;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,12 @@ public void addBean(final T bean) throws ClassCastException {
* @throws ClassCastException
* is bean is not an instance of beanTemplate
*/
@SuppressWarnings("unchecked")
public void addBean(final T bean, int index) throws ClassCastException {
if (!beanTemplate.getClass().isInstance(bean)) {
throw new ClassCastException("Bean passed to addBean is not an instance of beanTemplate.getClass()");
}
if (getBeanConfigurator()!=null) getBeanConfigurator().configure(bean, getBean(), getValue());
if (getBeanConfigurator()!=null) getBeanConfigurator().configure(bean, getBean(), (List<T>)getValue());
final BeanWrapper<T> wrapper = new BeanWrapper<>(bean);
String wrapperName = getFreeName(wrapper, getTemplateName(), index);
wrapper.setName(wrapperName);
Expand Down

0 comments on commit c08f66e

Please sign in to comment.