Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue 6601: Add right click menu for main table header #8762

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6b91159
Update issue templates
pratt33k Mar 30, 2022
cef3afa
Merge branch 'JabRef:main' into main
TranLinhDang Mar 30, 2022
ce929a3
Merge branch 'JabRef:main' into main
TranLinhDang Apr 5, 2022
2644a9b
Add MainTableHeaderRightClickMenu class
TranLinhDang Apr 13, 2022
923ad41
Merge branch 'JabRef:main' into fix-for-issue-6601
TranLinhDang Apr 13, 2022
953304e
Add MainTableHeaderRightClickMenu.show() to MainTable class
TranLinhDang Apr 15, 2022
be4cc17
rearrange import classes
TranLinhDang May 7, 2022
22b30e9
Create getList() in TableTab and new MainTableHeaderRightClickMenu
TranLinhDang May 7, 2022
9e4dd73
Merge branch 'JabRef:main' into contextmenu
pratt33k May 7, 2022
4f39dfa
Merge pull request #3 from pratt33k/contextmenu
pratt33k May 7, 2022
331778c
Deleted unneccessary files
pratt33k May 7, 2022
3c13860
Fix for checkstyle errors & Disabled optimize imports on the fly
pratt33k May 7, 2022
e72683c
Added function that will remove the column in the mainTable when we c…
May 14, 2022
1bb667b
Fixed checkstyle errors
pratt33k May 16, 2022
6ad1276
Fixed checkstyle errors & optimized deletion of column
pratt33k May 16, 2022
0569388
Merge pull request #4 from pratt33k/fix-for-issue-6601-test
pratt33k May 16, 2022
2463878
Uncommented clear() to avoid having duplicate columns
pratt33k May 17, 2022
18c6761
Add link to the preferences entry table in context menu.
TranLinhDang May 17, 2022
bcdf26c
Merge branch 'fix-for-issue-6601-test' of https://github.com/pratt33k…
TranLinhDang May 17, 2022
a02f962
Created a new function to create any type of column
pratt33k May 18, 2022
fc3be4a
Merge branch 'fix-for-issue-6601-test' of https://github.com/pratt33k…
pratt33k May 18, 2022
6163d4c
Merge pull request #5 from pratt33k/fix-for-issue-6601-test
pratt33k May 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public MainTable(MainTableDataModel model,

this.getSortOrder().clear();

new MainTableHeaderRightClickMenu().show(this, libraryTab, dialogService);

/* KEEP for debugging purposes
for (var colModel : mainTablePreferences.getColumnPreferences().getColumnSortOrder()) {
for (var col : this.getColumns()) {
Expand All @@ -149,10 +151,10 @@ public MainTable(MainTableDataModel model,

mainTablePreferences.getColumnPreferences().getColumnSortOrder().forEach(columnModel ->
this.getColumns().stream()
.map(column -> (MainTableColumn<?>) column)
.filter(column -> column.getModel().equals(columnModel))
.findFirst()
.ifPresent(column -> this.getSortOrder().add(column)));
.map(column -> (MainTableColumn<?>) column)
.filter(column -> column.getModel().equals(columnModel))
.findFirst()
.ifPresent(column -> this.getSortOrder().add(column)));
Comment on lines -152 to +157
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo all your whitespace changes and use our checkstyle config for your jabref workspace.


if (mainTablePreferences.getResizeColumnsToFit()) {
this.setColumnResizePolicy(new SmartConstrainedResizePolicy());
Expand Down Expand Up @@ -203,16 +205,16 @@ private void jumpToSearchKey(TableColumn<BibEntryTableViewModel, ?> sortedColumn
lastKeyPressTime = System.currentTimeMillis();

this.getItems().stream()
.filter(item -> Optional.ofNullable(sortedColumn.getCellObservableValue(item).getValue())
.map(Object::toString)
.orElse("")
.toLowerCase()
.startsWith(columnSearchTerm))
.findFirst()
.ifPresent(item -> {
this.scrollTo(item);
this.clearAndSelect(item.getEntry());
});
.filter(item -> Optional.ofNullable(sortedColumn.getCellObservableValue(item).getValue())
.map(Object::toString)
.orElse("")
.toLowerCase()
.startsWith(columnSearchTerm))
.findFirst()
.ifPresent(item -> {
this.scrollTo(item);
this.clearAndSelect(item.getEntry());
});
}

@Subscribe
Expand Down Expand Up @@ -250,8 +252,8 @@ private void setupKeyBindings(KeyBindingRepository keyBindings) {
this.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (event.getCode() == KeyCode.ENTER) {
getSelectedEntries().stream()
.findFirst()
.ifPresent(libraryTab::showAndEdit);
.findFirst()
.ifPresent(libraryTab::showAndEdit);
event.consume();
return;
}
Expand Down Expand Up @@ -436,8 +438,8 @@ public List<BibEntry> getSelectedEntries() {

private Optional<BibEntryTableViewModel> findEntry(BibEntry entry) {
return model.getEntriesFilteredAndSorted()
.stream()
.filter(viewModel -> viewModel.getEntry().equals(entry))
.findFirst();
.stream()
.filter(viewModel -> viewModel.getEntry().equals(entry))
.findFirst();
}
}
41 changes: 41 additions & 0 deletions src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,45 @@ private TableColumn<BibEntryTableViewModel, List<LinkedFile>> createExtraFileCol
private TableColumn<BibEntryTableViewModel, String> createLibraryColumn(MainTableColumnModel columnModel) {
return new LibraryColumn(columnModel);
}

/**
* Create any single column
*/
public TableColumn<BibEntryTableViewModel, ?> createColumn(MainTableColumnModel columnModel) {
switch (columnModel.getType()) {
case INDEX:
return createIndexColumn(columnModel);
case GROUPS:
return createGroupColumn(columnModel);
case FILES:
return createFilesColumn(columnModel);
case LINKED_IDENTIFIER:
return createIdentifierColumn(columnModel);
case LIBRARY_NAME:
return createLibraryColumn(columnModel);
case EXTRAFILE:
if (columnModel.getQualifier().isBlank()) {
return createExtraFileColumn(columnModel);
}
break;
case SPECIALFIELD:
if (!columnModel.getQualifier().isBlank()) {
Field field = FieldFactory.parseField(columnModel.getQualifier());
if (field instanceof SpecialField) {
return createSpecialFieldColumn(columnModel);
} else {
LOGGER.warn("Special field type '{}' is unknown. Using normal column type.", columnModel.getQualifier());
return createFieldColumn(columnModel);
}
}
break;
default:
case NORMALFIELD:
if (!columnModel.getQualifier().isBlank()) {
return createFieldColumn(columnModel);
}
break;
}
return null;
}
Comment on lines +260 to +299
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you introduce this method, you need to adjust the constructor of the MainTableColumnFactory to reduce code duplication

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.jabref.gui.maintable;

import java.util.ArrayList;
import java.util.List;

import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.layout.StackPane;

import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.maintable.columns.MainTableColumn;
import org.jabref.gui.preferences.PreferencesDialogView;
import org.jabref.logic.l10n.Localization;

public class MainTableHeaderRightClickMenu extends ContextMenu {
static MainTable mT = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this static? You could simply declare it as normal member variable.
I would pass the MainTable argument in the constructor and assign it there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not abbreviate variable names.


public void show(MainTable mainTable, LibraryTab libraryTab, DialogService dialogService) {
System.out.println("show()");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove these System.out statements...

mT = mainTable;
mainTable.setOnContextMenuRequested(clickEvent -> {

// Click on the tableColumns
if (!(clickEvent.getTarget() instanceof StackPane)) {
System.out.println("Click on the tableColumns");
// Create radioMenuItemList from tableColumnList
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid code-describing comments, instead try to write self-explaining code and put your ratilonale into a comment.

List<RadioMenuItem> radioMenuItems = new ArrayList<>();
mainTable.getColumns().forEach(tableColumn -> radioMenuItems.add(createRadioMenuItem(tableColumn)));

SeparatorMenuItem line = new SeparatorMenuItem();

// Show preferences Button
MenuItem columnsPreferences = new MenuItem(Localization.lang("Show preferences"));
columnsPreferences.setOnAction(event -> {

// Show Entry table
PreferencesDialogView preferencesDialogView = new PreferencesDialogView(libraryTab.frame());
preferencesDialogView.getPreferenceTabList().getSelectionModel().select(3);
dialogService.showCustomDialog(preferencesDialogView);
});

// Clean items and add newItems
this.getItems().clear();

this.getItems().addAll(radioMenuItems);
this.getItems().addAll(line, columnsPreferences);

// Show ContextMenu
this.show(mainTable, clickEvent.getScreenX(), clickEvent.getScreenY());
}

clickEvent.consume();
});
}

private RadioMenuItem createRadioMenuItem(TableColumn<BibEntryTableViewModel, ?> tableColumn) {
RadioMenuItem radioMenuItem = new RadioMenuItem(((MainTableColumn<?>) tableColumn).getDisplayName());
// System.out.println(((MainTableColumn<?>) tableColumn).getDisplayName());
radioMenuItem.setOnAction(event -> {
// Return the column name when we click an item from context menu
// System.out.println(((MainTableColumn) tableColumn).getModel().getName());
removeColumn(tableColumn);
});

return radioMenuItem;
}

public void addColumn(MainTableColumn column, MainTableColumnFactory creator) {
MainTableColumnModel columnModel = column.getModel();
creator.createColumn(columnModel);
}

public void removeColumn(TableColumn<BibEntryTableViewModel, ?> tableColumn) {
mT.getColumns().removeIf(tableCol -> tableCol == tableColumn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public PreferencesDialogViewModel getViewModel() {
return viewModel;
}

public ListView<PreferencesTab> getPreferenceTabList() {
return preferenceTabList;
}
Comment on lines +62 to +64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not touch the PreferencesDialog. Changes must happen in the Preferences, not in the Dialog

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or perhaps make the method more generic, so a new method could open any specific preferences tab.


@FXML
private void initialize() {
viewModel = new PreferencesDialogViewModel(dialogService, preferencesService, frame);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/jabref/gui/preferences/table/TableTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ private void setupTable() {
event.consume();
}
});

columnsList.itemsProperty().bind(viewModel.columnsListProperty());

new ViewModelListCellFactory<MainTableColumnModel>()
Expand Down Expand Up @@ -147,4 +146,8 @@ public void sortColumnDown() {
public void addColumn() {
viewModel.insertColumnInList();
}

public TableView<MainTableColumnModel> getList() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would give this a meaningful name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use a getter for the TableView, instead inject the columnslist of the table into your new class RightClickMenu and work on that. We need to untangle all the callbacks, otherwise the classes are untestable.

return columnsList;
}
}