diff --git a/FCL/src/main/java/com/tungsten/fcl/control/data/BaseInfoData.java b/FCL/src/main/java/com/tungsten/fcl/control/data/BaseInfoData.java index b46cd13d..906409ed 100644 --- a/FCL/src/main/java/com/tungsten/fcl/control/data/BaseInfoData.java +++ b/FCL/src/main/java/com/tungsten/fcl/control/data/BaseInfoData.java @@ -284,7 +284,7 @@ public class BaseInfoData implements Cloneable, Observable { } @JsonAdapter(PercentageSize.Serializer.class) - public static class PercentageSize implements Cloneable { + public static class PercentageSize implements Cloneable, Observable { public enum Reference { SCREEN_WIDTH, @@ -329,7 +329,7 @@ public class BaseInfoData implements Cloneable, Observable { } public PercentageSize() { - + addPropertyChangedListener(onInvalidating(this::invalidate)); } public void addPropertyChangedListener(InvalidationListener listener) { @@ -337,6 +337,22 @@ public class BaseInfoData implements Cloneable, Observable { sizeProperty.addListener(listener); } + private ObservableHelper observableHelper = new ObservableHelper(this); + + @Override + public void addListener(InvalidationListener listener) { + observableHelper.addListener(listener); + } + + @Override + public void removeListener(InvalidationListener listener) { + observableHelper.removeListener(listener); + } + + private void invalidate() { + observableHelper.invalidate(); + } + @Override public PercentageSize clone() { PercentageSize size = new PercentageSize(); diff --git a/FCL/src/main/java/com/tungsten/fcl/control/data/ButtonEventData.java b/FCL/src/main/java/com/tungsten/fcl/control/data/ButtonEventData.java index 587895b2..7af32b8f 100644 --- a/FCL/src/main/java/com/tungsten/fcl/control/data/ButtonEventData.java +++ b/FCL/src/main/java/com/tungsten/fcl/control/data/ButtonEventData.java @@ -22,6 +22,8 @@ import com.tungsten.fclcore.fakefx.beans.property.SimpleBooleanProperty; import com.tungsten.fclcore.fakefx.beans.property.SimpleObjectProperty; import com.tungsten.fclcore.fakefx.beans.property.SimpleStringProperty; import com.tungsten.fclcore.fakefx.beans.property.StringProperty; +import com.tungsten.fclcore.fakefx.collections.FXCollections; +import com.tungsten.fclcore.fakefx.collections.ObservableList; import com.tungsten.fclcore.util.fakefx.ObservableHelper; import java.lang.reflect.Type; @@ -312,74 +314,33 @@ public class ButtonEventData implements Cloneable, Observable { /** * Output keycodes */ - private final ObjectProperty> outputKeycodesProperty = new SimpleObjectProperty<>(this, "outputKeycodes", new ArrayList<>()); + private final ObservableList outputKeycodesList = FXCollections.observableList(new ArrayList<>()); - public ObjectProperty> outputKeycodesProperty() { - return outputKeycodesProperty; + public ObservableList outputKeycodesList() { + return outputKeycodesList; } - public void setOutputKeycodes(ArrayList outputKeycodes) { - outputKeycodesProperty.set(outputKeycodes); - } - - public ArrayList getOutputKeycodes() { - return outputKeycodesProperty.get(); + public void setOutputKeycodes(ObservableList outputKeycodes) { + outputKeycodesList.setAll(outputKeycodes); } /** * Switch view group visibility */ - private final ObjectProperty> bindViewGroupProperty = new SimpleObjectProperty<>(this, "bindViewGroup", new ArrayList<>()); + private final ObservableList bindViewGroupList = FXCollections.observableList(new ArrayList<>()); - public ObjectProperty> bindViewGroupProperty() { - return bindViewGroupProperty; + public ObservableList bindViewGroupList() { + return bindViewGroupList; } - public void setBindViewGroup(ArrayList bindViewGroup) { - bindViewGroupProperty.set(bindViewGroup); - } - - public ArrayList getBindViewGroup() { - return bindViewGroupProperty.get(); + public void setBindViewGroup(ObservableList bindViewGroup) { + bindViewGroupList.setAll(bindViewGroup); } public Event() { addPropertyChangedListener(onInvalidating(this::invalidate)); } - public void addKeycode(int keycode) { - ArrayList keycodes = getOutputKeycodes(); - if (!keycodes.contains(keycode)) { - keycodes.add(keycode); - setOutputKeycodes(keycodes); - } - } - - public void removeKeycode(int keycode) { - ArrayList keycodes = getOutputKeycodes(); - for (int i = 0; i < keycodes.size(); i++) { - if (keycodes.get(i) == keycode) { - keycodes.remove(i); - break; - } - } - setOutputKeycodes(keycodes); - } - - public void bindViewGroup(String groupId) { - ArrayList groups = getBindViewGroup(); - if (!groups.contains(groupId)) { - groups.add(groupId); - setBindViewGroup(groups); - } - } - - public void unbindViewGroup(String groupId) { - ArrayList groups = getBindViewGroup(); - groups.remove(groupId); - setBindViewGroup(groups); - } - public void addPropertyChangedListener(InvalidationListener listener) { pointerFollowProperty.addListener(listener); autoKeepProperty.addListener(listener); @@ -389,8 +350,8 @@ public class ButtonEventData implements Cloneable, Observable { switchTouchModeProperty.addListener(listener); inputProperty.addListener(listener); outputTextProperty.addListener(listener); - outputKeycodesProperty.addListener(listener); - bindViewGroupProperty.addListener(listener); + outputKeycodesList.addListener(listener); + bindViewGroupList.addListener(listener); } private ObservableHelper observableHelper = new ObservableHelper(this); @@ -420,8 +381,8 @@ public class ButtonEventData implements Cloneable, Observable { event.setSwitchTouchMode(isSwitchTouchMode()); event.setInput(isInput()); event.setOutputText(getOutputText()); - event.setOutputKeycodes(getOutputKeycodes()); - event.setBindViewGroup(getBindViewGroup()); + event.setOutputKeycodes(outputKeycodesList()); + event.setBindViewGroup(bindViewGroupList()); return event; } @@ -441,8 +402,8 @@ public class ButtonEventData implements Cloneable, Observable { obj.addProperty("switchTouchMode", src.isSwitchTouchMode()); obj.addProperty("input", src.isInput()); obj.addProperty("outputText", src.getOutputText()); - obj.addProperty("outputKeycodes", gson.toJson(src.getOutputKeycodes())); - obj.addProperty("bindViewGroup", gson.toJson(src.getBindViewGroup())); + obj.addProperty("outputKeycodes", gson.toJson(src.outputKeycodesList())); + obj.addProperty("bindViewGroup", gson.toJson(src.bindViewGroupList())); return obj; } @@ -464,8 +425,8 @@ public class ButtonEventData implements Cloneable, Observable { event.setSwitchTouchMode(Optional.ofNullable(obj.get("switchTouchMode")).map(JsonElement::getAsBoolean).orElse(false)); event.setInput(Optional.ofNullable(obj.get("input")).map(JsonElement::getAsBoolean).orElse(false)); event.setOutputText(Optional.ofNullable(obj.get("outputText")).map(JsonElement::getAsString).orElse("")); - event.setOutputKeycodes(gson.fromJson(Optional.ofNullable(obj.get("outputKeycodes")).map(JsonElement::getAsString).orElse(gson.toJson(new ArrayList<>())), new TypeToken(){}.getType())); - event.setBindViewGroup(gson.fromJson(Optional.ofNullable(obj.get("bindViewGroup")).map(JsonElement::getAsString).orElse(gson.toJson(new ArrayList<>())), new TypeToken(){}.getType())); + event.setOutputKeycodes(gson.fromJson(Optional.ofNullable(obj.get("outputKeycodes")).map(JsonElement::getAsString).orElse(gson.toJson(FXCollections.observableArrayList(new ArrayList<>()))), new TypeToken(){}.getType())); + event.setBindViewGroup(gson.fromJson(Optional.ofNullable(obj.get("bindViewGroup")).map(JsonElement::getAsString).orElse(gson.toJson(FXCollections.observableArrayList(new ArrayList<>()))), new TypeToken(){}.getType())); return event; } diff --git a/FCL/src/main/java/com/tungsten/fcl/control/data/ControlViewGroup.java b/FCL/src/main/java/com/tungsten/fcl/control/data/ControlViewGroup.java index 0d756eb6..5a39bc62 100644 --- a/FCL/src/main/java/com/tungsten/fcl/control/data/ControlViewGroup.java +++ b/FCL/src/main/java/com/tungsten/fcl/control/data/ControlViewGroup.java @@ -20,6 +20,8 @@ import com.tungsten.fclcore.fakefx.beans.property.ObjectProperty; import com.tungsten.fclcore.fakefx.beans.property.SimpleObjectProperty; import com.tungsten.fclcore.fakefx.beans.property.SimpleStringProperty; import com.tungsten.fclcore.fakefx.beans.property.StringProperty; +import com.tungsten.fclcore.fakefx.collections.FXCollections; +import com.tungsten.fclcore.fakefx.collections.ObservableList; import com.tungsten.fclcore.util.fakefx.ObservableHelper; import java.lang.reflect.Type; @@ -169,108 +171,108 @@ public class ControlViewGroup implements Cloneable, Observable { } @JsonAdapter(ViewData.Serializer.class) - public static class ViewData implements Cloneable { + public static class ViewData implements Cloneable, Observable { /** * Button data list */ - private final ObjectProperty> buttonListProperty = new SimpleObjectProperty<>(this, "buttonList", new ArrayList<>()); + private final ObservableList buttonList = FXCollections.observableArrayList(new ArrayList<>()); - public ObjectProperty> buttonListProperty() { - return buttonListProperty; + public ObservableList buttonList() { + return buttonList; } - public void setButtonList(ArrayList buttonList) { - buttonListProperty.set(buttonList); - } - - public ArrayList getButtonList() { - return buttonListProperty.get(); + public void setButtonList(ObservableList list) { + buttonList.setAll(list); } /** * Direction data list */ - private final ObjectProperty> directionListProperty = new SimpleObjectProperty<>(this, "directionList", new ArrayList<>()); + private final ObservableList directionList = FXCollections.observableArrayList(new ArrayList<>()); - public ObjectProperty> directionListProperty() { - return directionListProperty; + public ObservableList directionList() { + return directionList; } - public void setDirectionList(ArrayList directionList) { - directionListProperty.set(directionList); - } - - public ArrayList getDirectionList() { - return directionListProperty.get(); + public void setDirectionList(ObservableList list) { + directionList.setAll(list); } public void addButton(ControlButtonData data) { - ArrayList list = getButtonList(); boolean exist = false; - for (ControlButtonData buttonData : list) { + for (ControlButtonData buttonData : buttonList()) { if (buttonData.equals(data)) { exist = true; break; } } if (!exist) { - list.add(data); - setButtonList(list); + buttonList.add(data); } } public void removeButton(ControlButtonData data) { - ArrayList list = getButtonList(); - for (ControlButtonData buttonData : list) { + for (ControlButtonData buttonData : buttonList()) { if (buttonData.equals(data)) { - list.remove(buttonData); - setButtonList(list); + buttonList.remove(buttonData); break; } } } public void addDirection(ControlDirectionData data) { - ArrayList list = getDirectionList(); boolean exist = false; - for (ControlDirectionData directionData : list) { + for (ControlDirectionData directionData : directionList()) { if (directionData.equals(data)) { exist = true; break; } } if (!exist) { - list.add(data); - setDirectionList(list); + directionList.add(data); } } public void removeDirection(ControlDirectionData data) { - ArrayList list = getDirectionList(); - for (ControlDirectionData directionData : list) { + for (ControlDirectionData directionData : directionList()) { if (directionData.equals(data)) { - list.remove(directionData); - setDirectionList(list); + directionList.remove(directionData); break; } } } public ViewData() { - + addPropertyChangedListener(onInvalidating(this::invalidate)); } public void addPropertyChangedListener(InvalidationListener listener) { - buttonListProperty.addListener(listener); - directionListProperty.addListener(listener); + buttonList.addListener(listener); + directionList.addListener(listener); + } + + private ObservableHelper observableHelper = new ObservableHelper(this); + + @Override + public void addListener(InvalidationListener listener) { + observableHelper.addListener(listener); + } + + @Override + public void removeListener(InvalidationListener listener) { + observableHelper.removeListener(listener); + } + + private void invalidate() { + observableHelper.invalidate(); } @Override public ViewData clone() { ViewData data = new ViewData(); - data.setButtonList(getButtonList()); - data.setDirectionList(getDirectionList()); + data.setButtonList(buttonList()); + data.setDirectionList(directionList()); return data; } @@ -281,8 +283,8 @@ public class ControlViewGroup implements Cloneable, Observable { JsonObject obj = new JsonObject(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); - obj.addProperty("buttonList", gson.toJson(src.getButtonList())); - obj.addProperty("directionList", gson.toJson(src.getDirectionList())); + obj.addProperty("buttonList", gson.toJson(src.buttonList())); + obj.addProperty("directionList", gson.toJson(src.directionList())); return obj; } @@ -296,8 +298,8 @@ public class ControlViewGroup implements Cloneable, Observable { ViewData data = new ViewData(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); - data.setButtonList(gson.fromJson(Optional.ofNullable(obj.get("buttonList")).map(JsonElement::getAsString).orElse(gson.toJson(new ArrayList<>())), new TypeToken(){}.getType())); - data.setDirectionList(gson.fromJson(Optional.ofNullable(obj.get("directionList")).map(JsonElement::getAsString).orElse(gson.toJson(new ArrayList<>())), new TypeToken(){}.getType())); + data.setButtonList(gson.fromJson(Optional.ofNullable(obj.get("buttonList")).map(JsonElement::getAsString).orElse(gson.toJson(FXCollections.observableArrayList(new ArrayList<>()))), new TypeToken(){}.getType())); + data.setDirectionList(gson.fromJson(Optional.ofNullable(obj.get("directionList")).map(JsonElement::getAsString).orElse(gson.toJson(FXCollections.observableArrayList(new ArrayList<>()))), new TypeToken(){}.getType())); return data; } diff --git a/FCL/src/main/java/com/tungsten/fcl/setting/Controller.java b/FCL/src/main/java/com/tungsten/fcl/setting/Controller.java index bef100d7..a2e85455 100644 --- a/FCL/src/main/java/com/tungsten/fcl/setting/Controller.java +++ b/FCL/src/main/java/com/tungsten/fcl/setting/Controller.java @@ -22,12 +22,12 @@ import com.tungsten.fclauncher.FCLPath; import com.tungsten.fclcore.fakefx.beans.InvalidationListener; import com.tungsten.fclcore.fakefx.beans.Observable; import com.tungsten.fclcore.fakefx.beans.property.IntegerProperty; -import com.tungsten.fclcore.fakefx.beans.property.ObjectProperty; import com.tungsten.fclcore.fakefx.beans.property.ReadOnlyIntegerProperty; import com.tungsten.fclcore.fakefx.beans.property.SimpleIntegerProperty; -import com.tungsten.fclcore.fakefx.beans.property.SimpleObjectProperty; import com.tungsten.fclcore.fakefx.beans.property.SimpleStringProperty; import com.tungsten.fclcore.fakefx.beans.property.StringProperty; +import com.tungsten.fclcore.fakefx.collections.FXCollections; +import com.tungsten.fclcore.fakefx.collections.ObservableList; import com.tungsten.fclcore.util.ToStringBuilder; import com.tungsten.fclcore.util.fakefx.ObservableHelper; import com.tungsten.fclcore.util.gson.fakefx.factories.JavaFxPropertyTypeAdapterFactory; @@ -38,9 +38,10 @@ import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Optional; +import java.util.stream.Collectors; @JsonAdapter(Controller.Serializer.class) -public class Controller implements Observable { +public class Controller implements Cloneable, Observable { private final SimpleStringProperty name; @@ -108,18 +109,14 @@ public class Controller implements Observable { return controllerVersion.get(); } - private final SimpleObjectProperty> viewGroups; + private final ObservableList viewGroups; - public ObjectProperty> viewGroupsProperty() { + public ObservableList viewGroups() { return viewGroups; } - public ArrayList getViewGroups() { - return viewGroups.get(); - } - - public void setViewGroups(ArrayList viewGroups) { - this.viewGroups.set(viewGroups); + public void setViewGroups(ObservableList viewGroups) { + this.viewGroups.addAll(viewGroups); } public Controller(String name) { @@ -139,15 +136,15 @@ public class Controller implements Observable { } public Controller(String name, String version, String author, String description, int controllerVersion) { - this(name, version, author, description, controllerVersion, new ArrayList<>()); + this(name, version, author, description, controllerVersion, FXCollections.observableArrayList(new ArrayList<>())); } - public Controller(String name, String version, String author, String description, int controllerVersion, ArrayList viewGroups) { + public Controller(String name, String version, String author, String description, int controllerVersion, ObservableList viewGroups) { this.name = new SimpleStringProperty(this, "name", name); this.version = new SimpleStringProperty(this, "version", version); this.author = new SimpleStringProperty(this, "author", author); this.description = new SimpleStringProperty(this, "description", description); - this.viewGroups = new SimpleObjectProperty<>(this, "viewGroups", viewGroups); + this.viewGroups = viewGroups; this.controllerVersion.set(controllerVersion); @@ -155,26 +152,22 @@ public class Controller implements Observable { } public void addViewGroup(ControlViewGroup viewGroup) { - ArrayList list = getViewGroups(); boolean exist = false; - for (ControlViewGroup group : list) { + for (ControlViewGroup group : viewGroups()) { if (viewGroup.getId().equals(group.getId())) { exist = true; break; } } if (!exist) { - list.add(viewGroup); - setViewGroups(list); + viewGroups.add(viewGroup); } } public void removeViewGroup(ControlViewGroup viewGroup) { - ArrayList list = getViewGroups(); - for (ControlViewGroup group : list) { + for (ControlViewGroup group : viewGroups()) { if (viewGroup.getId().equals(group.getId())) { - list.remove(group); - setViewGroups(list); + viewGroups.remove(group); break; } } @@ -217,6 +210,13 @@ public class Controller implements Observable { observableHelper.invalidate(); } + @Override + public Controller clone() { + ObservableList viewGroups = FXCollections.observableArrayList(new ArrayList<>()); + viewGroups.addAll(viewGroups().stream().map(ControlViewGroup::clone).collect(Collectors.toList())); + return new Controller(getName() + "_clone", getVersion(), getAuthor(), getDescription(), getControllerVersion(), viewGroups); + } + // function public String getFileName() { @@ -249,7 +249,7 @@ public class Controller implements Observable { jsonObject.addProperty("author", src.getAuthor()); jsonObject.addProperty("description", src.getDescription()); jsonObject.addProperty("controllerVersion", src.getControllerVersion()); - jsonObject.addProperty("viewGroups", new GsonBuilder().setPrettyPrinting().create().toJson(src.getViewGroups())); + jsonObject.addProperty("viewGroups", new GsonBuilder().setPrettyPrinting().create().toJson(src.viewGroups())); return jsonObject; } @@ -265,7 +265,7 @@ public class Controller implements Observable { String author = Optional.ofNullable(obj.get("author")).map(JsonElement::getAsString).orElse(""); String description = Optional.ofNullable(obj.get("description")).map(JsonElement::getAsString).orElse(""); int controllerVersion = Optional.ofNullable(obj.get("controllerVersion")).map(JsonElement::getAsInt).orElse(Constants.CONTROLLER_VERSION); - ArrayList viewGroups = gson.fromJson(Optional.ofNullable(obj.get("controllerVersion")).map(JsonElement::getAsString).orElse(gson.toJson(new ArrayList<>())), new TypeToken(){}.getType()); + ObservableList viewGroups = gson.fromJson(Optional.ofNullable(obj.get("viewGroups")).map(JsonElement::getAsString).orElse(gson.toJson(FXCollections.observableArrayList(new ArrayList<>()))), new TypeToken(){}.getType()); return new Controller(name, version, author, description, controllerVersion, viewGroups); }