From d61e4245bdb5e2d4074be917f93e6b5b9a85a8bc Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Wed, 27 Oct 2021 04:09:50 +0100 Subject: [PATCH] build tabs dynamically based on presence of commments or local copies --- .../com/muwire/gui/ResultDetailsModel.groovy | 21 +++----- .../com/muwire/gui/ResultDetailsView.groovy | 49 ++++++++++++------- .../views/com/muwire/gui/SearchTabView.groovy | 1 + 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/gui/griffon-app/models/com/muwire/gui/ResultDetailsModel.groovy b/gui/griffon-app/models/com/muwire/gui/ResultDetailsModel.groovy index fc15fbf1..1c8648d5 100644 --- a/gui/griffon-app/models/com/muwire/gui/ResultDetailsModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/ResultDetailsModel.groovy @@ -39,7 +39,6 @@ class ResultDetailsModel { List results String key - List localFiles private final Set uniqueResults = new HashSet<>() List resultsWithComments = [] @@ -55,11 +54,6 @@ class ResultDetailsModel { void mvcGroupInit(Map args) { key = fileName + Base64.encode(infoHash.getRoot()) - SharedFile[] locals = core.fileManager.getSharedFiles(infoHash.getRoot()) - if (locals == null) - localFiles = Collections.emptyList() - else - localFiles = Arrays.asList(locals) certificates = new HashMap<>() collectionFetches = new HashMap<>() @@ -86,6 +80,13 @@ class ResultDetailsModel { core.eventBus.unregister(CollectionFetchStatusEvent.class, this) } } + + List getLocalCopies() { + SharedFile[] sharedFiles = core.fileManager.getSharedFiles(infoHash.getRoot()) + if (sharedFiles == null || sharedFiles.length == 0) + return null + sharedFiles.collect {it.getCachedPath()} + } void addResult(UIResultEvent event) { if (!uniqueResults.add(event)) @@ -197,12 +198,4 @@ class ResultDetailsModel { int count final List collections = new ArrayList<>() } - - boolean hasComments() { - for (UIResultEvent event : results) { - if (event.comment != null) - return true - } - false - } } diff --git a/gui/griffon-app/views/com/muwire/gui/ResultDetailsView.groovy b/gui/griffon-app/views/com/muwire/gui/ResultDetailsView.groovy index e709ccf4..29dd180d 100644 --- a/gui/griffon-app/views/com/muwire/gui/ResultDetailsView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/ResultDetailsView.groovy @@ -42,8 +42,10 @@ class ResultDetailsView { int rowHeight JTabbedPane tabs + + JPanel sendersPanel JTable sendersTable - + JPanel commentsPanel JList commentsList JTextArea commentTextArea @@ -59,7 +61,7 @@ class ResultDetailsView { tabs = tabbedPane(constraints: BorderLayout.CENTER) } - JPanel sendersPanel = builder.panel { + sendersPanel = builder.panel { borderLayout() panel(constraints: BorderLayout.NORTH) { label(text: trans("RESULTS_CAME_FROM")) @@ -83,20 +85,6 @@ class ResultDetailsView { button(text: trans("COPY_FULL_ID"), enabled: bind {model.copyIdActionEnabled}, copyIdAction) } } - tabs.addTab(trans("SENDERS"), sendersPanel) - - if (!model.localFiles.isEmpty()) { - JPanel localCopies = builder.panel { - borderLayout() - panel(constraints: BorderLayout.NORTH) { - label(text: trans("YOU_ALREADY_HAVE_FILE", model.localFiles.size())) - } - scrollPane(constraints: BorderLayout.CENTER) { - list(items : model.localFiles.collect {it.getCachedPath()}) - } - } - tabs.addTab(trans("LOCAL_COPIES"), localCopies) - } commentsPanel = builder.panel { cardLayout() @@ -115,7 +103,31 @@ class ResultDetailsView { } } } - tabs.addTab(trans("COMMENTS"), commentsPanel) + } + + private JPanel buildLocalCopies() { + List localCopies = model.getLocalCopies() + if (localCopies == null) + return null + builder.panel { + borderLayout() + panel(constraints: BorderLayout.NORTH) { + label(text: trans("YOU_ALREADY_HAVE_FILE", localCopies.size())) + } + scrollPane(constraints: BorderLayout.CENTER) { + list(items : localCopies) + } + } + } + + void buildTabs() { + tabs.removeAll() + tabs.addTab(trans("SENDERS"), sendersPanel) + JPanel localCopies = buildLocalCopies() + if (localCopies != null) + tabs.addTab(trans("LOCAL_COPIES"), localCopies) + if (!model.resultsWithComments.isEmpty()) + tabs.addTab(trans("COMMENTS"), commentsPanel) } void mvcGroupInit(Map args) { @@ -165,6 +177,9 @@ class ResultDetailsView { commentsPanel.getLayout().show(commentsPanel,"yes-comments") commentsList.setListData(model.resultsWithComments.toArray(new UIResultEvent[0])) } + + buildTabs() + p.updateUI() } void refreshCertificates() { diff --git a/gui/griffon-app/views/com/muwire/gui/SearchTabView.groovy b/gui/griffon-app/views/com/muwire/gui/SearchTabView.groovy index 70507040..657221a9 100644 --- a/gui/griffon-app/views/com/muwire/gui/SearchTabView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/SearchTabView.groovy @@ -457,6 +457,7 @@ class SearchTabView { resultDetails[infoHash] = group } + group.view.buildTabs() detailsPanelByFile.add(group.view.p, null) detailsPanelByFile.updateUI() }