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

Maintable leaves the Swing year and comes back in a new JavaFX dress #3591

Merged
merged 33 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2ebc439
Start converting
tobiasdiez Dec 28, 2017
497fcdf
Merge branch 'master' of https://github.com/JabRef/jabref into javafx…
tobiasdiez Dec 28, 2017
f70ea5d
Refactor icons (less awt)
tobiasdiez Dec 29, 2017
952c032
More icon related changes
tobiasdiez Dec 29, 2017
41eeb3c
First working prototype
tobiasdiez Dec 31, 2017
b233517
Add new classes
tobiasdiez Dec 31, 2017
c2b70c3
Hold reference of divider pane position binding
tobiasdiez Jan 1, 2018
3b07e3c
Merge remote-tracking branch 'refs/remotes/JabRef/master' into javafx…
tobiasdiez Jan 1, 2018
452147d
Fix divider location storage
tobiasdiez Jan 1, 2018
09b000b
Flat design for tab header
tobiasdiez Jan 1, 2018
d7d00a8
Add dummy context menu to row
tobiasdiez Jan 2, 2018
501b6cc
Merge branch 'master' of https://github.com/JabRef/jabref into javafx…
tobiasdiez Jan 3, 2018
7f6c924
Improve hover and selected row style
tobiasdiez Jan 3, 2018
dc41f29
Further improve hover and selected row style
tobiasdiez Jan 3, 2018
38f0d15
Restore column width (except when "fit to width" option is enabled)
tobiasdiez Jan 4, 2018
7e21bc4
Update entry editor and preview
tobiasdiez Jan 5, 2018
667f1c4
Filter by groups and search
tobiasdiez Jan 5, 2018
d6fe1f1
Fix a few smaller bugs
tobiasdiez Jan 8, 2018
530530a
Fix keyboard shortcuts
tobiasdiez Jan 8, 2018
11d4d59
Fix cleanup action
tobiasdiez Jan 8, 2018
3a69091
Changes to entry triggers change of list of entries
tobiasdiez Jan 8, 2018
6364d1a
Merge remote-tracking branch 'upstream/master' into javafxTable
Siedlerchr Jan 9, 2018
2c7b3a7
Remove clear Search keybinding as this overlaps with Close entry edit…
Siedlerchr Jan 9, 2018
01f0af2
Merge branch 'maintable-beta' into javafxTable
Siedlerchr Jan 9, 2018
35d32c5
wrap showandEdit in javafx thread to make new entry actions work
Siedlerchr Jan 9, 2018
1c41425
Merge branch 'javafxTable' of https://github.com/JabRef/jabref into j…
Siedlerchr Jan 9, 2018
e3515e9
Improve automatic sizing of columns
tobiasdiez Jan 9, 2018
bd2c1e3
Implement feedback
tobiasdiez Jan 9, 2018
b0af742
Merge branch 'master' of https://github.com/JabRef/jabref into javafx…
tobiasdiez Jan 9, 2018
8c68b86
Merge branch 'javafxTable' of https://github.com/JabRef/jabref into j…
tobiasdiez Jan 9, 2018
8980713
Bring back the context menu (#3666)
tobiasdiez Jan 29, 2018
fbc5707
Reimplement special field and file columns (#3647)
tobiasdiez Jan 29, 2018
3a2ee4e
Merge branch 'maintable-beta' into javafxTable
tobiasdiez Jan 29, 2018
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
Prev Previous commit
Next Next commit
Improve automatic sizing of columns
  • Loading branch information
tobiasdiez committed Jan 9, 2018
commit e3515e9979bc8753abbfdb294c8d312986904d24
24 changes: 12 additions & 12 deletions src/main/java/org/jabref/gui/maintable/ColumnFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,9 @@ private TableColumn<BibEntryTableViewModel, Optional<SpecialFieldValueViewModel>
column.setGraphic(new SpecialFieldViewModel(specialField).getIcon().getGraphicNode());
column.getStyleClass().add(STYLE_ICON);
if (specialField == SpecialField.RANKING) {
column.setMinWidth(GUIGlobals.WIDTH_ICON_COL_RANKING);
column.setMaxWidth(GUIGlobals.WIDTH_ICON_COL_RANKING);
setExactWidth(column, GUIGlobals.WIDTH_ICON_COL_RANKING);
} else {
column.setMinWidth(GUIGlobals.WIDTH_ICON_COL);
column.setMaxWidth(GUIGlobals.WIDTH_ICON_COL);
setExactWidth(column, GUIGlobals.WIDTH_ICON_COL);
}
column.setCellValueFactory(cellData -> cellData.getValue().getSpecialField(specialField));
column.setCellFactory(
Expand All @@ -114,12 +112,17 @@ private TableColumn<BibEntryTableViewModel, Optional<SpecialFieldValueViewModel>
return column;
}

private void setExactWidth(TableColumn<?, ?> column, int widthIconCol) {
column.setMinWidth(widthIconCol);
column.setPrefWidth(widthIconCol);
column.setMaxWidth(widthIconCol);
}

private TableColumn<BibEntryTableViewModel, List<LinkedFile>> createFileColumn() {
TableColumn<BibEntryTableViewModel, List<LinkedFile>> column = new TableColumn<>();
column.setGraphic(IconTheme.JabRefIcons.FILE.getGraphicNode());
column.getStyleClass().add(STYLE_ICON);
column.setMinWidth(GUIGlobals.WIDTH_ICON_COL);
column.setMaxWidth(GUIGlobals.WIDTH_ICON_COL);
setExactWidth(column, GUIGlobals.WIDTH_ICON_COL);
column.setCellValueFactory(cellData -> cellData.getValue().getLinkedFiles());
column.setCellFactory(
new ValueTableCellFactory<BibEntryTableViewModel, List<LinkedFile>>()
Expand All @@ -134,8 +137,7 @@ private TableColumn<BibEntryTableViewModel, String> createIconColumn(JabRefIcon
TableColumn<BibEntryTableViewModel, String> column = new TableColumn<>();
column.setGraphic(icon.getGraphicNode());
column.getStyleClass().add(STYLE_ICON);
column.setMinWidth(GUIGlobals.WIDTH_ICON_COL);
column.setMaxWidth(GUIGlobals.WIDTH_ICON_COL);
setExactWidth(column, GUIGlobals.WIDTH_ICON_COL);
column.setCellValueFactory(cellData -> EasyBind.monadic(cellData.getValue().getField(firstField)).orElse(cellData.getValue().getField(secondField)));
column.setCellFactory(
new ValueTableCellFactory<BibEntryTableViewModel, String>()
Expand All @@ -147,8 +149,7 @@ private TableColumn<BibEntryTableViewModel, String> createIconColumn(JabRefIcon
TableColumn<BibEntryTableViewModel, String> column = new TableColumn<>();
column.setGraphic(icon.getGraphicNode());
column.getStyleClass().add(STYLE_ICON);
column.setMinWidth(GUIGlobals.WIDTH_ICON_COL);
column.setMaxWidth(GUIGlobals.WIDTH_ICON_COL);
setExactWidth(column, GUIGlobals.WIDTH_ICON_COL);
column.setCellValueFactory(cellData -> cellData.getValue().getField(field));
column.setCellFactory(
new ValueTableCellFactory<BibEntryTableViewModel, String>()
Expand All @@ -168,8 +169,7 @@ private TableColumn<BibEntryTableViewModel, List<LinkedFile>> createExtraFileCol
.map(ExternalFileType::getIcon).orElse(IconTheme.JabRefIcons.FILE)
.getGraphicNode());
column.getStyleClass().add(STYLE_ICON);
column.setMinWidth(GUIGlobals.WIDTH_ICON_COL);
column.setMaxWidth(GUIGlobals.WIDTH_ICON_COL);
setExactWidth(column, GUIGlobals.WIDTH_ICON_COL);
column.setCellValueFactory(cellData -> cellData.getValue().getLinkedFiles());
column.setCellFactory(
new ValueTableCellFactory<BibEntryTableViewModel, List<LinkedFile>>()
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public MainTable(MainTableDataModel model, JabRefFrame frame,
})
.withContextMenu(RightClickMenu::create));
if (preferences.resizeColumnsToFit()) {
this.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
this.setColumnResizePolicy(new SmartConstrainedResizePolicy());
}
this.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
this.setItems(model.getEntriesFiltered());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.jabref.gui.maintable;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import javafx.scene.control.ResizeFeaturesBase;
import javafx.scene.control.TableColumnBase;
import javafx.scene.control.TableView;
import javafx.util.Callback;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* This resize policy is almost the same as {@link TableView#CONSTRAINED_RESIZE_POLICY}
* We make sure that the width of all columns sums up to the total width of the table.
* However, in contrast to {@link TableView#CONSTRAINED_RESIZE_POLICY} we size the columns initially by their preferred width.
*/
public class SmartConstrainedResizePolicy implements Callback<TableView.ResizeFeatures, Boolean> {

private static final Log LOGGER = LogFactory.getLog(SmartConstrainedResizePolicy.class);

@Override
public Boolean call(TableView.ResizeFeatures prop) {
if (prop.getColumn() == null) {
return initColumnSize(prop.getTable());
} else {
return constrainedResize(prop);
}
}

private Boolean initColumnSize(TableView<?> table) {
double tableWidth = table.getWidth();
List<? extends TableColumnBase<?, ?>> visibleLeafColumns = table.getVisibleLeafColumns();
double totalWidth = visibleLeafColumns.stream().mapToDouble(TableColumnBase::getWidth).sum();

if (Math.abs(totalWidth - tableWidth) > 1) {
double totalPrefWidth = visibleLeafColumns.stream().mapToDouble(TableColumnBase::getPrefWidth).sum();
if (totalPrefWidth > 0) {
for (TableColumnBase col : visibleLeafColumns) {
double share = col.getPrefWidth() / totalPrefWidth;
double newSize = tableWidth * share;
resize(col, newSize - col.getWidth());
}
}
}

return false;
}

private void resize(TableColumnBase column, double delta) {
// We have to use reflection since TableUtil is not visible to us
try {
Class<?> clazz = Class.forName("javafx.scene.control.TableUtil");
Method constrainedResize = clazz.getDeclaredMethod("resize", TableColumnBase.class, double.class);
constrainedResize.setAccessible(true);
constrainedResize.invoke(null, column, delta);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
LOGGER.error("Could not invoke resize in TableUtil", e);
}
}

private Boolean constrainedResize(TableView.ResizeFeatures<?> prop) {
TableView<?> table = prop.getTable();
List<? extends TableColumnBase<?, ?>> visibleLeafColumns = table.getVisibleLeafColumns();
return constrainedResize(prop,
false,
getContentWidth(table),
visibleLeafColumns);
}

private Boolean constrainedResize(TableView.ResizeFeatures prop, Boolean isFirstRun, Double contentWidth, List<? extends TableColumnBase<?, ?>> visibleLeafColumns) {
// We have to use reflection since TableUtil is not visible to us
try {
Class<?> clazz = Class.forName("javafx.scene.control.TableUtil");
Method constrainedResize = clazz.getDeclaredMethod("constrainedResize", ResizeFeaturesBase.class, Boolean.TYPE, Double.TYPE, List.class);
constrainedResize.setAccessible(true);
Object returnValue = constrainedResize.invoke(null, prop, isFirstRun, contentWidth, visibleLeafColumns);
return (Boolean) returnValue;
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
LOGGER.error("Could not invoke constrainedResize in TableUtil", e);
return false;
}
}

private Double getContentWidth(TableView<?> table) {
try {
Field privateStringField = TableView.class.getDeclaredField("contentWidth");
privateStringField.setAccessible(true);
return (Double) privateStringField.get(table);
} catch (IllegalAccessException | NoSuchFieldException e) {
return 0d;
}
}
}

This file was deleted.