Skip to content

Commit

Permalink
GT-3222 - review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonmacher committed Oct 24, 2019
1 parent c2f2a41 commit 0db9205
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,17 @@ public NextPreviousDataTypeAction(DataTypesProvider provider, String owner, bool

@Override
public void actionPerformed(ActionContext context) {
if (isNext) {
history.goForward();
}
else {
history.goBack();
}
// rely on the individual actions to navigate, as they know them item that is being
// navigated to, allowing them to skip through the list
List<DockingActionIf> actions = getActionList(context);
DockingActionIf action = actions.get(0);
action.actionPerformed(context);
provider.contextChanged();
}

@Override
public boolean isEnabledForContext(ActionContext context) {

if (isNext) {
return history.hasNext();
}
return history.hasPrevious();
return !getActionList(context).isEmpty();
}

@Override
Expand Down Expand Up @@ -111,7 +106,7 @@ private List<DockingActionIf> createNavigationActions() {
results.add(createHorizontalRule(lastDtm, dtm));
}

results.add(new NavigationAction(dt.getDisplayName()));
results.add(new NavigationAction(url, dt));
lastDtm = dtm;
}

Expand All @@ -129,21 +124,28 @@ private DockingActionIf createHorizontalRule(DataTypeManager lastDtm, DataTypeMa

private class NavigationAction extends DockingAction {

private NavigationAction(String dtDisplayName) {
private DataTypeUrl url;

private NavigationAction(DataTypeUrl url, DataType dt) {
super("DataTypeNavigationAction_" + ++navigationActionIdCount, owner);
this.url = url;

setMenuBarData(new MenuData(new String[] { dtDisplayName }));
setMenuBarData(new MenuData(new String[] { dt.getDisplayName() }));
setEnabled(true);
setHelpLocation(new HelpLocation("DataTypeManagerPlugin", "Navigation_Actions"));
}

@Override
public void actionPerformed(ActionContext context) {

// note: we use 'goBackTo()' and 'goForwardTo()' since items in the history list
// may not have been added to the multi-action; we have to tell the list
// to skip those items.
if (isNext) {
history.goForward();
history.goForwardTo(url);
}
else {
history.goBack();
history.goBackTo(url);
}
provider.contextChanged();
}
Expand Down
24 changes: 24 additions & 0 deletions Ghidra/Framework/Generic/src/main/java/util/HistoryList.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ public void goBack() {
broadcast(t);
}

/**
* Performs a {@link #goBack()} until the given item becomes the current item. This is
* useful if you wish to go backward to a specific item in the list.
*
* @param t the item
*/
public void goBackTo(T t) {
while (!getCurrentHistoryItem().equals(t) && hasPrevious()) {
goBack();
}
}

/**
* Moves this history list's current item pointer forward one and then calls the user-provided
* callback to signal the newly selected item.
Expand All @@ -190,6 +202,18 @@ public void goForward() {
broadcast(t);
}

/**
* Performs a {@link #goForward()} until the given item becomes the current item. This is
* useful if you wish to go forward to a specific item in the list.
*
* @param t the item
*/
public void goForwardTo(T t) {
while (!getCurrentHistoryItem().equals(t) && hasNext()) {
goForward();
}
}

/**
* Returns the item currently pointed to within the list of items. When an item is
* added, this will be that item. Otherwise, it will be the last item navigated.
Expand Down
28 changes: 27 additions & 1 deletion Ghidra/Framework/Generic/src/test/java/util/HistoryListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package util;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import java.util.LinkedList;
Expand Down Expand Up @@ -425,6 +425,32 @@ public void testGetPreviousAndNextHistoryItems() {
assertNextItems();
}

@Test
public void testBackToItem() {

addHistory(A);
addHistory(B);
addHistory(C);

historyList.goBackTo(A);
assertCurrentItem(A);
}

@Test
public void testForwardToItem() {

addHistory(A);
addHistory(B);
addHistory(C);
goBack();
goBack();
goBack();
assertCurrentItem(A);

historyList.goForwardTo(C);
assertCurrentItem(C);
}

//==================================================================================================
// Private Methods
//==================================================================================================
Expand Down

0 comments on commit 0db9205

Please sign in to comment.