Skip to content

Commit

Permalink
Fixes bug on history
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed Apr 1, 2015
1 parent 990576b commit 92e654f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 35 deletions.
19 changes: 12 additions & 7 deletions app/src/main/groovy/jd/gui/model/history/History.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ class History {
List<URI> forward = []

void add(URI uri) {
if (current.equals(uri)) {
// Already stored -> Quit
return
}

if (current == null) {
// Init history
forward.clear()
current = uri
return
}

if (current.equals(uri)) {
// Already stored -> Nothing to do
return
}

if (uri.path.toString().equals(current.path.toString())) {
if (uri.fragment == null) {
if ((uri.fragment == null) && (uri.query == null)) {
// Ignore
} else if (current.fragment == null) {
} else if ((current.fragment == null) && (current.query == null)) {
// Replace current URI
current = uri
} else {
Expand All @@ -44,6 +44,11 @@ class History {
return
}

if (current.toString().startsWith(uri.toString())) {
// Parent URI -> Nothing to do
return
}

// Store URI
forward.clear()
backward.add(current)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import jd.gui.service.platform.PlatformService

class MainTabbedPanel extends TabbedPanel implements UriOpenable, PageChangeListener {
List<PageChangeListener> pageChangedListeners = []
// Flag to prevent the event cascades
boolean pageChangedListenersEnabled = true

void create() {
Color bg = darker(background)
Expand All @@ -38,15 +40,16 @@ class MainTabbedPanel extends TabbedPanel implements UriOpenable, PageChangeList
tabbedPane = createTabPanel()
tabbedPane.addChangeListener(new ChangeListener() {
void stateChanged(ChangeEvent e) {
def page = tabbedPane.selectedComponent?.getClientProperty('currentPage')

if (page == null) {
page = tabbedPane.selectedComponent
}

// Notify all container pages are closeClosure
for (def listener : pageChangedListeners) {
listener.pageChanged(page)
if (pageChangedListenersEnabled) {
def page = tabbedPane.selectedComponent?.getClientProperty('currentPage')

if (page == null) {
page = tabbedPane.selectedComponent
}
// Fire page changed event
for (def listener : pageChangedListeners) {
listener.pageChanged(page)
}
}
}
})
Expand Down Expand Up @@ -121,20 +124,31 @@ class MainTabbedPanel extends TabbedPanel implements UriOpenable, PageChangeList

// --- URIOpener --- //
boolean openUri(URI uri) {
def page = showPage(uri)
if (page) {
if (page instanceof UriOpenable) {
page.openUri(uri)
try {
// Disable page changed event
pageChangedListenersEnabled = false
// Search & display main tab
def page = showPage(uri)
if (page) {
if (page instanceof UriOpenable) {
// Enable page changed event
pageChangedListenersEnabled = true
// Search & display sub tab
page.openUri(uri)
}
return true
} else {
return false
}
return true
} else {
return false
} finally {
// Enable page changed event
pageChangedListenersEnabled = true
}
}

// --- PageChangedListener --- //
public <T extends JComponent & UriGettable> void pageChanged(T page) {
// Store active page for current tabbed pane
// Store active page for current sub tabbed pane
tabbedPane.selectedComponent?.putClientProperty('currentPage', page)
// Forward event
for (def listener : pageChangedListeners) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ class TreeTabbedPanel extends JPanel implements UriGettable, UriOpenable, PageCh
Tree tree
TabbedPanel tabbedPanel
List<PageChangeListener> pageChangedListeners = []
boolean updateTreeMenuFlag = true
boolean openUriFlag = true
// Flags to prevent the event cascades
boolean updateTreeMenuEnabled = true
boolean openUriEnabled = true
boolean treeNodeChangedEnabled = true

TreeTabbedPanel(API api, URI uri) {
this.api = api
Expand Down Expand Up @@ -79,10 +81,10 @@ class TreeTabbedPanel extends JPanel implements UriGettable, UriOpenable, PageCh
}

protected <T extends DefaultMutableTreeNode & UriGettable> void treeNodeChanged(T node) {
if (node) {
if (treeNodeChangedEnabled && node) {
try {
// Disable tabbedPane.changeListener
updateTreeMenuFlag = false
updateTreeMenuEnabled = false

// Search base tree node
def uri = node.uri
Expand All @@ -102,7 +104,7 @@ class TreeTabbedPanel extends JPanel implements UriGettable, UriOpenable, PageCh
}
} finally {
// Enable tabbedPane.changeListener
updateTreeMenuFlag = true
updateTreeMenuEnabled = true
}
}
}
Expand All @@ -127,7 +129,7 @@ class TreeTabbedPanel extends JPanel implements UriGettable, UriOpenable, PageCh
}
}

if (openUriFlag && page instanceof UriOpenable) {
if (openUriEnabled && page instanceof UriOpenable) {
api.addURI(uri)
page.openUri(uri)
}
Expand All @@ -138,11 +140,11 @@ class TreeTabbedPanel extends JPanel implements UriGettable, UriOpenable, PageCh
void pageChanged() {
try {
// Disable highlight
openUriFlag = false
openUriEnabled = false

def page = tabbedPanel.tabbedPane.selectedComponent

if (updateTreeMenuFlag) {
if (updateTreeMenuEnabled) {
// Synchronize tree
if (page) {
def node = page.getClientProperty('node')
Expand All @@ -159,7 +161,7 @@ class TreeTabbedPanel extends JPanel implements UriGettable, UriOpenable, PageCh
}
} finally {
// Enable highlight
openUriFlag = true
openUriEnabled = true
}
}

Expand All @@ -170,7 +172,25 @@ class TreeTabbedPanel extends JPanel implements UriGettable, UriOpenable, PageCh
boolean openUri(URI uri) {
def baseUri = new URI(uri.scheme, uri.host, uri.path, null)
def baseNode = searchTreeNode(baseUri, tree.model.root)
return baseNode && showPage(uri, baseUri, baseNode)

if (baseNode && showPage(uri, baseUri, baseNode)) {
def node = searchTreeNode(uri, baseNode)
if (node) {
try {
// Disable tree node changed listener
treeNodeChangedEnabled = false
// Select tree node
tree.selectionPath = node.path
tree.scrollPathToVisible(tree.selectionPath)
} finally {
// Enable tree node changed listener
treeNodeChangedEnabled = true
}
}
return true
} else {
return false
}
}

protected DefaultMutableTreeNode searchTreeNode(URI uri, DefaultMutableTreeNode node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class TextPage extends JPanel implements ContentCopyable, ContentSelectable, Lin
if (position.isNumber()) {
int pos = position.toInteger()
if (textArea.document.length > pos) {
RSyntaxUtilities.selectAndPossiblyCenter(textArea, new DocumentRange(pos, pos), false)
setCaretPositionAndCenter(new DocumentRange(pos, pos))
return true
}
}
Expand Down

0 comments on commit 92e654f

Please sign in to comment.