Asset editor works now

This commit is contained in:
7u83 2018-12-02 00:36:32 +01:00
parent c426a5bb3e
commit 7a707cd10c
6 changed files with 116 additions and 101 deletions

View File

@ -47,8 +47,8 @@ import org.json.JSONObject;
*/ */
public class AssetEditorDialog extends EscDialog { public class AssetEditorDialog extends EscDialog {
AbstractAsset asset;
World world;
/** /**
* Creates new form CreateAssetDialog * Creates new form CreateAssetDialog
@ -57,13 +57,13 @@ public class AssetEditorDialog extends EscDialog {
super(parent, true); super(parent, true);
initComponents(); initComponents();
world = Globals.world;
} }
public AssetEditorDialog(Window parent) { public AssetEditorDialog(Window parent) {
super(parent, true); super(parent, true);
initComponents(); initComponents();
world = Globals.world;
} }
/** /**
@ -126,26 +126,22 @@ public class AssetEditorDialog extends EscDialog {
pack(); pack();
}// </editor-fold>//GEN-END:initComponents }// </editor-fold>//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 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 { try {
int selected = this.assetEditorPanel.assetTypesComboBox.getSelectedIndex(); int selected = this.assetEditorPanel.assetTypesComboBox.getSelectedIndex();
Class<AbstractAsset> cls = (Class<AbstractAsset>) this.assetEditorPanel.asset_types.get(selected); Class<AbstractAsset> cls = (Class<AbstractAsset>) this.assetEditorPanel.asset_types.get(selected);
asset = AbstractAsset.create(world, cls, assetEditorPanel.symField.getText()); asset = AbstractAsset.create(world, cls, assetEditorPanel.symField.getText());
//new BasicAsset(assetEditor.symField.getText());
// newId = BasicAsset.newAssed(asset);
} catch (Exception ex) { } catch (Exception ex) {
javax.swing.JOptionPane.showMessageDialog(this, ex.getMessage(), javax.swing.JOptionPane.showMessageDialog(this, ex.getMessage(),
"Error", "Error",
@ -153,7 +149,8 @@ public class AssetEditorDialog extends EscDialog {
return; return;
} }
} }
*/
/*else { /*else {
if (!this.asset.getSymbol().equals(assetEditor.symField.getText())) { if (!this.asset.getSymbol().equals(assetEditor.symField.getText())) {
try { try {
@ -169,12 +166,12 @@ public class AssetEditorDialog extends EscDialog {
} }
}*/ }*/
asset.setName(assetEditorPanel.nameField.getText()); // asset.setName(assetEditorPanel.nameField.getText());
asset.setDecimals((int) assetEditorPanel.decimalsField.getValue()); // asset.setDecimals((int) assetEditorPanel.decimalsField.getValue());
JSONObject cfg = world.getConfig(); // JSONObject cfg = world.getConfig();
try { /* try {
String jstr = cfg.toString(5); String jstr = cfg.toString(5);
} catch (JSONException ex) { } catch (JSONException ex) {
System.out.println(ex.getMessage()); System.out.println(ex.getMessage());
@ -182,7 +179,7 @@ public class AssetEditorDialog extends EscDialog {
System.out.printf("JSONARRAY %s\n", cfg.toString(3)); System.out.printf("JSONARRAY %s\n", cfg.toString(3));
JSONObject world_cfg = Globals.world.getConfig(); JSONObject world_cfg = Globals.world.getConfig();
Globals.prefs.put("world", world_cfg.toString()); Globals.prefs.put("world", world_cfg.toString());
dispose(); */ dispose();
}//GEN-LAST:event_okButtonActionPerformed }//GEN-LAST:event_okButtonActionPerformed

View File

@ -25,13 +25,8 @@
*/ */
package opensesim.gui.AssetEditor; 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.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.swing.ComboBoxModel; import javax.swing.ComboBoxModel;
@ -40,7 +35,6 @@ import javax.swing.JDialog;
import javax.swing.JPanel; import javax.swing.JPanel;
import opensesim.AbstractAsset; import opensesim.AbstractAsset;
import opensesim.gui.Globals; import opensesim.gui.Globals;
import opensesim.gui.util.JTextFieldLimit;
import opensesim.gui.util.Json.Export; import opensesim.gui.util.Json.Export;
import opensesim.gui.util.Json.Import; import opensesim.gui.util.Json.Import;
@ -59,7 +53,6 @@ public class AssetEditorPanel extends javax.swing.JPanel {
super(); super();
asset_types = Globals.getAvailableAssetsTypes(true); asset_types = Globals.getAvailableAssetsTypes(true);
initComponents(); initComponents();
symField.setLimit(Globals.MAX.SYMLEN); symField.setLimit(Globals.MAX.SYMLEN);
nameField.setLimit(Globals.MAX.NAMELEN); nameField.setLimit(Globals.MAX.NAMELEN);
@ -84,8 +77,6 @@ public class AssetEditorPanel extends javax.swing.JPanel {
return symField.getText(); return symField.getText();
} }
public void putType(String type) { public void putType(String type) {
System.out.printf("Here we have a type: %s\n", type); System.out.printf("Here we have a type: %s\n", type);
} }
@ -221,17 +212,30 @@ public class AssetEditorPanel extends javax.swing.JPanel {
); );
}// </editor-fold>//GEN-END:initComponents }// </editor-fold>//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") @Import("type")
public void setType(String type) { public void setType(String type) {
Class<AbstractAsset> ac = (Class<AbstractAsset>) Globals.getClassByName(type); Class<AbstractAsset> ac = (Class<AbstractAsset>) Globals.getClassByName(type);
if (ac == null) { if (ac == null) {
return; return;
} }
AbstractAsset a; AbstractAsset a;
try { try {
@ -262,7 +266,6 @@ public class AssetEditorPanel extends javax.swing.JPanel {
assetTypesComboBox.setSelectedIndex(i); assetTypesComboBox.setSelectedIndex(i);
} }
} }
} }

