Skip to content

Commit

Permalink
Fixes focus issue on tree node
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed Mar 28, 2015
1 parent 44b26e9 commit 9fd7c2b
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 72 deletions.
121 changes: 60 additions & 61 deletions app/src/main/groovy/jd/gui/view/component/panel/TreeTabbedPanel.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TreeTabbedPanel extends JPanel implements UriGettable, UriOpenable, PageCh
Tree tree
TabbedPanel tabbedPanel
List<PageChangeListener> pageChangedListeners = []
boolean listenerEnabled = true

TreeTabbedPanel(API api, URI uri) {
this.api = api
Expand All @@ -48,20 +49,7 @@ class TreeTabbedPanel extends JPanel implements UriGettable, UriOpenable, PageCh
tree.expandsSelectedPaths = true
tree.cellRenderer = new TreeNodeRenderer()
tree.addTreeSelectionListener(new TreeSelectionListener() {
void valueChanged(TreeSelectionEvent e) {
def path = tree.lastSelectedPathComponent
if (path) {
showPage(path)
}
}
})
tree.addMouseListener(new MouseAdapter() {
void mouseClicked(MouseEvent e) {
TreePath path = tree.getPathForLocation(e.x, e.y)
if (path) {
showPage(path.lastPathComponent)
}
}
void valueChanged(TreeSelectionEvent e) { showPage(tree.lastSelectedPathComponent) }
})
tree.addTreeExpansionListener(new TreeExpansionListener() {
void treeExpanded(TreeExpansionEvent e) {
Expand Down Expand Up @@ -93,67 +81,78 @@ class TreeTabbedPanel extends JPanel implements UriGettable, UriOpenable, PageCh
}

protected <T extends DefaultMutableTreeNode & UriGettable> void showPage(T node) {
// Search base tree node
def uri = node.uri

if ((uri.fragment == null) && (uri.query == null)) {
showPage(uri, uri, node)
} else {
def baseUri = new URI(uri.scheme, uri.host, uri.path, null)
def baseNode = node
if (node) {
// Search base tree node
def uri = node.uri

while (!baseNode?.uri.equals(baseUri)) {
baseNode = baseNode.parent
}
if ((uri.fragment == null) && (uri.query == null)) {
showPage(uri, uri, node)
} else {
def baseUri = new URI(uri.scheme, uri.host, uri.path, null)
def baseNode = node

if (baseNode?.uri.equals(baseUri)) {
showPage(uri, baseUri, baseNode)
while (!baseNode?.uri.equals(baseUri)) {
baseNode = baseNode.parent
}
if (baseNode?.uri.equals(baseUri)) {
showPage(uri, baseUri, baseNode)
}
}
}
}

protected boolean showPage(URI uri, URI baseUri, DefaultMutableTreeNode baseNode) {
def page = tabbedPanel.showPage(baseUri)

if ((page == null) && (baseNode instanceof PageCreator)) {
page = baseNode.createPage(api)
page.putClientProperty('node', baseNode)
page.putClientProperty('preferences-stamp', Integer.valueOf(api.preferences.hashCode()))
page.putClientProperty('collectionOfIndexes-stamp', Integer.valueOf(api.collectionOfIndexes.hashCode()))

def path = baseUri.path
def label = path.substring(path.lastIndexOf('/')+1)
def data = baseNode.userObject
try {
// Disable tabbedPane.changeListener
listenerEnabled = false

def page = tabbedPanel.showPage(baseUri)

if ((page == null) && (baseNode instanceof PageCreator)) {
page = baseNode.createPage(api)
page.putClientProperty('node', baseNode)
page.putClientProperty('preferences-stamp', Integer.valueOf(api.preferences.hashCode()))
page.putClientProperty('collectionOfIndexes-stamp', Integer.valueOf(api.collectionOfIndexes.hashCode()))

def path = baseUri.path
def label = path.substring(path.lastIndexOf('/')+1)
def data = baseNode.userObject

if (data instanceof TreeNodeData) {
tabbedPanel.addPage(label, data.icon, data.tip, page)
} else {
tabbedPanel.addPage(label, null, null, page)
}
}

if (data instanceof TreeNodeData) {
tabbedPanel.addPage(label, data.icon, data.tip, page)
} else {
tabbedPanel.addPage(label, null, null, page)
if (page instanceof UriOpenable) {
api.addURI(uri)
page.openUri(uri)
}
}

if (page instanceof UriOpenable) {
api.addURI(uri)
page.openUri(uri)
return (page != null)
} finally {
// Enable tabbedPane.changeListener
listenerEnabled = true
}

return (page != null)
}

void pageChanged() {
def page = tabbedPanel.tabbedPane.selectedComponent
// Synchronize tree
if (page) {
def node = page.getClientProperty('node')
// Select tree node
tree.selectionPath = node.path
tree.scrollPathToVisible(tree.selectionPath)
} else {
tree.clearSelection()
}
// Fire page changed event
for (def listener : pageChangedListeners) {
listener.pageChanged(page)
if (listenerEnabled) {
def page = tabbedPanel.tabbedPane.selectedComponent
// Synchronize tree
if (page) {
def node = page.getClientProperty('node')
// Select tree node
tree.selectionPath = node.path
tree.scrollPathToVisible(tree.selectionPath)
} else {
tree.clearSelection()
}
// Fire page changed event
for (def listener : pageChangedListeners) {
listener.pageChanged(page)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import jd.gui.util.decompiler.ClassFileSourcePrinter
import jd.gui.util.decompiler.ContainerLoader
import jd.gui.util.decompiler.GuiPreferences
import org.fife.ui.rsyntaxtextarea.DocumentRange
import org.fife.ui.rsyntaxtextarea.RSyntaxUtilities
import org.fife.ui.rsyntaxtextarea.SyntaxConstants

import javax.swing.text.DefaultCaret
Expand Down Expand Up @@ -338,10 +337,10 @@ class ClassFilePage
}
}

boolean t = (highlightFlags.indexOf('t') != -1)
boolean f = (highlightFlags.indexOf('f') != -1)
boolean m = (highlightFlags.indexOf('m') != -1)
boolean c = (highlightFlags.indexOf('c') != -1)
boolean t = (highlightFlags.indexOf('t') != -1) // Highlight types
boolean f = (highlightFlags.indexOf('f') != -1) // Highlight fields
boolean m = (highlightFlags.indexOf('m') != -1) // Highlight methods
boolean c = (highlightFlags.indexOf('c') != -1) // Highlight constructors

if (highlightFlags.indexOf('d') != -1) {
// Highlight declarations
Expand Down Expand Up @@ -380,12 +379,7 @@ class ClassFilePage
textArea.markAllHighlightColor = searchHighlightColor
textArea.highlighter.clearMarkAllHighlights()
textArea.markAll(ranges)

ranges.sort()

def first = ranges[0]
RSyntaxUtilities.selectAndPossiblyCenter(textArea, first, false)
textArea.caretPosition = first.startOffset
setCaretPositionAndCenter(ranges.sort().get(0))
}
}

Expand Down
59 changes: 59 additions & 0 deletions services/src/main/groovy/jd/gui/view/component/TextPage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea
import org.fife.ui.rsyntaxtextarea.RSyntaxUtilities
import org.fife.ui.rsyntaxtextarea.SyntaxConstants
import org.fife.ui.rsyntaxtextarea.Theme
import org.fife.ui.rsyntaxtextarea.folding.FoldManager
import org.fife.ui.rtextarea.RTextScrollPane
import org.fife.ui.rtextarea.SearchContext
import org.fife.ui.rtextarea.SearchEngine

import javax.swing.*
import javax.swing.text.BadLocationException
import java.awt.*
import java.awt.datatransfer.StringSelection
import java.awt.event.KeyEvent
Expand Down Expand Up @@ -98,6 +100,63 @@ class TextPage extends JPanel implements ContentCopyable, ContentSelectable, Lin

String getSyntaxStyle() { SyntaxConstants.SYNTAX_STYLE_NONE }

/**
* @see org.fife.ui.rsyntaxtextarea.RSyntaxUtilities#selectAndPossiblyCenter
* Force center and do not select
*/
void setCaretPositionAndCenter(DocumentRange range) {
int start = range.startOffset
int end = range.endOffset
boolean foldsExpanded = false
FoldManager fm = textArea.foldManager

if (fm.isCodeFoldingSupportedAndEnabled()) {
foldsExpanded = fm.ensureOffsetNotInClosedFold(start)
foldsExpanded |= fm.ensureOffsetNotInClosedFold(end)
}

if (foldsExpanded == false) {
try {
Rectangle r = textArea.modelToView(start)

if (r) { // Visible
if (end != start) {
r = r.union(textArea.modelToView(end))
}

Rectangle visible = textArea.visibleRect

visible.@x = r.@x - (visible.@width - r.@width) / 2 as int
visible.@y = r.@y - (visible.@height - r.@height) / 2 as int

Rectangle bounds = textArea.bounds
Insets i = textArea.insets
bounds.@x = i.left
bounds.@y = i.top
bounds.@width -= i.left + i.right
bounds.@height -= i.top + i.bottom

if (visible.@x < bounds.@x) {
visible.@x = bounds.@x
}
if (visible.@x + visible.@width > bounds.@x + bounds.@width) {
visible.@x = bounds.@x + bounds.@width - visible.@width
}
if (visible.@y < bounds.@y) {
visible.@y = bounds.@y
}
if (visible.@y + visible.@height > bounds.@y + bounds.@height) {
visible.@y = bounds.@y + bounds.@height - visible.@height
}

textArea.scrollRectToVisible(visible)
textArea.caretPosition = start
}
} catch (BadLocationException ignore) {
}
}
}

// --- ContentCopyable --- //
void copy() {
if (textArea.selectionStart == textArea.selectionEnd) {
Expand Down

0 comments on commit 9fd7c2b

Please sign in to comment.