How should someone bind a JList component to a model in Griffon using Groovy and Swing - swing

I am trying to write a Griffon/Swing/Groovy app.
I am having some trouble binding a JList control to my model.
Here is my model:
package com.halcon.scheduler
import com.halcon.scheduler.domain.EventType
import griffon.core.artifact.GriffonModel
import griffon.metadata.ArtifactProviderFor
import griffon.transform.Observable
#ArtifactProviderFor(GriffonModel)
class EventTypeListModel {
#Observable String newEventType = "";
#Observable Collection<EventType> eventTypes = new HashSet<>();
}
my view:
package com.halcon.scheduler
import griffon.core.artifact.GriffonView
import griffon.inject.MVCMember
import griffon.metadata.ArtifactProviderFor
import javax.swing.SwingConstants
import javax.annotation.Nonnull
#ArtifactProviderFor(GriffonView)
class EventTypeListView {
#MVCMember #Nonnull
FactoryBuilderSupport builder
#MVCMember #Nonnull
EventTypeListModel model
void initUI() {
builder.with {
parentView.desktop.add(internalFrame(title: "Event Type List", visible: true, bounds: [25, 25, 200, 100]) {
gridLayout(rows: 3, cols: 1)
textField(id: 'newEventType', text: bind('newEventType', target:model, mutual: true))
list(id: 'eventTypes', dataModel: bind('eventTypes', source:model))
button(id: 'addNewEventType', addNewEventTypeAction)
})
}
}
}
and my controller:
package com.halcon.scheduler
import griffon.core.artifact.GriffonController
import griffon.core.controller.ControllerAction
import griffon.inject.MVCMember
import griffon.metadata.ArtifactProviderFor
import javax.annotation.Nonnull
import com.halcon.scheduler.domain.EventType
#ArtifactProviderFor(GriffonController)
class EventTypeListController {
#MVCMember #Nonnull
EventTypeListModel model
#ControllerAction
void addNewEventType() {
log.info("Adding new event type: " + model.newEventType)
model.eventTypes.add(new EventType(model.newEventType))
model.newEventType = ""
}
}
My goal is to have the model's eventTypes collection bound to the eventTypes list in the view.
The bidirectional binding on the text field works great. However, when I call the addNewEventType action in my controller, I get the log message, the eventTypes set is updated and the text field gets blanked. Only the view isn't updated.

