Can partially fill Objects from JSONObject

This commit is contained in:
7u83 2018-12-01 03:55:38 +01:00
parent f9949fa5ad
commit abee1a8d36
9 changed files with 152 additions and 65 deletions

View File

@ -1,4 +1,4 @@
#Sat, 01 Dec 2018 03:21:26 +0100
#Sat, 01 Dec 2018 03:52:02 +0100
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=

View File

@ -201,6 +201,14 @@ public class AssetEditorDialog extends EscDialog {
AssetEditorDialog d = new AssetEditorDialog(parent);
Json.put(d.assetEditorPanel, o);
d.setVisible(true);
d.dispose();
d.assetEditorPanel.symField.setText("Hallo");
//d.assetEditorPanel.symField.setText("Herr");
JSONObject jo = Json.get(d.assetEditorPanel);

View File

@ -105,7 +105,7 @@
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="symFieldActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_DeclarationPre" type="java.lang.String" value="@Export"/>
<AuxValue name="JavaCodeGenerator_DeclarationPre" type="java.lang.String" value="@Export(&quot;symbol&quot;)&#xa;@Import(&quot;symbol&quot;)"/>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="1"/>
</AuxValues>
</Component>
@ -116,7 +116,8 @@
</Component>
<Component class="opensesim.gui.util.JTextFieldLimit" name="nameField">
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="4"/>
<AuxValue name="JavaCodeGenerator_DeclarationPre" type="java.lang.String" value="@Import(&quot;name&quot;)&#xa;@Export(&quot;name&quot;)"/>
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="1"/>
</AuxValues>
</Component>
<Component class="javax.swing.JLabel" name="jLabel4">

View File

@ -43,6 +43,7 @@ import opensesim.AbstractAsset;
import opensesim.gui.Globals;
import opensesim.gui.util.JTextFieldLimit;
import opensesim.gui.util.Json.Export;
import opensesim.gui.util.Json.Import;
/**
*
@ -328,8 +329,11 @@ public class AssetEditorPanel extends javax.swing.JPanel {
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel label;
protected opensesim.gui.util.JTextFieldLimit nameField;
@Export
@Import("name")
@Export("name")
public opensesim.gui.util.JTextFieldLimit nameField;
@Export("symbol")
@Import("symbol")
public opensesim.gui.util.JTextFieldLimit symField;
// End of variables declaration//GEN-END:variables
}

View File

@ -39,7 +39,7 @@
</Group>
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="assetListPanel1" max="32767" attributes="0"/>
<Component id="assetListPanel" max="32767" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
@ -50,7 +50,7 @@
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="assetListPanel1" pref="282" max="32767" attributes="0"/>
<Component id="assetListPanel" pref="282" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="newButton" alignment="3" min="-2" max="-2" attributes="0"/>
@ -94,7 +94,7 @@
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="editButtonActionPerformed"/>
</Events>
</Component>
<Component class="opensesim.gui.AssetEditor.AssetListPanel" name="assetListPanel1">
<Component class="opensesim.gui.AssetEditor.AssetListPanel" name="assetListPanel">
</Component>
</SubComponents>
</Form>

View File

@ -28,11 +28,14 @@ package opensesim.gui.AssetEditor;
import com.sun.rowset.internal.Row;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import opensesim.gui.EscDialog;
import opensesim.sesim.Assets.BasicAsset;
import opensesim.util.IDGenerator.Id;
import org.json.JSONObject;
/**
*
@ -40,12 +43,29 @@ import opensesim.util.IDGenerator.Id;
*/
public class AssetListDialog extends EscDialog {
String getMyName(){
return "Miau!";
}
/**
* Creates new form EditAssetsDialog
*/
public AssetListDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
java.awt.event.MouseAdapter l = new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
doEdit();
}
}
};
this.assetListPanel.assetTable.addMouseListener(l);
}
/**
@ -61,7 +81,7 @@ public class AssetListDialog extends EscDialog {
newButton = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
editButton = new javax.swing.JButton();
assetListPanel1 = new opensesim.gui.AssetEditor.AssetListPanel();
assetListPanel = new opensesim.gui.AssetEditor.AssetListPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Edit Assets");
@ -108,14 +128,14 @@ public class AssetListDialog extends EscDialog {
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(assetListPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addComponent(assetListPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(assetListPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
.addComponent(assetListPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(newButton)
@ -130,15 +150,15 @@ public class AssetListDialog extends EscDialog {
private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
AssetEditorDialog.runDialog(this, null);
this.assetListPanel1.reload();
//AssetEditorDialog.runDialog(this, null);
this.assetListPanel.reload();
/* javax.swing.JOptionPane.showMessageDialog(this, "Hello", "Error",
javax.swing.JOptionPane.ERROR_MESSAGE);*/
/* Id id = AssteEditorDialog.runDialog(this, null);
if (id != null) {
assetList1.addAsset(id);
}
*/ this.repaint();
*/ this.repaint();
// JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);
/*
@ -179,9 +199,16 @@ public class AssetListDialog extends EscDialog {
dispose();
}//GEN-LAST:event_jButton1ActionPerformed
private void doEdit() {
JSONObject o = assetListPanel.getSelectedObject();
System.out.printf("JON: %s",o.toString(4));
AssetEditorDialog.runDialog(this, o);
}
private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
// int row = assetList1.assetList.getSelectedRow();
doEdit();
// Id selId = (Id)assetList1.assetList.getValueAt(row, 0);
/*
BasicAsset a = BasicAsset.getAsset(selId);
@ -191,7 +218,7 @@ public class AssetListDialog extends EscDialog {
}
this.assetList1.reload();
this.repaint();
*/
*/
}//GEN-LAST:event_editButtonActionPerformed
/**
@ -238,7 +265,7 @@ public class AssetListDialog extends EscDialog {
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private opensesim.gui.AssetEditor.AssetListPanel assetListPanel1;
private opensesim.gui.AssetEditor.AssetListPanel assetListPanel;
private javax.swing.JButton editButton;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;

View File

@ -39,7 +39,7 @@
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTable" name="assetList">
<Component class="javax.swing.JTable" name="assetTable">
<Properties>
<Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="getModel()" type="code"/>

View File

@ -25,49 +25,52 @@
*/
package opensesim.gui.AssetEditor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import opensesim.AbstractAsset;
import opensesim.sesim.Assets.BasicAsset;
import opensesim.World;
import opensesim.gui.Globals;
import opensesim.util.IDGenerator.Id;
import opensesim.util.SeSimObjectMapper;
import org.json.JSONObject;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class AssetListPanel extends javax.swing.JPanel {
public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionList{
World world;
JSONObject json_set;
/**
* Creates new form AssetList
*/
public AssetListPanel() {
world = Globals.world;
initComponents();
if (Globals.prefs == null) {
return;
}
json_set = new JSONObject(Globals.prefs.get("myassets", "{EUR:{name:Euro,decimals:8}}"));
reload();
assetList.setRowSelectionAllowed(true);
assetList.getColumnModel().getColumn(0).setPreferredWidth(10);
assetList.getColumnModel().getColumn(1).setPreferredWidth(30);
assetList.getColumnModel().getColumn(2).setPreferredWidth(300);
assetList.getColumnModel().getColumn(3).setPreferredWidth(30);
assetTable.setRowSelectionAllowed(true);
assetTable.getColumnModel().getColumn(0).setPreferredWidth(10);
assetTable.getColumnModel().getColumn(1).setPreferredWidth(30);
assetTable.getColumnModel().getColumn(2).setPreferredWidth(300);
assetTable.getColumnModel().getColumn(3).setPreferredWidth(30);
}
@Override
public JSONObject getSelectedObject() {
int row = assetTable.getSelectedRow();
String symbol = (String) assetTable.getValueAt(row, 1);
return json_set.getJSONObject(symbol);
}
final void reload() {
DefaultTableModel m = (DefaultTableModel) assetList.getModel();
DefaultTableModel m = (DefaultTableModel) assetTable.getModel();
/* m.setRowCount(0);
Map assets = BasicAsset.getAssets();
for (Object key : assets.keySet()) {
@ -80,18 +83,32 @@ public class AssetListPanel extends javax.swing.JPanel {
}
m.setRowCount(0);
for (AbstractAsset a : world.getAssetCollection()) {
/* for (AbstractAsset a : world.getAssetCollection()) {
m.addRow(new Object[]{
a.getID(),
a.getSymbol(),
a.getName(),
a.getTypeName()
});
}
*/
m.setRowCount(0);
for (String symbol : json_set.keySet()) {
JSONObject o = json_set.optJSONObject(symbol);
if (o == null) {
continue;
}
m.addRow(new Object[]{
o.opt("id"),
symbol,
o.opt("name")
});
}
Collection ac;
ObjectMapper om = new ObjectMapper();
/* Collection ac;
ObjectMapper om = new ObjectMapper();
om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
try {
String s = om.writeValueAsString(world.getAssetCollection());
@ -99,8 +116,7 @@ public class AssetListPanel extends javax.swing.JPanel {
} catch (JsonProcessingException ex) {
Logger.getLogger(AssetListPanel.class.getName()).log(Level.SEVERE, null, ex);
}
*/
}
private TableModel getModel() {
@ -122,8 +138,8 @@ public class AssetListPanel extends javax.swing.JPanel {
new Object[]{"ID", "Symbol", "Name", "Type"}, 0
);
assetList.setAutoCreateRowSorter(true);
assetList.getTableHeader().setReorderingAllowed(false);
assetTable.setAutoCreateRowSorter(true);
assetTable.getTableHeader().setReorderingAllowed(false);
return model;
}
@ -131,7 +147,7 @@ public class AssetListPanel extends javax.swing.JPanel {
public void uppdate() {
DefaultTableModel m;
m = (DefaultTableModel) this.assetList.getModel();
m = (DefaultTableModel) this.assetTable.getModel();
m.fireTableDataChanged();
}
@ -156,11 +172,11 @@ public class AssetListPanel extends javax.swing.JPanel {
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
assetList = new javax.swing.JTable();
assetTable = new javax.swing.JTable();
assetList.setModel(getModel());
assetList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.setViewportView(assetList);
assetTable.setModel(getModel());
assetTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
jScrollPane1.setViewportView(assetTable);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
@ -180,7 +196,7 @@ public class AssetListPanel extends javax.swing.JPanel {
// Variables declaration - do not modify//GEN-BEGIN:variables
public javax.swing.JTable assetList;
public javax.swing.JTable assetTable;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
}

View File

@ -54,35 +54,66 @@ public class Json {
public String value() default "";
}
/**
* Get fields from JSON Object
*
* @param o Object to get fields from
* @return the created JSONObject
*/
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) {
Export exp = f.getAnnotation(Export.class);
if (exp == null) {
continue;
}
Class cls = f.getType();
if (JTextField.class.isAssignableFrom(cls)){
if (JTextField.class.isAssignableFrom(cls)) {
try {
JTextField tf = (JTextField) f.get(o);
jo.put(f.getName(), tf.getText());
String name = null == exp.value() ? f.getName() : exp.value();
jo.put(name, 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;
}
/**
* Inverse to get
* @param o Object
* @param jo JSONObject
*/
public static void put(Object o, JSONObject jo) {
Field[] fields = o.getClass().getFields();
for (Field f : fields) {
System.out.printf("ANNOT: %s\n",f.getName());
Import imp = f.getAnnotation(Import.class);
if (imp == null) {
continue;
}
Class cls = f.getType();
if (JTextField.class.isAssignableFrom(cls)) {
try {
JTextField tf = (JTextField) f.get(o);
String name = null == imp.value() ? f.getName() : imp.value();
tf.setText(jo.optString(name));
} catch (IllegalArgumentException | IllegalAccessException ex1) {
Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex1);
}
}
}
}
}