From 295ddac4fbe86058f76ce1be827fc3491bd7894e Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 24 Sep 2020 16:13:05 -0300 Subject: [PATCH] Add Nodesax changes --- .../knowcode/{parse => ui}/CustomInitUI.java | 2 +- .../XmlContainerConstraintLayout.java | 4 +- .../XmlContainerFrameLayout.java | 4 +- .../{parse => ui}/XmlContainerLayout.java | 5 +- .../XmlContainerLinearLayout.java | 4 +- .../XmlContainerRelativeLayout.java | 4 +- .../knowcode/{parse => xml}/NodeSax.java | 451 +++++++++--------- .../knowcode/xml/NodeSaxAbsolute.java | 17 + .../knowcode/xml/NodeSaxRelative.java | 21 + .../{parse => xml}/XmlContainerFactory.java | 5 +- 10 files changed, 291 insertions(+), 226 deletions(-) rename src/main/java/com/totalcross/knowcode/{parse => ui}/CustomInitUI.java (92%) rename src/main/java/com/totalcross/knowcode/{parse => ui}/XmlContainerConstraintLayout.java (98%) rename src/main/java/com/totalcross/knowcode/{parse => ui}/XmlContainerFrameLayout.java (96%) rename src/main/java/com/totalcross/knowcode/{parse => ui}/XmlContainerLayout.java (98%) rename src/main/java/com/totalcross/knowcode/{parse => ui}/XmlContainerLinearLayout.java (97%) rename src/main/java/com/totalcross/knowcode/{parse => ui}/XmlContainerRelativeLayout.java (97%) rename src/main/java/com/totalcross/knowcode/{parse => xml}/NodeSax.java (98%) create mode 100644 src/main/java/com/totalcross/knowcode/xml/NodeSaxAbsolute.java create mode 100644 src/main/java/com/totalcross/knowcode/xml/NodeSaxRelative.java rename src/main/java/com/totalcross/knowcode/{parse => xml}/XmlContainerFactory.java (97%) diff --git a/src/main/java/com/totalcross/knowcode/parse/CustomInitUI.java b/src/main/java/com/totalcross/knowcode/ui/CustomInitUI.java similarity index 92% rename from src/main/java/com/totalcross/knowcode/parse/CustomInitUI.java rename to src/main/java/com/totalcross/knowcode/ui/CustomInitUI.java index ebaebad..4cdcd48 100644 --- a/src/main/java/com/totalcross/knowcode/parse/CustomInitUI.java +++ b/src/main/java/com/totalcross/knowcode/ui/CustomInitUI.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: LGPL-3.0-only *********************************************************************************/ -package com.totalcross.knowcode.parse; +package com.totalcross.knowcode.ui; /** * CustomInitUI is a interface to edit the components of the XML file before swap the window. diff --git a/src/main/java/com/totalcross/knowcode/parse/XmlContainerConstraintLayout.java b/src/main/java/com/totalcross/knowcode/ui/XmlContainerConstraintLayout.java similarity index 98% rename from src/main/java/com/totalcross/knowcode/parse/XmlContainerConstraintLayout.java rename to src/main/java/com/totalcross/knowcode/ui/XmlContainerConstraintLayout.java index 24d83bb..0ec8b0a 100644 --- a/src/main/java/com/totalcross/knowcode/parse/XmlContainerConstraintLayout.java +++ b/src/main/java/com/totalcross/knowcode/ui/XmlContainerConstraintLayout.java @@ -2,7 +2,9 @@ * (c) 2020 by TotalCross Global Mobile Platform LTDA * SPDX-License-Identifier: LGPL-3.0-only *********************************************************************************/ -package com.totalcross.knowcode.parse; +package com.totalcross.knowcode.ui; + +import com.totalcross.knowcode.xml.NodeSax; import totalcross.sys.InvalidNumberException; import totalcross.sys.Settings; diff --git a/src/main/java/com/totalcross/knowcode/parse/XmlContainerFrameLayout.java b/src/main/java/com/totalcross/knowcode/ui/XmlContainerFrameLayout.java similarity index 96% rename from src/main/java/com/totalcross/knowcode/parse/XmlContainerFrameLayout.java rename to src/main/java/com/totalcross/knowcode/ui/XmlContainerFrameLayout.java index 6af936a..fcbe90c 100644 --- a/src/main/java/com/totalcross/knowcode/parse/XmlContainerFrameLayout.java +++ b/src/main/java/com/totalcross/knowcode/ui/XmlContainerFrameLayout.java @@ -2,7 +2,9 @@ * (c) 2020 by TotalCross Global Mobile Platform LTDA * SPDX-License-Identifier: LGPL-3.0-only *********************************************************************************/ -package com.totalcross.knowcode.parse; +package com.totalcross.knowcode.ui; + +import com.totalcross.knowcode.xml.NodeSax; import totalcross.sys.InvalidNumberException; import totalcross.ui.Container; diff --git a/src/main/java/com/totalcross/knowcode/parse/XmlContainerLayout.java b/src/main/java/com/totalcross/knowcode/ui/XmlContainerLayout.java similarity index 98% rename from src/main/java/com/totalcross/knowcode/parse/XmlContainerLayout.java rename to src/main/java/com/totalcross/knowcode/ui/XmlContainerLayout.java index d572b5a..fba5939 100644 --- a/src/main/java/com/totalcross/knowcode/parse/XmlContainerLayout.java +++ b/src/main/java/com/totalcross/knowcode/ui/XmlContainerLayout.java @@ -2,12 +2,15 @@ * (c) 2020 by TotalCross Global Mobile Platform LTDA * SPDX-License-Identifier: LGPL-3.0-only *********************************************************************************/ -package com.totalcross.knowcode.parse; +package com.totalcross.knowcode.ui; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.TreeMap; +import com.totalcross.knowcode.xml.NodeSax; +import com.totalcross.knowcode.xml.XmlContainerFactory; + import totalcross.sys.InvalidNumberException; import totalcross.sys.Settings; import totalcross.sys.Vm; diff --git a/src/main/java/com/totalcross/knowcode/parse/XmlContainerLinearLayout.java b/src/main/java/com/totalcross/knowcode/ui/XmlContainerLinearLayout.java similarity index 97% rename from src/main/java/com/totalcross/knowcode/parse/XmlContainerLinearLayout.java rename to src/main/java/com/totalcross/knowcode/ui/XmlContainerLinearLayout.java index 57769c3..c3a666f 100644 --- a/src/main/java/com/totalcross/knowcode/parse/XmlContainerLinearLayout.java +++ b/src/main/java/com/totalcross/knowcode/ui/XmlContainerLinearLayout.java @@ -2,7 +2,9 @@ * (c) 2020 by TotalCross Global Mobile Platform LTDA * SPDX-License-Identifier: LGPL-3.0-only *********************************************************************************/ -package com.totalcross.knowcode.parse; +package com.totalcross.knowcode.ui; + +import com.totalcross.knowcode.xml.NodeSax; import totalcross.sys.InvalidNumberException; diff --git a/src/main/java/com/totalcross/knowcode/parse/XmlContainerRelativeLayout.java b/src/main/java/com/totalcross/knowcode/ui/XmlContainerRelativeLayout.java similarity index 97% rename from src/main/java/com/totalcross/knowcode/parse/XmlContainerRelativeLayout.java rename to src/main/java/com/totalcross/knowcode/ui/XmlContainerRelativeLayout.java index 726d922..db82f34 100644 --- a/src/main/java/com/totalcross/knowcode/parse/XmlContainerRelativeLayout.java +++ b/src/main/java/com/totalcross/knowcode/ui/XmlContainerRelativeLayout.java @@ -2,7 +2,9 @@ * (c) 2020 by TotalCross Global Mobile Platform LTDA * SPDX-License-Identifier: LGPL-3.0-only *********************************************************************************/ -package com.totalcross.knowcode.parse; +package com.totalcross.knowcode.ui; + +import com.totalcross.knowcode.xml.NodeSax; import totalcross.sys.InvalidNumberException; import totalcross.ui.Container; diff --git a/src/main/java/com/totalcross/knowcode/parse/NodeSax.java b/src/main/java/com/totalcross/knowcode/xml/NodeSax.java similarity index 98% rename from src/main/java/com/totalcross/knowcode/parse/NodeSax.java rename to src/main/java/com/totalcross/knowcode/xml/NodeSax.java index e430dfd..efa254f 100644 --- a/src/main/java/com/totalcross/knowcode/parse/NodeSax.java +++ b/src/main/java/com/totalcross/knowcode/xml/NodeSax.java @@ -2,7 +2,7 @@ * (c) 2020 by TotalCross Global Mobile Platform LTDA * SPDX-License-Identifier: LGPL-3.0-only *********************************************************************************/ -package com.totalcross.knowcode.parse; +package com.totalcross.knowcode.xml; import static totalcross.ui.Control.*; @@ -24,11 +24,12 @@ */ public class NodeSax { private Map attributesMap = new HashMap(); - private String attributeValue; - private String attributeName = new String(); - private String id; - private String relative; - private float wp = 0, hp = 0; + protected String attributeValue; + protected String attributeName = new String(); + protected String id; + protected String relative; + protected float wp = 0, hp = 0; + NodeSax node = new NodeSax(); /** * Creates a NodeSax object @@ -38,6 +39,14 @@ public class NodeSax { */ public NodeSax() throws IOException, ImageException { } + + /** Creates a NodeSax object for the others NodeSax childrens + * @exception ImageException + * @throws IOException + * */ + public NodeSax(NodeSax n) throws IOException, ImageException { + this.node = n.node; + } /** * Get the attribute name of tag @@ -48,19 +57,7 @@ public String getAttributeName() { return attributeName; } - /** - * Get relative position set by getRelativeX() and getRelativeY() - * - * @return attribute value of tags related to relative positioning - */ - public String getRelative() { - if (relative != null && !relative.contains("@+")) { - relative = relative.replace("@", "@+"); - } - return relative; - } - - private String getValue(String a) { + protected String getValue(String a) { return attributesMap.get(a); } @@ -506,6 +503,213 @@ public String getOrientation() { } } + + + + + + /** + * Get Layout Gravity based on tag "android:layout_gravity" + * + * @return value of tag + */ + public String getLayout_gravity() { + attributeValue = getValue("android:layout_gravity"); + if (attributeValue == null || "".equals(attributeValue)) { + return null; + } else { + return attributeValue; + } + } + + /** + * Get gravity based on tag "android:gravity" + * + * @return constant of position + */ + public int getGravity() { + attributeValue = getValue("android:gravity"); + if (attributeValue != null) { + if (attributeValue.equals("left")) { + return FILL; + } + if (attributeValue.equals("center")) + return CENTER; + if (attributeValue.equals("right")) + return RIGHT; + } + return LEFT; + } + + /** + * Get wp + * + * @return value of wp + */ + public float getWp() { + return wp; + } + + /** + * Get hp + * + * @return value of hp + */ + public float getHp() { + return hp; + } + + /** + * Get text based on tag "android:text" + * + * @return value of the tag + */ + public String getText() { + return getValue("android:text"); + } + + /** + * Get text based on tag "android:scaleY" + * + * @return value of the tag + */ + public String getScaleY() { + attributeValue = getValue("android:scaleY"); + if (attributeValue == null || "".equals(attributeValue)) { + return "1"; + } + return attributeValue; + } + + /** + * Get text based on tag "android:scaleY" + * + * @return value of the tag + */ + public String getScaleX() { + attributeValue = getValue("android:scaleX"); + if (attributeValue == null || "".equals(attributeValue)) { + return "1"; + } + return attributeValue; + } + + /** + * Get source of a image based on tag "android:src" + * + * @return value of the tag + */ + public String getSrc() { + attributeValue = getValue("android:src"); + + if (attributeValue == null || "".equals(attributeValue)) { + return null; + } else { + if ('@' == attributeValue.charAt(0)) { + attributeValue = attributeValue.substring(1); + } + + if (attributeValue.contains(".png") || attributeValue.contains(".jpg")) { + return attributeValue; + } else { + attributeValue = getImageExtension(attributeValue); + } + } + return attributeValue; + } + + /** + * Get information of the tag "android:hint" + * + * @return value of the tag + */ + public String getHint() { + attributeValue = getValue("android:hint"); + if (attributeValue == null || "".equals(attributeValue)) { + return null; + } + return attributeValue; + } + + /** + * Clear the attributesMap HashMap of the XML component + */ + public void reset() { + attributesMap.clear(); + } + + /** + * Get information if it is absolute layout based on tags "tools:layout_editor_absoluteX" and + * "tools:layout_editor_absoluteY" + * + * @return true or false + */ + public boolean isAbsoluteLayout() { + if (getValue("tools:layout_editor_absoluteX") != null + || getValue("tools:layout_editor_absoluteY") != null) { + return true; + } + return false; + } + + /** + * Inserts a item in a attributesMap HashMap + * + * @param name name of tag + * @param value value of tag + */ + public void inserts(String name, String value) { + attributesMap.put(name, value); + } + + /** + * Set the id of a tag + * + * @param id id of XML component + */ + public void setId(String id) { + this.id = id; + } + + /** + * set parameter width to try to resize a predefined screen size + */ + public void setWp() throws InvalidNumberException { + attributeValue = getValue("android:layout_width"); + if (attributeValue != null) { + if (attributeValue.equals("match_parent") || attributeValue.equals("fill_parent")) { + wp = Settings.screenWidth; + } else { + if (attributeValue.contains("dp")) { + attributeValue = attributeValue.replace("dp", ""); + } + wp = UnitsConverter.toPixels(Integer.parseInt(attributeValue) + DP); + } + + } + wp = Settings.screenWidth / wp; + + } + + /** + * set parameter height to try to resize a predefined screen size + */ + public void setHp() throws InvalidNumberException { + attributeValue = getValue("android:layout_height"); + if (attributeValue != null) { + if (attributeValue.equals("match_parent") || attributeValue.equals("fill_parent")) { + hp = Settings.screenHeight; + } else { + if (attributeValue.contains("dp")) { + attributeValue = attributeValue.replace("dp", ""); + } + hp = UnitsConverter.toPixels(Integer.parseInt(attributeValue) + DP); + } + } + hp = Settings.screenHeight / hp; + } + + /** * Get the relative positioning of x axis. * Checks the following tags: @@ -673,8 +877,7 @@ public int getRelativeX() throws InvalidNumberException { } return new BigDecimal(UnitsConverter.toPixels(Integer.parseInt(attributeValue))).multiply(BigDecimal.valueOf(wp)).intValue(); } - - + /** * Get the relative positioning of y axis. * Checks the following tags: @@ -800,209 +1003,19 @@ public int getRelativeY() throws InvalidNumberException { return new BigDecimal(UnitsConverter.toPixels(Integer.parseInt(attributeValue))).multiply(BigDecimal.valueOf(hp)).intValue(); } + /** - * Get Layout Gravity based on tag "android:layout_gravity" - * - * @return value of tag - */ - public String getLayout_gravity() { - attributeValue = getValue("android:layout_gravity"); - if (attributeValue == null || "".equals(attributeValue)) { - return null; - } else { - return attributeValue; - } - } - - /** - * Get gravity based on tag "android:gravity" - * - * @return constant of position - */ - public int getGravity() { - attributeValue = getValue("android:gravity"); - if (attributeValue != null) { - if (attributeValue.equals("left")) { - return FILL; - } - if (attributeValue.equals("center")) - return CENTER; - if (attributeValue.equals("right")) - return RIGHT; - } - return LEFT; - } - - /** - * Get wp - * - * @return value of wp - */ - public float getWp() { - return wp; - } - - /** - * Get hp - * - * @return value of hp - */ - public float getHp() { - return hp; - } - - /** - * Get text based on tag "android:text" - * - * @return value of the tag - */ - public String getText() { - return getValue("android:text"); - } - - /** - * Get text based on tag "android:scaleY" - * - * @return value of the tag - */ - public String getScaleY() { - attributeValue = getValue("android:scaleY"); - if (attributeValue == null || "".equals(attributeValue)) { - return "1"; - } - return attributeValue; - } - - /** - * Get text based on tag "android:scaleY" - * - * @return value of the tag - */ - public String getScaleX() { - attributeValue = getValue("android:scaleX"); - if (attributeValue == null || "".equals(attributeValue)) { - return "1"; - } - return attributeValue; - } - - /** - * Get source of a image based on tag "android:src" - * - * @return value of the tag - */ - public String getSrc() { - attributeValue = getValue("android:src"); - - if (attributeValue == null || "".equals(attributeValue)) { - return null; - } else { - if ('@' == attributeValue.charAt(0)) { - attributeValue = attributeValue.substring(1); - } - - if (attributeValue.contains(".png") || attributeValue.contains(".jpg")) { - return attributeValue; - } else { - attributeValue = getImageExtension(attributeValue); - } - } - return attributeValue; - } - - /** - * Get information of the tag "android:hint" - * - * @return value of the tag - */ - public String getHint() { - attributeValue = getValue("android:hint"); - if (attributeValue == null || "".equals(attributeValue)) { - return null; - } - return attributeValue; - } - - /** - * Clear the attributesMap HashMap of the XML component - */ - public void reset() { - attributesMap.clear(); - } - - /** - * Get information if it is absolute layout based on tags "tools:layout_editor_absoluteX" and - * "tools:layout_editor_absoluteY" - * - * @return true or false - */ - public boolean isAbsoluteLayout() { - if (getValue("tools:layout_editor_absoluteX") != null - || getValue("tools:layout_editor_absoluteY") != null) { - return true; - } - return false; - } - - /** - * Inserts a item in a attributesMap HashMap - * - * @param name name of tag - * @param value value of tag - */ - public void inserts(String name, String value) { - attributesMap.put(name, value); - } - - /** - * Set the id of a tag + * Get relative position set by getRelativeX() and getRelativeY() * - * @param id id of XML component - */ - public void setId(String id) { - this.id = id; - } - - /** - * set parameter width to try to resize a predefined screen size - */ - public void setWp() throws InvalidNumberException { - attributeValue = getValue("android:layout_width"); - if (attributeValue != null) { - if (attributeValue.equals("match_parent") || attributeValue.equals("fill_parent")) { - wp = Settings.screenWidth; - } else { - if (attributeValue.contains("dp")) { - attributeValue = attributeValue.replace("dp", ""); - } - wp = UnitsConverter.toPixels(Integer.parseInt(attributeValue) + DP); - } - - } - wp = Settings.screenWidth / wp; - - } - - /** - * set parameter height to try to resize a predefined screen size + * @return attribute value of tags related to relative positioning */ - public void setHp() throws InvalidNumberException { - attributeValue = getValue("android:layout_height"); - if (attributeValue != null) { - if (attributeValue.equals("match_parent") || attributeValue.equals("fill_parent")) { - hp = Settings.screenHeight; - } else { - if (attributeValue.contains("dp")) { - attributeValue = attributeValue.replace("dp", ""); - } - hp = UnitsConverter.toPixels(Integer.parseInt(attributeValue) + DP); - } - - + public String getRelative() { + if (relative != null && !relative.contains("@+")) { + relative = relative.replace("@", "@+"); } - hp = Settings.screenHeight / hp; + return relative; } - + /** * Set attribute name of a tag * diff --git a/src/main/java/com/totalcross/knowcode/xml/NodeSaxAbsolute.java b/src/main/java/com/totalcross/knowcode/xml/NodeSaxAbsolute.java new file mode 100644 index 0000000..a4eca83 --- /dev/null +++ b/src/main/java/com/totalcross/knowcode/xml/NodeSaxAbsolute.java @@ -0,0 +1,17 @@ +package com.totalcross.knowcode.xml; + +import totalcross.io.IOException; +import totalcross.ui.image.ImageException; + +public class NodeSaxAbsolute extends NodeSax{ + + /** + * Creates a NodeSaxConstraintAbsolute object + * + * @throws ImageException + * @throws IOException + */ + public NodeSaxAbsolute(NodeSax node) throws IOException, ImageException { + super(node); + } +} diff --git a/src/main/java/com/totalcross/knowcode/xml/NodeSaxRelative.java b/src/main/java/com/totalcross/knowcode/xml/NodeSaxRelative.java new file mode 100644 index 0000000..bf7f764 --- /dev/null +++ b/src/main/java/com/totalcross/knowcode/xml/NodeSaxRelative.java @@ -0,0 +1,21 @@ +package com.totalcross.knowcode.xml; + +import totalcross.io.IOException; + +import totalcross.ui.image.ImageException; + + +public class NodeSaxRelative extends NodeSax { + + /** + * Creates a NodeSaxRelative object + * + * @throws ImageException + * @throws IOException + */ + public NodeSaxRelative(NodeSax node) throws IOException, ImageException { + super(node); + } + + +} diff --git a/src/main/java/com/totalcross/knowcode/parse/XmlContainerFactory.java b/src/main/java/com/totalcross/knowcode/xml/XmlContainerFactory.java similarity index 97% rename from src/main/java/com/totalcross/knowcode/parse/XmlContainerFactory.java rename to src/main/java/com/totalcross/knowcode/xml/XmlContainerFactory.java index ffa7061..6f08502 100644 --- a/src/main/java/com/totalcross/knowcode/parse/XmlContainerFactory.java +++ b/src/main/java/com/totalcross/knowcode/xml/XmlContainerFactory.java @@ -2,7 +2,7 @@ * (c) 2020 by TotalCross Global Mobile Platform LTDA * SPDX-License-Identifier: LGPL-3.0-only *********************************************************************************/ -package com.totalcross.knowcode.parse; +package com.totalcross.knowcode.xml; import totalcross.sys.Vm; import totalcross.ui.Container; @@ -15,6 +15,9 @@ import java.io.UnsupportedEncodingException; +import com.totalcross.knowcode.ui.CustomInitUI; +import com.totalcross.knowcode.ui.XmlContainerLayout; + /** * XmlContainerFactory is responsible to read the XML file, verify what kind of view group is the layout * and to instantiate the specialized class of XmlContainerLayout.