Skip to content

Commit

Permalink
Fixed a bug that might lead to infinite recursive loops in dialog tree
Browse files Browse the repository at this point in the history
viewer when using NI's search functions
  • Loading branch information
Argent77 committed Oct 7, 2014
1 parent f4339ec commit ae706d3
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 232 deletions.
5 changes: 0 additions & 5 deletions src/infinity/resource/dlg/DlgResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,6 @@ public void showStateWithStructEntry(StructEntry entry) {
getViewerTab(0);
}
detailViewer.showStateWithStructEntry(entry);

if (treeViewer == null) {
getViewerTab(1);
}
treeViewer.showStateWithStructEntry(entry);
}
}

227 changes: 0 additions & 227 deletions src/infinity/resource/dlg/TreeViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import infinity.datatype.Flag;
import infinity.datatype.ResourceRef;
import infinity.datatype.SectionCount;
import infinity.datatype.StringRef;
import infinity.gui.BrowserMenuBar;
import infinity.gui.ViewFrame;
import infinity.gui.ViewerUtil;
Expand Down Expand Up @@ -259,37 +258,6 @@ public void propertyChange(PropertyChangeEvent event)

//--------------------- End Interface PropertyChangeListener ---------------------

/** Jumps to the first available node containing the specified structure. */
public void showStateWithStructEntry(StructEntry entry)
{
DefaultMutableTreeNode node = null;
if (entry instanceof State) {
int stateIdx = ((State)entry).getNumber();
node = dlgModel.findStateNode(stateIdx);
} else if (entry instanceof Transition) {
int transIdx = ((Transition)entry).getNumber();
node = dlgModel.findTransitionNode(transIdx);
} else if (entry instanceof StateTrigger) {
int triggerIdx = ((StateTrigger)entry).getNumber();
node = dlgModel.findStateTriggerNode(triggerIdx);
} else if (entry instanceof ResponseTrigger) {
int triggerIdx = ((ResponseTrigger)entry).getNumber();
node = dlgModel.findResponseTriggerNode(triggerIdx);
} else if (entry instanceof Action) {
int actionIdx = ((Action)entry).getNumber();
node = dlgModel.findActionNode(actionIdx);
} else if (entry instanceof StringRef) {
// may happen when using the DLG Search
node = dlgModel.findStringRefNode(((StringRef)entry).getValue());
}

// selecting node in tree view
if (node != null) {
dlgTree.setSelectionPath(dlgModel.getTreePath(node));
}
}