View File

@ -85,6 +85,9 @@
<Properties> <Properties>
<Property name="text" type="java.lang.String" value="Delete"/> <Property name="text" type="java.lang.String" value="Delete"/>
</Properties> </Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton3ActionPerformed"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JButton" name="editButton"> <Component class="javax.swing.JButton" name="editButton">
<Properties> <Properties>

View File

@ -26,6 +26,7 @@
package opensesim.gui.AssetEditor; package opensesim.gui.AssetEditor;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import opensesim.gui.Globals;
import opensesim.gui.util.EscDialog; import opensesim.gui.util.EscDialog;
import org.json.JSONObject; import org.json.JSONObject;
@ -92,6 +93,11 @@ public class AssetListDialog extends EscDialog {
}); });
jButton3.setText("Delete"); jButton3.setText("Delete");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
editButton.setText("Edit ..."); editButton.setText("Edit ...");
editButton.addActionListener(new java.awt.event.ActionListener() { 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 private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
String type = SelectAssetTypeDialog.runDialog(this); String type = SelectAssetTypeDialog.runDialog(this);
if (type==null) if (type == null) {
return; return;
}
JSONObject o = new JSONObject(); JSONObject o = new JSONObject();
o.put("type", type); o.put("type", type);
AssetEditorDialog.runDialog(this, o, o); AssetEditorDialog.runDialog(this, o, o);
this.assetListPanel.reload(); assetListPanel.reload();
}//GEN-LAST:event_newButtonActionPerformed }//GEN-LAST:event_newButtonActionPerformed
@ -154,26 +161,25 @@ public class AssetListDialog extends EscDialog {
private void doEdit() { private void doEdit() {
JSONObject o = assetListPanel.getSelectedObject(); JSONObject o = assetListPanel.getSelectedObject();
System.out.printf("JON: %s", o.toString(4)); AssetEditorDialog.runDialog(this, o, o);
AssetEditorDialog.runDialog(this, o, null); assetListPanel.reload();
} }
private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
doEdit(); 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 }//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 * @param args the command line arguments
*/ */

View File

@ -25,8 +25,12 @@
*/ */
package opensesim.gui.AssetEditor; 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.DefaultTableModel;
import javax.swing.table.TableModel; import javax.swing.table.TableModel;
import opensesim.AbstractAsset;
import opensesim.World; import opensesim.World;
import opensesim.gui.Globals; import opensesim.gui.Globals;
import org.json.JSONObject; 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", "{" json_set = new JSONObject(Globals.prefs.get("myassets", "{"
+ "EUR:{name:Euro,decimals:8,type:opensesim.sesim.Assets.FurtureAsset}}")); + "EUR:{name:Euro,decimals:8,type:opensesim.sesim.Assets.FurtureAsset}}"));
json_set = Globals.getAssets();
reload(); reload();
assetTable.setRowSelectionAllowed(true); assetTable.setRowSelectionAllowed(true);
assetTable.getColumnModel().getColumn(0).setPreferredWidth(10);
assetTable.getColumnModel().getColumn(1).setPreferredWidth(30); assetTable.getColumnModel().getColumn(0).setPreferredWidth(30);
assetTable.getColumnModel().getColumn(2).setPreferredWidth(300); assetTable.getColumnModel().getColumn(1).setPreferredWidth(250);
assetTable.getColumnModel().getColumn(3).setPreferredWidth(30); assetTable.getColumnModel().getColumn(2).setPreferredWidth(80);
} }
@Override @Override
public JSONObject getSelectedObject() { public JSONObject getSelectedObject() {
int row = assetTable.getSelectedRow(); int row = assetTable.getSelectedRow();
String symbol = (String) assetTable.getValueAt(row, 1); String symbol = (String) assetTable.getValueAt(row, 0);
return json_set.getJSONObject(symbol); return json_set.getJSONObject(symbol);
} }
final void reload() { final void reload() {
json_set = Globals.getAssets();
DefaultTableModel m = (DefaultTableModel) assetTable.getModel(); 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); m.setRowCount(0);
for (String symbol : json_set.keySet()) { for (String symbol : json_set.keySet()) {
JSONObject o = json_set.optJSONObject(symbol); JSONObject o = json_set.optJSONObject(symbol);
@ -100,25 +88,27 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi
continue; continue;
} }
Class <AbstractAsset> 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[]{ m.addRow(new Object[]{
o.opt("id"),
symbol, symbol,
o.opt("name"), 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() { private TableModel getModel() {
@ -137,7 +127,7 @@ public class AssetListPanel extends javax.swing.JPanel implements GuiSelectionLi
} }
DefaultTableModel model = new TModel( DefaultTableModel model = new TModel(
new Object[]{"ID", "Symbol", "Name", "Type"}, 0 new Object[]{"Symbol", "Name", "Type"}, 0
); );
assetTable.setAutoCreateRowSorter(true); 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. * 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 * WARNING: Do NOT modify this code. The content of this method is always

View File

@ -84,8 +84,33 @@ public class Json {
Logger.getLogger(Json.class.getName()).log(Level.SEVERE, null, ex1); 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; return jo;
} }
@ -124,14 +149,14 @@ public class Json {
} }
if (m.getParameterCount() != 1) { 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; continue;
} }
String name = null == imp.value() ? m.getName() : imp.value(); String name = null == imp.value() ? m.getName() : imp.value();
Class p0 = m.getParameterTypes()[0]; Class p0 = m.getParameterTypes()[0];
if (String.class.isAssignableFrom(p0)){ if (String.class.isAssignableFrom(p0)) {
String param = jo.optString(name, ""); String param = jo.optString(name, "");
try { try {
m.invoke(o, param); m.invoke(o, param);