diff --git a/src/opensesim/gui/AssetEditor/AssetEditorDialog.java b/src/opensesim/gui/AssetEditor/AssetEditorDialog.java index b42e6b0..e610a6a 100644 --- a/src/opensesim/gui/AssetEditor/AssetEditorDialog.java +++ b/src/opensesim/gui/AssetEditor/AssetEditorDialog.java @@ -47,8 +47,8 @@ import org.json.JSONObject; */ public class AssetEditorDialog extends EscDialog { - AbstractAsset asset; - World world; + + /** * Creates new form CreateAssetDialog @@ -57,13 +57,13 @@ public class AssetEditorDialog extends EscDialog { super(parent, true); initComponents(); - world = Globals.world; + } public AssetEditorDialog(Window parent) { super(parent, true); initComponents(); - world = Globals.world; + } /** @@ -126,26 +126,22 @@ public class AssetEditorDialog extends EscDialog { pack(); }// //GEN-END:initComponents - private Id newId = null; - - @Export - public String hallo = "hallo"; - - - public Id getCreatedId() { - return newId; - } - + private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed - if (this.asset == null) { + JSONObject result = Json.get(assetEditorPanel); + System.out.printf("JSON: %s\n", result.toString(5)); + JSONObject all = Globals.getAssets(); + all.put(result.getString("symbol"), result); + Globals.putAssets(all); + + /* if (this.asset == null) { try { int selected = this.assetEditorPanel.assetTypesComboBox.getSelectedIndex(); Class cls = (Class) this.assetEditorPanel.asset_types.get(selected); asset = AbstractAsset.create(world, cls, assetEditorPanel.symField.getText()); - //new BasicAsset(assetEditor.symField.getText()); - // newId = BasicAsset.newAssed(asset); + } catch (Exception ex) { javax.swing.JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", @@ -153,7 +149,8 @@ public class AssetEditorDialog extends EscDialog { return; } } - +*/ + /*else { if (!this.asset.getSymbol().equals(assetEditor.symField.getText())) { try { @@ -169,12 +166,12 @@ public class AssetEditorDialog extends EscDialog { } }*/ - asset.setName(assetEditorPanel.nameField.getText()); - asset.setDecimals((int) assetEditorPanel.decimalsField.getValue()); + // asset.setName(assetEditorPanel.nameField.getText()); + // asset.setDecimals((int) assetEditorPanel.decimalsField.getValue()); - JSONObject cfg = world.getConfig(); + // JSONObject cfg = world.getConfig(); - try { + /* try { String jstr = cfg.toString(5); } catch (JSONException ex) { System.out.println(ex.getMessage()); @@ -182,7 +179,7 @@ public class AssetEditorDialog extends EscDialog { System.out.printf("JSONARRAY %s\n", cfg.toString(3)); JSONObject world_cfg = Globals.world.getConfig(); Globals.prefs.put("world", world_cfg.toString()); - dispose(); + */ dispose(); }//GEN-LAST:event_okButtonActionPerformed diff --git a/src/opensesim/gui/AssetEditor/AssetEditorPanel.java b/src/opensesim/gui/AssetEditor/AssetEditorPanel.java index ddc1e0d..de9eace 100644 --- a/src/opensesim/gui/AssetEditor/AssetEditorPanel.java +++ b/src/opensesim/gui/AssetEditor/AssetEditorPanel.java @@ -25,13 +25,8 @@ */ package opensesim.gui.AssetEditor; -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; import java.util.logging.Logger; import javax.swing.ComboBoxModel; @@ -40,7 +35,6 @@ import javax.swing.JDialog; import javax.swing.JPanel; import opensesim.AbstractAsset; import opensesim.gui.Globals; -import opensesim.gui.util.JTextFieldLimit; import opensesim.gui.util.Json.Export; import opensesim.gui.util.Json.Import; @@ -59,7 +53,6 @@ public class AssetEditorPanel extends javax.swing.JPanel { super(); asset_types = Globals.getAvailableAssetsTypes(true); - initComponents(); symField.setLimit(Globals.MAX.SYMLEN); nameField.setLimit(Globals.MAX.NAMELEN); @@ -84,8 +77,6 @@ public class AssetEditorPanel extends javax.swing.JPanel { return symField.getText(); } - - public void putType(String type) { System.out.printf("Here we have a type: %s\n", type); } @@ -221,17 +212,30 @@ public class AssetEditorPanel extends javax.swing.JPanel { ); }// //GEN-END:initComponents + @Export + public String getDeecimals(){ + return decimalsField.getValue().toString(); + } + + @Import + public void setDecimals(String d){ + decimalsField.setValue(Integer.parseInt(d)); + } + + @Export("type") + public String getType() { + int selected = assetTypesComboBox.getSelectedIndex(); + return asset_types.get(selected).getName(); + } + @Import("type") public void setType(String type) { - Class ac = (Class) Globals.getClassByName(type); if (ac == null) { return; } - - AbstractAsset a; try { @@ -262,7 +266,6 @@ public class AssetEditorPanel extends javax.swing.JPanel { assetTypesComboBox.setSelectedIndex(i); } } - } diff --git a/src/opensesim/gui/AssetEditor/AssetListDialog.form b/src/opensesim/gui/AssetEditor/AssetListDialog.form index 2efcec2..f3a55dd 100644 --- a/src/opensesim/gui/AssetEditor/AssetListDialog.form +++ b/src/opensesim/gui/AssetEditor/AssetListDialog.form @@ -85,6 +85,9 @@ + + + diff --git a/src/opensesim/gui/AssetEditor/AssetListDialog.java b/src/opensesim/gui/AssetEditor/AssetListDialog.java index 3665453..117d58c 100644 --- a/src/opensesim/gui/AssetEditor/AssetListDialog.java +++ b/src/opensesim/gui/AssetEditor/AssetListDialog.java @@ -26,6 +26,7 @@ package opensesim.gui.AssetEditor; import java.awt.event.MouseEvent; +import opensesim.gui.Globals; import opensesim.gui.util.EscDialog; import org.json.JSONObject; @@ -92,6 +93,11 @@ public class AssetListDialog extends EscDialog { }); jButton3.setText("Delete"); + jButton3.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton3ActionPerformed(evt); + } + }); editButton.setText("Edit ..."); editButton.addActionListener(new java.awt.event.ActionListener() { @@ -139,12 +145,13 @@ public class AssetListDialog extends EscDialog { private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed String type = SelectAssetTypeDialog.runDialog(this); - if (type==null) + if (type == null) { return; + } JSONObject o = new JSONObject(); o.put("type", type); AssetEditorDialog.runDialog(this, o, o); - this.assetListPanel.reload(); + assetListPanel.reload(); }//GEN-LAST:event_newButtonActionPerformed @@ -154,26 +161,25 @@ public class AssetListDialog extends EscDialog { private void doEdit() { JSONObject o = assetListPanel.getSelectedObject(); - System.out.printf("JON: %s", o.toString(4)); - AssetEditorDialog.runDialog(this, o, null); + AssetEditorDialog.runDialog(this, o, o); + assetListPanel.reload(); } private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed doEdit(); -// Id selId = (Id)assetList1.assetList.getValueAt(row, 0); -/* - BasicAsset a = BasicAsset.getAsset(selId); - Id id = AssteEditorDialog.runDialog(this, a); - if (id != null) { - // assetList1.addAsset(id); - } - this.assetList1.reload(); - this.repaint(); - */ + }//GEN-LAST:event_editButtonActionPerformed + private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed + JSONObject o = assetListPanel.getSelectedObject(); + JSONObject ass = Globals.getAssets(); + ass.remove(o.getString("symbol")); + Globals.putAssets(ass); + assetListPanel.reload(); + }//GEN-LAST:event_jButton3ActionPerformed + /** * @param args the command line arguments */ diff --git a/src/opensesim/gui/AssetEditor/AssetListPanel.java b/src/opensesim/gui/AssetEditor/AssetListPanel.java index 33b7805..c1f4d25 100644 --- a/src/opensesim/gui/AssetEditor/AssetListPanel.java +++ b/src/opensesim/gui/AssetEditor/AssetListPanel.java @@ -25,8 +25,12 @@ */ package opensesim.gui.AssetEditor; +import java.lang.reflect.InvocationTargetException; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; +import opensesim.AbstractAsset; import opensesim.World; import opensesim.gui.Globals; import org.json.JSONObject; @@ -54,45 +58,29 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi json_set = new JSONObject(Globals.prefs.get("myassets", "{" + "EUR:{name:Euro,decimals:8,type:opensesim.sesim.Assets.FurtureAsset}}")); + + json_set = Globals.getAssets(); reload(); 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); + + assetTable.getColumnModel().getColumn(0).setPreferredWidth(30); + assetTable.getColumnModel().getColumn(1).setPreferredWidth(250); + assetTable.getColumnModel().getColumn(2).setPreferredWidth(80); } @Override public JSONObject getSelectedObject() { int row = assetTable.getSelectedRow(); - String symbol = (String) assetTable.getValueAt(row, 1); + String symbol = (String) assetTable.getValueAt(row, 0); return json_set.getJSONObject(symbol); } final void reload() { + json_set = Globals.getAssets(); DefaultTableModel m = (DefaultTableModel) assetTable.getModel(); - /* m.setRowCount(0); - Map assets = BasicAsset.getAssets(); - for (Object key : assets.keySet()) { - addAsset((Id) key); - } - m.fireTableDataChanged(); - */ - if (world == null) { - return; - } - m.setRowCount(0); - /* 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); @@ -100,25 +88,27 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi continue; } + Class a = Globals.getClassByName(o.optString("type")); + String type_name; + + try { + type_name=a.getConstructor().newInstance().getTypeName(); + + } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { + Logger.getLogger(AssetListPanel.class.getName()).log(Level.SEVERE, null, ex); + continue; + } + + m.addRow(new Object[]{ - o.opt("id"), + symbol, o.opt("name"), - o.opt("type") + type_name }); } - /* Collection ac; - ObjectMapper om = new ObjectMapper(); - om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); - try { - String s = om.writeValueAsString(world.getAssetCollection()); - System.out.printf("MyValues %s", s); - } catch (JsonProcessingException ex) { - Logger.getLogger(AssetListPanel.class.getName()).log(Level.SEVERE, null, ex); - } - */ } private TableModel getModel() { @@ -137,7 +127,7 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi } DefaultTableModel model = new TModel( - new Object[]{"ID", "Symbol", "Name", "Type"}, 0 + new Object[]{"Symbol", "Name", "Type"}, 0 ); assetTable.setAutoCreateRowSorter(true); @@ -154,16 +144,7 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi } - /* public void addAsset(Id id) { - BasicAsset a = (BasicAsset) BasicAsset.getAsset(id); - DefaultTableModel m = (DefaultTableModel) this.assetList.getModel(); - m.addRow(new Object[]{ - a.getID(), - a.getSymbol(), - a.getName()}); - } - */ /** * 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 diff --git a/src/opensesim/gui/util/Json.java b/src/opensesim/gui/util/Json.java index 4014adc..d5c75aa 100644 --- a/src/opensesim/gui/util/Json.java +++ b/src/opensesim/gui/util/Json.java @@ -84,8 +84,33 @@ public class Json { Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex1); } } - } + + Method[] methods = o.getClass().getMethods(); + for (Method m : methods) { + Export exp = m.getAnnotation(Export.class); + if (exp == null) { + continue; + } + + if (m.getParameterCount() != 0) { + Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, "Wrong pcount"); + continue; + } + + String name = null == exp.value() ? m.getName() : exp.value(); + + Class rt = m.getReturnType(); + if (String.class.isAssignableFrom(rt)) { + try { + String str = (String) m.invoke(o); + jo.put(name, str); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { + Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + return jo; } @@ -124,14 +149,14 @@ public class Json { } if (m.getParameterCount() != 1) { - Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, "Wrong pcouunt"); + Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, "Wrong pcount"); continue; } String name = null == imp.value() ? m.getName() : imp.value(); - + Class p0 = m.getParameterTypes()[0]; - if (String.class.isAssignableFrom(p0)){ + if (String.class.isAssignableFrom(p0)) { String param = jo.optString(name, ""); try { m.invoke(o, param);