JavaFx include multiple gui in the main gui - javafx-8

I have a main gui in fxml file created with SceneBuilder and its related controller.
In that main gui I've a gridpane where I want to put, for each cell, three child (a label, a TextView and a CheckBox).
So I've created an additional fxml with an HBox as root and the three childs.
Now... How could I add by code in the gridpane of the main gui the defined component for each cell and interacts with them?
I mean... what I want to do is something like this in the main gui controller:
for (int i)
for (int j)
gridpane.add("the_composed_view_in_the_other_fxml", i, j)

If I understand your question correctly, you would do something like this in the initialize method of your "main" controller:
public class MainController {
#FXML
private GridPane gridpane ;
public void initialize() throws IOException {
int numCols = ... ;
int numRows = ... ;
for (int rowIndex = 0 ; rowIndex < numRows ; rowIndex++) {
for (int colIndex = 0 ; colIndex < numCols ; colIndex++) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/addtional/fxml"));
gridpane.add(loader.load(), colIndex, rowIndex);
}
}
}
}
For "interacting" with the components loaded from the additional fxml file, it is really the responsibility of the controller for the additional fxml. You can get a reference to each of those controllers after you load the fxml file:
gridpane.add(loader.load(), colIndex, rowIndex);
AdditionalController controller = loader.getController();
and then you can call methods that you have defined in that controller class. You haven't really provided enough detail about what you might want to do here, but, e.g.:
public class AdditionalController {
#FXML
private CheckBox checkBox ;
public BooleanProperty selectedProperty() {
return checkBox.selectedProperty();
}
// etc...
}
and then something like
gridpane.add(loader.load(), colIndex, rowIndex);
AdditionalController controller = loader.getController();
String s = String.format("Item [%d, %d]", colIndex, rowIndex);
controller.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
// process selection...
System.out.println(s + " is selected");
}
});

Related

Nebula NatTable didn't display correct cell editor when use GridLayer.

I edited a Nebula example (_301_CustomDataProviderExample) to test the cell editing feature on pressing tab key (enable next cell editing on pressing tab key). The problem occurred when I used GridLayer: the cell editor was display incorrect. I think it's because of row and column headers but I don't know how to debug or fix. Can you help me or give me some hints? This is the code I have edited or added:
#Override
public Control createExampleControl(Composite parent) {
String[][] testData = new String[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
testData[i][j] = "" + i + "/" + j;
}
}
IDataProvider bodyDataProvider = new TwoDimensionalArrayDataProvider(testData);
final DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer);
ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
viewportLayer.addConfiguration(new DefaultEditConfiguration());
viewportLayer.addConfiguration(new DefaultEditBindings());
IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
DataLayer rowHeaderDataLayer = new DataLayer(rowHeaderDataProvider, 40, DataLayer.DEFAULT_ROW_HEIGHT);
rowHeaderDataLayer.setColumnsResizableByDefault(true);
ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, viewportLayer, selectionLayer);
IDataProvider columnHeaderDataProvider = new LetterColumnHeaderDataProvider(20);
ILayer columnHeaderLayer = new ColumnHeaderLayer(
new DefaultColumnHeaderDataLayer(columnHeaderDataProvider), viewportLayer, selectionLayer);
IDataProvider conerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider);
ILayer cornerLayer = new CornerLayer(new DataLayer(conerDataProvider), rowHeaderLayer, columnHeaderLayer);
GridLayer gridLayer = new GridLayer(viewportLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer, false);
final NatTable natTable = new NatTable(parent, false);
natTable.setLayer(/* viewportLayer */ gridLayer);
natTable.addConfiguration(new DefaultNatTableStyleConfiguration() {
#Override
public void configureRegistry(IConfigRegistry configRegistry) {
super.configureRegistry(configRegistry);
configRegistry.registerConfigAttribute(
EditConfigAttributes.CELL_EDITABLE_RULE,
IEditableRule.ALWAYS_EDITABLE);
}
});
natTable.configure();
return natTable;
}
Added:
class LetterColumnHeaderDataProvider implements IDataProvider {
private int columns = 0;
public LetterColumnHeaderDataProvider(int columns) {
this.columns = columns;
}
#Override
public Object getDataValue(int columnIndex, int rowIndex) {
return (char) ('A' + columnIndex);
// TODO: support column header AA-AZ, ZA-ZZ, ZZZ...
}
#Override
public void setDataValue(int columnIndex, int rowIndex, Object newValue) {
throw new UnsupportedOperationException();
}
#Override
public int getColumnCount() {
return this.columns;
}
#Override
public int getRowCount() {
return 1;
}
}
The issue is related to registering the DefaultEditConfiguration on the ViewportLayer. Doing this will result in placing the editor that is identified on key press relative to the body layer stack and not the GridLayer. The default configuration applied with the GridLayer contains the necessary edit configurations already. But you disabled it via the last parameter in the GridLayer constructor.
In short, remove the following two lines from your example, they are only needed in case of compositions without a GridLayer.
viewportLayer.addConfiguration(new DefaultEditConfiguration());
viewportLayer.addConfiguration(new DefaultEditBindings());
and change the creation of the GridLayer to this
GridLayer gridLayer = new GridLayer(viewportLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer);
And as a hint, be careful when copying code from different examples when not understanding their purpose. The NatTable examples typically contain explanations in code to explain them in more detail.

