How to implement a JQueryUI slider in GWT - java

I recently needed to implement a slider in GWT to capture the percentage progress an user had made on a task. I was not happy with the slider from the GWT Incubator and was not too keen on using an external library like spiffy UI or SmartGWT. What alternatives could I use to implement an effective slider in GWT without doing too much donkey work ?

After a fair amount of searching I decided on going with a JQuery-Ui slider that would be implemented through a java wrapper class. The interaction between GWT and the JQuery slider would be through the GWT Java Script Native Interface. To keep the additions to the required library as small as possible I downloaded a custom Jquery-Ui package (just core+slider) which was pretty light weight. The result was satisfactory for our needs, fitted into our MVP design pattern and was UI Bound.
The slider wrapper was as follows :
package za.co.bsg.ems.client.framework.ui.slider;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Widget;
import java.util.ArrayList;
import java.util.List;
/**
* This widget wraps the JQuery UI Slider and allows for single slider .
*
* All options can be get or set using generic get/setIntOption, get/setStringOption, get/setBooleanOption
* methods, but some convenience methods are provided for most popular such as
* setValues and setMinimum and setMaximum. See SliderOptions for full list of options.
* #see SliderOption
*
*/
#SuppressWarnings("Convert2Diamond")
public class SingleSlider extends Widget {
private JSONObject defaultOptions;
private List<SliderListener> listeners = new ArrayList<SliderListener>();
/**
* Create the default slider with the specified ID. The ID is required
* because the slider needs a specific ID to connect to.
* #param id - id of the element to create
*/
public SingleSlider(String id) {
this(id, null);
}
/**
* Create a slider with the specified ID. The ID is required
* because the slider needs a specific ID to connect to.
* #param id - id of the element to create
* #param options - JSONObject of any possible option, can be null for defaults
*/
public SingleSlider(String id, JSONObject options) {
super();
Element divEle = DOM.createDiv();
setElement(divEle);
divEle.setId(id);
defaultOptions = options;
if (defaultOptions == null) {
defaultOptions = getOptions(0, 100, 0);
}
}
/**
* A convenient way to create an options JSONObject. Use SliderOption for keys.
* #param min - default minimum of the slider
* #param max - default maximum of the slider
* #param defaultValue - default point of anchor
* #return a JSONObject of Slider options
*/
public static JSONObject getOptions(int min, int max, int defaultValue) {
JSONObject options = new JSONObject();
options.put(SliderOption.MIN.toString(), new JSONNumber(min));
options.put(SliderOption.MAX.toString(), new JSONNumber(max));
options.put(SliderOption.VALUE.toString(), new JSONNumber(defaultValue));
options.put(SliderOption.RANGE.toString(), new JSONString("min"));
return options;
}
#Override
protected void onLoad() {
createSliderJS(this, getElement().getId(), defaultOptions.getJavaScriptObject());
super.onLoad();
}
#Override
protected void onUnload() {
destroySliderJS(this, getElement().getId());
super.onUnload();
}
/**
* Gets the minimum possible value for the slider
* #return Returns the minimum.
*/
public int getMinimum() {
return getIntOptionJS(getElement().getId(), SliderOption.MIN.toString());
}
/**
* Sets the minimum possible value for the slider
* #param minimum The minimum to set.
*/
public void setMinimum(int minimum) {
setIntOptionJS(getElement().getId(), SliderOption.MIN.toString(), minimum);
}
/**
* Gets the maximum possible value for the slider
* #return Returns the maximum.
*/
public int getMaximum() {
return getIntOptionJS(getElement().getId(), SliderOption.MAX.toString());
}
/**
* Sets the maximum possible value for the slider
* #param maximum The maximum to set.
*/
public void setMaximum(int maximum) {
setIntOptionJS(getElement().getId(), SliderOption.MAX.toString(), maximum);
}
/**
* Convenience method for only 1 anchor
* #param value to set.
*/
public void setValue(int value) {
setValueJS(getElement().getId(), value);
}
/**
* Set an option numeric value
* #param option the SliderOption
* #param value the numeric
*/
public void setIntOption(SliderOption option, int value) {
setIntOptionJS(getElement().getId(), option.toString(), value);
}
/**
* Get an option numeric value
* #param option the SliderOption
* #return value the numeric
*/
public int getIntOption(SliderOption option) {
return getIntOptionJS(getElement().getId(), option.toString());
}
/**
* Set an option boolean value
* #param option the SliderOption
* #param value the boolean
*/
public void setBooleanOption(SliderOption option, boolean value) {
setBooleanOptionJS(getElement().getId(), option.toString(), value);
}
/**
* Get an option boolean value
* #param option the SliderOption
* #return value the boolean
*/
public boolean getBooleanOption(SliderOption option) {
return getBooleanOptionJS(getElement().getId(), option.toString());
}
/**
* Set an option string value
* #param option the SliderOption
* #param value the String
*/
public void setStringOption(SliderOption option, String value) {
setStringOptionJS(getElement().getId(), option.toString(), value);
}
/**
* Set an option string value
* #param option the SliderOption
* #return value the String
*/
public String setStringOption(SliderOption option) {
return getStringOptionJS(getElement().getId(), option.toString());
}
/**
* Add a SliderListener
* #param l - SliderListener
*/
public void addListener(SliderListener l) {
listeners.add(l);
}
/**
* Removes the SliderListener
* #param l - SliderListener
*/
public void removeListener(SliderListener l) {
listeners.remove(l);
}
private void fireOnStartEvent(Event evt, int value) {
SliderEvent e = new SliderEvent(evt, this, value);
for (SliderListener l : listeners) {
l.onStart(e);
}
}
private boolean fireOnSlideEvent(Event evt, int value) {
SliderEvent e = new SliderEvent(evt, this, value);
for (SliderListener l : listeners) {
l.onStart(e);
}
boolean ret = true;
for (SliderListener l : listeners) {
if (!l.onSlide(e)) {
//if any of the listeners returns false, return false,
//but let them all do their thing
ret = false;
}
}
return ret;
}
private void fireOnChangeEvent(Event evt, int value, boolean hasOriginalEvent) {
SliderEvent e = new SliderEvent(evt, this, value, hasOriginalEvent);
for (SliderListener l : listeners) {
l.onChange(e);
}
}
private void fireOnStopEvent(Event evt, int value) {
SliderEvent e = new SliderEvent(evt, this, value);
for (SliderListener l : listeners) {
l.onStop(e);
}
}
private native void setIntOptionJS(String id, String option, int value) /*-{
$wnd.$("#" + id).slider("option", option, value);
}-*/;
private native int getIntOptionJS(String id, String option) /*-{
return $wnd.$("#" + id).slider("option", option);
}-*/;
private native void setBooleanOptionJS(String id, String option, boolean value) /*-{
$wnd.$("#" + id).slider("option", option, value);
}-*/;
private native boolean getBooleanOptionJS(String id, String option) /*-{
return $wnd.$("#" + id).slider("option", option);
}-*/;
private native void setStringOptionJS(String id, String option, String value) /*-{
$wnd.$("#" + id).slider("option", option, value);
}-*/;
private native String getStringOptionJS(String id, String option) /*-{
return $wnd.$("#" + id).slider("option", option);
}-*/;
private native void setValueJS(String id, int value) /*-{
$wnd.$("#" + id).slider("option", "value", value);
}-*/;
private native void createSliderJS(SingleSlider x, String id, JavaScriptObject options) /*-{
options.start = function(event, ui) {
x.#za.co.bsg.ems.client.framework.ui.slider.SingleSlider::fireOnStartEvent(Lcom/google/gwt/user/client/Event;I)(event, ui.value);
};
options.slide = function(event, ui) {
return x.#za.co.bsg.ems.client.framework.ui.slider.SingleSlider::fireOnSlideEvent(Lcom/google/gwt/user/client/Event;I)(event, ui.value);
};
options.change = function(event, ui) {
var has = event.originalEvent ? true : false;
x.#za.co.bsg.ems.client.framework.ui.slider.SingleSlider::fireOnChangeEvent(Lcom/google/gwt/user/client/Event;IZ)(event, ui.value, has);
};
options.stop = function(event, ui) {
x.#za.co.bsg.ems.client.framework.ui.slider.SingleSlider::fireOnStopEvent(Lcom/google/gwt/user/client/Event;I)(event, ui.value);
};
$wnd.$("#" + id).slider(options);
}-*/;
private native void destroySliderJS(SingleSlider x, String id) /*-{
$wnd.$("#" + id).slider("destroy");
}-*/;
}
This slider was then incorporated into the following widget to create a percentage slider
package za.co.bsg.ems.client.framework.ui.slider;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import za.co.bsg.ems.client.event.IntegerValueChangeEvent;
import java.util.HashSet;
import java.util.Set;
public class PercentageSliderWidget extends Composite implements SliderListener, HasValueChangeHandlers<Integer> {
interface PercentageSliderUiBinder extends UiBinder<Widget, PercentageSliderWidget> {}
private static final PercentageSliderUiBinder UI_BINDER = GWT.create(PercentageSliderUiBinder.class);
#UiField
Label headingLabel;
#UiField(provided = true)
SingleSlider singleSlider;
#UiField
IntegerBox percentBox;
#UiField
Label percentageSignLabel;
private Set<ValueChangeHandler<Integer>> valueChangeHandlers;
public PercentageSliderWidget(long taskId, String heading, int existingProgress) {
valueChangeHandlers = new HashSet<>();
JSONObject options = SingleSlider.getOptions(0, 100, existingProgress);
singleSlider = new SingleSlider("singleSlider" + taskId, options);
singleSlider.setTitle(existingProgress + "%");
initWidget(UI_BINDER.createAndBindUi(this));
singleSlider.addListener(this);
percentBox.setValue(existingProgress);
percentBox.addValueChangeHandler(new ValueChangeHandler<Integer>() {
#Override
public void onValueChange(ValueChangeEvent<Integer> event) {
updateValues(event.getValue(), true);
}
});
headingLabel.setText(heading);
percentageSignLabel.setText("%");
}
public void setValue(int value) {
updateValues(value, false);
}
#Override
public HandlerRegistration addValueChangeHandler(final ValueChangeHandler<Integer> handler) {
valueChangeHandlers.add(handler);
return new HandlerRegistration() {
#Override
public void removeHandler() {
valueChangeHandlers.remove(handler);
}
};
}
#Override
public boolean onSlide(SliderEvent e) {
percentBox.setValue(e.getValue());
return true;
}
#Override
public void onChange(SliderEvent e) {
// do nothing
}
#Override
public void onStop(SliderEvent e) {
updateValues(e.getValue(), true);
}
#Override
public void onStart(SliderEvent e) {
// do nothing
}
private void updateValues(int progressValue, boolean fireEvents) {
singleSlider.setTitle(progressValue + "%");
singleSlider.setValue(progressValue);
percentBox.setValue(progressValue);
if (fireEvents) {
for (ValueChangeHandler<Integer> valueChangeHandler : valueChangeHandlers) {
valueChangeHandler.onValueChange(new IntegerValueChangeEvent(progressValue));
}
}
}
}
My starting resource was the following
http://www.zackgrossbart.com/hackito/gwt-slider/

