Create JSONObject by annotations

This commit is contained in:
2018-12-01 03:28:05 +01:00
parent 044f23b338
commit f9949fa5ad
11 changed files with 914 additions and 285 deletions

View File

@ -25,25 +25,22 @@
*/
package opensesim.gui.AssetEditor;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.awt.Dialog;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.Window;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import opensesim.AbstractAsset;
import opensesim.World;
import opensesim.gui.EscDialog;
import opensesim.gui.Globals;
import opensesim.gui.util.Json;
import opensesim.gui.util.Json.Export;
import opensesim.util.IDGenerator.Id;
import org.json.JSONException;
import org.json.JSONObject;
@JsonIgnoreProperties(ignoreUnknown = true)
/**
*
* @author 7u83 <7u83@mail.ru>
@ -63,6 +60,12 @@ public class AssetEditorDialog extends EscDialog {
world = Globals.world;
}
public AssetEditorDialog(Window parent) {
super(parent, true);
initComponents();
world = Globals.world;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
@ -124,6 +127,10 @@ public class AssetEditorDialog extends EscDialog {
}// </editor-fold>//GEN-END:initComponents
private Id newId = null;
@Export
public String hallo = "hallo";
public Id getCreatedId() {
return newId;
@ -185,6 +192,47 @@ public class AssetEditorDialog extends EscDialog {
}//GEN-LAST:event_cancelButtonActionPerformed
static public boolean runDialog(Window parent, JSONObject o) {
//JSONObject jo = new org.json.JSONObject(parent, new String[]{"getMyName"});
//System.out.printf("PARENT: %s", jo.toString());
// JSONObject.
AssetEditorDialog d = new AssetEditorDialog(parent);
d.assetEditorPanel.symField.setText("Hallo");
//d.assetEditorPanel.symField.setText("Herr");
JSONObject jo = Json.get(d.assetEditorPanel);
System.out.printf("Resulting JSONN %s\n", jo.toString(5));
//Class aClass = d.assetEditorPanel.getClass().getDeclaredFields();
Field[] fields = d.assetEditorPanel.getClass().getFields();
for (Field f : fields) {
Export ex = f.getAnnotation(Export.class);
System.out.printf("Fieldname: %s\n",f.getName());
if (ex == null){
continue;
}
System.out.printf("EX: %s\n", ex.getClass().getName());
}
return true;
}
/*
static public Id runDialog(Dialog parent, AbstractAsset asset) {
AssetEditorDialog dialog = new AssetEditorDialog(parent, asset);
dialog.assetEditorPanel.initFields(asset);
@ -215,7 +263,7 @@ public class AssetEditorDialog extends EscDialog {
return dialog.newId;
}
*/
/**
* @param args the command line arguments
*/

View File

