Fix HopVfsFileDialog calculate optimal column width in the
user-interface thread
package org.apache.hop.ui.core.widget;
import org.apache.hop.ui.core.PropsUi;
-import org.eclipse.swt.graphics.GC;
-import org.eclipse.swt.graphics.Image;
-import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
public class TreeUtil {
- public static final void setOptimalWidthOnColumns( Tree tree ) {
- int nrCols = tree.getColumnCount();
- int[] max = new int[ nrCols ];
- Image image = new Image( tree.getDisplay(), 10, 10 );
- GC gc = new GC( image );
-
- for ( int i = 0; i < max.length; i++ ) {
- TreeColumn treeColumn = tree.getColumn( i );
- Point point = gc.textExtent( treeColumn.getText() );
- max[ i ] = point.x;
- }
-
- getMaxWidths( tree.getItems(), max, gc );
-
- gc.dispose();
- image.dispose();
-
- for ( int i = 0; i < max.length; i++ ) {
- TreeColumn treeColumn = tree.getColumn( i );
- treeColumn.setWidth( max[ i ] + (int)(40* PropsUi.getInstance().getZoomFactor()) );
- }
- }
-
- private static final void getMaxWidths( TreeItem[] items, int[] max, GC gc ) {
- for ( int i = 0; i < items.length; i++ ) {
-
- if (items[i].isDisposed()) break;
-
- gc.setFont( items[i].getFont() );
- for ( int c = 0; c < max.length; c++ ) {
- String string = items[ i ].getText( c );
- Point point = gc.textExtent( string );
- if ( point.x > max[ c ] ) {
- max[ c ] = point.x;
- }
- }
- getMaxWidths( items[ i ].getItems(), max, gc );
- }
+ public static final void setOptimalWidthOnColumns(final Tree tree) {
+ if (tree.isDisposed()) return;
+ // Compute size in UI Thread to avoid NPE
+ tree.getDisplay().asyncExec(() -> {
+ tree.setRedraw(false);
+ for (TreeColumn column : tree.getColumns()) {
+ if (column.isDisposed()) break;
+ column.pack();
+ column.setWidth(column.getWidth() + (int) (40 * PropsUi.getInstance().getZoomFactor()));
+ }
+ tree.setRedraw(true);
+ });
}
public static final TreeItem findTreeItem(Tree tree, String[] path) {