Related

Jackson won't deserialize on org.immutable interface

Following scenario.
Little SpringBoot application with the following classes:
FooDto:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.LinkedList;
import java.util.List;
import org.immutables.value.Value;
#Value.Immutable
#JsonDeserialize(builder = FooDto.Builder.class)
public interface FooDto{
#JsonProperty("fee")
String getFee();
#JsonProperty("fii")
String getFii();
#JsonProperty("url")
String getUrl();
#Value.Default
#JsonProperty("values")
default String[] getValues(){
return new String[0];
}
}
FooJsonMapper:
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
#Component
/*package*/ class FooJsonMapper {
private final ObjectMapper objectMapper;
FooJsonMapper() {
objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
}
public List<FooDto> toDtos(File jsonFile) throws IOException {
return Arrays.asList(objectMapper.readValue(jsonFile, FooDto[].class));
}
}
and a JSON like this:
[
{
"fee":"SOME_MESSAGE",
"fii":"SOME_TECH_MESSAGE",
"url":"/some",
"values":[
"test",
"some"
]
},
{
"fee":"OTHER_MESSAGE",
"fii":"OTHER_TECH_MESSAGE",
"url":"/other",
"values":[
"other",
"some"
]
},
{
"fee":"4711_MESSAGE",
"fii":"4711_TECH_MESSAGE",
"url":"/4711",
"values":[
]
}
]
Somehow when running the code jackson reports the following:
com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of `org.some.package.ImmutableFooDto$Builder`, problem: Cannot build FooDto, some of required attributes are not set [fee, fii, url]
at [Source: (File); line: 13, column: 3] (through reference chain: java.lang.Object[][0])
at com.fasterxml.jackson.databind.exc.ValueInstantiationException.from(ValueInstantiationException.java:47) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1907) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.DeserializationContext.handleInstantiationProblem(DeserializationContext.java:1260) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapInstantiationProblem(BeanDeserializerBase.java:1865) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.finishBuild(BuilderBasedDeserializer.java:202) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.deserialize(BuilderBasedDeserializer.java:217) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:214) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:24) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4593) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3413) ~[jackson-databind-2.12.3.jar:2.12.3]
at org.some.package.FooJsonMapper.toDtos(FooJsonMapper.java:25) ~[classes/:na]
I can confirm that the json is correct as the following line of code creates Objects with the data in them:
return Arrays.asList(objectMapper.readValue(jsonFile, Object[].class));
I also tried using #Value.Style(builder = "new") at the FooDto.class as sugested here: https://immutables.github.io/json.html
Jackson: 2.12.3
org.immutables: 2.8.2
EDIT:
As Requested the ImmutableDTO with Builder:
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Generated;
/**
* Immutable implementation of {#link FooDto}.
* <p>
* Use the builder to create immutable instances:
* {#code ImmutableFooDto.builder()}.
*/
#SuppressWarnings("all")
#Generated({"Immutables.generator", "FooDto"})
public final class ImmutableFooDto implements FooDto {
private final String fee;
private final String fii;
private final String url;
private final String[] values;
private ImmutableFooDto(ImmutableFooDto.Builder builder) {
this.fee = builder.fee;
this.fii = builder.fii;
this.url = builder.url;
if (builder.values != null) {
initShim.values(builder.values);
}
this.values = initShim.getValues();
this.initShim = null;
}
private ImmutableFooDto(
String fee,
String fii,
String url,
String[] values) {
this.fee = fee;
this.fii = fii;
this.url = url;
this.values = values;
this.initShim = null;
}
private static final int STAGE_INITIALIZING = -1;
private static final int STAGE_UNINITIALIZED = 0;
private static final int STAGE_INITIALIZED = 1;
private transient volatile InitShim initShim = new InitShim();
private final class InitShim {
private String[] values;
private int valuesStage;
String[] getValues() {
if (valuesStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (valuesStage == STAGE_UNINITIALIZED) {
valuesStage = STAGE_INITIALIZING;
this.values = getValuesInitialize().clone();
valuesStage = STAGE_INITIALIZED;
}
return this.values;
}
void values(String[] values) {
this.values = values;
valuesStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
ArrayList<String> attributes = new ArrayList<String>();
if (valuesStage == STAGE_INITIALIZING) attributes.add("values");
return "Cannot build FooDto, attribute initializers form cycle" + attributes;
}
}
private String[] getValuesInitialize() {
return FooDto.super.getValues();
}
/**
* #return The value of the {#code fee} attribute
*/
#JsonProperty("fee")
#Override
public String getFee() {
return fee;
}
/**
* #return The value of the {#code fii} attribute
*/
#JsonProperty("fii")
#Override
public String getFii() {
return fii;
}
/**
* #return The value of the {#code url} attribute
*/
#JsonProperty("url")
#Override
public String getUrl() {
return url;
}
/**
* #return A cloned {#code values} array
*/
#JsonProperty("values")
#Override
public String[] getValues() {
InitShim shim = this.initShim;
return shim != null
? shim.getValues().clone()
: this.values.clone();
}
/**
* Copy the current immutable object by setting a value for the {#link FooDto#getFee() fee} attribute.
* An equals check used to prevent copying of the same value by returning {#code this}.
* #param fee A new value for fee
* #return A modified copy of the {#code this} object
*/
public final ImmutableFooDto withFee(String fee) {
if (this.fee.equals(fee)) return this;
String newValue = Objects.requireNonNull(fee, "fee");
return new ImmutableFooDto(newValue, this.fii, this.url, this.values);
}
/**
* Copy the current immutable object by setting a value for the {#link FooDto#getFii() fii} attribute.
* An equals check used to prevent copying of the same value by returning {#code this}.
* #param fii A new value for fii
* #return A modified copy of the {#code this} object
*/
public final ImmutableFooDto withFii(String fii) {
if (this.fii.equals(fii)) return this;
String newValue = Objects.requireNonNull(fii, "fii");
return new ImmutableFooDto(this.fee, newValue, this.url, this.values);
}
/**
* Copy the current immutable object by setting a value for the {#link FooDto#getUrl() url} attribute.
* An equals check used to prevent copying of the same value by returning {#code this}.
* #param url A new value for url
* #return A modified copy of the {#code this} object
*/
public final ImmutableFooDto withUrl(String url) {
if (this.url.equals(url)) return this;
String newValue = Objects.requireNonNull(url, "url");
return new ImmutableFooDto(this.fee, this.fii, newValue, this.values);
}
/**
* Copy the current immutable object with elements that replace the content of {#link FooDto#getValues() values}.
* The array is cloned before being saved as attribute values.
* #param elements The non-null elements for values
* #return A modified copy of {#code this} object
*/
public final ImmutableFooDto withValues(String... elements) {
String[] newValue = elements.clone();
return new ImmutableFooDto(this.fee, this.fii, this.url, newValue);
}
/**
* This instance is equal to all instances of {#code ImmutableFooDto} that have equal attribute values.
* #return {#code true} if {#code this} is equal to {#code another} instance
*/
#Override
public boolean equals(Object another) {
if (this == another) return true;
return another instanceof ImmutableFooDto
&& equalTo((ImmutableFooDto) another);
}
private boolean equalTo(ImmutableFooDto another) {
return fee.equals(another.fee)
&& fii.equals(another.fii)
&& url.equals(another.url)
&& Arrays.equals(values, another.values);
}
/**
* Computes a hash code from attributes: {#code fee}, {#code fii}, {#code url}, {#code values}, {#code roles}.
* #return hashCode value
*/
#Override
public int hashCode() {
int h = 31;
h = h * 17 + fee.hashCode();
h = h * 17 + fii.hashCode();
h = h * 17 + url.hashCode();
h = h * 17 + Arrays.hashCode(values);
return h;
}
/**
* Prints the immutable value {#code FooDto} with attribute values.
* #return A string representation of the value
*/
#Override
public String toString() {
return "FooDto{"
+ "fee=" + fee
+ ", fii=" + fii
+ ", url=" + url
+ ", values=" + Arrays.toString(values)
+ "}";
}
/**
* Utility type used to correctly read immutable object from JSON representation.
* #deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding infrastructure
*/
#Deprecated
#JsonDeserialize
#JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static final class Json implements FooDto {
String fee;
String fii;
String url;
String[] values;
List<String> roles = Collections.emptyList();
boolean rolesIsSet;
#JsonProperty("fee")
public void setFee(String fee) {
this.fee = fee;
}
#JsonProperty("fii")
public void setFii(String fii) {
this.fii = fii;
}
#JsonProperty("url")
public void setUrl(String url) {
this.url = url;
}
#JsonProperty("values")
public void setValues(String[] values) {
this.values = values;
}
#Override
public String getFee() { throw new UnsupportedOperationException(); }
#Override
public String getFii() { throw new UnsupportedOperationException(); }
#Override
public String getUrl() { throw new UnsupportedOperationException(); }
#Override
public String[] getValues() { throw new UnsupportedOperationException(); }
}
/**
* #param json A JSON-bindable data structure
* #return An immutable value type
* #deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding infrastructure
*/
#Deprecated
#JsonCreator
static ImmutableFooDto fromJson(Json json) {
ImmutableFooDto.Builder builder = ImmutableFooDto.builder();
if (json.fee != null) {
builder.fee(json.fee);
}
if (json.fii != null) {
builder.fii(json.fii);
}
if (json.url != null) {
builder.url(json.url);
}
if (json.values != null) {
builder.values(json.values);
}
return builder.build();
}
/**
* Creates an immutable copy of a {#link FooDto} value.
* Uses accessors to get values to initialize the new immutable instance.
* If an instance is already immutable, it is returned as is.
* #param instance The instance to copy
* #return A copied immutable FooDto instance
*/
public static ImmutableFooDto copyOf(FooDto instance) {
if (instance instanceof ImmutableFooDto) {
return (ImmutableFooDto) instance;
}
return ImmutableFooDto.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {#link ImmutableFooDto ImmutableFooDto}.
* #return A new ImmutableFooDto builder
*/
public static ImmutableFooDto.Builder builder() {
return new ImmutableFooDto.Builder();
}
/**
* Builds instances of type {#link ImmutableFooDto ImmutableFooDto}.
* Initialize attributes and then invoke the {#link #build()} method to create an
* immutable instance.
* <p><em>{#code Builder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.</em>
*/
public static final class Builder {
private static final long INIT_BIT_TITLE = 0x1L;
private static final long INIT_BIT_TECHNICAL_KEY = 0x2L;
private static final long INIT_BIT_URL = 0x4L;
private long initBits = 0x7L;
private long optBits;
private String fee;
private String fii;
private String url;
private String[] values;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {#code FooDto} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* Collection elements and entries will be added, not replaced.
* #param instance The instance from which to copy values
* #return {#code this} builder for use in a chained invocation
*/
public final Builder from(FooDto instance) {
Objects.requireNonNull(instance, "instance");
fee(instance.getFee());
fii(instance.getFii());
url(instance.getUrl());
values(instance.getValues());
return this;
}
/**
* Initializes the value for the {#link FooDto#getFee() fee} attribute.
* #param fee The value for fee
* #return {#code this} builder for use in a chained invocation
*/
public final Builder fee(String fee) {
this.fee = Objects.requireNonNull(fee, "fee");
initBits &= ~INIT_BIT_TITLE;
return this;
}
/**
* Initializes the value for the {#link FooDto#getFii() fii} attribute.
* #param fii The value for fii
* #return {#code this} builder for use in a chained invocation
*/
public final Builder fii(String fii) {
this.fii = Objects.requireNonNull(fii, "fii");
initBits &= ~INIT_BIT_TECHNICAL_KEY;
return this;
}
/**
* Initializes the value for the {#link FooDto#getUrl() url} attribute.
* #param url The value for url
* #return {#code this} builder for use in a chained invocation
*/
public final Builder url(String url) {
this.url = Objects.requireNonNull(url, "url");
initBits &= ~INIT_BIT_URL;
return this;
}
/**
* Initializes the value for the {#link FooDto#getValues() values} attribute.
* <p><em>If not set, this attribute will have a default value as defined by {#link FooDto#getValues() values}.</em>
* #param values The elements for values
* #return {#code this} builder for use in a chained invocation
*/
public final Builder values(String... values) {
this.values = values.clone();
return this;
}
/**
* Builds a new {#link ImmutableFooDto ImmutableFooDto}.
* #return An immutable instance of FooDto
* #throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableFooDto build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableFooDto(this);
}
private String formatRequiredAttributesMessage() {
List<String> attributes = new ArrayList<String>();
if ((initBits & INIT_BIT_TITLE) != 0) attributes.add("fee");
if ((initBits & INIT_BIT_TECHNICAL_KEY) != 0) attributes.add("fii");
if ((initBits & INIT_BIT_URL) != 0) attributes.add("url");
return "Cannot build FooDto, some of required attributes are not set " + attributes;
}
}
private static <T> List<T> createSafeList(Iterable<? extends T> iterable, boolean checkNulls, boolean skipNulls) {
ArrayList<T> list;
if (iterable instanceof Collection<?>) {
int size = ((Collection<?>) iterable).size();
if (size == 0) return Collections.emptyList();
list = new ArrayList<T>();
} else {
list = new ArrayList<T>();
}
for (T element : iterable) {
if (skipNulls && element == null) continue;
if (checkNulls) Objects.requireNonNull(element, "element");
list.add(element);
}
return list;
}
private static <T> List<T> createUnmodifiableList(boolean clone, List<T> list) {
switch(list.size()) {
case 0: return Collections.emptyList();
case 1: return Collections.singletonList(list.get(0));
default:
if (clone) {
return Collections.unmodifiableList(new ArrayList<T>(list));
} else {
if (list instanceof ArrayList<?>) {
((ArrayList<?>) list).trimToSize();
}
return Collections.unmodifiableList(list);
}
}
}
}
I think the problem is that jackson can't find a way to construct your class.
Here's what works for me
import org.immutables.value.Value;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
#Value.Immutable
#Value.Style(privateNoargConstructor = true) //<<<< this is what you need I think
#JsonSerialize(as = ImmutableVal.class)
#JsonDeserialize(as = ImmutableVal.class)
interface Val {
int a();
#JsonProperty("b")
String second();
}
and
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ValMain {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(
ImmutableVal.builder()
.a(1)
.second("B")
.build());
System.out.println(json);
//{"a":1,"b":"B"}
Val val = objectMapper.readValue(json, Val.class);
System.out.println(val);
//Val{a=1, second=B}
}
}
Then you can even get rid of the DTOs

Avro generated class issue with json conversion [kotlin]

I'm having a strange issue with marshalling/unmarshalling an avro generated class. The error I'm getting is throwing a not an enum error - except there aren't any enum's in my class.
The error is specifically this:
com.fasterxml.jackson.databind.JsonMappingException: Not an enum: {"type":"record","name":"TimeUpdateTopic","namespace":"org.company.mmd.time","fields":[{"name":"time","type":"double"}]} (through reference chain: org.company.mmd.time.TimeUpdateTopic["schema"]->org.apache.avro.Schema$RecordSchema["enumDefault"])
Test Case
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import org.junit.Test
class TimeUpdateTopicTest {
val objectMapper = ObjectMapper().registerModule(JavaTimeModule())
#Test
fun decode() {
val t = TimeUpdateTopic(1.0)
objectMapper.writeValueAsString(t)
}
}
AVDL
#namespace("org.company.mmd.time")
protocol TimeMonitor {
record TimeUpdateTopic {
double time;
}
}
Java class generated by the avro task
/**
* Autogenerated by Avro
*
* DO NOT EDIT DIRECTLY
*/
package org.company.mmd.time;
import org.apache.avro.generic.GenericArray;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.util.Utf8;
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.SchemaStore;
#org.apache.avro.specific.AvroGenerated
public class TimeUpdateTopic extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
private static final long serialVersionUID = -4648318619505855037L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"TimeUpdateTopic\",\"namespace\":\"org.company.mmd.time\",\"fields\":[{\"name\":\"time\",\"type\":\"double\"}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
private static SpecificData MODEL$ = new SpecificData();
private static final BinaryMessageEncoder<TimeUpdateTopic> ENCODER =
new BinaryMessageEncoder<TimeUpdateTopic>(MODEL$, SCHEMA$);
private static final BinaryMessageDecoder<TimeUpdateTopic> DECODER =
new BinaryMessageDecoder<TimeUpdateTopic>(MODEL$, SCHEMA$);
/**
* Return the BinaryMessageEncoder instance used by this class.
* #return the message encoder used by this class
*/
public static BinaryMessageEncoder<TimeUpdateTopic> getEncoder() {
return ENCODER;
}
/**
* Return the BinaryMessageDecoder instance used by this class.
* #return the message decoder used by this class
*/
public static BinaryMessageDecoder<TimeUpdateTopic> getDecoder() {
return DECODER;
}
/**
* Create a new BinaryMessageDecoder instance for this class that uses the specified {#link SchemaStore}.
* #param resolver a {#link SchemaStore} used to find schemas by fingerprint
* #return a BinaryMessageDecoder instance for this class backed by the given SchemaStore
*/
public static BinaryMessageDecoder<TimeUpdateTopic> createDecoder(SchemaStore resolver) {
return new BinaryMessageDecoder<TimeUpdateTopic>(MODEL$, SCHEMA$, resolver);
}
/**
* Serializes this TimeUpdateTopic to a ByteBuffer.
* #return a buffer holding the serialized data for this instance
* #throws java.io.IOException if this instance could not be serialized
*/
public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
return ENCODER.encode(this);
}
/**
* Deserializes a TimeUpdateTopic from a ByteBuffer.
* #param b a byte buffer holding serialized data for an instance of this class
* #return a TimeUpdateTopic instance decoded from the given buffer
* #throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
*/
public static TimeUpdateTopic fromByteBuffer(
java.nio.ByteBuffer b) throws java.io.IOException {
return DECODER.decode(b);
}
#Deprecated public double time;
/**
* Default constructor. Note that this does not initialize fields
* to their default values from the schema. If that is desired then
* one should use <code>newBuilder()</code>.
*/
public TimeUpdateTopic() {}
/**
* All-args constructor.
* #param time The new value for time
*/
public TimeUpdateTopic(java.lang.Double time) {
this.time = time;
}
public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
public java.lang.Object get(int field$) {
switch (field$) {
case 0: return time;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
// Used by DatumReader. Applications should not call.
#SuppressWarnings(value="unchecked")
public void put(int field$, java.lang.Object value$) {
switch (field$) {
case 0: time = (java.lang.Double)value$; break;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
/**
* Gets the value of the 'time' field.
* #return The value of the 'time' field.
*/
public double getTime() {
return time;
}
/**
* Sets the value of the 'time' field.
* #param value the value to set.
*/
public void setTime(double value) {
this.time = value;
}
/**
* Creates a new TimeUpdateTopic RecordBuilder.
* #return A new TimeUpdateTopic RecordBuilder
*/
public static org.company.mmd.time.TimeUpdateTopic.Builder newBuilder() {
return new org.company.mmd.time.TimeUpdateTopic.Builder();
}
/**
* Creates a new TimeUpdateTopic RecordBuilder by copying an existing Builder.
* #param other The existing builder to copy.
* #return A new TimeUpdateTopic RecordBuilder
*/
public static org.company.mmd.time.TimeUpdateTopic.Builder newBuilder(org.company.mmd.time.TimeUpdateTopic.Builder other) {
if (other == null) {
return new org.company.mmd.time.TimeUpdateTopic.Builder();
} else {
return new org.company.mmd.time.TimeUpdateTopic.Builder(other);
}
}
/**
* Creates a new TimeUpdateTopic RecordBuilder by copying an existing TimeUpdateTopic instance.
* #param other The existing instance to copy.
* #return A new TimeUpdateTopic RecordBuilder
*/
public static org.company.mmd.time.TimeUpdateTopic.Builder newBuilder(org.company.mmd.time.TimeUpdateTopic other) {
if (other == null) {
return new org.company.mmd.time.TimeUpdateTopic.Builder();
} else {
return new org.company.mmd.time.TimeUpdateTopic.Builder(other);
}
}
/**
* RecordBuilder for TimeUpdateTopic instances.
*/
public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<TimeUpdateTopic>
implements org.apache.avro.data.RecordBuilder<TimeUpdateTopic> {
private double time;
/** Creates a new Builder */
private Builder() {
super(SCHEMA$);
}
/**
* Creates a Builder by copying an existing Builder.
* #param other The existing Builder to copy.
*/
private Builder(org.company.mmd.time.TimeUpdateTopic.Builder other) {
super(other);
if (isValidValue(fields()[0], other.time)) {
this.time = data().deepCopy(fields()[0].schema(), other.time);
fieldSetFlags()[0] = other.fieldSetFlags()[0];
}
}
/**
* Creates a Builder by copying an existing TimeUpdateTopic instance
* #param other The existing instance to copy.
*/
private Builder(org.company.mmd.time.TimeUpdateTopic other) {
super(SCHEMA$);
if (isValidValue(fields()[0], other.time)) {
this.time = data().deepCopy(fields()[0].schema(), other.time);
fieldSetFlags()[0] = true;
}
}
/**
* Gets the value of the 'time' field.
* #return The value.
*/
public double getTime() {
return time;
}
/**
* Sets the value of the 'time' field.
* #param value The value of 'time'.
* #return This builder.
*/
public org.company.mmd.time.TimeUpdateTopic.Builder setTime(double value) {
validate(fields()[0], value);
this.time = value;
fieldSetFlags()[0] = true;
return this;
}
/**
* Checks whether the 'time' field has been set.
* #return True if the 'time' field has been set, false otherwise.
*/
public boolean hasTime() {
return fieldSetFlags()[0];
}
/**
* Clears the value of the 'time' field.
* #return This builder.
*/
public org.company.mmd.time.TimeUpdateTopic.Builder clearTime() {
fieldSetFlags()[0] = false;
return this;
}
#Override
#SuppressWarnings("unchecked")
public TimeUpdateTopic build() {
try {
TimeUpdateTopic record = new TimeUpdateTopic();
record.time = fieldSetFlags()[0] ? this.time : (java.lang.Double) defaultValue(fields()[0]);
return record;
} catch (org.apache.avro.AvroMissingFieldException e) {
throw e;
} catch (java.lang.Exception e) {
throw new org.apache.avro.AvroRuntimeException(e);
}
}
}
#SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumWriter<TimeUpdateTopic>
WRITER$ = (org.apache.avro.io.DatumWriter<TimeUpdateTopic>)MODEL$.createDatumWriter(SCHEMA$);
#Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
WRITER$.write(this, SpecificData.getEncoder(out));
}
#SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumReader<TimeUpdateTopic>
READER$ = (org.apache.avro.io.DatumReader<TimeUpdateTopic>)MODEL$.createDatumReader(SCHEMA$);
#Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
READER$.read(this, SpecificData.getDecoder(in));
}
#Override protected boolean hasCustomCoders() { return true; }
#Override public void customEncode(org.apache.avro.io.Encoder out)
throws java.io.IOException
{
out.writeDouble(this.time);
}
#Override public void customDecode(org.apache.avro.io.ResolvingDecoder in)
throws java.io.IOException
{
org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff();
if (fieldOrder == null) {
this.time = in.readDouble();
} else {
for (int i = 0; i < 1; i++) {
switch (fieldOrder[i].pos()) {
case 0:
this.time = in.readDouble();
break;
default:
throw new java.io.IOException("Corrupt ResolvingDecoder.");
}
}
}
}
}
Am I doing something stupid and/or wrong here? Or is this an actual bug
Updates
I'm able to get JSON out using this function:
inline fun <reified T: SpecificRecordBase> StringFromAvroGenerated(obj: T) : String {
val schema = obj.schema
val writer = SpecificDatumWriter(T::class.java)
val stream = ByteArrayOutputStream()
var jsonEncoder = EncoderFactory.get().jsonEncoder(schema, stream)
writer.write(obj, jsonEncoder)
jsonEncoder.flush()
return stream.toString("UTF-8")
}
but I was assuming this should be automatic with Jackson
So it appears there are two ways to solve my issues (thanks to JsonMappingException when serializing avro generated object to json)
Write a Jackson MixIn to handle the getSchema call
So the first option required me to create a Mixin such as this:
abstract class AvroMixIn {
#JsonIgnore
abstract fun getSchema(): org.apache.avro.Schema
#JsonIgnore
abstract fun getSpecificData() : org.apache.avro.specific.SpecificData
}
And then when i make an object mapper:
val objectMapper = ObjectMapper()
.registerModule(JavaTimeModule())
.addMixIn(Object::class.java, AvroMixIn::class.java)
I chose Object::class.java instead of the actual class because it should apply to all classes. Probably a better solution is to apply it to a shared base-class all the AvroGenerated stuff has.
Rewrite the Avro Velocity Templates to automatically add this
This is actually the 1st approach I took because it seemed more "seemless".
1) Check out the avro project
2) Copy enum.vm, fixed.vm, protocol.vm, record.vm into a /avro_templates directory off the main root of my project
3) Add the #com.fasterxml.jackson.annotation.JsonIgnore property to the template:
#end
#com.fasterxml.jackson.annotation.JsonIgnore
public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
#com.fasterxml.jackson.annotation.JsonIgnore
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
4) Update the gradle task:
avro {
dateTimeLogicalType="JSR310"
templateDirectory = "avro_templates/"
}
5) re-build avro classes
(everything now works)