private void updateStateInfo(StateItem si)
{
if (si != null && si.getDialog() != null && si.getState() != null) {
Expand Down Expand Up @@ -998,8 +966,6 @@ public String toString()
// Creates and manages the dialog tree structure
private static final class DlgTreeModel implements TreeModel
{
private enum ParamType { State, StateTrigger, Transition, ResponseTrigger, Action, Strref }

private final ArrayList<TreeModelListener> listeners = new ArrayList<TreeModelListener>();
// maps dialog resources to tables of state index/item pairs
private final HashMap<String, HashMap<Integer, StateItem>> mapState = new HashMap<String, HashMap<Integer, StateItem>>();
Expand Down Expand Up @@ -1111,28 +1077,6 @@ public void nodeChanged(TreeNode node)
}
}

// public void nodesChanged(TreeNode node, int[] childIndices)
// {
// if (node != null && childIndices != null) {
// boolean isValid = true;
// for (int i = 0; i < childIndices.length; i++) {
// if (childIndices[i] < 0 || childIndices[i] >= node.getChildCount()) {
// isValid = false;
// break;
// }
// }
// if (isValid) {
// Object[] children = new Object[childIndices.length];
// for (int i = 0; i < children.length; i++) {
// children[i] = node.getChildAt(childIndices[i]);
// }
// fireTreeNodesChanged(this, createNodePath(node), childIndices, children);
// }
// } else {
// fireTreeNodesChanged(this, null, null, null);
// }
// }

public void nodeStructureChanged(TreeNode node)
{
if (node.getParent() == null) {
Expand All @@ -1143,31 +1087,6 @@ public void nodeStructureChanged(TreeNode node)
}
}

// public void nodesWereInserted(TreeNode node, int[] childIndices)
// {
// if (node != null && childIndices != null) {
// boolean isValid = true;
// for (int i = 0; i < childIndices.length; i++) {
// if (childIndices[i] < 0 || childIndices[i] >= node.getChildCount()) {
// isValid = false;
// break;
// }
// }
// if (isValid) {
// Object[] children = new Object[childIndices.length];
// for (int i = 0; i < children.length; i++) {
// children[i] = node.getChildAt(childIndices[i]);
// }
// fireTreeNodesInserted(this, createNodePath(node), childIndices, children);
// }
// }
// }

// public void nodesWereRemoved(TreeNode node, int[] childIndices, Object[] removedChildren)
// {
// fireTreeNodesRemoved(this, createNodePath(node), childIndices, removedChildren);
// }

/** Removes any old content and re-initializes the model with the data from the given dialog resource. */
public void reset(DlgResource dlg)
{
Expand Down Expand Up @@ -1205,71 +1124,6 @@ public void reset(DlgResource dlg)
nodeStructureChanged((DefaultMutableTreeNode)getRoot());
}

/**
* Returns a fully qualified TreePath object from root to the specified node.
* Returns a root node TreePath object on error.
*/
public TreePath getTreePath(TreeNode node)
{
if (node != null) {
// building reverse tree path
Stack<TreeNode> stack = new Stack<TreeNode>();
while (node != null) {
stack.push(node);
node = node.getParent();
}

// returning valid TreePath object
TreeNode[] nodes = new TreeNode[stack.size()];
for (int i = 0; i < nodes.length; i++) {
nodes[i] = stack.pop();
}
return new TreePath(nodes);
}

// defaults to selecting root node
return new TreePath(getRoot());
}

/** Returns the first available StateItem node matching the given state index. */
public DefaultMutableTreeNode findStateNode(int stateIdx)
{
return searchTreeNode(nodeRoot, ParamType.State, stateIdx, MAX_DEPTH);
}

/** Returns the first available TransitionItem node matching the given transition index. */
public DefaultMutableTreeNode findTransitionNode(int transIdx)
{
return searchTreeNode(nodeRoot, ParamType.Transition, transIdx, MAX_DEPTH);
}

/** Returns the first available StateItem node matching the given state trigger index. */
public DefaultMutableTreeNode findStateTriggerNode(int triggerIdx)
{
return searchTreeNode(nodeRoot, ParamType.StateTrigger, triggerIdx, MAX_DEPTH);
}

/** Returns the first available TransitionItem node matching the given response trigger index. */
public DefaultMutableTreeNode findResponseTriggerNode(int triggerIdx)
{
return searchTreeNode(nodeRoot, ParamType.ResponseTrigger, triggerIdx, MAX_DEPTH);
}

/** Returns the first available TransitionItem node matching the given action index. */
public DefaultMutableTreeNode findActionNode(int actionIdx)
{
return searchTreeNode(nodeRoot, ParamType.Action, actionIdx, MAX_DEPTH);
}

/**
* Returns the first available StateItem or TransitionItem node matching the given string reference
* of their associated text messages
*/
public DefaultMutableTreeNode findStringRefNode(int strref)
{
return searchTreeNode(nodeRoot, ParamType.Strref, strref, MAX_DEPTH);
}

public void updateState(State state)
{
if (state != null) {
Expand Down Expand Up @@ -1433,87 +1287,6 @@ private void fireTreeStructureChanged(Object source, Object[] path, int[] childI
}
}

// Recursively searches tree nodes based on the specified parameters up to a max. node depth
private DefaultMutableTreeNode searchTreeNode(DefaultMutableTreeNode parent,
ParamType param, int value, int maxDepth)
{
if (parent != null && maxDepth > 0) {
parent = updateNodeChildren(parent);

ItemBase item = null;
if (parent.getUserObject() instanceof ItemBase) {
item = (ItemBase)parent.getUserObject();
}

boolean isDlg = false;
if (item != null) {
isDlg = item.getDialogName().equals(dlg.getResourceEntry().getResourceName());
}

// checking node properties
switch (param) {
case State:
if (isDlg && item instanceof StateItem &&
((StateItem)item).getState().getNumber() == value) {
return parent;
}
break;
case StateTrigger:
if (isDlg && item instanceof StateItem &&
((StateItem)item).getState().getTriggerIndex() == value) {
return parent;
}
break;
case Transition:
if (isDlg && item instanceof TransitionItem &&
((TransitionItem)item).getTransition().getNumber() == value) {
return parent;
}
break;
case ResponseTrigger:
if (isDlg && item instanceof TransitionItem &&
((TransitionItem)item).getTransition().getTriggerIndex() == value) {
return parent;
}
break;
case Action:
if (isDlg && item instanceof TransitionItem &&
((TransitionItem)item).getTransition().getActionIndex() == value) {
return parent;
}
break;
case Strref:
if (isDlg) {
if (item instanceof StateItem) {
if (((StateItem)item).getState().getResponse().getValue() == value) {
return parent;
}
} else if (item instanceof TransitionItem) {
if (((TransitionItem)item).getTransition().getAssociatedText().getValue() == value) {
return parent;
} else if (((TransitionItem)item).getTransition().getJournalEntry().getValue() == value) {
return parent;
}
}
}
break;
}

// continue searching in child nodes
for (int i = 0; i < parent.getChildCount(); i++) {
DefaultMutableTreeNode retVal = searchTreeNode((DefaultMutableTreeNode)parent.getChildAt(i),
param, value, maxDepth-1);
if (retVal != null) {
return retVal;
}
}
}

// no match found
return null;
}


private void initState(StateItem state)
{
if (state != null) {
Expand Down

0 comments on commit ae706d3

Please sign in to comment.