@ -25,7 +25,12 @@
*/
package opensesim.gui.AssetEditor;
import com.fasterxml.jackson.annotation.JsonGetter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.logging.Level;
@ -37,6 +42,7 @@ import javax.swing.JPanel;
import opensesim.AbstractAsset;
import opensesim.gui.Globals;
import opensesim.gui.util.JTextFieldLimit;
import opensesim.gui.util.Json.Export;
/**
*
@ -44,6 +50,10 @@ import opensesim.gui.util.JTextFieldLimit;
*/
public class AssetEditorPanel extends javax.swing.JPanel {
ArrayList<Class<AbstractAsset>> asset_types;
/**
@ -58,6 +68,19 @@ public class AssetEditorPanel extends javax.swing.JPanel {
AbstractAsset a1, a2;
try {
a1 = o1.newInstance();
try {
try {
a1 = o1.getConstructor().newInstance(null);
} catch (IllegalArgumentException ex) {
Logger.getLogger(AssetEditorPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(AssetEditorPanel.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (NoSuchMethodException ex) {
Logger.getLogger(AssetEditorPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(AssetEditorPanel.class.getName()).log(Level.SEVERE, null, ex);
}
a2 = o2.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(AssetEditorPanel.class.getName()).log(Level.SEVERE, null, ex);
@ -86,20 +109,25 @@ public class AssetEditorPanel extends javax.swing.JPanel {
symField.setText(asset.getSymbol());
nameField.setText(asset.getName());
decimalsField.getModel().setValue(asset.getDecimals());
}
@JsonGetter("name")
public String getNameField() {
return nameField.getText();
}
@JsonGetter("sym")
public String getSymField() {
return symField.getText();
}
@Export
public String hallo = "hello";
public JDialog dialog;
ComboBoxModel getComboBoxModel() {
ArrayList vector = new ArrayList();
@ -116,7 +144,7 @@ public class AssetEditorPanel extends javax.swing.JPanel {
for (i = 0; i < asset_types.size(); i++) {
AbstractAsset ait;
Class <AbstractAsset> asset_type = asset_types.get(i);
Class<AbstractAsset> asset_type = asset_types.get(i);
try {
ait = asset_type.newInstance();
@ -164,6 +192,12 @@ public class AssetEditorPanel extends javax.swing.JPanel {
jLabel2.setText("Type:");
symField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
symFieldActionPerformed(evt);
}
});
jLabel3.setText("Name:");
jLabel4.setText("Decimals:");
@ -279,6 +313,10 @@ public class AssetEditorPanel extends javax.swing.JPanel {
}//GEN-LAST:event_assetTypesComboBoxActionPerformed
private void symFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_symFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_symFieldActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
public javax.swing.JComboBox<String> assetTypesComboBox;
@ -291,6 +329,7 @@ public class AssetEditorPanel extends javax.swing.JPanel {
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel label;
protected opensesim.gui.util.JTextFieldLimit nameField;
protected opensesim.gui.util.JTextFieldLimit symField;
@Export
public opensesim.gui.util.JTextFieldLimit symField;
// End of variables declaration//GEN-END:variables
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2018, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.gui.AssetEditor;
import org.json.JSONObject;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public interface GuiSelectionList {
JSONObject getSelectedObject();
}

View File

@ -25,6 +25,7 @@
*/
package opensesim.gui;
import com.google.gson.Gson;
import opensesim.chart.Help;
import opensesim.chart.SuperDlg;
import java.awt.Dialog;
@ -940,13 +941,50 @@ public class SeSimApplication extends javax.swing.JFrame {
ExchangeListDialog.runDialog(this);
}//GEN-LAST:event_exchangesMenuItemActionPerformed
public static class Pojo {
public String getSymbol() {
return symbol;
}
public String getName() {
return name;
}
public String symbol = "EUR";
public String name = "Euro";
};
/**
* @param args the command line arguments
* @throws java.lang.IllegalAccessException
* @throws java.lang.InstantiationException
*/
public static void main(String args[]) throws IllegalAccessException, InstantiationException {
// testing
Pojo p = new Pojo();
Gson g = new Gson();
String r = g.toJson(p);
System.out.printf("GSON: %s\n", r);
JSONObject o = new JSONObject(p,new String[]{"name","symbol"});
System.out.printf("OJSON: %s\n", o.toString(8));
// System.exit(0);
// end testing
// Initialize globals
Class<?> c = opensesim.gui.SeSimApplication.class;

View File

@ -0,0 +1,88 @@
/*
* Copyright (c) 2018, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.gui.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextField;
import org.json.JSONObject;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class Json {
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public static @interface Export {
public String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public static @interface Import {
public String value() default "";
}
public static JSONObject get(Object o) {
Field[] fields = o.getClass().getFields();
JSONObject jo = new JSONObject();
for (Field f : fields) {
System.out.printf("CHECKFIELD %s\n", f.getName());
Export ex = f.getAnnotation(Export.class);
if (ex == null) {
continue;
}
Class cls = f.getType();
if (JTextField.class.isAssignableFrom(cls)){
try {
JTextField tf = (JTextField) f.get(o);
jo.put(f.getName(), tf.getText());
} catch (IllegalArgumentException | IllegalAccessException ex1) {
Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex1);
}
}
System.out.printf("EX: %s\n", ex.getClass().getName());
}
return jo;
}
}