How to get all row data from multicolumn TreeViewer - java

How to read all the row data from a multicolumn TreeViewer.
m_viewer.addSelectionChangedListener(new ISelectionChangedListener() {
#Override
public void selectionChanged(final SelectionChangedEvent event)
{
IStructuredSelection selection = (IStructuredSelection)event.getSelection();
Object [] selections = selection.toArray();
}
}
This code snippet gives only selected row value. I want to read all the data and save it in an xml file on click of a Save button. Can anyone help me to achieve that?

The objects you get from the structured selection are the objects provided by the content provider for the tree viewer. So you should be able to cast them to whatever class(es) the content provider uses and get the data from them.
This is similar to the objects that are passed to the tree viewer label provider.

If you want a serialize what is close to the visual representation of the tree, you can access the underlying Tree of the TreeViewer and traverse its TreeItems. This approach is described here: Get all TreeItems in an SWT Tree
Or you consider to just use the data model that the TreeViewer is showing and serialize its objects. Traversing the children of your data model is presumably already implemented in the content provider, usually a ITreeContentProvider, or an extension thereof and could be reused.

Related

Packing tree columns on property sheet entry change

We have an RCP app which uses a Tree and its corresponding TreeViewer. This tree uses an ObservableMapLabelProvider which provides the label text and an ObservableListTreeContentProvider for the content. We're using org.eclipse.jface.databinding-1.5.0-SDK-3.7.2.
We supply an array of IObservableMap using EMFObservables.observeMaps(contentProvider.getKnownElements(), new EStructuralFeature[]) to construct the ObservableMapLabelProvider.
We have an implementation of IPropertySourceProvider which seems to be used for populating the property view by overriding getPropertySource(Object).
Now I can see that when I modify the property sheet entry for a label, IPropertySource#setPropertyValue(Object, Object) is invoked. I want to add a change listener to our ObservableMapLabelProvider or IObservableMap to ensure that the tree columns get packed once the label text is modified. I tried adding change map listener to each element of IObservableMap but it doesn't seem to work.
Any suggestions/pointers on where should I be adding the change listener to pack tree columns once label text is changed on the property sheet?
The Properties view part is implemented by the PropertySheet class, and it's worth reading its Javadoc. PropertySheet is a type of PageBookView, a view that shows one of many managed pages. The Properties view's current page shows the properties of the current selection.
So you could try obtaining the tree viewer from the current page from the Properties view part (based on its view ID, org.eclipse.ui.views.PropertySheet) via getCurrentPage().getControl(), and then perform whatever column voodoo is required.
Alternatively you could provide your own IPropertySheetPage that behaves how you want.

javafx: sort TableView by default

I'm developing a small game that has a highscore list displayed by a javafx TableView. I've created a subclass HighscoreTableView extends TableView which is a TableView node that automatically creates the TableColumns I need and fills them with data on construction.
I want that table to be sorted by a default column on initialisation. I've added the following code lines:
duration.setSortType(TableColumn.SortType.DESCENDING);
this.getSortOrder().add(duration);
duration.setSortable(true);
this.sort();
where duration is the TableColumn that should define the sorting. Of course, it's added to the TableView. But when I create a new instance of that HighscoreTableView, it remains unsorted by default, until the user clicks on one of the column headers. This is unexpected, since this question, this question and this question say it should work that way. Any ideas?
Further information for reproduction:
The HighscoreTableView class is used by a HighscoreStage extends Stage class, which contains a TabPane with four Tabs. Each Tab contains a HighscoreTableView with different data taken from a static Data object. The data model is a class HighscoreEntry, an ObservableList of them gets added to the HighscoreTableViews. My full code is available here.
You are calling sort() before any data is loaded. Instead you should call it each time after adding/changing data to the table view.
Also you can use SortedList with appropriate comparator to wrap your original backing list. Than all changes will be automatically propagated to view.

How can I make a JList interact with my database?

I have a list of items that are pulled from a database, it combines the various fields with a rs.getString method to create a longer string of items, this is done in an action button method.
I would like to be able to click on an item in this list and have one of the fields display as text in a textbox, so this needs to be done through the list selection event method where I instruct the program to set the text to my desired value.
My problem is, I am not sure of the logic to follow in order to specify how to retrieve that fields information that will be corresponding to the item that is selected in the list, can you give me any ideas?
Rather the combining the fields into a single String, create a POJO (Plain Old Java Object), which provides getters (and possible setters) for the fields you want and these objects to the ListModel
Use a ListCellRenderer to customise the way which the JList renders the POJO the way you want to. See Writing a Custom Cell Renderer for more details.
When the user selects an item from the list, use JList#getSelectedValue and cast to the same class as your POJO. You can now use the POJO's getters to extract the properties you want to display.
The idea is to generate a self contained unit of work, which, based on what you want to do, you can customise how the object is displayed.
This concept is a corner stone to the separation of data (model) and the UI (view) behind the Model-View-Controller paradigm and OOP generally...

Prefuse set different color to every nodes group

I am new to prefuse and don't understand several things regarding nodes.
How can I get an event raised whenever a node clicked?
How can I group similar nodes with the same color?
How can I keep an extra meta-data on every node?
Its important to say that I build the graph in real-time and not loading it from any file.
Thanks,
Ozrad.
Three answers to your three questions:
The best way to react on node clicks is to extend prefuse.controls.ControlAdapter and add it to the Display.
prefuse.action.assignment.DataColorAction assigns color based on a data field. For more advanced scenarios you can use ColorAction with predicates or extend the class DataColorAction.
You can add columns to the node table to store metadata. A column can also store objects if you need it:
vg.getNodeTable().addColumn("meta", MyMetaData.class);

GWT Cell Tree, Selecting too many options!

I am using a gwt cell tree and I want only one node to be selected in the whole tree but many nodes are being selected.
I am also trying this
S1= new SelectionModel();......
S1.setSelected(S1.getSelected(),false); but using this technique nothing is being selected.
I am having the following problem:
Can someone help??
Maybe SingleSelectionModel helps you;
SingleSelectionModel selectionModel = new SingleSelectionModel();
See Google Example 2
to SingleSelectionModel usage.
Code looks weird to me, because you firstly get selected object with S1.getSelected() command and then kinda re-select same object, so no wonder that nothing happens. Instead S1.getSelected() pass object that you want to select.
You need to provide a key provider to the selection model like this
selectionModel = new SingleSelectionModel<NamedObject>(new ProvidesKey<NamedObject>() {
#Override
public Object getKey(NamedObject item) {
return item.getKey();
}
});
This will uniquely identify the nodes in the tree.

Categories

Resources