-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a69091
commit e3515e9
Showing
4 changed files
with
110 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/main/java/org/jabref/gui/maintable/SmartConstrainedResizePolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
23 changes: 0 additions & 23 deletions
23
src/main/java/org/jabref/gui/maintable/SmartConstraintedResizePolicy.java
This file was deleted.
Oops, something went wrong.