Skip to content
Permalink
Browse files
Merge pull request #465 from nadment/HOP-2296
HOP-2296 Fix HopVfsFileDialog optimal column width
  • Loading branch information
hansva committed Dec 14, 2020
2 parents db9b484 + 585723a commit 58426b8fb7d5defad8ee494fdf3489c59c03bc17
Showing 1 changed file with 22 additions and 47 deletions.
@@ -18,72 +18,47 @@
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++ ) {
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;
}
public static final void setOptimalWidthOnColumns(final Tree tree) {
if (tree.isDisposed()) return;
// Compute size in UI Thread to avoid NPE
tree.getDisplay().asyncExec(() -> {
if(tree.isDisposed()) return;
tree.setRedraw(false);
for (TreeColumn column : tree.getColumns()) {
if (column.isDisposed()) break;
column.pack();
column.setWidth(column.getWidth() + (int) (40 * PropsUi.getInstance().getZoomFactor()));
}
getMaxWidths( items[ i ].getItems(), max, gc );
}
tree.setRedraw(true);
});
}

public static final TreeItem findTreeItem( Tree tree, String[] path ) {
public static final TreeItem findTreeItem(Tree tree, String[] path) {
TreeItem[] items = tree.getItems();
for ( int i = 0; i < items.length; i++ ) {
TreeItem treeItem = findTreeItem( items[ i ], path, 0 );
if ( treeItem != null ) {
for (int i = 0; i < items.length; i++) {
TreeItem treeItem = findTreeItem(items[i], path, 0);
if (treeItem != null) {
return treeItem;
}
}
return null;
}

private static final TreeItem findTreeItem( TreeItem treeItem, String[] path, int level ) {
if ( treeItem.getText().equals( path[ level ] ) ) {
if ( level == path.length - 1 ) {
private static final TreeItem findTreeItem(TreeItem treeItem, String[] path, int level) {
if (treeItem.getText().equals(path[level])) {
if (level == path.length - 1) {
return treeItem;
}

TreeItem[] items = treeItem.getItems();
for ( int i = 0; i < items.length; i++ ) {
TreeItem found = findTreeItem( items[ i ], path, level + 1 );
if ( found != null ) {
for (int i = 0; i < items.length; i++) {
TreeItem found = findTreeItem(items[i], path, level + 1);
if (found != null) {
return found;
}
}

0 comments on commit 58426b8

Please sign in to comment.