It is possible to issue java.lang.reflect.Field to javafx.scene.control.TextField?

It is possible to issue java.lang.reflect.Field to javafx.scene.control.TextField?
For example:
Field[] nodes;
nodes = clase.getDeclaredFields();
for (Field n : nodes)
if (n.getType().getSimpleName().equals("TextField"))
((TextField)((Object) n)).setText("Text");
If you want to modify the TextFields, you need to retrieve the value from those fields (and cast this value to TextField).
The following example should demonstrate the approach:
private TextField t1 = new TextField();
private TextField t2 = new TextField();
#Override
public void start(Stage primaryStage) {
Button btn = new Button("Say 'Hello World'");
btn.setOnAction((ActionEvent event) -> {
Object object = this;
Class clazz = object.getClass();
for (Field field : clazz.getDeclaredFields()) {
if (field.getType().getName().equals("javafx.scene.control.TextField")) {
try {
// get field value here
TextField textField = (TextField) field.get(object);
if (textField != null) {
textField.setText("Hello World");
}
} catch (IllegalArgumentException | IllegalAccessException ex) {
Logger.getLogger(ReflectTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
VBox root = new VBox();
root.getChildren().addAll(btn, t1, t2);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
Reflection is probably a really bad approach to this. Among many problems is that you make the functionality dependent on how the code is written. Specifically, you assume that each text field is stored in a specific instance field in some class. If you change the implementation, e.g. so that you keep the text fields in a data structure instead of maintaining references to them yourself, then your functionality will break. It is bad practice to write code that is so tightly coupled to the actual implementation of the code, for obvious reasons.
One better approach would simply to be to put all the text fields in a list (or other data structure), so you can do whatever you need with them easily. E.g.
public class MyForm {
private GridPane view ;
private String[] messages = {"First name:", "Last name", "Email"} ;
private List<TextField> textFields ;
public MyForm {
view = new GridPane();
textFields = new ArrayList<>();
for (int r = 0; r < messages.length ; r++) {
view.addRow(r, new Label(messages[r]), createTextField(messages[r]));
}
}
private TextField createTextField(String text) {
TextField textField = new TextField();
textField.setPromptText(text);
textFields.add(textField);
return textField ;
}
public void processTextFields() {
textField.forEach(tf -> tf.setText("Hello"));
}
}
Another approach would be to use a CSS lookup. If myForm is some node that is an ancestor of all the text fields:
myForm.lookupAll(".text-field").forEach(node -> {
TextField textField = (TextField)node ;
textField.setText("Hello");
});
but note that CSS lookups will not work until after CSS has been applied (by default, this means after the scene has been rendered for the first time).
Another way, if all the text fields are all contained in a single direct parent (such as the grid pane in the first example), would be to iterate through the child nodes and filter the text fields:
textFieldParentNode.getChildrenUnmodifiable().stream()
.filter(TextField.class::isInstance)
.map(TextField.class::cast)
.forEach(tf -> tf.setText("Hello"));

iTextSharp 7 object reference not set to an instance of an object

Is there some recommendation to build tables with cells having paragraphs in order to avoid an exception at adding of some cell to table or table to document? I get this and I can't figure out what happens:
[NullReferenceException: Object reference not set to an instance of an object.]
iText.Layout.Renderer.TableRenderer.DrawBorders(DrawContext drawContext) +2493
iText.Layout.Renderer.TableRenderer.DrawChildren(DrawContext drawContext) +1497
iText.Layout.Renderer.AbstractRenderer.Draw(DrawContext drawContext) +153
iText.Layout.Renderer.TableRenderer.Draw(DrawContext drawContext) +637
iText.Layout.Renderer.AbstractRenderer.DrawChildren(DrawContext drawContext) +104
iText.Layout.Renderer.BlockRenderer.Draw(DrawContext drawContext) +525
iText.Layout.Renderer.TableRenderer.DrawChildren(DrawContext drawContext) +1382
iText.Layout.Renderer.AbstractRenderer.Draw(DrawContext drawContext) +153
iText.Layout.Renderer.TableRenderer.Draw(DrawContext drawContext) +637
iText.Layout.Renderer.DocumentRenderer.FlushSingleRenderer(IRenderer resultRenderer) +473
iText.Layout.Renderer.RootRenderer.AddChild(IRenderer renderer) +1999
iText.Layout.RootElement`1.Add(BlockElement`1 element) +92
iText.Layout.Document.Add(BlockElement`1 element) +81
Here is a simple snapshot (compared to the real project) using a Windows console project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
namespace iTextTest
{
public static class iTextSharpHelper
{
public static T SetBorderEx<T>(this ElementPropertyContainer<T> element, Border border)
where T : ElementPropertyContainer<T>
{
element.SetBorder(border);
return (T)element;
}
public static Paragraph Style(this BlockElement<Paragraph> element)
{
element
.SetBorderEx(iText.Layout.Borders.Border.NO_BORDER)
.SetFont(iText.Kernel.Font.PdfFontFactory.CreateFont(iText.IO.Font.FontConstants.HELVETICA))
.SetFontSize(10.0f)
.SetFixedLeading(12.0f)
.SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.BOTTOM)
.SetMargin(0f);
return (Paragraph)element;
}
}
class Program
{
private static float[] tableColumns = { 0.35f, 0.25f, 0.15f, 0.25f };
static void Main(string[] args)
{
iText.Kernel.Pdf.PdfDocument pdf = new iText.Kernel.Pdf.PdfDocument(new iText.Kernel.Pdf.PdfWriter("test.pdf"));
iText.Layout.Document document = new iText.Layout.Document(pdf, iText.Kernel.Geom.PageSize.A4);
document.SetMargins(50f, 50f, 25f, 50f);
iText.Layout.Element.Table mainTable = new iText.Layout.Element.Table(tableColumns)
.SetBorderEx(iText.Layout.Borders.Border.NO_BORDER)
.SetWidthPercent(100)
.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.LEFT)
.SetPadding(0f);
for (int i = 0; i < 10; i++)
{
AddRow(mainTable, "ABCDEFGHIJ", "ABCDEFGHIJ", "ABCDEFGHIJ");
}
document.Add(mainTable);
document.Close();
}
private static void AddRow(iText.Layout.Element.Table table, string col1, string col2, string col3)
{
// Label
AddCell(table, col1, true)
.SetBorderTop(new iText.Layout.Borders.SolidBorder(iText.Kernel.Colors.Color.BLACK, 0.5f));
// Product - Voucher and price/pcs
AddCell(table, col2, true)
.SetBorderTop(new iText.Layout.Borders.SolidBorder(iText.Kernel.Colors.Color.BLACK, 0.5f));
// Message
AddCell(table, col3, true, 2)
.SetBorderTop(new iText.Layout.Borders.SolidBorder(iText.Kernel.Colors.Color.BLACK, 0.5f))
//.SetBorderRight(new iText.Layout.Borders.SolidBorder(iText.Kernel.Colors.Color.BLACK, 0.5f))
.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.RIGHT)
.SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT);
}
private static iText.Layout.Element.Cell AddCell(iText.Layout.Element.Table table, string text, bool setBold = false, int colSpan = 1)
{
iText.Layout.Element.Cell cell = new iText.Layout.Element.Cell(1, colSpan)
.SetBorderEx(iText.Layout.Borders.Border.NO_BORDER)
.SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.BOTTOM);
if (!string.IsNullOrEmpty(text))
{
iText.Layout.Element.Paragraph paragraph = new iText.Layout.Element.Paragraph(text)
.Style();
if (setBold)
paragraph.SetBold();
cell.Add(paragraph);
}
table.AddCell(cell);
return cell;
}
}
}
Note, a commented out line of code:
//.SetBorderRight(new iText.Layout.Borders.SolidBorder(iText.Kernel.Colors.Color.BLACK, 0.5f))
Adding it serves as a workaround to make the document render without the exception.
Given the sample code added by the OP the issue can easily be reproduced.
Furthermore after porting the code to iText/Java the issue could be reproduced there, too, cf. MikesTableIssue.java test method testMikesCode. Thus, it is no porting error from Java (the original iText code) to C#.
The sample could even be considerably simplified and still reproduce the issue:
try ( FileOutputStream target = new FileOutputStream("mikesTableIssueSimple.pdf");
PdfWriter pdfWriter = new PdfWriter(target);
PdfDocument pdfDocument = new PdfDocument(pdfWriter) )
{
Document document = new Document(pdfDocument);
Table mainTable = new Table(1);
Cell cell = new Cell()
.setBorder(Border.NO_BORDER)
//.setBorderRight(new SolidBorder(Color.BLACK, 0.5f))
.setBorderTop(new SolidBorder(Color.BLACK, 0.5f));
cell.add("TESCHTINK");
mainTable.addCell(cell);
document.add(mainTable);
}
(MikesTableIssue.java test method testSimplified)
The issue does not occur if one
removes setBorder(Border.NO_BORDER) or
removes setBorderTop(new SolidBorder(Color.BLACK, 0.5f)) or
adds setBorderRight(new SolidBorder(Color.BLACK, 0.5f)).
In this situation com.itextpdf.layout.renderer.TableRenderer.drawBorders(DrawContext) executes this code:
if (lastBorder != null) {
if (verticalBorders.get(j).size() > 0) {
if (i == 0) {
x2 += verticalBorders.get(j).get(i).getWidth() / 2;
} else if(i == horizontalBorders.size() - 1 && verticalBorders.get(j).size() >= i - 1 && verticalBorders.get(j).get(i - 1) != null) {
x2 += verticalBorders.get(j).get(i - 1).getWidth() / 2;
}
}
lastBorder.drawCellBorder(drawContext.getCanvas(), x1, y1, x2, y1);
}
while lastBorder is the SolidBorder instance, verticalBorders is [[null], [null]], j == 1 and i == 0.
Thus, some additional null checks ought to be introduced here.

How to make GWT Datagrid have its first column fixed and scroll horizontally and vertically

Currently GWT DataGrid header does this trick with a fixed header row during a vertical scroll. Is there a way to acheive the same on an entire (first) column?
I have implemented ScrolledGrid that freezes first column in DataGrid. You need to use it instead of DataGrid in order to make first column be frozen.
import com.google.gwt.dom.client.*;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.client.ui.HeaderPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
/**
*
* #author Yuri Plaksyuk
*/
public class ScrolledGrid extends DataGrid {
private final Text cssText;
private boolean addedClass = false;
private int currentScrollLeft = 0;
public ScrolledGrid() {
cssText = Document.get().createTextNode("");
StyleElement styleElement = Document.get().createStyleElement();
styleElement.setType("text/css");
styleElement.appendChild(cssText);
HeaderPanel headerPanel = (HeaderPanel) getWidget();
headerPanel.getElement().insertFirst(styleElement);
final ScrollPanel scrollPanel = (ScrollPanel) headerPanel.getContentWidget();
scrollPanel.addScrollHandler(new ScrollHandler() {
#Override
public void onScroll(ScrollEvent event) {
int scrollLeft = scrollPanel.getHorizontalScrollPosition();
if (scrollLeft != currentScrollLeft) {
StringBuilder css = new StringBuilder();
if (scrollLeft > 0) {
css.append(".ScrolledGrid-frozen {");
css.append("background-color: inherit;");
css.append("}");
css.append(".ScrolledGrid-frozen div {");
css.append("position: absolute;");
css.append("left: ").append(scrollLeft).append("px;");
css.append("width: ").append(getColumnWidth(getColumn(0))).append(";");
css.append("padding-left: 1.3em;");
css.append("padding-right: 0.5em;");
css.append("margin-top: -0.7em;");
css.append("white-space: nowrap;");
css.append("background-color: inherit;");
css.append("}");
}
else
css.append(".ScrolledGrid-frozen { }");
css.append("th.ScrolledGrid-frozen { background-color: white; }");
cssText.setData(css.toString());
if (!addedClass) {
NodeList<TableRowElement> rows;
TableRowElement row;
TableCellElement cell;
rows = getTableHeadElement().getRows();
for (int i = 0; i < rows.getLength(); ++i) {
row = rows.getItem(i);
cell = row.getCells().getItem(0);
cell.setInnerHTML("<div>" + cell.getInnerHTML() + "</div>");
cell.addClassName("ScrolledGrid-frozen");
}
rows = getTableBodyElement().getRows();
for (int i = 0; i < rows.getLength(); ++i) {
row = rows.getItem(i);
cell = row.getCells().getItem(0);
cell.addClassName("ScrolledGrid-frozen");
}
addedClass = true;
}
currentScrollLeft = scrollLeft;
}
}
});
}
}
Unfortunately, some CSS values are hard-coded.
I adapted Yuri's solution to achieve the following goals:
does not flicker
copes with arbitrary row-heights
works with SelectionModel
more uniform solution
It does not mess with the columns itself, but instead shows arbitrary "frozen" information on row-level.
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.*;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.DefaultCellTableBuilder;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HeaderPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
/**
* #author Daniel Lintner
*
* A DataGrid extension with the ability to display some row-level-information
* when scrolling left (horizontal), hence important columns out of sight of the user.
*/
public class FrozenDataGrid extends DataGrid
{
//textnode getting updated dynamically when scolling horizontally
private Text cssText;
//the latest scroll-position
private int currentScrollLeft = 0;
//an object extracting String-info from your rowdata
private FrozenValueProvider valueProvider;
//inject basic styling into the document - once
//this is how the frozen row-info looks like
static
{
Text baseCss = Document.get().createTextNode("");
StyleElement styleElement = Document.get().createStyleElement();
styleElement.setType("text/css");
styleElement.appendChild(baseCss);
StringBuilder css = new StringBuilder();
css.append(".ScrolledGrid-base {");
css.append("position: absolute;");
css.append("background-color: gray;");
css.append("padding: .3em;");
css.append("padding-left: .5em;");
css.append("padding-right: .5em;");
css.append("border-radius: 3px 3px;");
css.append("transition: opacity 500ms;");
css.append("color: white;");
css.append("margin-top: 2px;");
css.append("white-space: nowrap;");
css.append("}");
baseCss.setData(css.toString());
Document.get().getBody().insertFirst(styleElement);
}
public FrozenDataGrid()
{
super();
init();
}
public FrozenDataGrid(int pageSize, DataGrid.Resources resources)
{
super(pageSize, resources);
init();
}
public void init()
{
//create a css textnode
cssText = Document.get().createTextNode("");
//create dynamic css Style
StyleElement styleElement = Document.get().createStyleElement();
styleElement.setType("text/css");
styleElement.appendChild(cssText);
//append the initial style condition
//todo the name of this style might be built dynamically per instance - if multiple grid-instances exist/not the use-case by now
StringBuilder css = new StringBuilder();
css.append(".ScrolledGrid-frozen {");
css.append("opacity:0;");
css.append("}");
cssText.setData(css.toString());
//set a custom CellTableBuilder in order to inject the info-div to the row
setTableBuilder(new DefaultCellTableBuilder(this)
{
#Override
public void buildRowImpl(final Object rowValue, final int absRowIndex)
{
//do what DefaultCellTableBuilder does
super.buildRowImpl(rowValue, absRowIndex);
//only do something if there is a valueProvider
if(valueProvider != null) {
//we do this deferred because this row has to created first in order to access it
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand()
{
#Override
public void execute()
{
createInfoDiv(getTableBodyElement().getRows().getItem(absRowIndex % getPageSize()), rowValue);
}
});
}
}
});
//fetch the ScrollPanel from the grid
HeaderPanel headerPanel = (HeaderPanel) getWidget();
headerPanel.getElement().insertFirst(styleElement);
final ScrollPanel scrollPanel = (ScrollPanel) headerPanel.getContentWidget();
//setup a timer handling the left-offset-css thing
//we use a timer to be able to cancel this operation -> e.g. continuous scroll
final Timer timer = new Timer(){
#Override
public void run() {
StringBuilder css = new StringBuilder();
//we need to left-offset the info-divs
if (scrollPanel.getHorizontalScrollPosition() > 100)
{
css.append(".ScrolledGrid-frozen {");
css.append("left: ").append(3 + scrollPanel.getHorizontalScrollPosition()).append("px;");
css.append("opacity: 1;");
css.append("}");
}
//we are close to the leftmost scroll position: info hidden
else
{
css.append(".ScrolledGrid-frozen {");
css.append("opacity:0;");
css.append("}");
}
cssText.setData(css.toString());
}
};
//track scrolling
scrollPanel.addScrollHandler(new ScrollHandler()
{
#Override
public void onScroll(ScrollEvent event)
{
//cancel previous actions to scroll events
if(timer.isRunning())
timer.cancel();
//actual horizontal scrollposition
int scrollLeft = scrollPanel.getHorizontalScrollPosition();
//a horizontal scroll takes places
if (scrollLeft != currentScrollLeft)
{
//first we hide the row-info
StringBuilder css = new StringBuilder();
css.append(".ScrolledGrid-frozen {");
css.append("opacity:0;");
css.append("}");
cssText.setData(css.toString());
//render left offset after a delay
timer.schedule(500);
//remember the current horizontal position
currentScrollLeft = scrollLeft;
}
}
});
}
private void createInfoDiv(TableRowElement row, Object value)
{
//create a div element and add value and style to it
DivElement div = Document.get().createDivElement();
div.setInnerText(valueProvider.getFrozenValue(value));
div.addClassName("ScrolledGrid-base");
div.addClassName("ScrolledGrid-frozen");
//we add it to the first child of the row, because added as child of the row directly
// confuses the CellTable with coordinating header positions
row.getFirstChildElement().insertFirst(div);
}
public void setFrozenValueProvider(FrozenValueProvider valueProvider) {
this.valueProvider = valueProvider;
}
public interface FrozenValueProvider<T>{
String getFrozenValue(T data);
}
}
Hope this helps developers on this rarely and unsatisfactorily solved problem.
And... there is still room for improvement left.
Cheers Dan

How to get a different context-menu showing on a TreeViewers header

I wish to let users toggle column's visibility in a TreeViewer. I already have a context menu bound to the right-click of my TreeViewer using ..
MenuManager.addMenuListener( new IMenuListener() ... );
.. but I can find no way of detecting when the right click is on the header of the table, rather than on the currently selected node. Down at the SWT level this is all possible, as is demonstrated by this snippet: http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet312.java The JFace layer however may not expose it as far as I can see.
Your clues and other musings are most welcome
M.
Thanks for your example, it helped alot. Since I actually needed to know which column header was clicked, I built upon your code and came to the following solution (which also works in case the original column ordering was changed by dragging):
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Tree;
public class TreeColumnHeaderMenuDetectListener implements MenuDetectListener {
private Tree fTree;
private TreeViewer fTreeViewer;
public TreeColumnHeaderMenuDetectListener(TreeViewer treeViewer)
{
fTree = treeViewer.getTree();
fTreeViewer = treeViewer;
}
#Override
public void menuDetected(MenuDetectEvent event) {
Point curLoc = Display.getCurrent().map(null, fTreeViewer.getControl(), new Point(event.x, event.y));
Rectangle clientArea = fTree.getClientArea();
if (clientArea.y <= curLoc.y && curLoc.y < (clientArea.y + fTree.getHeaderHeight()) &&
clientArea.x <= curLoc.x && curLoc.x < (clientArea.x + clientArea.width)) {
int xOffset = 0; // Accumulates previous column widths
for (int colIdx : fTree.getColumnOrder()) {
int colWidth = fTree.getColumn(colIdx).getWidth();
// Check if cursor location lies within the current column
if (xOffset <= curLoc.x && curLoc.x < (xOffset + colWidth)) {
System.out.println("column header "+colIdx); // Your code goes here
break;
}
xOffset += colWidth;
}
}
}
}
Use it by registering with
tree.addMenuDetectListener(new TreeColumnHeaderMenuDetectListener(treeViewer))
for a given tree and corresponding treeViewer.
In the end, I wrote this class to listen to the underlying tree object, and tell me when the column headers had been clicked on ...
import org.eclipse.jface.viewers.TreeViewer;
public class HeaderClickDetector implements Listener
{
TreeViewer viewer;
private boolean headerClicked;
public HeaderClickDetector( TreeViewer viewer )
{
this.viewer = viewer;
}
public void handleEvent(Event event)
{
Point pt = Display.getCurrent().map(null, viewer.getControl(), new Point(event.x, event.y));
Rectangle clientArea = viewer.getTree().getClientArea();
headerClicked = (clientArea.y <= pt.y && pt.y < (clientArea.y + viewer.getTree().getHeaderHeight()));
}
public boolean isHeaderClicked()
{
return headerClicked;
}
}