Skip to content

Commit

Permalink
Implement better scaling of main table showing entries
Browse files Browse the repository at this point in the history
Co-authored-by: Dominik Voigt <dominik.ingo.voigt@gmail.com>
  • Loading branch information
koppor and DominikVoigt committed Dec 12, 2020
1 parent d1fb9e2 commit 2fdb153
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 72 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We changed connect timeouts for server requests to 30 seconds in general and 5 seconds for GROBID server (special) and improved user notifications on connection issues. [7026](https://github.com/JabRef/jabref/pull/7026)
- We changed the order of the library tab context menu items. [#7171](https://github.com/JabRef/jabref/issues/7171)
- We changed the way linked files are opened on Linux to use the native openFile method, compatible with confined packages. [7037](https://github.com/JabRef/jabref/pull/7037)
- We refined the entry preview to show the full names of authors and editors, to list the editor only if no author is present, have the year ealier. [#7083](https://github.com/JabRef/jabref/issues/7083)
- We refined the entry preview to show the full names of authors and editors, to list the editor only if no author is present, have the year earlier. [#7083](https://github.com/JabRef/jabref/issues/7083)
- We changed the resize behavior of table columns to change the width of the current column only. [#967](https://github.com/JabRef/jabref/issues/967)

### Fixed

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,14 @@ public MainTable(MainTableDataModel model,
}
}
*/
mainTablePreferences.getColumnPreferences().getColumnSortOrder().forEach(columnModel ->
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)));

if (mainTablePreferences.getResizeColumnsToFit()) {
this.setColumnResizePolicy(new SmartConstrainedResizePolicy());
}
this.setColumnResizePolicy(new SmartConstrainedResizePolicy());

this.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.field.SpecialField;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.groups.AbstractGroup;
import org.jabref.model.util.OptionalUtil;
import org.jabref.preferences.PreferencesService;
Expand Down Expand Up @@ -109,7 +110,16 @@ public MainTableColumnFactory(BibDatabaseContext database,
default:
case NORMALFIELD:
if (!column.getQualifier().isBlank()) {
columns.add(createFieldColumn(column));
TableColumn<BibEntryTableViewModel, ?> fieldColumn = createFieldColumn(column);
columns.add(fieldColumn);
if (column.getQualifier().equalsIgnoreCase(StandardField.YEAR.getName())) {
// We adjust the min width and the current width, but not the max width to allow the user to enlarge this field
// 75 is chosen, because of the optimal width of a four digit field
fieldColumn.setMinWidth(60);
fieldColumn.setPrefWidth(60);
} else {
fieldColumn.setMinWidth(ColumnPreferences.DEFAULT_COLUMN_WIDTH);
}
}
break;
}
Expand All @@ -125,7 +135,7 @@ public static void setExactWidth(TableColumn<?, ?> column, double width) {
}

/**
* Creates a column with a continous number
* Creates a column with a continuous number
*/
private TableColumn<BibEntryTableViewModel, String> createIndexColumn(MainTableColumnModel columnModel) {
TableColumn<BibEntryTableViewModel, String> column = new MainTableColumn<>(columnModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ public Type getType() {
return typeProperty.getValue();
}

/**
* Returns the field name of the column (e.g., year)
*/
public String getQualifier() {
return qualifierProperty.getValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;

import javafx.scene.control.ResizeFeaturesBase;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumnBase;
import javafx.scene.control.TableView;
import javafx.util.Callback;
Expand All @@ -25,80 +26,37 @@ public class SmartConstrainedResizePolicy implements Callback<TableView.ResizeFe
@Override
public Boolean call(TableView.ResizeFeatures prop) {
if (prop.getColumn() == null) {
return initColumnSize(prop.getTable());
// table is initialized
// no need to adjust
return false;
} else {
return constrainedResize(prop);
}
}

private Boolean initColumnSize(TableView<?> table) {
double tableWidth = getContentWidth(table);
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;

// Just to make sure that we are staying under the total table width (due to rounding errors)
newSize -= 2;

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 {
// TODO: reflective access, should be removed
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) - 2,
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 {
// TODO: reflective access, should be removed
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;
Double delta = prop.getDelta();
TableColumn<?, ?> userChosenColumnToResize = prop.getColumn();

double oldWidth = userChosenColumnToResize.getWidth();
double newWidth = oldWidth + delta;
prop.getColumn().getCo
if (delta < 0) {
double minWidth = userChosenColumnToResize.getMinWidth();
LOGGER.trace("MinWidth {}", minWidth);
newWidth = Math.max(minWidth, oldWidth + delta);
} else {
double maxWidth = userChosenColumnToResize.getMaxWidth();
LOGGER.trace("MaxWidth {}", maxWidth);
newWidth = Math.min(maxWidth, oldWidth + delta);
}
}

private Double getContentWidth(TableView<?> table) {
try {
// TODO: reflective access, should be removed
Field privateStringField = TableView.class.getDeclaredField("contentWidth");
privateStringField.setAccessible(true);
return (Double) privateStringField.get(table);
} catch (IllegalAccessException | NoSuchFieldException e) {
return 0d;
LOGGER.trace("Size: {} -> {}", oldWidth, newWidth);
if (oldWidth == newWidth) {
return false;
}
userChosenColumnToResize.setPrefWidth(newWidth);
return true;
}
}

0 comments on commit 2fdb153

Please sign in to comment.