From ac9d0573aa4b3055c20656c8ee7df443055d9952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=F8=EDk?= Date: Sat, 15 Feb 2014 12:13:31 +0100 Subject: [PATCH] Generic tag color editor --- .../decompiler/flash/gui/GenericTagPanel.java | 22 ++- .../gui/generictageditors/BooleanEditor.java | 6 + .../gui/generictageditors/ColorEditor.java | 182 ++++++++++++++++++ .../generictageditors/GenericTagEditor.java | 2 + .../gui/generictageditors/NumberEditor.java | 4 + .../gui/generictageditors/StringEditor.java | 7 + .../jpexs/decompiler/flash/types/ARGB.java | 9 + trunk/src/com/jpexs/helpers/Helper.java | 9 + 8 files changed, 237 insertions(+), 4 deletions(-) create mode 100644 trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/ColorEditor.java diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/GenericTagPanel.java b/trunk/src/com/jpexs/decompiler/flash/gui/GenericTagPanel.java index 20688bfc2..0415344ac 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/GenericTagPanel.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/GenericTagPanel.java @@ -18,12 +18,16 @@ package com.jpexs.decompiler.flash.gui; import com.jpexs.decompiler.flash.gui.generictageditors.BooleanEditor; import com.jpexs.decompiler.flash.gui.generictageditors.ChangeListener; +import com.jpexs.decompiler.flash.gui.generictageditors.ColorEditor; 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; +import com.jpexs.decompiler.flash.types.ARGB; +import com.jpexs.decompiler.flash.types.RGB; +import com.jpexs.decompiler.flash.types.RGBA; import com.jpexs.decompiler.flash.types.annotations.Calculated; import com.jpexs.decompiler.flash.types.annotations.Conditional; import com.jpexs.decompiler.flash.types.annotations.Internal; @@ -43,6 +47,7 @@ import java.util.Map; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; +import javax.swing.Box; import javax.swing.JEditorPane; import javax.swing.JLabel; import javax.swing.JPanel; @@ -120,9 +125,12 @@ public class GenericTagPanel extends JPanel implements ChangeListener { for (String key : keys) { GenericTagEditor ed = editors.get(key); if (((Component) ed).isVisible()) { - val += key + " : " + ed.getChangedValue() + "\r\n"; + val += key + " : " + ed.getReadOnlyValue()+ "
"; } } + //HTML for colors: + val = ""+val+""; + genericTagPropertiesEditorPane.setContentType("text/html"); genericTagPropertiesEditorPane.setText(val); genericTagPropertiesEditorPane.setCaretPosition(0); hdr.setText(tag.toString()); @@ -209,7 +217,9 @@ public class GenericTagPanel extends JPanel implements ChangeListener { editor = new BooleanEditor(name, obj, field, index, type); } else if (type.equals(String.class)) { editor = new StringEditor(name, obj, field, index, type); - } else { + } else if (type.equals(RGB.class) || type.equals(RGBA.class) || type.equals(ARGB.class)){ + editor = new ColorEditor(name, obj, field, index, type); + }else { if (value == null) { if (readonly) { return 0; @@ -238,6 +248,7 @@ public class GenericTagPanel extends JPanel implements ChangeListener { } JLabel label = new JLabel(name + ":", JLabel.TRAILING); + label.setVerticalAlignment(JLabel.TOP); genericTagPropertiesEditPanel.add(label); label.setLabelFor(editor); labels.put(name, label); @@ -357,7 +368,10 @@ public class GenericTagPanel extends JPanel implements ChangeListener { genericTagPropertiesEditPanel.add(dependentTypeLabel); propCount++; } - } - relayout(propCount); + } + genericTagPropertiesEditPanel.add(new JPanel()); + genericTagPropertiesEditPanel.add(new JPanel()); + genericTagPropertiesEditPanel.add(new JPanel()); + relayout(propCount+1); } } diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/BooleanEditor.java b/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/BooleanEditor.java index 3fce3c0b6..1c62ada13 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/BooleanEditor.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/BooleanEditor.java @@ -80,6 +80,12 @@ public class BooleanEditor extends JCheckBox implements GenericTagEditor { public Field getField() { return field; } + + @Override + public String getReadOnlyValue() { + return getChangedValue().toString(); + } + } diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/ColorEditor.java b/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/ColorEditor.java new file mode 100644 index 000000000..e6bd18804 --- /dev/null +++ b/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/ColorEditor.java @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2010-2014 JPEXS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.jpexs.decompiler.flash.gui.generictageditors; + +import com.jpexs.decompiler.flash.AppStrings; +import com.jpexs.decompiler.flash.types.ARGB; +import com.jpexs.decompiler.flash.types.RGB; +import com.jpexs.decompiler.flash.types.RGBA; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.Graphics; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.FocusAdapter; +import java.awt.event.FocusEvent; +import java.lang.reflect.Field; +import javax.swing.BorderFactory; +import javax.swing.JButton; +import javax.swing.JColorChooser; +import javax.swing.JPanel; +import javax.swing.border.BevelBorder; + +/** + * + * @author JPEXS + */ +public class ColorEditor extends JPanel implements GenericTagEditor, ActionListener { + + private final Object obj; + private final Field field; + private final int index; + private final Class type; + private String fieldName; + private Color color; + public static final int COLOR_TYPE_RGB = 0; + public static final int COLOR_TYPE_RGBA = 1; + public static final int COLOR_TYPE_ARGB = 2; + private int colorType; + private JButton buttonChange; + + public int getColorType() { + return colorType; + } + + + + + public ColorEditor(String fieldName,Object obj, Field field, int index, Class type) { + this.obj = obj; + this.field = field; + this.index = index; + this.type = type; + this.fieldName = fieldName; + try { + Object val = ReflectionTools.getValue(obj, field, index); + if(val instanceof RGBA){ + colorType = COLOR_TYPE_RGBA; + }else if(val instanceof RGB){ + colorType = COLOR_TYPE_RGB; + }else if(val instanceof ARGB){ + colorType = COLOR_TYPE_ARGB; + }else{ + throw new IllegalArgumentException("Invalid value type"); + } + if(val instanceof RGB){ //Note: Can be RGBA too + color = ((RGB)val).toColor(); + } + if(val instanceof ARGB){ + color = ((ARGB)val).toColor(); + } + setLayout(new FlowLayout()); + + //add(colorPanel); + buttonChange=new JButton(""){ + + + @Override + protected void paintComponent(Graphics g) { + g.setColor(getBackground()); + g.fillRect(0, 0, getWidth(), getHeight()); + super.paintBorder(g); + } + + }; + buttonChange.addActionListener(this); + buttonChange.setBackground(color); + buttonChange.setBorderPainted(true); + buttonChange.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); + Dimension colorDim=new Dimension(16,16); + buttonChange.setSize(colorDim); + buttonChange.setPreferredSize(colorDim); + add(buttonChange); + } catch (IllegalArgumentException | IllegalAccessException ex) { + // ignore + } + + } + + + @Override + public void save() { + Object val = getChangedValue(); + try { + ReflectionTools.setValue(obj, field, index, val); + } catch (IllegalArgumentException | IllegalAccessException ex) { + // ignore + } + } + + @Override + public void addChangeListener(final ChangeListener l) { + final GenericTagEditor t = this; + addFocusListener(new FocusAdapter() { + + @Override + public void focusLost(FocusEvent e) { + l.change(t); + } + + }); + } + + @Override + public Object getChangedValue() { + Object val = null; + switch(colorType){ + case COLOR_TYPE_RGB: + val = new RGB(color); + break; + case COLOR_TYPE_RGBA: + val = new RGBA(color); + break; + case COLOR_TYPE_ARGB: + val = new ARGB(); + break; + } + return val; + } + + @Override + public String getFieldName() { + return fieldName; + } + + @Override + public Field getField() { + return field; + } + + @Override + public void actionPerformed(ActionEvent e) { + Color newColor = JColorChooser.showDialog(null, AppStrings.translate("dialog.selectcolor.title"), color); + if(newColor!=null){ + color = newColor; + buttonChange.setBackground(color); + repaint(); + } + } + + + @Override + public String getReadOnlyValue() { + int h=System.identityHashCode(this); + return "     "+getChangedValue().toString(); + } + +} diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/GenericTagEditor.java b/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/GenericTagEditor.java index f2e8471fa..d6b1b66e2 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/GenericTagEditor.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/GenericTagEditor.java @@ -30,4 +30,6 @@ public interface GenericTagEditor { public Object getChangedValue(); public String getFieldName(); public Field getField(); + + public String getReadOnlyValue(); } diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/NumberEditor.java b/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/NumberEditor.java index 3c2f3716c..6171651bd 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/NumberEditor.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/NumberEditor.java @@ -193,4 +193,8 @@ public class NumberEditor extends JSpinner implements GenericTagEditor { return field; } + @Override + public String getReadOnlyValue() { + return getChangedValue().toString(); + } } diff --git a/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/StringEditor.java b/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/StringEditor.java index b84e9fe24..cf734886c 100644 --- a/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/StringEditor.java +++ b/trunk/src/com/jpexs/decompiler/flash/gui/generictageditors/StringEditor.java @@ -16,10 +16,12 @@ */ package com.jpexs.decompiler.flash.gui.generictageditors; +import com.jpexs.helpers.Helper; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.lang.reflect.Field; import javax.swing.JTextArea; +import jsyntaxpane.util.StringUtils; /** * @@ -83,4 +85,9 @@ public class StringEditor extends JTextArea implements GenericTagEditor { return field; } + @Override + public String getReadOnlyValue() { + return Helper.escapeHTML(getChangedValue().toString()); + } + } diff --git a/trunk/src/com/jpexs/decompiler/flash/types/ARGB.java b/trunk/src/com/jpexs/decompiler/flash/types/ARGB.java index 762ac635a..024ec5879 100644 --- a/trunk/src/com/jpexs/decompiler/flash/types/ARGB.java +++ b/trunk/src/com/jpexs/decompiler/flash/types/ARGB.java @@ -55,4 +55,13 @@ public class ARGB { public Color toColor() { return new Color(red, green, blue, alpha); } + public ARGB(){ + + } + public ARGB(Color color) { + this.alpha = color.getAlpha(); + this.red = color.getRed(); + this.green = color.getGreen(); + this.blue = color.getBlue(); + } } diff --git a/trunk/src/com/jpexs/helpers/Helper.java b/trunk/src/com/jpexs/helpers/Helper.java index cc78b75a0..9908f5c07 100644 --- a/trunk/src/com/jpexs/helpers/Helper.java +++ b/trunk/src/com/jpexs/helpers/Helper.java @@ -793,4 +793,13 @@ public class Helper { appendNoHilight(AppStrings.translate("decompilationError.error.description")). appendNoHilight("\");").newLine(); } + + public static String escapeHTML(String text){ + String from[] = new String[]{"&", "<", ">", "\"", "'", "/"}; + String to[] = new String[]{"&", "<", ">", """, "'", "/"}; + for(int i=0;i