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=true
annotation.processing.enabled.in.editor=false annotation.processing.enabled.in.editor=false
annotation.processing.processors.list= annotation.processing.processors.list=

View File

@ -201,6 +201,14 @@ public class AssetEditorDialog extends EscDialog {
AssetEditorDialog d = new AssetEditorDialog(parent); AssetEditorDialog d = new AssetEditorDialog(parent);
Json.put(d.assetEditorPanel, o);
d.setVisible(true);
d.dispose();
d.assetEditorPanel.symField.setText("Hallo"); d.assetEditorPanel.symField.setText("Hallo");
//d.assetEditorPanel.symField.setText("Herr"); //d.assetEditorPanel.symField.setText("Herr");
JSONObject jo = Json.get(d.assetEditorPanel); 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"/> <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="symFieldActionPerformed"/>
</Events> </Events>
<AuxValues> <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"/> <AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="1"/>
</AuxValues> </AuxValues>
</Component> </Component>
@ -116,7 +116,8 @@
</Component> </Component>
<Component class="opensesim.gui.util.JTextFieldLimit" name="nameField"> <Component class="opensesim.gui.util.JTextFieldLimit" name="nameField">
<AuxValues> <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> </AuxValues>
</Component> </Component>
<Component class="javax.swing.JLabel" name="jLabel4"> <Component class="javax.swing.JLabel" name="jLabel4">

View File

@ -43,6 +43,7 @@ import opensesim.AbstractAsset;
import opensesim.gui.Globals; import opensesim.gui.Globals;
import opensesim.gui.util.JTextFieldLimit; import opensesim.gui.util.JTextFieldLimit;
import opensesim.gui.util.Json.Export; 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 jLabel3;
private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel4;
private javax.swing.JLabel label; private javax.swing.JLabel label;
protected opensesim.gui.util.JTextFieldLimit nameField; @Import("name")
@Export @Export("name")
public opensesim.gui.util.JTextFieldLimit nameField;
@Export("symbol")
@Import("symbol")
public opensesim.gui.util.JTextFieldLimit symField; public opensesim.gui.util.JTextFieldLimit symField;
// End of variables declaration//GEN-END:variables // End of variables declaration//GEN-END:variables
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -54,35 +54,66 @@ public class Json {
public String value() default ""; 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) { public static JSONObject get(Object o) {
Field[] fields = o.getClass().getFields(); Field[] fields = o.getClass().getFields();
JSONObject jo = new JSONObject(); JSONObject jo = new JSONObject();
for (Field f : fields) { for (Field f : fields) {
System.out.printf("CHECKFIELD %s\n", f.getName()); Export exp = f.getAnnotation(Export.class);
Export ex = f.getAnnotation(Export.class); if (exp == null) {
if (ex == null) {
continue; continue;
} }
Class cls = f.getType(); Class cls = f.getType();
if (JTextField.class.isAssignableFrom(cls)){ if (JTextField.class.isAssignableFrom(cls)) {
try { try {
JTextField tf = (JTextField) f.get(o); 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) { } catch (IllegalArgumentException | IllegalAccessException ex1) {
Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex1); Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex1);
} }
} }
System.out.printf("EX: %s\n", ex.getClass().getName());
} }
return jo; 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);
}
}
}
}
} }