From bbfae087089fa3126eefb18207fde0cfaa7315a3 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Thu, 17 May 2018 14:14:53 +0300 Subject: [PATCH] Fix column width issues on HiDPI displays. KeyTiSelection fixes. gitlab #7 Change-Id: I03a51472f2e9d38a78e38b92617d0e9f975f6c54 --- .../ui/nattable/KeyToSelectionAdapter.java | 21 +++++++--- .../ui/nattable/NatTableColumnLayout.java | 7 ++++ .../ui/nattable/NatTableGraphExplorer.java | 2 +- .../ui/nattable/NatTableSelectionAdaptor.java | 6 ++- .../browsing/ui/nattable/TreeNode.java | 39 ++++++++++++------- 5 files changed, 53 insertions(+), 22 deletions(-) diff --git a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/KeyToSelectionAdapter.java b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/KeyToSelectionAdapter.java index 7e0202be3..9ededd52f 100644 --- a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/KeyToSelectionAdapter.java +++ b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/KeyToSelectionAdapter.java @@ -35,20 +35,27 @@ public class KeyToSelectionAdapter extends KeyAdapter { * @param explorer */ public KeyToSelectionAdapter(GraphExplorer explorer) { + this(explorer,"\\p{Alnum}"); + } + + public KeyToSelectionAdapter(GraphExplorer explorer, String pattern) { assert explorer != null; this.explorer = (NatTableGraphExplorer)explorer; - this.alphaNum = Pattern.compile("\\p{Alnum}"); + this.alphaNum = Pattern.compile(pattern); } - + + public boolean acceptKey(char key) { + return alphaNum.matcher(Character.toString(key)).matches(); + } + @Override public void keyPressed(KeyEvent e) { if (explorer.isDisposed()) return; - - if (!alphaNum.matcher(Character.toString(e.character)).matches()) - return; + if (!acceptKey(e.character)) + return; // concatenate / replace matcher. if ((e.time - prevEvent) > KEY_INPUT_DELAY ) matcher = ""; @@ -70,8 +77,8 @@ public class KeyToSelectionAdapter extends KeyAdapter { } if (item != null) { - explorer.select(item); explorer.show(item); + explorer.select(item); } // without this the default handling would take over. e.doit = false; @@ -144,6 +151,8 @@ public class KeyToSelectionAdapter extends KeyAdapter { */ protected boolean matchesColumn(TreeNode item, int column, String matcher) { String text = item.getValueString(column); + if (text == null) + return false; if (text.toLowerCase().startsWith(matcher)) { return true; } diff --git a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableColumnLayout.java b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableColumnLayout.java index 3bff31255..2979ab8c1 100644 --- a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableColumnLayout.java +++ b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableColumnLayout.java @@ -68,6 +68,13 @@ public class NatTableColumnLayout extends Layout implements ILayerListener{ } protected void setColumnWidths(Scrollable tree, int[] widths) { + // NatTable HiDPI workaround + double displayScale = NatTableGraphExplorer.getDisplayScale(); + if (displayScale != 1.0) { + for (int i=0; i < widths.length; i++) { + widths[i] = (int)Math.floor(((double)widths[i]/ displayScale)); + } + } for (int i=0; i < widths.length; i++) { columnHeaderDataProvider.getDataLayer().setColumnWidthByPosition(i, widths[i]); } diff --git a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableGraphExplorer.java b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableGraphExplorer.java index 2e9d5a1fc..6765f65ea 100644 --- a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableGraphExplorer.java +++ b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableGraphExplorer.java @@ -1506,7 +1506,7 @@ public class NatTableGraphExplorer extends GraphExplorerImplBase implements Grap final ExecutorService queryUpdateScheduler = Threads.getExecutor(); - private double getDisplayScale() { + public static double getDisplayScale() { Point dpi = Display.getCurrent().getDPI(); return (double)dpi.x/96.0; } diff --git a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableSelectionAdaptor.java b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableSelectionAdaptor.java index 8a37d048b..ec3cb221d 100644 --- a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableSelectionAdaptor.java +++ b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/NatTableSelectionAdaptor.java @@ -107,8 +107,10 @@ public class NatTableSelectionAdaptor implements ISelectionProvider, IPostSelect private void setSelectionExternal(List items) { selectionLayer.clear(true); for (RowSelectionItem item : items) { - for (int c : item.columnIndex) - selectionLayer.selectCell(c, item.rowIndex, false, true); + for (int c : item.columnIndex) { + int r = selectionLayer.getRowPositionByIndex(item.rowIndex); + selectionLayer.selectCell(c, r, false, true); + } } selection = new StructuredSelection(items); fireEvents(); diff --git a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/TreeNode.java b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/TreeNode.java index 269966c30..4f9273d91 100644 --- a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/TreeNode.java +++ b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/TreeNode.java @@ -118,20 +118,33 @@ public class TreeNode implements IAdaptable { initData(); if (labeler != null) { String key = explorerContext.getGe().getColumns()[column].getKey(); - String s = null; - if (runtimeLabels != null) - s = runtimeLabels.get(key); - if (s == null) - s = labels.get(key); - if (labelDecorators != null && !labelDecorators.isEmpty()) { - int index = 0; - for (LabelDecorator ld : labelDecorators) { - String ds = ld.decorateLabel(s, key, index); - if (ds != null) - s = ds; - } + return getValue(key); + } + return null; + } + + private String getValue(String key) { + String s = null; + if (runtimeLabels != null) + s = runtimeLabels.get(key); + if (s == null) + s = labels.get(key); + if (labelDecorators != null && !labelDecorators.isEmpty()) { + int index = 0; + for (LabelDecorator ld : labelDecorators) { + String ds = ld.decorateLabel(s, key, index); + if (ds != null) + s = ds; } - return s; + } + return s; + } + + public String getValueString(String key) { + if (labels == null) + initData(); + if (labeler != null) { + return getValue(key); } return null; } -- 2.43.2