diff --git a/src/com/gpl/rpg/atcontentstudio/model/GameDataElement.java b/src/com/gpl/rpg/atcontentstudio/model/GameDataElement.java index 46bc743..65f5fae 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/GameDataElement.java +++ b/src/com/gpl/rpg/atcontentstudio/model/GameDataElement.java @@ -36,29 +36,6 @@ public abstract class GameDataElement implements ProjectTreeNode, Serializable { public String id = null; - protected boolean linkCheck() { - if (checkNotRelatedToParseOrLink()) { - //This type of state is unrelated to parsing/linking. - return false; - } else if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. - return false; - } - return true; - - } - - protected boolean checkNotRelatedToParseOrLink() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return true; - } - return false; - } - @Override public Enumeration children() { return null; @@ -226,4 +203,34 @@ public abstract class GameDataElement implements ProjectTreeNode, Serializable { public abstract List attemptSave(); + /** + * Checks if the current state indicates that parsing/linking should be skipped. + * @return true if the operation should be skipped, false otherwise + */ + protected boolean shouldSkipParseOrLink() { + if (shouldSkipParse()) return true; + if (this.state == State.linked) { + //Already linked. + return true; + } + return false; + } + + protected boolean shouldSkipParse() { + if (this.state == State.created || this.state == State.modified || this.state == State.saved) { + //This type of state is unrelated to parsing/linking. + return true; + } + return false; + } + + /** + * Ensures the element is parsed if needed based on its current state. + */ + protected void ensureParseIfNeeded() { + if (this.state == State.init) { + //Not parsed yet. + this.parse(); + } + } } diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/ActorCondition.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/ActorCondition.java index 882c006..2643a4f 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/ActorCondition.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/ActorCondition.java @@ -222,7 +222,10 @@ public class ActorCondition extends JSONElement { @Override public void link() { - if (!this.linkCheck()) return; + if (shouldSkipParseOrLink()) { + return; + } + ensureParseIfNeeded(); if (this.icon_id != null) { String spritesheetId = this.icon_id.split(":")[0]; if (getProject().getSpritesheet(spritesheetId) == null) { diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Dialogue.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Dialogue.java index d3e17c7..657217f 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Dialogue.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Dialogue.java @@ -205,7 +205,10 @@ public class Dialogue extends JSONElement { @Override public void link() { - if (!this.linkCheck()) return; + if (shouldSkipParseOrLink()) { + return; + } + ensureParseIfNeeded(); Project proj = getProject(); if (proj == null) { Notification.addError("Error linking dialogue " + id + ". No parent project found."); diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Droplist.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Droplist.java index c835ee5..900b80c 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Droplist.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Droplist.java @@ -128,7 +128,10 @@ public class Droplist extends JSONElement { @Override public void link() { - if (!this.linkCheck()) return; + if (shouldSkipParseOrLink()) { + return; + } + ensureParseIfNeeded(); Project proj = getProject(); if (proj == null) { Notification.addError("Error linking droplist " + id + ". No parent project found."); diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java index c7cfa2c..1daaeda 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java @@ -206,7 +206,10 @@ public class Item extends JSONElement { @Override public void link() { - if (!this.linkCheck()) return; + if (shouldSkipParseOrLink()) { + return; + } + ensureParseIfNeeded(); Project proj = getProject(); if (proj == null) { Notification.addError("Error linking item " + id + ". No parent project found."); diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/ItemCategory.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/ItemCategory.java index 7c8db56..1b9fe82 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/ItemCategory.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/ItemCategory.java @@ -171,7 +171,10 @@ public class ItemCategory extends JSONElement { @Override public void link() { - if (!this.linkCheck()) return; + if (shouldSkipParseOrLink()) { + return; + } + ensureParseIfNeeded(); //Nothing to link to :D this.state = State.linked; diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/JSONElement.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/JSONElement.java index b044670..fdc9502 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/JSONElement.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/JSONElement.java @@ -21,8 +21,7 @@ public abstract class JSONElement extends GameDataElement { @SuppressWarnings("rawtypes") public void parse() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. + if (shouldSkipParse()) { return; } JSONParser parser = new JSONParser(); diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java index 743245a..505c2d9 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java @@ -210,7 +210,10 @@ public class NPC extends JSONElement { @Override public void link() { - if (!this.linkCheck()) return; + if (shouldSkipParseOrLink()) { + return; + } + ensureParseIfNeeded(); Project proj = getProject(); if (proj == null) { Notification.addError("Error linking item " + id + ". No parent project found."); diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Quest.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Quest.java index 176dc63..6a2a79a 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Quest.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Quest.java @@ -112,7 +112,10 @@ public class Quest extends JSONElement { @Override public void link() { - if (!this.linkCheck()) return; + if (shouldSkipParseOrLink()) { + return; + } + ensureParseIfNeeded(); for (QuestStage stage : stages) { stage.link(); diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/QuestStage.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/QuestStage.java index 838a158..f8133ca 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/QuestStage.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/QuestStage.java @@ -59,7 +59,10 @@ public class QuestStage extends JSONElement { @Override public void link() { - if (!this.linkCheck()) return; + if (shouldSkipParseOrLink()) { + return; + } + ensureParseIfNeeded(); //Nothing to link to :D this.state = State.linked; diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Requirement.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Requirement.java index 9a15578..7bd1de9 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Requirement.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Requirement.java @@ -122,7 +122,10 @@ public class Requirement extends JSONElement { @Override public void link() { - if (!this.linkCheck()) return; + if (shouldSkipParseOrLink()) { + return; + } + ensureParseIfNeeded(); Project proj = getProject(); if (proj == null) { Notification.addError("Error linking requirement " + getDesc() + ". No parent project found."); diff --git a/src/com/gpl/rpg/atcontentstudio/model/tools/writermode/WriterModeData.java b/src/com/gpl/rpg/atcontentstudio/model/tools/writermode/WriterModeData.java index d2bf908..bdb7995 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/tools/writermode/WriterModeData.java +++ b/src/com/gpl/rpg/atcontentstudio/model/tools/writermode/WriterModeData.java @@ -530,7 +530,7 @@ public class WriterModeData extends GameDataElement { @SuppressWarnings("rawtypes") public void parse() { - if (checkNotRelatedToParseOrLink()) return; + if (shouldSkipParse()) return; JSONParser parser = new JSONParser(); FileReader reader = null; try { diff --git a/src/com/gpl/rpg/atcontentstudio/utils/UiUtils.java b/src/com/gpl/rpg/atcontentstudio/utils/UiUtils.java index a40dbcc..65631b8 100644 --- a/src/com/gpl/rpg/atcontentstudio/utils/UiUtils.java +++ b/src/com/gpl/rpg/atcontentstudio/utils/UiUtils.java @@ -29,23 +29,23 @@ public class UiUtils { } public static > CollapsibleItemListCreation getCollapsibleItemList(FieldUpdateListener listener, - M itemsListModel, - BasicLambda selectedItemReset, - BasicLambdaWithArg setSelectedItem, - BasicLambdaWithReturn selectedItem, + M listModel, + BasicLambda selectedReset, + BasicLambdaWithArg setSelected, + BasicLambdaWithReturn getSelected, BasicLambdaWithArg valueChanged, BasicLambdaWithArg updateEditorPane, boolean writable, - Supplier tempSupplier, + Supplier newValueSupplier, DefaultListCellRenderer cellRenderer, String title, BasicLambdaWithArgAndReturn getReferencedObj) { CollapsiblePanel itemsPane = new CollapsiblePanel(title); itemsPane.setLayout(new JideBoxLayout(itemsPane, JideBoxLayout.PAGE_AXIS)); - final JList itemsList = new JList<>(itemsListModel); - itemsList.setCellRenderer(cellRenderer); - itemsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - itemsPane.add(new JScrollPane(itemsList), JideBoxLayout.FIX); + final JList list = new JList<>(listModel); + list.setCellRenderer(cellRenderer); + list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + itemsPane.add(new JScrollPane(list), JideBoxLayout.FIX); final JPanel editorPane = new JPanel(); final JButton createBtn = new JButton(new ImageIcon(DefaultIcons.getCreateIcon())); final JButton deleteBtn = new JButton(new ImageIcon(DefaultIcons.getNullifyIcon())); @@ -54,18 +54,18 @@ public class UiUtils { deleteBtn.setEnabled(false); moveUpBtn.setEnabled(false); moveDownBtn.setEnabled(false); - itemsList.addListSelectionListener(e -> { - E selectedValue = itemsList.getSelectedValue(); + list.addListSelectionListener(e -> { + E selectedValue = list.getSelectedValue(); valueChanged.doIt(selectedValue); - setSelectedItem.doIt(selectedValue); + setSelected.doIt(selectedValue); if (selectedValue == null) { deleteBtn.setEnabled(false); moveUpBtn.setEnabled(false); moveDownBtn.setEnabled(false); } else { deleteBtn.setEnabled(true); - moveUpBtn.setEnabled(itemsList.getSelectedIndex() > 0); - moveDownBtn.setEnabled(itemsList.getSelectedIndex() < (itemsListModel.getSize() - 1)); + moveUpBtn.setEnabled(list.getSelectedIndex() > 0); + moveDownBtn.setEnabled(list.getSelectedIndex() < (listModel.getSize() - 1)); } updateEditorPane.doIt(editorPane); @@ -74,14 +74,14 @@ public class UiUtils { JPanel listButtonsPane = new JPanel(); listButtonsPane.setLayout(new JideBoxLayout(listButtonsPane, JideBoxLayout.LINE_AXIS, 6)); - addRemoveAndAddButtons(listener, itemsListModel, selectedItemReset, selectedItem, tempSupplier, createBtn, itemsList, listButtonsPane, deleteBtn); - addMoveButtonListeners(listener, itemsListModel, selectedItem, moveUpBtn, itemsList, listButtonsPane, moveDownBtn); + addRemoveAndAddButtons(listener, listModel, selectedReset, getSelected, newValueSupplier, createBtn, list, listButtonsPane, deleteBtn); + addMoveButtonListeners(listener, listModel, getSelected, moveUpBtn, list, listButtonsPane, moveDownBtn); listButtonsPane.add(new JPanel(), JideBoxLayout.VARY); itemsPane.add(listButtonsPane, JideBoxLayout.FIX); } - addNavigationListeners(getReferencedObj, itemsList); + addNavigationListeners(getReferencedObj, list); editorPane.setLayout(new JideBoxLayout(editorPane, JideBoxLayout.PAGE_AXIS)); itemsPane.add(editorPane, JideBoxLayout.FIX); @@ -89,14 +89,14 @@ public class UiUtils { return new CollapsibleItemListCreation() { { collapsiblePanel = itemsPane; - list = itemsList; + list = list; } }; } - private static > void addRemoveAndAddButtons(FieldUpdateListener listener, M itemsListModel, BasicLambda selectedItemReset, BasicLambdaWithReturn selectedItem, Supplier tempSupplier, JButton createBtn, JList itemsList, JPanel listButtonsPane, JButton deleteBtn) { + private static > void addRemoveAndAddButtons(FieldUpdateListener listener, M itemsListModel, BasicLambda selectedItemReset, BasicLambdaWithReturn selectedItem, Supplier newValueSupplier, JButton createBtn, JList itemsList, JPanel listButtonsPane, JButton deleteBtn) { createBtn.addActionListener(e -> { - E tempItem = tempSupplier.get(); + E tempItem = newValueSupplier.get(); itemsListModel.addItem(tempItem); itemsList.setSelectedValue(tempItem, true); listener.valueChanged(new JLabel(), null); //Item changed, but we took care of it, just do the usual notification and JSON update stuff.