Question

I'm developing a application based on the netbeans platform.

In one TopComponent I'm subclassing a OutlineView for displaying a collection of nodes. This OutlineView have some columns that the user can use to sort the collection of nodes on different properties.

My problem is that after I've sorted the nodes (from the GUI, with clicking on the columns) I would like to get the collection of nodes in their current ordered state. I haven't found a way to do this?

This is how my OutlineView could look... (before sorting)

------------------------
|Nodes | Prop1 | Prop2 |
|-----------------------
|Node1 |   1   |   a   |
|-----------------------
|Node2 |   5   |   y   |
|----------------------
|Node3 |   2   |   h   |
|-----------------------
|Node4 |   3   |   x   |
|-----------------------

Then after I click 'Prop1' it will be rearranged.

If I get the nodes from the ExplorerManager they are in their original order, not in the sorted order.

If I get the nodes from the model;

  for (int i = 0; i < getOutline().getModel().getRowCount(); i++) {
        TreeNode treeNode = (TreeNode) getOutline().getModel().getValueAt(i, 0);
        // still in the original order
    }

They will still be in the original order...

So do anyone know how I should get them in the new sorted order?

Was it helpful?

Solution

Ok I've found a solution that's sufficient for my needs, not very pretty but I thought I'd share it. I added a column model listener, that will be called when the user clicks on the column, and then get the sorted nodes 'after a while'.

I'm still interested in a way to listen to the sorting, since this column model listener doesn't feel very pretty...

    getOutline().getColumnModel().addColumnModelListener(new TableColumnModelListener() {

        @Override
        public void columnAdded(TableColumnModelEvent tcme) { }

        @Override
        public void columnRemoved(TableColumnModelEvent tcme) { }

        @Override
        public void columnMoved(TableColumnModelEvent tcme) {


            /* Must be done with invoke later, since    
             * this will be called when the user clicks on the column, not
             * when the actual sorting takes place...
             */
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    getSortedNodes();
                }
            });
        }

        @Override
        public void columnMarginChanged(ChangeEvent ce) { }

        @Override
        public void columnSelectionChanged(ListSelectionEvent lse) { }
    });


private List<Node> getSortedNodes() {
        List<Node> sortedNodes = new ArrayList<>();
        for (int i = 0; i < getOutline().getRowCount(); i++) {
            int sortedIndex = getOutline().convertRowIndexToModel(i);
            ETable.RowMapping mapping = new ETable.RowMapping(sortedIndex, getOutline().getModel(), getOutline());
            Node node = (Node) mapping.getTransformedValue(0);
            sortedNodes.add(node);
        }
        return sortedNodes;
    }

OTHER TIPS

I think the problem here is that you're looking only at the model and the explorer manager. The table itself holds its display data and the model holds the actual data. If you look directly at the Outline you can see the order things are displayed.

public void actionPerformed(ActionEvent e) {
    final Outline outline = ov.getOutline();
    int cols = outline.getColumnCount();
    int rows = outline.getRowCount();
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            System.out.print(outline.getValueAt(i, j) + ", ");
        }
        System.out.println();
    }
}

Output unsorted:

[Col 1]

Table: contentmanagement-gui.xml
Model: contentmanagement-gui.xml

[Col 2]

Table: org.openide.nodes.PropertySupport$Reflection[name=updated]
Model: org.openide.nodes.PropertySupport$Reflection[name=updated]

[Col 3]

Table: org.openide.nodes.PropertySupport$Reflection[name=installedTime]
Model: org.openide.nodes.PropertySupport$Reflection[name=installedTime]

Output after sorting tree nodes ascending, and moving the tree column to position 2.

[Col 1]

Table: org.openide.nodes.PropertySupport$Reflection[name=updated]
Model: contentmanagement-gui.xml

[Col 2]

Table: beansbinding-1.2.1.jar
Model: org.openide.nodes.PropertySupport$Reflection[name=updated]

[Col 3]

Table: org.openide.nodes.PropertySupport$Reflection[name=installedTime]
Model: org.openide.nodes.PropertySupport$Reflection[name=installedTime]

This isn't exactly clean but it will print the text ordered as you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top