The binding mechanism exposed by SwingBuilder only works for scalar values. The code you posted will react when the reference to the list is updated, it won't trigger events when the contents of the list are updated. You'll have to resort to GlazedLists (http://www.glazedlists.com/) for the desired behavior.

Related

Generating unattached dynamic components in Angular

I went through this issue while working on the ScheduleJS framework. At some point I am provided with a HTMLCanvasElement which I want to replace with a dynamically generated component programatically. To do so, and to keep the code as clean as possible, I'd like to create my own Angular components at runtime and use the HTMLCanvasElement.replaceWith(component) method from the provided HTMLCanvasElement replacing the canvas with the dynamically created component.
Here is the Angular service I came up with, which does the job the way I expected:
import {ApplicationRef, ComponentFactoryResolver, ComponentRef, Injectable, Injector, Type} from "#angular/core";
import {ReplacementComponent} from "xxx"; // This is a higher order type of Component
#Injectable({providedIn: "root"})
export class DynamicComponentGenerator {
// Attributes
private _components: Map<string, ComponentRef<ReplacementComponent>> = new Map();
private _currentKey: number = 0;
// Constructor
constructor(private _appRef: ApplicationRef,
private _resolver: ComponentFactoryResolver,
private _injector: Injector) { }
// Methods
create(componentType: Type<ReplacementComponent>): ComponentRef<ReplacementComponent> {
const componentRef = componentType instanceof ComponentRef
? componentType
: this._resolver.resolveComponentFactory(componentType).create(this._injector);
this._appRef.attachView(componentRef.hostView);
this._components.set(`${this._currentKey}`, componentRef);
componentRef.instance.key = `${this._currentKey}`;
this._currentKey += 1;
return componentRef;
}
remove(componentKey: string): void {
const componentRef = this._components.get(componentKey);
if (componentRef) {
this._appRef.detachView(componentRef.hostView);
componentRef.destroy();
this._components.delete(componentKey);
}
}
clear(): void {
this._components.forEach((componentRef, key) => {
this._appRef.detachView(componentRef.hostView);
componentRef.destroy();
this._components.delete(key);
});
this._currentKey = 0;
}
}
So basically this service lets me create a component with .create(ComponentClass) remove it by providing the component key .remove(key) and clear() to remove all the components.
My issues are the following:
The ComponentFactoryResolver class is deprecated, should I use it anyways?
Could not manage to use the newer API to create unattached components (not able to have access to an Angular hostView)
Is there a better way to do this?
Thank you for reading me.
You could try using new createComponent function:
import { createComponent, ... } from "#angular/core";
const componentRef =
createComponent(componentType, { environmentInjector: this._appRef.injector});
this._appRef.attachView(componentRef.hostView);

Flink Nested Json Serialization Issue

I'm trying to serialize flink Row to kafka, I don't have json schema with me, but have columns names, also Row can be accessed with index and fields, with plain json below code is working fine, however with nested json, for type Row, it is printing rowking and arity. I'm using JsonRowSerializationSchema with withTypeInfo builder.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.typeutils.RowTypeInfo;
import org.apache.flink.connector.kafka.sink.KafkaRecordSerializationSchema;
import org.apache.flink.formats.json.JsonRowSerializationSchema;
import org.apache.flink.types.Row;
public class JsonSerializerBuilder {
private final String[] columnNames;
public JsonSerializerBuilder(String[] columnNames) {
this.columnNames = columnNames;
}
public KafkaRecordSerializationSchema<Row> build() {
String outputTopic = "test_topic";
TypeInformation<Row> opTypeInfo = getTypeInformation();
JsonRowSerializationSchema jsonRowSerializationSchema =
JsonRowSerializationSchema.builder().withTypeInfo(opTypeInfo).build();
return KafkaRecordSerializationSchema.builder()
.setValueSerializationSchema(jsonRowSerializationSchema)
.setTopic(outputTopic)
.build();
}
private TypeInformation<Row> getTypeInformation() {
TypeInformation[] typeInformationArray = new TypeInformation[columnNames.length];
Arrays.fill(typeInformationArray, TypeInformation.of(Object.class));
return new RowTypeInfo(typeInformationArray, columnNames);
}
}
input: {"id": 1,"name":"mike","school_details":{"location": "uk","name": "test"}}
output: {"id": 1,"name":"mike","school_details":{"kind":"INSERT","arity":2}}
so basically Object TypeInformation is not working with Row type objects, how can I fix it.

json to Instant with a field "yyyy-MM-ddThh:mm:ss" using GSON but get java.lang.NumberFormatException: For input string: "1999-08-24T00:00:00"

I am trying json parsing with gson in a small java applicaiton. I have a json string which comes from .Net business layer, has a field as "1999-08-24T00:00:00". In my model like User model, I have java.time.Instant birthDay field. With gson i am trying to get json string to my user model. Also I have a InstantDeserializer class. But when I try to convert it I got a message like java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)..
Before the instant type I was using Date class. I wrote DateDeserializer class but I know Date class is deprecated. I googled to much page. I tried many things but i didn't how to figure out. So i just want to ask where I am making mistakes. What sould I do? How can I make my code more clear or what is the best approch? If you could give some code examples, I can understand better.
Any advice is appreciated..
Here is my code..
JSON String :
{
"Value":{
"ID":"123",
"NAME":"John",
"SURNAME":"Concept",
"BIRTHDAY":"1999-08-24T00:00:00",
"PAYMENTINFORMATION":[
{
"ID":"1",
"PAYMENTINFO":"RECIEVED"
}
]
},
"Succued": true
}
UserModel class
package Models;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.time.LocalDateTime;
import java.util.Date;
import com.google.gson.annotations.SerializedName;
import java.time.Instant;
public class UserModel {
private long id;
private String name;
private String surname;
private Instant birthday;
private List<PaymentModel> paymentInformation;
//GETTER SETTER
public UserModel() {
paymentInformation= new ArrayList<>();
}
}
InstantDeserializer class
package Util;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class InstantDeSerializer implements JsonDeserializer<Instant> {
#Override
public Instant deserialize(JsonElement jelement, Type type, JsonDeserializationContext jdc) throws JsonParseException {
Instant insObj= Instant.ofEpochMilli(jelement.getAsJsonPrimitive().getAsLong());
return insObj;
}
}
And Main class
public class JSONTryMe {
public static void main(String[] args) throws Exception {
JSONObject responseJSON = new JSONObject(jsonString);
if (responseJSON.isNull("Value")) {
return;
}
GsonBuilder build = new GsonBuilder();
build.registerTypeAdapter(Instant.class, new InstantDeSerializer());
Gson gObj = build.create();
UserModel user = gObj.fromJson(responseJSON.getJSONObject("Value").toString(), UserModel.class);
System.out.println(user.getBirthday().toString());
}
}
Ant the error stackTrace is
java.lang.NumberFormatException: For input string: "1999-08-24T00:00:00"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at com.google.gson.JsonPrimitive.getAsLong(JsonPrimitive.java:206)
at Util.InstantDeSerializer.deserialize(InstantDeSerializer.java:25)
at Util.InstantDeSerializer.deserialize(InstantDeSerializer.java:21)
at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.Gson.fromJson(Gson.java:932)
at com.google.gson.Gson.fromJson(Gson.java:897)
at com.google.gson.Gson.fromJson(Gson.java:846)
at com.google.gson.Gson.fromJson(Gson.java:817)
at Source.JSONTryMe.main(JSONTryMe.java:85)
Here are several conceptual flaws:
Birth dates should use LocalDate
Your JSON input provides ISO datetime, but your deserializer tries to read milliseconds since epoch. Use LocalDate#parse() for this

