From 6b951fcf09c62f292193850e676ca623998fcd68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=F8=EDk?= Date: Thu, 13 Feb 2014 22:34:35 +0100 Subject: [PATCH] Fixed arrays and lists in generic tag editor --- .../decompiler/flash/gui/GenericTagPanel.java | 78 +++++++------------ .../generictageditors/ReflectionTools.java | 10 +++ 2 files changed, 40 insertions(+), 48 deletions(-) diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/GenericTagPanel.java b/trunk/src/com/jpexs/decompiler/flash/gui/GenericTagPanel.java index 4e522ab6c..20688bfc2 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/GenericTagPanel.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/GenericTagPanel.java @@ -20,6 +20,7 @@ import com.jpexs.decompiler.flash.gui.generictageditors.BooleanEditor; import com.jpexs.decompiler.flash.gui.generictageditors.ChangeListener; import com.jpexs.decompiler.flash.gui.generictageditors.GenericTagEditor; import com.jpexs.decompiler.flash.gui.generictageditors.NumberEditor; +import com.jpexs.decompiler.flash.gui.generictageditors.ReflectionTools; import com.jpexs.decompiler.flash.gui.generictageditors.StringEditor; import com.jpexs.decompiler.flash.gui.helpers.SpringUtilities; import com.jpexs.decompiler.flash.tags.Tag; @@ -64,6 +65,7 @@ public class GenericTagPanel extends JPanel implements ChangeListener { private Map labels = new HashMap<>(); private Map types = new HashMap<>(); private Map> fieldPaths = new HashMap<>(); + private Map> fieldIndices = new HashMap<>(); private HeaderLabel hdr; public GenericTagPanel() { @@ -88,6 +90,12 @@ public class GenericTagPanel extends JPanel implements ChangeListener { public void clear() { tag = null; + editors.clear(); + fieldPaths.clear(); + fieldIndices.clear(); + labels.clear(); + types.clear(); + keys.clear(); genericTagPropertiesEditPanel.removeAll(); genericTagPropertiesEditPanel.setSize(0, 0); } @@ -106,6 +114,7 @@ public class GenericTagPanel extends JPanel implements ChangeListener { } public void setTagText(Tag tag) { + clear(); generateEditControls(tag, true); String val = ""; for (String key : keys) { @@ -116,45 +125,13 @@ public class GenericTagPanel extends JPanel implements ChangeListener { } genericTagPropertiesEditorPane.setText(val); genericTagPropertiesEditorPane.setCaretPosition(0); - hdr.setText(tag.toString()); - /*StringBuilder sb = new StringBuilder(); - Field[] fields = tag.getClass().getDeclaredFields(); - for (Field field : fields) { - try { - field.setAccessible(true); - Object value = field.get(tag); - sb.append(field.getName()).append(": "); - if (field.getType().isArray()) { - sb.append("["); - for (int i = 0; i < Array.getLength(value); i++) { - if (i != 0) { - sb.append(", "); - } - sb.append(Array.get(value, i)); - } - sb.append("]"); - } else { - sb.append(value); - } - sb.append(GraphTextWriter.NEW_LINE); - } catch (IllegalArgumentException | IllegalAccessException ex) { - Logger.getLogger(GenericTagPanel.class.getName()).log(Level.SEVERE, null, ex); - } - } - genericTagPropertiesEditorPane.setText(sb.toString()); - genericTagPropertiesEditorPane.setCaretPosition(0);*/ + hdr.setText(tag.toString()); } public void generateEditControls(Tag tag, boolean readonly) { - editors.clear(); - fieldPaths.clear(); - labels.clear(); - types.clear(); - keys.clear(); - genericTagPropertiesEditPanel.removeAll(); - genericTagPropertiesEditPanel.setSize(0, 0); + clear(); this.tag = tag; - generateEditControlsRecursive(tag, "", new ArrayList(), readonly); + generateEditControlsRecursive(tag, "", new ArrayList(),new ArrayList(), readonly); change(null); } @@ -167,7 +144,7 @@ public class GenericTagPanel extends JPanel implements ChangeListener { repaint(); } - private int generateEditControlsRecursive(Object obj, String parent, List parentFields, boolean readonly) { + private int generateEditControlsRecursive(Object obj, String parent, List parentFields, List parentIndices, boolean readonly) { if (obj == null) { return 0; } @@ -185,7 +162,7 @@ public class GenericTagPanel extends JPanel implements ChangeListener { if (value != null) { int i = 0; for (Object obj1 : (Iterable) value) { - propCount += addEditor(name + "[" + i + "]", obj, field, i, obj1.getClass(), obj1, parentFields, readonly); + propCount += addEditor(name + "[" + i + "]", obj, field, i, obj1.getClass(), obj1, parentFields,parentIndices, readonly); i++; } } @@ -193,11 +170,11 @@ public class GenericTagPanel extends JPanel implements ChangeListener { if (value != null) { for (int i = 0; i < Array.getLength(value); i++) { Object item = Array.get(value, i); - propCount += addEditor(name + "[" + i + "]", obj, field, i, item.getClass(), item, parentFields, readonly); + propCount += addEditor(name + "[" + i + "]", obj, field, i, item.getClass(), item, parentFields,parentIndices, readonly); } } } else { - propCount += addEditor(name, obj, field, 0, field.getType(), value, parentFields, readonly); + propCount += addEditor(name, obj, field, 0, field.getType(), value, parentFields,parentIndices, readonly); } } catch (IllegalArgumentException | IllegalAccessException ex) { Logger.getLogger(GenericTagPanel.class.getName()).log(Level.SEVERE, null, ex); @@ -206,13 +183,16 @@ public class GenericTagPanel extends JPanel implements ChangeListener { return propCount; } - private int addEditor(String name, Object obj, Field field, int index, Class type, Object value, List parentList, boolean readonly) throws IllegalArgumentException, IllegalAccessException { + private int addEditor(String name, Object obj, Field field, int index, Class type, Object value, List parentList,List parentIndices, boolean readonly) throws IllegalArgumentException, IllegalAccessException { Calculated calculated = field.getAnnotation(Calculated.class); if (calculated != null) { return 0; } List parList = new ArrayList<>(parentList); parList.add(field); + + List parIndices = new ArrayList<>(parentIndices); + parIndices.add(index); Internal inter = field.getAnnotation(Internal.class); if (inter != null) { return 0; @@ -247,18 +227,14 @@ public class GenericTagPanel extends JPanel implements ChangeListener { return 0; } } - return generateEditControlsRecursive(value, field.getName() + ".", parList, readonly); - /*} else { - JTextArea textArea = new JTextArea(value == null ? "" : value.toString()); - textArea.setLineWrap(true); - textArea.setEditable(false); - editor = textArea;*/ + return generateEditControlsRecursive(value, name + ".", parList,parIndices, readonly); } if (editor instanceof GenericTagEditor) { GenericTagEditor ce = (GenericTagEditor) editor; ce.addChangeListener(this); editors.put(name, ce); fieldPaths.put(name, parList); + fieldIndices.put(name,parIndices); } JLabel label = new JLabel(name + ":", JLabel.TRAILING); @@ -316,14 +292,20 @@ public class GenericTagPanel extends JPanel implements ChangeListener { Component dependentLabel = labels.get(key); Component dependentTypeLabel = types.get(key); List path = fieldPaths.get(key); + List indices = fieldIndices.get(key); String p = ""; - boolean conditionMet = true; - for (Field f : path) { + boolean conditionMet = true; + for (int i=0;i