How to read opened file and display content in Eclipse Master Details block?

I have generated a sample Multi-page Editor through the Eclipse wizard. Then, I have modified the sample plugin in order to have two pages:
Text Editor
Master Details Block
For the Master Details Block, I have used this tutorial.
I can open the Master Details Block page and I am also able to view the initialized objects in the list and display the corresponding details page. Now, I want to replace the static object with entries from the loaded file. My problem is, that I don't know how I can parse these entries from the text file. Do I need to implement my own parser, including the file handling or is this already implemented through a IFileEditorInput interface?
In my ScrolledPropertiesBlock class, I call the method viewer.setContentProvider(new MasterContentProvider());. I am sure that I need to modify the MasterContentProvider class implementation. So far, I have this:
class MasterContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
if (inputElement instanceof FileEditorInput) {
//MyEditorInput input = (MyEditorInput) inputElement;
MyEditorInput input = new MyEditorInput("test");
return input.getModel().getContents();
}
return new Object[0];
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
If I delete the line MyEditorInput input = new MyEditorInput("test"); and do my cast instead, I get the following exception:
java.lang.ClassCastException: org.eclipse.ui.part.FileEditorInput cannot be cast to my.plugin.editor.MyEditorInput
Do I need to have a MyEditorInput which extends FormEditorInput (like in the example) and then implements IFileEditorInput?
public class MyEditorInput extends FormEditorInput {
private SimpleModel model;
public MyEditorInput(String name) {
super(name);
model = new SimpleModel();
}
public SimpleModel getModel() {
return model;
}
}
public class FormEditorInput implements IEditorInput {
private String name;
public FormEditorInput(String name) {
this.name = name;
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#exists()
*/
public boolean exists() {
return true;
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#getImageDescriptor()
*/
public ImageDescriptor getImageDescriptor() {
return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
ISharedImages.IMG_OBJ_ELEMENT);
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#getName()
*/
public String getName() {
return name;
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#getPersistable()
*/
public IPersistableElement getPersistable() {
return null;
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#getToolTipText()
*/
public String getToolTipText() {
return getName();
}
/*
* (non-Javadoc)
*
* #see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
*/
public Object getAdapter(Class adapter) {
return null;
}
}
The SimpleModel class looks like this:
public class SimpleModel {
private ArrayList modelListeners;
private ArrayList objects;
public SimpleModel() {
modelListeners = new ArrayList();
initialize();
}
public void addModelListener(IModelListener listener) {
if (!modelListeners.contains(listener))
modelListeners.add(listener);
}
public void removeModelListener(IModelListener listener) {
modelListeners.remove(listener);
}
public void fireModelChanged(Object[] objects, String type, String property) {
for (int i = 0; i < modelListeners.size(); i++) {
((IModelListener) modelListeners.get(i)).modelChanged(objects,
type, property);
}
}
public Object[] getContents() {
return objects.toArray();
}
private void initialize() {
objects = new ArrayList();
NamedObject[] objects = {
new TypeOne(Messages.getString("SimpleModel.t1_i1"), 2, true, Messages.getString("SimpleModel.text1")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i2"), 1, false, Messages.getString("SimpleModel.text2")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i3"), 3, true, Messages.getString("SimpleModel.text3")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i4"), 0, false, Messages.getString("SimpleModel.text4")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i5"), 1, true, Messages.getString("SimpleModel.text5")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeTwo(Messages.getString("SimpleModel.t2_i1"), false, true), //$NON-NLS-1$
new TypeTwo(Messages.getString("SimpleModel.t2_i2"), true, false)}; //$NON-NLS-1$
add(objects, false);
}
public void add(NamedObject[] objs, boolean notify) {
for (int i = 0; i < objs.length; i++) {
objects.add(objs[i]);
objs[i].setModel(this);
}
if (notify)
fireModelChanged(objs, IModelListener.ADDED, ""); //$NON-NLS-1$
}
public void remove(NamedObject[] objs, boolean notify) {
for (int i = 0; i < objs.length; i++) {
objects.remove(objs[i]);
objs[i].setModel(null);
}
if (notify)
fireModelChanged(objs, IModelListener.REMOVED, ""); //$NON-NLS-1$
}
}
Eclipse gives you the input in an IEditorInput - this probably be an IFileEditorInput if the file is in the workspace. You can't cast this to something else. Creating new editor inputs does not help.
So you will have to read and parse the contents of the file from the editor input Eclipse gives you.
For a IFileEditorInput you can call getFile to get the input IFile. You can then call IFile.getContents to get an input stream containing the file's contents.

fireEvent from Custom class

I have the following class ProtokollEvent
public class ProtokollEvent extends Event {
//variable holds all devices in given protokoll
private ObservableList<Device> devicesList;
//variable holds SaveResult
private SaveResult result;
//final ProtokollEvents
public static final EventType<ProtokollEvent> PROTOKOLL_SAVE = new EventType(ANY, "PROTOKOLL_SAVE");
public static final EventType<ProtokollEvent> PROTOKOLL_SAVE_DONE = new EventType(ANY, "PROTOKOLL_SAVE_DONE");
public static final EventType<ProtokollEvent> PROTOKOLL_UPDATED = new EventType(ANY, "PROTOKOLL_UPDATED");
public static final EventType<ProtokollEvent> PROTOKOLL_DELETED = new EventType(ANY, "PROTOKOLL_DELETED");
public ProtokollEvent() {
this(PROTOKOLL_SAVE);
}
public ProtokollEvent(EventType<? extends Event> arg0) {
super(arg0);
}
public ProtokollEvent(Object arg0, EventTarget arg1, EventType<? extends Event> arg2) {
super(arg0, arg1, arg2);
}
/**
* getDevices will return current {#link Device} as ObservableList
*
* #return {#link ObservableList} of type {#link Device}
*/
public ObservableList getDev() {
return devicesList;
}
/**
* setDevices will set devicesList
* #param devices ObservableList {#link Device}
*/
public void setDev(ObservableList devices) {
this.devicesList = devices;
}
/**
* get the result which is returned from calling saveProtokoll
* #return result {#link SaveResult}
*/
public SaveResult getResult() {
return result;
}
/**
* set the result which is returned after calling saveMessprotokoll in RestCall
* #param result {#link SaveResult}
*/
public void setResult(SaveResult result) {
this.result = result;
}
}
in the second class
public class SaveUtils {
private MainWindowController controller;
private ObservableList<RowContainerPruefvorschriftController> rows;
private Protokoll lastSavedProtokoll;
private Protokoll currentSavingProtokoll;
public SaveUtils(MainWindowController control){
this.controller = control;
}
private void startSaving(){
currentSavingProtokoll = createProtokoll();
boolean state = controller.networkOnline.get() ? saveOnline() :saveOffline();
}
public void setRows(ObservableList<RowContainerPruefvorschriftController> rows) {
this.rows = rows;
//if rows get set start saveing the data
startSaving();
}
private boolean saveOffline(){
return false;
}
private boolean saveOnline() {
RestCall call = controller.getCall();
//call saveMessprotokoll and look what SaveResult returns
SaveResult result = call.saveMessprotokoll(currentSavingProtokoll);
//create ProtokollEvent to tell all consumers if all was ok
ProtokollEvent save = new ProtokollEvent(ProtokollEvent.PROTOKOLL_SAVE_DONE);
save.setResult(result);
//HOW to fire/dispatch the ProtokollEvent here??????
//TODO: need to fire this event and listen for it in other classes
if(result.getResult()>0){
controller.setLoggerMessage("SavedOnline->Protokoll-Nr.:"+result.getProtokollnr());
}
else {
controller.setLoggerMessage("SavedOnline not successful->Error:"+result.getError_message());
}
return true;
}
}
in the saveOnline function i create a ProtokollEvent and pass it some values.
What i now want is to fire or dispatch this event so other code parts could listen to this.
I tried with fireEvent() but as i understood the Oracle-DOCs only NODEs, Windows and Scene are able to do so. But how could i solve it with my custom class?
Additional i ask myself whats is the difference between fireEvent and dispatchEvent?
Found the solution.
All events could be fired through the static method
Event.fireEvent(EventTarget eventTarget,Event event)
where eventTarget specifies the path through which the event will travel (taken from java docs).
So for my example i just added the following line
Event.fireEvent(controller.getMainWindowBorderPane(),save);
did it...

Component based composition

Can anyone assist me to implement a component-based project? I have designed two components i.e calculator and engine (source codes below) which need to be inside any of the swing components (either JList , JTree, or any). Then there should be an ability of dragging any of the two components (calculator or engine) unto the editor pane for composition using a connector (code give below). If the composition is right, let the composite return unto the palette where the initial components were dragged from.
Components:
public class Engine {
private String name = "";
private boolean running = false;
private int speed;
public Engine(String name) {
this.name = name;
}
public void start() {
if (!running) {
running = true;
System.out.println("Engine starts.");
}
else
System.out.println("Engine already starts.");
}
public void stop() {
if (running) {
running = false;
speed = 0;
System.out.println("Engine stops.");
}
else
System.out.println("Engine already stops.");
}
public void setSpeed(int speed) {
if (speed >=0)
this.speed = speed;
}
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
public class Calc {
public Calc() {
}
public Double squareRoot(Double a_double){
return Math.sqrt(a_double);
}
// pre an_int >= 0
public synchronized Integer factorial(Integer an_int){
int fact=1;
for(int i=1;i<=an_int;i++){
fact *= i;
}
return fact;
}
/**
*
* #param an_int
* #return the squared root of the input integer
*/
public Integer getSquareRoot(Integer an_int){
return (int) Math.sqrt(an_int);
}
public Double getSquareRoot(Double aDouble){
return Math.sqrt(aDouble);
}
/**
* Converts an Integer object into a Double object.
*
* #param an_int The Integer to be converted.
* #return The Double object representing the same value.
*
*/
public Double integerToDouble(Integer an_int){
return new Double(an_int.intValue());
}
/**
* Converts a Double object into an Integer object. Decimal digits are
* truncated. Useful when passing the output of a method as the input to another.
*
* #param a_double The Double to be converted.
* #return The Integer object representing the same value.
*
*/
public Integer doubleToInteger(Double a_double){
return new Integer(a_double.intValue());
}
public Integer sum(Integer a, Integer b) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a+b;
}
/**
* Returns a string representation of the calculator.
*/
public String toString(){
return("Calculator computation unit:\n"+super.toString());
}
void doNothing(){
}}
/////////////////////////////////////////////////////////////////////////////////////////
Your best bet is probably to leverage an existing GUI editor, where the one in Netbeans is rumoured to be good.
You will then need to add your components to the component palette. Check the Netbeans online help for further information.
Start with NetBeans, take a look at this answer, i give full details of how to create a component in NetBeans.

Categories