Passing a variable between classes

I am trying to pass a variable "budget" from my DocumentClass of a flash file, to a class.
Currently I have :
(DocumentClassv5 , this is the code thats attached to the flash file in the properties panel, some code ommited)
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.*;
import flash.ui.Keyboard;
import flash.display.Stage;
import flash.text.TextFieldType;
import flash.media.Sound;
import flash.media.SoundChannel;
import miniGameOne;
import floorTileMC;
import flash.display.Loader;
import flash.net.URLRequest;
public class DocumentClassv5 extends MovieClip
{
/*#################################
## Defining Variables ##
#################################*/
public var budget:int = 0;
var gameOne:miniGameOne = new miniGameOne();
/*#################################
## Constructor ##
#################################*/
public function DocumentClassv5()
{
/*#################################
## Adding Event Listeners ##
#################################*/
trace("Document class loaded");
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*###################################################
## Begins the mini game ##
###################################################*/
public function begin(evt: MouseEvent)
{
beginGame.visible = false;
beginGame.removeEventListener(MouseEvent.CLICK, begin);
budget = 500;
cleanListeners();
gameOne.loadGame();
trace(gameOne.testVar);
trace(floorTile.testVar2);
/*#################################
## Adding Event Listeners ##
#################################*/
trace("Game started");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
Then I have the miniGameOne class file, which at the moment does nothing
I also have another class file, called tileFloorMC. This is attached to a symbol.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import DocumentClassv5;
public class floorTileMC extends MovieClip
{
var propertyA:Number;
//var hackerClass:DocumentClassv5 = new DocumentClassv5;
public var testVar2:int = 50;
public function floorTileMC()
{
this.propertyA = randomRange(100, 500);
this.addEventListener(MouseEvent.ROLL_OVER, manageMouseOver, false, 0, true);
this.addEventListener(MouseEvent.ROLL_OUT, manageMouseOut, false, 0, true);
}
private function manageMouseOver(evt: MouseEvent)
{
this.gotoAndStop(2);
//trace(mainClass.budget);
}
private function manageMouseOut(evt: MouseEvent)
{
this.gotoAndStop(1);
//mainClass.budget += 1;
}
private function randomRange(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
}
}
Now, essentially I will need to be able to pass budget from DocumentClassv5 TO floorTileMC, and then BACK to DocumentClassv5. At the moment, I can pass anything from floorTileMC and anything from miniGameOne into DocumentClassv5, but when i try and pass from floorTileMC to DocumentClassv5, I get error
Error #2136: The SWF file file:///yadayada/GameV5.swf contains invalid data.
More specifically, as soon as I uncomment //var hackerClass:DocumentClassv5 = new DocumentClassv5;
Any help would be greatly appreciated!
Thanks,
Tiffany
You're trying to instantiate your document class in a subclass:
var hackerClass:DocumentClassv5 = new DocumentClassv5();
You want access to access the existing instance, not create a new one.
One thing you can do, is create a static reference to your document class. (see code sample below)
Static references can get ugly though, and you may just want to pass a reference of your doc class to your other classes when you instantiate them.
Both method below:
In your document class:
//instead of the line below:
var gameOne:miniGameOne = new miniGameOne(); //It's a bad idea to instantiate non primitive objects before the constructor of your document class runs.
//just declare it, and instantiate it in the constructor
var gameOne:miniGameOne;
//if you want to use a static reference:
public static var me:DocumentClassv5;
public function DocumentClassv5()
{
/*#################################
## Adding Event Listeners ##
#################################*/
//if using the static var me, set it's value to this (the instance of the document class):
me = this;
trace("Document class loaded");
gameOne = new miniGameOne(this); //pass a reference to the document class if NOT using the static var me
}
If using the static var me, you access it by doing the following from any class:
DocumentClassV5.me.budget;
Another cleaner alternative (if the values you need access to aren't really directly tied to any class, eg. global preferences), is make a whole new class that is just static (doesn't get instantiated) to hold your preferences.
package {
public class Global {
public static var budget:Number = 50;
}
}
Then you'd access budget by importing your Global class and doing Global.budget = 5

Grails Date unmarshalling

If I get the following json from a RESTful client, how do I elegantly unmarshal the java.util.Date? (Is it possible without providing (aka. hard-coding) the format, that's what I mean by elegantly...)
{
"class": "url",
"link": "http://www.empa.ch",
"rating": 5,
"lastcrawl" : "2009-06-04 16:53:26.706 CEST",
"checksum" : "837261836712xxxkfjhds",
}
The cleanest way is probably to register a custom DataBinder for possible date formats.
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CustomDateBinder extends PropertyEditorSupport {
private final List<String> formats;
public CustomDateBinder(List formats) {
List<String> formatList = new ArrayList<String>(formats.size());
for (Object format : formats) {
formatList.add(format.toString()); // Force String values (eg. for GStrings)
}
this.formats = Collections.unmodifiableList(formatList);
}
#Override
public void setAsText(String s) throws IllegalArgumentException {
if (s != null)
for (String format : formats) {
// Need to create the SimpleDateFormat every time, since it's not thead-safe
SimpleDateFormat df = new SimpleDateFormat(format);
try {
setValue(df.parse(s));
return;
} catch (ParseException e) {
// Ignore
}
}
}
}
You'd also need to implement a PropertyEditorRegistrar
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import grails.util.GrailsConfig;
import java.util.Date;
import java.util.List;
public class CustomEditorRegistrar implements PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry reg) {
reg.registerCustomEditor(Date.class, new CustomDateBinder(GrailsConfig.get("grails.date.formats", List.class)));
}
}
and create a Spring-bean definition in your grails-app/conf/spring/resources.groovy:
beans = {
"customEditorRegistrar"(CustomEditorRegistrar)
}
and finally define the date formats in your grails-app/conf/Config.groovy:
grails.date.formats = ["yyyy-MM-dd HH:mm:ss.SSS ZZZZ", "dd.MM.yyyy HH:mm:ss"]
Be aware that the new version of Grails 2.3+ supports this type of feature out of the box.
See Date Formats for Data Binding
With that said, if you are forced to use a version of Grails prior to 2.3, the CustomEditorRegistrar
can be updated using the following code to eliminate the deprecation warning, and also uses the #Component annotation, which allows you to remove / skip the step of adding the bean directly in resources.groovy.
Also not that I changed the grails configuration property name to grails.databinding.dateFormats, which matches the property now supported in Grails 2.3+. Finally, my version is a .groovy, not .java file.
import javax.annotation.Resource
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.springframework.beans.PropertyEditorRegistrar
import org.springframework.beans.PropertyEditorRegistry
import org.springframework.stereotype.Component
#Component
public class CustomEditorRegistrar implements PropertyEditorRegistrar {
#Resource
GrailsApplication grailsApplication
public void registerCustomEditors(PropertyEditorRegistry reg){
def dateFormats = grailsApplication.config.grails.databinding.dateFormats as List
reg.registerCustomEditor(Date.class, new CustomDateBinder(dateFormats))
}
}