mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/jpexs-decompiler.git
synced 2026-07-18 13:38:26 +00:00
Generic tag color editor
This commit is contained in:
@@ -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()+ "<br>";
|
||||
}
|
||||
}
|
||||
//HTML for colors:
|
||||
val = "<html>"+val+"</html>";
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,6 +80,12 @@ public class BooleanEditor extends JCheckBox implements GenericTagEditor {
|
||||
public Field getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getReadOnlyValue() {
|
||||
return getChangedValue().toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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 "<cite style=\"background-color:rgb("+color.getRed()+","+color.getGreen()+","+color.getBlue()+");\"> </cite> "+getChangedValue().toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,4 +30,6 @@ public interface GenericTagEditor {
|
||||
public Object getChangedValue();
|
||||
public String getFieldName();
|
||||
public Field getField();
|
||||
|
||||
public String getReadOnlyValue();
|
||||
}
|
||||
|
||||
@@ -193,4 +193,8 @@ public class NumberEditor extends JSpinner implements GenericTagEditor {
|
||||
return field;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getReadOnlyValue() {
|
||||
return getChangedValue().toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<from.length;i++){
|
||||
text = text.replace(from[i], to[i]);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user