mirror of
https://github.com/macocianradu/javaGUItoolkit.git
synced 2026-03-18 13:40:04 +00:00
initial parser working as intended
This commit is contained in:
@@ -1,38 +1,22 @@
|
||||
import guiTree.Window;
|
||||
import parser.XAMLParser;
|
||||
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Window win = new Window();
|
||||
win.setVisible(true);
|
||||
win.setSize(640, 480);
|
||||
win.setPositionRelativeTo(null);
|
||||
win.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
win.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
Button button = new Button();
|
||||
win.addVisual(button);
|
||||
int x = 20;
|
||||
int y = 20;
|
||||
while(true){
|
||||
if(x > win.getWidth()){
|
||||
x = 0;
|
||||
y+=20;
|
||||
}
|
||||
if(y > win.getHeight()){
|
||||
y = 0;
|
||||
}
|
||||
button.setLocation(x, y);
|
||||
x+=10;
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try{
|
||||
Window window = XAMLParser.parse("company.xml");
|
||||
assert window != null;
|
||||
window.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
window.dispose();
|
||||
}
|
||||
});
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
9
src/converters/BooleanConverter.java
Normal file
9
src/converters/BooleanConverter.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package converters;
|
||||
|
||||
public class BooleanConverter implements ConverterInterface<Boolean> {
|
||||
|
||||
@Override
|
||||
public Boolean convert(String content) {
|
||||
return Boolean.valueOf(content);
|
||||
}
|
||||
}
|
||||
10
src/converters/ColorConverter.java
Normal file
10
src/converters/ColorConverter.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package converters;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class ColorConverter implements ConverterInterface<Color> {
|
||||
@Override
|
||||
public Color convert(String content) {
|
||||
return Color.decode(content);
|
||||
}
|
||||
}
|
||||
31
src/converters/Converter.java
Normal file
31
src/converters/Converter.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package converters;
|
||||
|
||||
import com.sun.jdi.InvalidTypeException;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Converter {
|
||||
private HashMap<Class<?>, ConverterInterface<?>> converterTable;
|
||||
|
||||
public Converter(){
|
||||
this.converterTable = new HashMap<>();
|
||||
this.converterTable.put(Integer.class, new IntegerConverter());
|
||||
this.converterTable.put(Float.class, new FloatConverter());
|
||||
this.converterTable.put(Double.class, new DoubleConverter());
|
||||
this.converterTable.put(String.class, new StringConverter());
|
||||
this.converterTable.put(Boolean.class, new BooleanConverter());
|
||||
this.converterTable.put(Integer.TYPE, new IntegerConverter());
|
||||
this.converterTable.put(Float.TYPE, new FloatConverter());
|
||||
this.converterTable.put(Double.TYPE, new DoubleConverter());
|
||||
this.converterTable.put(Boolean.TYPE, new BooleanConverter());
|
||||
this.converterTable.put(Color.class, new ColorConverter());
|
||||
}
|
||||
|
||||
public Object objectCreatorFactory (Class<?> type, String content) throws InvalidTypeException {
|
||||
if(this.converterTable.containsKey(type)) {
|
||||
return this.converterTable.get(type).convert(content);
|
||||
}
|
||||
throw new InvalidTypeException();
|
||||
}
|
||||
}
|
||||
5
src/converters/ConverterInterface.java
Normal file
5
src/converters/ConverterInterface.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package converters;
|
||||
|
||||
public interface ConverterInterface<T> {
|
||||
T convert(String content);
|
||||
}
|
||||
9
src/converters/DoubleConverter.java
Normal file
9
src/converters/DoubleConverter.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package converters;
|
||||
|
||||
public class DoubleConverter implements ConverterInterface<Double>{
|
||||
|
||||
@Override
|
||||
public Double convert(String content) {
|
||||
return Double.parseDouble(content);
|
||||
}
|
||||
}
|
||||
9
src/converters/FloatConverter.java
Normal file
9
src/converters/FloatConverter.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package converters;
|
||||
|
||||
public class FloatConverter implements ConverterInterface<Float> {
|
||||
|
||||
@Override
|
||||
public Float convert(String content) {
|
||||
return Float.parseFloat(content);
|
||||
}
|
||||
}
|
||||
9
src/converters/IntegerConverter.java
Normal file
9
src/converters/IntegerConverter.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package converters;
|
||||
|
||||
public class IntegerConverter implements ConverterInterface<Integer> {
|
||||
|
||||
@Override
|
||||
public Integer convert(String content) {
|
||||
return Integer.parseInt(content);
|
||||
}
|
||||
}
|
||||
9
src/converters/StringConverter.java
Normal file
9
src/converters/StringConverter.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package converters;
|
||||
|
||||
public class StringConverter implements ConverterInterface<String> {
|
||||
|
||||
@Override
|
||||
public String convert(String content) {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,7 @@
|
||||
package guiTree;
|
||||
|
||||
import guiTree.Visual;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
@@ -12,7 +16,7 @@ public class Button extends Visual {
|
||||
public void paint(BufferedImage imageBuffer)
|
||||
{
|
||||
Graphics g = imageBuffer.getGraphics();
|
||||
g.setColor(Color.BLUE);
|
||||
g.setColor(this.getBackgroundColor());
|
||||
g.fillRect(0, 0, getWidth(), getHeight());
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
package guiTree;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
package guiTree;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
@@ -16,13 +18,14 @@ public class Visual {
|
||||
/*--------------------------------------------------------------------
|
||||
Attributes
|
||||
---------------------------------------------------------------------*/
|
||||
private int width;
|
||||
private int height;
|
||||
private int locationX;
|
||||
private int locationY;
|
||||
private String name;
|
||||
private Integer width;
|
||||
private Integer height;
|
||||
private Integer locationX;
|
||||
private Integer locationY;
|
||||
private Color backgroundColor;
|
||||
private Color foregroundColor;
|
||||
private Boolean valid;
|
||||
private Boolean active;
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------
|
||||
@@ -33,13 +36,23 @@ public class Visual {
|
||||
this.parent = null;
|
||||
this.backgroundColor = Color.WHITE;
|
||||
this.foregroundColor = Color.BLACK;
|
||||
valid = false;
|
||||
|
||||
this.active = this instanceof Window;
|
||||
}
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------
|
||||
Attributes Methods
|
||||
---------------------------------------------------------------------*/
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getWidth()
|
||||
{
|
||||
return this.width;
|
||||
@@ -50,13 +63,12 @@ public class Visual {
|
||||
return this.height;
|
||||
}
|
||||
|
||||
public void setSize(int width, int height){
|
||||
public void setSize(Integer width, Integer height){
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
initializeImageBuffer(width, height);
|
||||
|
||||
this.valid = false;
|
||||
this.revalidate();
|
||||
}
|
||||
|
||||
@@ -68,10 +80,9 @@ public class Visual {
|
||||
return this.locationY;
|
||||
}
|
||||
|
||||
public void setLocation(int x, int y){
|
||||
public void setLocation(Integer x, Integer y){
|
||||
this.locationX = x;
|
||||
this.locationY = y;
|
||||
this.valid = false;
|
||||
this.revalidate();
|
||||
}
|
||||
|
||||
@@ -81,7 +92,6 @@ public class Visual {
|
||||
|
||||
public void setBackgroundColor(Color backgroundColor) {
|
||||
this.backgroundColor = backgroundColor;
|
||||
this.valid = false;
|
||||
this.revalidate();
|
||||
}
|
||||
|
||||
@@ -91,7 +101,6 @@ public class Visual {
|
||||
|
||||
public void setForegroundColor(Color foregroundColor) {
|
||||
this.foregroundColor = foregroundColor;
|
||||
this.valid = false;
|
||||
this.revalidate();
|
||||
}
|
||||
|
||||
@@ -101,8 +110,8 @@ public class Visual {
|
||||
}
|
||||
|
||||
private void calculateInitialLocation(){
|
||||
this.locationX = 200;
|
||||
this.locationY = 200;
|
||||
this.locationX = 20;
|
||||
this.locationY = 50;
|
||||
}
|
||||
|
||||
|
||||
@@ -116,9 +125,9 @@ public class Visual {
|
||||
child.calculateInitialLocation();
|
||||
child.calculateInitialSize();
|
||||
child.imageBuffer = new BufferedImage(child.getWidth(), child.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
|
||||
child.valid = false;
|
||||
|
||||
this.revalidate();
|
||||
child.active = true;
|
||||
child.revalidate();
|
||||
}
|
||||
|
||||
private void setParent(Visual parent)
|
||||
@@ -142,16 +151,14 @@ public class Visual {
|
||||
}
|
||||
|
||||
public void revalidate() {
|
||||
if(!this.active){
|
||||
return;
|
||||
}
|
||||
initializeImageBuffer(width, height);
|
||||
this.paint(imageBuffer);
|
||||
for(Visual v:children){
|
||||
if(!v.valid){
|
||||
v.paint(v.imageBuffer);
|
||||
}
|
||||
this.imageBuffer.getGraphics().drawImage(v.imageBuffer, v.locationX, v.locationY, null);
|
||||
v.valid = true;
|
||||
}
|
||||
this.valid = true;
|
||||
if(!(this instanceof Window)){
|
||||
this.parent.revalidate();
|
||||
}
|
||||
@@ -169,7 +176,7 @@ public class Visual {
|
||||
Helper Methods
|
||||
---------------------------------------------------------------------*/
|
||||
|
||||
private void initializeImageBuffer(int width, int height){
|
||||
private void initializeImageBuffer(Integer width, Integer height){
|
||||
this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
|
||||
this.imageBuffer.getGraphics().setColor(backgroundColor);
|
||||
this.imageBuffer.getGraphics().fillRect(0, 0, width, height);
|
||||
@@ -1,3 +1,7 @@
|
||||
package guiTree;
|
||||
|
||||
import guiTree.Visual;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.WindowListener;
|
||||
import java.awt.event.WindowStateListener;
|
||||
@@ -17,7 +21,7 @@ public class Window extends Visual {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSize(int width, int height)
|
||||
public void setSize(Integer width, Integer height)
|
||||
{
|
||||
this.frame.setSize(width, height);
|
||||
super.setSize(width, height);
|
||||
@@ -26,6 +30,7 @@ public class Window extends Visual {
|
||||
|
||||
public void setFrameImageBuffer(BufferedImage imageBuffer){
|
||||
this.frame.setImageBuffer(imageBuffer);
|
||||
this.frame.repaint();
|
||||
}
|
||||
|
||||
public void setSize(Dimension dimension) {
|
||||
@@ -55,4 +60,8 @@ public class Window extends Visual {
|
||||
public void setPositionRelativeTo(Component c){
|
||||
frame.setLocationRelativeTo(c);
|
||||
}
|
||||
|
||||
public void setPositionRelativeTo(){
|
||||
frame.setLocationRelativeTo(null);
|
||||
}
|
||||
}
|
||||
141
src/parser/XAMLParser.java
Normal file
141
src/parser/XAMLParser.java
Normal file
@@ -0,0 +1,141 @@
|
||||
package parser;
|
||||
|
||||
import com.sun.jdi.InvalidTypeException;
|
||||
import converters.Converter;
|
||||
import guiTree.Visual;
|
||||
import guiTree.Window;
|
||||
import org.w3c.dom.*;
|
||||
|
||||
import javax.xml.parsers.*;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class XAMLParser {
|
||||
private final static String packageName = "guiTree.";
|
||||
private static Converter valueConverter = new Converter();
|
||||
|
||||
private static void setAttributes(Object object, NamedNodeMap attributeList){
|
||||
for(int i = 0; i < attributeList.getLength(); i++){
|
||||
Node attribute = attributeList.item(i);
|
||||
String methodName = "set";
|
||||
methodName = methodName.concat(attribute.getNodeName());
|
||||
List<Object> parameterList = convertStringToPrimitives(object, attribute.getNodeValue(), methodName);
|
||||
Method method = getMethod(object, methodName, parameterList);
|
||||
try {
|
||||
assert method != null;
|
||||
method.invoke(object, parameterList.toArray());
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Method getMethod(Object object, String methodName, List<Object> parameterList){
|
||||
Method method;
|
||||
Class<?>[] args = new Class[parameterList.size()];
|
||||
for(Object o: parameterList){
|
||||
try {
|
||||
args[parameterList.indexOf(o)] = o.getClass();
|
||||
} catch (NullPointerException e) {
|
||||
System.err.println("Null Pointer Exception: " + methodName + " with parameters + " + parameterList.toString());
|
||||
}
|
||||
}
|
||||
try {
|
||||
method = object.getClass().getMethod(methodName, args);
|
||||
return method;
|
||||
} catch (NoSuchMethodException e) {
|
||||
System.err.println("Method does not exist: " + methodName + " with parameters + " + parameterList.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static List<Method> getMethodsFromName(Object object, String methodName){
|
||||
Method[] methods = object.getClass().getMethods();
|
||||
List<Method> returnMethods = new ArrayList<>();
|
||||
for(Method method: methods){
|
||||
if(method.getName().equals(methodName)){
|
||||
returnMethods.add(method);
|
||||
}
|
||||
}
|
||||
return returnMethods;
|
||||
}
|
||||
|
||||
public static Window parse(String filepath) throws Exception {
|
||||
Object rootObject;
|
||||
FileInputStream fileIS = new FileInputStream(new File("resources/" + filepath));
|
||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
||||
Document xmlDocument = builder.parse(fileIS);
|
||||
|
||||
xmlDocument.normalize();
|
||||
|
||||
Element rootNode = xmlDocument.getDocumentElement();
|
||||
|
||||
rootObject = parseNode(rootNode);
|
||||
|
||||
if(rootObject instanceof Window) {
|
||||
return (Window) rootObject;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static List<Object> convertStringToPrimitives(Object object, String value, String methodName){
|
||||
List<Object> primitiveAttributes = new ArrayList<>();
|
||||
List<String> values = new ArrayList<>();
|
||||
|
||||
value = value.replaceAll(" ", "");
|
||||
while (value.contains(",")) {
|
||||
values.add(value.substring(0, value.indexOf(',')));
|
||||
value = value.substring(value.indexOf(',') + 1);
|
||||
}
|
||||
values.add(value);
|
||||
List<Method> methods = getMethodsFromName(object, methodName);
|
||||
for(Method method: methods){
|
||||
Class<?>[] types = method.getParameterTypes();
|
||||
for(int i = 0; i < types.length; i++){
|
||||
try{
|
||||
primitiveAttributes.add(valueConverter.objectCreatorFactory(types[i], values.get(i)));
|
||||
} catch (InvalidTypeException | NumberFormatException e){
|
||||
primitiveAttributes.clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(primitiveAttributes.size() == types.length){
|
||||
return primitiveAttributes;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Object parseNode(Node parentNode)throws Exception{
|
||||
Class<?> parentClass = Class.forName(packageName.concat(parentNode.getNodeName()));
|
||||
Object parentObject = parentClass.getDeclaredConstructor().newInstance();
|
||||
|
||||
setAttributes(parentObject, parentNode.getAttributes());
|
||||
|
||||
NodeList childList = parentNode.getChildNodes();
|
||||
|
||||
for(int index = 0; index < childList.getLength(); index++){
|
||||
Node childNode = childList.item(index);
|
||||
|
||||
if(childNode.getNodeType() == Node.ELEMENT_NODE) {
|
||||
|
||||
Object childObject = parseNode(childNode);
|
||||
|
||||
if(parentObject instanceof Visual && childObject instanceof Visual) {
|
||||
addVisual((Visual) parentObject, (Visual) childObject);
|
||||
}
|
||||
System.out.println("\nCurrent Element :" + childNode.getNodeName());
|
||||
}
|
||||
}
|
||||
return parentObject;
|
||||
}
|
||||
|
||||
private static void addVisual(Visual parentObject, Visual childObject){
|
||||
parentObject.addVisual(childObject);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user