From c932e5675c748576a9041e32299926ee0a742634 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 2 Nov 2020 19:32:50 +0000 Subject: [PATCH] repurpose collection tool to show hits --- .../gui/CollectionsToolController.groovy | 62 +---- .../com/muwire/gui/MainFrameController.groovy | 12 + gui/griffon-app/i18n/messages.properties | 3 +- .../muwire/gui/CollectionsToolModel.groovy | 18 +- .../com/muwire/gui/CollectionsToolView.groovy | 221 +----------------- .../views/com/muwire/gui/MainFrameView.groovy | 5 + 6 files changed, 37 insertions(+), 284 deletions(-) diff --git a/gui/griffon-app/controllers/com/muwire/gui/CollectionsToolController.groovy b/gui/griffon-app/controllers/com/muwire/gui/CollectionsToolController.groovy index 3b416f34..adadd175 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/CollectionsToolController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/CollectionsToolController.groovy @@ -18,70 +18,14 @@ import com.muwire.core.util.DataUtil @ArtifactProviderFor(GriffonController) class CollectionsToolController { - @MVCMember @Nonnull - CollectionsToolModel model @MVCMember @Nonnull CollectionsToolView view - - @ControllerAction - void delete() { - int row = view.selectedCollectionRow() - if (row < 0) - return - FileCollection collection = model.collections.get(row) - UICollectionDeletedEvent e = new UICollectionDeletedEvent(collection : collection) - model.eventBus.publish(e) - model.collections.remove(row) - view.collectionsTable.model.fireTableDataChanged() - view.clearFilesTable() - } - - @ControllerAction - void close() { - view.dialog.setVisible(false) - mvcGroup.destroy() - } - - @ControllerAction - void viewComment() { - int row = view.selectedCollectionRow() - if (row < 0) - return - FileCollection collection = model.collections.get(row) - - def params = [:] - params['text'] = collection.comment - mvcGroup.createMVCGroup("show-comment", params) - } - - @ControllerAction - void viewFileComment() { - int row = view.selectedFileRow() - if (row < 0) - return - SharedFile sf = model.files.get(row) - - def params = [:] - params['text'] = DataUtil.readi18nString(Base64.decode(sf.getComment())) - mvcGroup.createMVCGroup("show-comment", params) - } - - @ControllerAction - void copyHash() { - int row = view.selectedCollectionRow() - if (row < 0) - return - FileCollection collection = model.collections.get(row) - - String b64 = Base64.encode(collection.getInfoHash().getRoot()) - StringSelection selection = new StringSelection(b64) - def clipboard = Toolkit.getDefaultToolkit().getSystemClipboard() - clipboard.setContents(selection, null) - } + @MVCMember @Nonnull + CollectionsToolModel model @ControllerAction void clearHits() { - model.selectedCollection.getHits().clear() + model.collection.hits.clear() model.hits.clear() view.hitsTable.model.fireTableDataChanged() } diff --git a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy index 83a2174c..858cf5b1 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/MainFrameController.groovy @@ -728,6 +728,18 @@ class MainFrameController { mvcGroup.createMVCGroup("show-comment", params) } + @ControllerAction + void showCollectionTool() { + int row = view.selectedCollectionRow() + if (row < 0) + return + FileCollection collection = model.localCollections.get(row) + + def params = [:] + params['collection'] = collection + mvcGroup.createMVCGroup("collections-tool", params) + } + void startChat(Persona p) { if (!mvcGroup.getChildrenGroups().containsKey(p.getHumanReadableName())) { def params = [:] diff --git a/gui/griffon-app/i18n/messages.properties b/gui/griffon-app/i18n/messages.properties index c6cae0f1..644b3e5e 100644 --- a/gui/griffon-app/i18n/messages.properties +++ b/gui/griffon-app/i18n/messages.properties @@ -543,7 +543,8 @@ COLLECTION_NAME_WARNING=The collection name cannot be blank. Please select a na COLLECTION_NAME_WARNING_TITLE=Missing collection name ## Collections tool -COLLECTION_TOOL_TITLE=Collections in your repository +COLLECTION_TOOL_TITLE=Search hits for {0} +COLLECTION_SHOW_HITS=Show Hits CREATED=Created AUTHOR=Author COLLECTION_TOOL_HEADER=These are the collections in your repository. Deleting a collection will not affect the files included in it. diff --git a/gui/griffon-app/models/com/muwire/gui/CollectionsToolModel.groovy b/gui/griffon-app/models/com/muwire/gui/CollectionsToolModel.groovy index ecfcd879..57a76ae1 100644 --- a/gui/griffon-app/models/com/muwire/gui/CollectionsToolModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/CollectionsToolModel.groovy @@ -12,22 +12,10 @@ import griffon.metadata.ArtifactProviderFor @ArtifactProviderFor(GriffonModel) class CollectionsToolModel { - - FileManager fileManager - CollectionManager collectionManager - EventBus eventBus - - @Observable boolean viewCommentButtonEnabled - @Observable boolean viewFileCommentButtonEnabled - @Observable boolean deleteButtonEnabled - @Observable boolean clearHitsButtonEnabled - - List collections = new ArrayList<>() - List files = new ArrayList<>() - FileCollection selectedCollection + FileCollection collection List hits = new ArrayList<>() void mvcGroupInit(Map args) { - collections.addAll(collectionManager.getCollections()) - } + hits.addAll(collection.hits) + } } \ No newline at end of file diff --git a/gui/griffon-app/views/com/muwire/gui/CollectionsToolView.groovy b/gui/griffon-app/views/com/muwire/gui/CollectionsToolView.groovy index 98bf0436..d4d2ec13 100644 --- a/gui/griffon-app/views/com/muwire/gui/CollectionsToolView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/CollectionsToolView.groovy @@ -40,189 +40,34 @@ class CollectionsToolView { def dialog def mainPanel - JTable collectionsTable - JTable filesTable JTable hitsTable - def lastCollectionSortEvent - def lastFilesSortEvent - void initUI() { mainFrame = application.windowManager.findWindow("main-frame") int rowHeight = application.context.get("row-height") - dialog = new JDialog(mainFrame,trans("COLLECTION_TOOL_TITLE"),true) + dialog = new JDialog(mainFrame,trans("COLLECTION_TOOL_TITLE", model.collection.name),true) dialog.setResizable(true) mainPanel = builder.panel { - gridLayout(rows : 1, cols : 2) - panel { - gridLayout(rows : 2, cols : 1) - panel { - borderLayout() - panel (constraints : BorderLayout.NORTH) { - label(text : trans("COLLECTION_TOOL_HEADER")) - } - scrollPane(constraints : BorderLayout.CENTER) { - collectionsTable = table(autoCreateRowSorter : true, rowHeight : rowHeight) { - tableModel(list : model.collections) { - closureColumn(header : trans("NAME"), preferredWidth : 100, type : String, read : {it.name}) - closureColumn(header : trans("AUTHOR"), preferredWidth : 100, type : String, read : {it.author.getHumanReadableName()}) - closureColumn(header : trans("FILES"), preferredWidth: 10, type : Integer, read : {it.numFiles()}) - closureColumn(header : trans("SIZE"), preferredWidth : 10, type : Long, read : {it.totalSize()}) - closureColumn(header : trans("COMMENT"), preferredWidth : 10, type : Boolean, read : {it.comment != ""}) - closureColumn(header : trans("SEARCH_HITS"), preferredWidth : 10, type : Integer, read : {it.hits.size()}) - closureColumn(header : trans("CREATED"), preferredWidth : 30, type : Long, read : {it.timestamp}) - } - } - } - panel(constraints : BorderLayout.SOUTH) { - button(text : trans("VIEW_COMMENT"), enabled : bind {model.viewCommentButtonEnabled}, viewCommentAction) - button(text : trans("COPY_HASH_TO_CLIPBOARD"), enabled : bind {model.deleteButtonEnabled}, copyHashAction) - button(text : trans("DELETE"), enabled : bind {model.deleteButtonEnabled}, deleteAction) - } - } - panel { - borderLayout() - panel(constraints : BorderLayout.NORTH) { - label(text : trans("COLLECTION_VIEWS")) - } - scrollPane(constraints : BorderLayout.CENTER) { - hitsTable = table(autoCreateRowSorter : true, rowHeight : rowHeight) { - tableModel(list : model.hits) { - closureColumn(header : trans("SEARCHER"), preferredWidth : 100, type : String, read : {it.searcher.getHumanReadableName()}) - closureColumn(header : trans("TIMESTAMP"), preferredWidth : 100, type : Long, read : {it.timestamp}) - } - } - } - panel(constraints : BorderLayout.SOUTH) { - button(text : trans("CLEAR_HITS"), enabled : bind {model.clearHitsButtonEnabled}, clearHitsAction) + borderLayout() + panel(constraints : BorderLayout.NORTH) { + label(text : trans("COLLECTION_VIEWS")) + } + scrollPane(constraints : BorderLayout.CENTER) { + hitsTable = table(autoCreateRowSorter : true, rowHeight : rowHeight) { + tableModel(list : model.hits) { + closureColumn(header : trans("SEARCHER"), preferredWidth : 100, type : String, read : {it.searcher.getHumanReadableName()}) + closureColumn(header : trans("TIMESTAMP"), preferredWidth : 100, type : Long, read : {it.timestamp}) } } } - panel { - borderLayout() - panel(constraints : BorderLayout.NORTH) { - label(text : trans("FILES")) - } - scrollPane(constraints : BorderLayout.CENTER) { - filesTable = table(autoCreateRowSorter : true, rowHeight : rowHeight) { - tableModel(list : model.files) { - closureColumn(header : trans("NAME"), preferredWidth : 200, type : String, read : {it.getCachedPath()}) - closureColumn(header : trans("SIZE"), preferredWidth : 10, type : Long, read : {it.getCachedLength()}) - closureColumn(header : trans("COMMENT"), preferredWidth : 10, type : Boolean, read : {it.getComment() != null}) - } - } - } - panel(constraints : BorderLayout.SOUTH) { - button(text : trans("VIEW_COMMENT"), enabled : bind{model.viewFileCommentButtonEnabled}, viewFileCommentAction) - button(text : trans("CLOSE"), closeAction) - } + panel(constraints : BorderLayout.SOUTH) { + button(text : trans("CLEAR_HITS"), clearHitsAction) } } } - int selectedCollectionRow() { - int selectedRow = collectionsTable.getSelectedRow() - if (selectedRow < 0) - return -1 - if (lastCollectionSortEvent != null) - selectedRow = collectionsTable.rowSorter.convertRowIndexToModel(selectedRow) - selectedRow - } - - int selectedFileRow() { - int selectedRow = filesTable.getSelectedRow() - if (selectedRow < 0) - return -1 - if (lastFilesSortEvent != null) - selectedRow = filesTable.rowSorter.convertRowIndexToModel(selectedRow) - selectedRow - } - - void clearFilesTable() { - model.files.clear() - filesTable.model.fireTableDataChanged() - } - void mvcGroupInit(Map args) { - def centerRenderer = new DefaultTableCellRenderer() - centerRenderer.setHorizontalAlignment(JLabel.CENTER) - collectionsTable.setDefaultRenderer(Integer.class,centerRenderer) - collectionsTable.columnModel.getColumn(3).setCellRenderer(new SizeRenderer()) - collectionsTable.columnModel.getColumn(6).setCellRenderer(new DateRenderer()) - - collectionsTable.rowSorter.addRowSorterListener({evt -> lastCollectionSortEvent = evt}) - - def selectionModel = collectionsTable.getSelectionModel() - selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION) - selectionModel.addListSelectionListener({ - int selectedRow = selectedCollectionRow() - if (selectedRow < 0) { - model.viewCommentButtonEnabled = false - model.deleteButtonEnabled = false - model.clearHitsButtonEnabled = false - return - } - - model.deleteButtonEnabled = true - FileCollection collection = model.collections.get(selectedRow) - model.selectedCollection = collection - model.viewCommentButtonEnabled = collection.getComment() != "" - - model.files.clear() - collection.files.each { - SharedFile sf = model.fileManager.getRootToFiles().get(it.infoHash).first() - model.files.add(sf) - } - filesTable.model.fireTableDataChanged() - - model.hits.clear() - model.hits.addAll(collection.getHits()) - if (!model.hits.isEmpty()) - model.clearHitsButtonEnabled = true - hitsTable.model.fireTableDataChanged() - }) - - collectionsTable.addMouseListener(new MouseAdapter() { - public void mouseReleased(MouseEvent e) { - if (e.isPopupTrigger()) - showCollectionTableMenu(e) - } - public void mousePressed(MouseEvent e) { - if (e.isPopupTrigger()) - showCollectionTableMenu(e) - } - }) - - - // files table - - filesTable.setDefaultRenderer(Long.class, new SizeRenderer()) - filesTable.rowSorter.addRowSorterListener({evt -> lastFilesSortEvent = evt}) - filesTable.rowSorter.setSortsOnUpdates(true) - selectionModel = filesTable.getSelectionModel() - selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION) - selectionModel.addListSelectionListener({ - int selectedRow = selectedFileRow() - if (selectedRow < 0) { - model.viewFileCommentButtonEnabled = false - return - } - SharedFile sf = model.files.get(selectedRow) - model.viewFileCommentButtonEnabled = sf.getComment() != null - }) - - filesTable.addMouseListener(new MouseAdapter() { - public void mouseReleased(MouseEvent e) { - if (e.isPopupTrigger()) - showItemsMenu(e) - } - public void mousePressed(MouseEvent e) { - if (e.isPopupTrigger()) - showItemsMenu(e) - } - }) - // hits table hitsTable.setDefaultRenderer(Long.class, new DateRenderer()) @@ -237,46 +82,4 @@ class CollectionsToolView { }) dialog.show() } - - private void showCollectionTableMenu(MouseEvent e) { - int row = selectedCollectionRow() - if (row < 0) - return - FileCollection collection = model.collections.get(row) - - JPopupMenu menu = new JPopupMenu() - JMenuItem copyHashToClipboard = new JMenuItem(trans("COPY_HASH_TO_CLIPBOARD")) - copyHashToClipboard.addActionListener({controller.copyHash()}) - menu.add(copyHashToClipboard) - - if (collection.comment != "") { - JMenuItem viewComment = new JMenuItem(trans("VIEW_COMMENT")) - viewComment.addActionListener({controller.viewComment()}) - menu.add(viewComment) - } - - JMenuItem delete = new JMenuItem(trans("DELETE")) - delete.addActionListener({controller.delete()}) - menu.add(delete) - - showPopupMenu(menu, e) - } - - private void showItemsMenu(MouseEvent e) { - int row = selectedFileRow() - if (row < 0) - return - SharedFile item = model.files.get(row) - if (item.getComment() == null || item.getComment() == "") - return - JPopupMenu menu = new JPopupMenu() - JMenuItem viewComment = new JMenuItem(trans("VIEW_COMMENT")) - viewComment.addActionListener({controller.viewFileComment()}) - menu.add(viewComment) - showPopupMenu(menu, e) - } - - private static void showPopupMenu(JPopupMenu menu, MouseEvent event) { - menu.show(event.getComponent(), event.getX(), event.getY()) - } } \ No newline at end of file diff --git a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy index 130a896b..d24d6d7b 100644 --- a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy @@ -444,6 +444,7 @@ class MainFrameView { } panel(constraints : BorderLayout.SOUTH) { button(text : trans("VIEW_COMMENT"), enabled : bind {model.viewCollectionCommentButtonEnabled}, viewCollectionCommentAction) + button(text : trans("COLLECTION_SHOW_HITS"), enabled : bind {model.deleteCollectionButtonEnabled}, showCollectionToolAction) button(text : trans("COPY_HASH_TO_CLIPBOARD"), enabled : bind {model.deleteCollectionButtonEnabled}, copyCollectionHashAction) button(text : trans("DELETE"), enabled : bind {model.deleteCollectionButtonEnabled}, deleteCollectionAction) } @@ -1715,6 +1716,10 @@ class MainFrameView { delete.addActionListener({controller.deleteCollection()}) menu.add(delete) + JMenuItem showHits = new JMenuItem(trans("COLLECTION_SHOW_HITS")) + showHits.addActionListener({controller.showCollectionTool()}) + menu.add(showHits) + showPopupMenu(menu, e) }