Skip to content

Commit a1a5ccc

Browse files
kopporDominikVoigt
andcommitted
Implement better scaling of main table showing entries
Co-authored-by: Dominik Voigt <[email protected]>
1 parent d1fb9e2 commit a1a5ccc

File tree

5 files changed

+41
-72
lines changed

5 files changed

+41
-72
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
4848
- 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)
4949
- We changed the order of the library tab context menu items. [#7171](https://github.com/JabRef/jabref/issues/7171)
5050
- 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)
51-
- 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)
51+
- 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)
52+
- 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)
5253

5354
### Fixed
5455

src/main/java/org/jabref/gui/maintable/MainTable.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,14 @@ public MainTable(MainTableDataModel model,
137137
}
138138
}
139139
*/
140-
mainTablePreferences.getColumnPreferences().getColumnSortOrder().forEach(columnModel ->
140+
mainTablePreferences.getColumnPreferences().getColumnSortOrder().forEach(columnModel ->
141141
this.getColumns().stream()
142142
.map(column -> (MainTableColumn<?>) column)
143143
.filter(column -> column.getModel().equals(columnModel))
144144
.findFirst()
145145
.ifPresent(column -> this.getSortOrder().add(column)));
146146

147-
if (mainTablePreferences.getResizeColumnsToFit()) {
148-
this.setColumnResizePolicy(new SmartConstrainedResizePolicy());
149-
}
147+
this.setColumnResizePolicy(new SmartConstrainedResizePolicy());
150148

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

src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.jabref.model.entry.field.Field;
3838
import org.jabref.model.entry.field.FieldFactory;
3939
import org.jabref.model.entry.field.SpecialField;
40+
import org.jabref.model.entry.field.StandardField;
4041
import org.jabref.model.groups.AbstractGroup;
4142
import org.jabref.model.util.OptionalUtil;
4243
import org.jabref.preferences.PreferencesService;
@@ -109,7 +110,16 @@ public MainTableColumnFactory(BibDatabaseContext database,
109110
default:
110111
case NORMALFIELD:
111112
if (!column.getQualifier().isBlank()) {
112-
columns.add(createFieldColumn(column));
113+
TableColumn<BibEntryTableViewModel, ?> fieldColumn = createFieldColumn(column);
114+
columns.add(fieldColumn);
115+
if (column.getQualifier().equalsIgnoreCase(StandardField.YEAR.getName())) {
116+
// We adjust the min width and the current width, but not the max width to allow the user to enlarge this field
117+
// 75 is chosen, because of the optimal width of a four digit field
118+
fieldColumn.setMinWidth(60);
119+
fieldColumn.setPrefWidth(60);
120+
} else {
121+
fieldColumn.setMinWidth(ColumnPreferences.DEFAULT_COLUMN_WIDTH);
122+
}
113123
}
114124
break;
115125
}
@@ -125,7 +135,7 @@ public static void setExactWidth(TableColumn<?, ?> column, double width) {
125135
}
126136

127137
/**
128-
* Creates a column with a continous number
138+
* Creates a column with a continuous number
129139
*/
130140
private TableColumn<BibEntryTableViewModel, String> createIndexColumn(MainTableColumnModel columnModel) {
131141
TableColumn<BibEntryTableViewModel, String> column = new MainTableColumn<>(columnModel);

src/main/java/org/jabref/gui/maintable/MainTableColumnModel.java

+3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ public Type getType() {
124124
return typeProperty.getValue();
125125
}
126126

127+
/**
128+
* Returns the field name of the column (e.g., year)
129+
*/
127130
public String getQualifier() {
128131
return qualifierProperty.getValue();
129132
}

src/main/java/org/jabref/gui/maintable/SmartConstrainedResizePolicy.java

+22-65
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.List;
77

88
import javafx.scene.control.ResizeFeaturesBase;
9+
import javafx.scene.control.TableColumn;
910
import javafx.scene.control.TableColumnBase;
1011
import javafx.scene.control.TableView;
1112
import javafx.util.Callback;
@@ -25,80 +26,36 @@ public class SmartConstrainedResizePolicy implements Callback<TableView.ResizeFe
2526
@Override
2627
public Boolean call(TableView.ResizeFeatures prop) {
2728
if (prop.getColumn() == null) {
28-
return initColumnSize(prop.getTable());
29+
// table is initialized
30+
// no need to adjust
31+
return false;
2932
} else {
3033
return constrainedResize(prop);
3134
}
3235
}
3336

34-
private Boolean initColumnSize(TableView<?> table) {
35-
double tableWidth = getContentWidth(table);
36-
List<? extends TableColumnBase<?, ?>> visibleLeafColumns = table.getVisibleLeafColumns();
37-
double totalWidth = visibleLeafColumns.stream().mapToDouble(TableColumnBase::getWidth).sum();
38-
39-
if (Math.abs(totalWidth - tableWidth) > 1) {
40-
double totalPrefWidth = visibleLeafColumns.stream().mapToDouble(TableColumnBase::getPrefWidth).sum();
41-
if (totalPrefWidth > 0) {
42-
for (TableColumnBase col : visibleLeafColumns) {
43-
double share = col.getPrefWidth() / totalPrefWidth;
44-
double newSize = tableWidth * share;
45-
46-
// Just to make sure that we are staying under the total table width (due to rounding errors)
47-
newSize -= 2;
48-
49-
resize(col, newSize - col.getWidth());
50-
}
51-
}
52-
}
53-
54-
return false;
55-
}
56-
57-
private void resize(TableColumnBase column, double delta) {
58-
// We have to use reflection since TableUtil is not visible to us
59-
try {
60-
// TODO: reflective access, should be removed
61-
Class<?> clazz = Class.forName("javafx.scene.control.TableUtil");
62-
Method constrainedResize = clazz.getDeclaredMethod("resize", TableColumnBase.class, double.class);
63-
constrainedResize.setAccessible(true);
64-
constrainedResize.invoke(null, column, delta);
65-
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
66-
LOGGER.error("Could not invoke resize in TableUtil", e);
67-
}
68-
}
69-
7037
private Boolean constrainedResize(TableView.ResizeFeatures<?> prop) {
7138
TableView<?> table = prop.getTable();
7239
List<? extends TableColumnBase<?, ?>> visibleLeafColumns = table.getVisibleLeafColumns();
73-
return constrainedResize(prop,
74-
false,
75-
getContentWidth(table) - 2,
76-
visibleLeafColumns);
77-
}
78-
79-
private Boolean constrainedResize(TableView.ResizeFeatures prop, Boolean isFirstRun, Double contentWidth, List<? extends TableColumnBase<?, ?>> visibleLeafColumns) {
80-
// We have to use reflection since TableUtil is not visible to us
81-
try {
82-
// TODO: reflective access, should be removed
83-
Class<?> clazz = Class.forName("javafx.scene.control.TableUtil");
84-
Method constrainedResize = clazz.getDeclaredMethod("constrainedResize", ResizeFeaturesBase.class, Boolean.TYPE, Double.TYPE, List.class);
85-
constrainedResize.setAccessible(true);
86-
Object returnValue = constrainedResize.invoke(null, prop, isFirstRun, contentWidth, visibleLeafColumns);
87-
return (Boolean) returnValue;
88-
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
89-
LOGGER.error("Could not invoke constrainedResize in TableUtil", e);
90-
return false;
40+
Double delta = prop.getDelta();
41+
TableColumn<?, ?> userChosenColumnToResize = prop.getColumn();
42+
43+
double oldWidth = userChosenColumnToResize.getWidth();
44+
double newWidth;
45+
if (delta < 0) {
46+
double minWidth = userChosenColumnToResize.getMinWidth();
47+
LOGGER.trace("MinWidth {}", minWidth);
48+
newWidth = Math.max(minWidth, oldWidth + delta);
49+
} else {
50+
double maxWidth = userChosenColumnToResize.getMaxWidth();
51+
LOGGER.trace("MaxWidth {}", maxWidth);
52+
newWidth = Math.min(maxWidth, oldWidth + delta);
9153
}
92-
}
93-
94-
private Double getContentWidth(TableView<?> table) {
95-
try {
96-
// TODO: reflective access, should be removed
97-
Field privateStringField = TableView.class.getDeclaredField("contentWidth");
98-
privateStringField.setAccessible(true);
99-
return (Double) privateStringField.get(table);
100-
} catch (IllegalAccessException | NoSuchFieldException e) {
101-
return 0d;
54+
LOGGER.trace("Size: {} -> {}", oldWidth, newWidth);
55+
if (oldWidth == newWidth) {
56+
return false;
10257
}
58+
userChosenColumnToResize.setPrefWidth(newWidth);
59+
return true;
10360
}
10461
}

0 commit comments

Comments
 (0)