Allow only 8 selection in a Telerik radTreeListView - telerik

I have a radTreeListView with multiple parent nodes,which can contain multiple child nodes which can also contain multiple sub-child nodes.
I want to make sure only 8 items are selected and no more than that. Those 8 selections MUST also be under the same parent node.

You could try something like that:
private RadTreeNode GetParentNode(RadTreeNode currentNode) {
if (currentNode.Parent!=null) {
return GetParentNode(currentNode.Parent);
}
return currentNode;
}
private void CountSelectedNodes(RadTreeNode firstNode, ref int selectedCount) {
if (firstNode.Nodes!=null) {
foreach(RadTreeNode node in firstNode.Nodes) {
CountSelectedNodes(node, ref selectedCount);
}
if (firstNode.IsSelected) {
selectedCount+=1;
}
}
}
private bool SelectionAllowed(RadTreeNode currentNode) {
RadTreeNode treeNode=GetParentNode(currentNode);
int selectedCount;
CountSelectedNodes(treeNode, ref selectedCount);
return selectedCount<=8;
}
(this code is not tested so there could be some bugs)
Call selectionAllowed() with the Node currently clicked

Related

CharmListView Infinite Scroll

I need basically an event that triggers at each 200 records loaded, so more data can be loaded until the end of data.
I tried to extend CharmListCell and using the method updateItem like this:
#Override
public void updateItem(Model item, boolean empty) {
super.updateItem(item, empty);
currentItem = item;
if (!empty && item != null) {
update();
setGraphic(slidingTile);
} else {
setGraphic(null);
}
System.out.println(getIndex());
}
But the System.out.println(getIndex()); method returns -1;
I would like to call my backend method when the scroll down gets the end of last fetched block and so on, until get the end of data like the "infinite scroll" technique.
Thanks!
The CharmListCell doesn't expose the index of the underlying listView, but even if it did, that wouldn't be of much help to find out if you are scrolling over the end of the current list or not.
I'd suggest a different approach, which is also valid for a regular ListView, with the advantage of having the CharmListView features (mainly headers and the refresh indicator).
This short sample, created with a single view project using the Gluon IDE plugin and Charm 5.0.0, shows how to create a CharmListView control, and fill it with 30 items at a time. I haven't provided a factory cell, nor the headers, and for the sake of simplicity I'm just adding consecutive integers.
With a lookup, and after the view is shown (so the listView is added to the scene) we find the vertical ScrollBar of the listView, and then we add a listener to track its position. When it gets closer to 1, we simulate the load of another batch of items, with a pause transition that represents a heavy task.
Note the use of the refresh indicator. When new data is added, we scroll back to the first of the new items, so we can keep scrolling again.
public class BasicView extends View {
private final ObservableList<Integer> data;
private CharmListView<Integer, Integer> listView;
private final int batchSize = 30;
private PauseTransition pause;
public BasicView() {
data = FXCollections.observableArrayList();
listView = new CharmListView<>(data);
setOnShown(e -> {
ScrollBar scrollBar = null;
for (Node bar : listView.lookupAll(".scroll-bar")) {
if (bar instanceof ScrollBar && ((ScrollBar) bar).getOrientation().equals(Orientation.VERTICAL)) {
scrollBar = (ScrollBar) bar;
break;
}
}
if (scrollBar != null) {
scrollBar.valueProperty().addListener((obs, ov, nv) -> {
if (nv.doubleValue() > 0.95) {
addBatch();
}
});
addBatch();
}
});
setCenter(new VBox(listView));
}
private void addBatch() {
listView.setRefreshIndicatorVisible(true);
if (pause == null) {
pause = new PauseTransition(Duration.seconds(1));
pause.setOnFinished(f -> {
int size = data.size();
List<Integer> list = new ArrayList<>();
for (int i = size; i < size + batchSize; i++) {
list.add(i);
}
data.addAll(list);
listView.scrollTo(list.get(0));
listView.setRefreshIndicatorVisible(false);
});
} else {
pause.stop();
}
pause.playFromStart();
}
}
Note also that you could benefit from the setOnPullToRefresh() method, at any time. For instance, if you add this:
listView.setOnPullToRefresh(e -> addBatch());
whenever you go to the top of the list and drag it down (on a mobile device), it will make another call to load a new batch of items. Obviously, this is the opposite behavior as the "infinite scrolling", but it is possible as well with the CharmListView control.

JavaFX Selecting a TREE ITEM behaves does not work in the first walk behaves correctly after that

I have a tree of family members. I am attempting to provide a SEARCH facility which will allow me to select all the tree items where person name contains the SEARCH name. See this screen shot, though the first item matches RAJA it is not selected.
Now when I click on the LOCATE button again, the selections are correct as you can see.
This is the code for LOCATE BUTTON CLICKED.
#FXML
void onLocateClicked(MouseEvent event) {
childrenTreeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
childrenTreeView.getSelectionModel().clearSelection();
String tmpLocateString = txtLocate.getText().toLowerCase();
TreeItem<APerson> item = childrenTreeView.getRoot();
item.setExpanded(true);
System.out.println("Locate ->"+tmpLocateString+" Root value = "+item.getValue().getPersonName());
if (item == null) {
// Don't do anything
return;
}
if (item.getValue().getPersonName().toLowerCase().contains(tmpLocateString)){
System.out.println("Item ->"+item+" MATCHES (LocateClicked)");
childrenTreeView.getSelectionModel().select(item);
}
doSearch(item, tmpLocateString);
}
private void doSearch(TreeItem<APerson> item, String tmpLocateString) {
System.out.println("Current Parent :" + item.getValue());
if (item != childrenTreeView.getRoot()) {
if (item.getValue().getPersonName().toLowerCase().contains(tmpLocateString)){
System.out.println("Item ->"+item+" MATCHES (in doSearch)");
childrenTreeView.getSelectionModel().select(item);
}
}
for(TreeItem<APerson> child: item.getChildren()){
String personName = child.getValue().getPersonName();
if (personName.toLowerCase().contains(tmpLocateString)) {
childrenTreeView.getSelectionModel().select(child);
}
if(child.getChildren().isEmpty()){
System.out.println(" No Children for this node : "+personName);
} else {
doSearch(child, tmpLocateString);
}
}
}
After the first error, the selections work correctly for all other searches..... Can anyone guess what is wrong? "doSearch" function is recursive to walk thru entire tree.
Thanks for your help, in advance.
Hornigold
This is the change I did to onLocateClicked but it did not work.
void onLocateClicked(MouseEvent event) {
TreeItem<APerson> item = childrenTreeView.getRoot();
if (item == null) {
// Don't do anything
return;
}
String tmpLocateString = txtLocate.getText().toLowerCase();
childrenTreeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
childrenTreeView.getSelectionModel().clearSelection();
item.setExpanded(true);
System.out.println("Locate ->"+tmpLocateString+" Root value = "+item.getValue().getPersonName());
if (item.getValue().getPersonName().toLowerCase().contains(tmpLocateString)){
System.out.println("Item ->"+item+" MATCHES (LocateClicked)");
childrenTreeView.getSelectionModel().select(item);
}
doSearch(item, tmpLocateString);
}

Binary search tree insertion method doesn't work

I want to implement a insertion method for a Binary search tree, and come up with a solution below. I know there are plenty of code examples but I wonder what is the problem in my implementation? Or is there a problem? When I had traced it I thought I have missed something.
public void insertBST(Node<Double> head, int value){
if (head == null){
head = new Node<Double>(value);
return;
}
else {
if (head.getValue() > value)
insertBST(head.getLeft(), value);
else
insertBST(head.getRight(), value);
}
}
When you reassign a passed parameter, you're only changing the local variable, not the value passed to the function. You can read this question for more information - Is Java "pass-by-reference"? This is Java, right? Either way, a similar argument likely applies.
This is the problem with this line of code:
head = new Node<Double>(value);
You aren't changing the value passed into the function, so you never add to the tree.
You have two alternatives here, either the option presented by amdorra, or returning the current node:
public void insertBST(Node<Double> current, int value)
{
if (current == null)
{
return new Node<Double>(value);
}
else
{
if (head.getValue() > value)
head.setLeft(insertBST(head.getLeft(),value));
else
head.setRight(insertBST(head.getRight(),value));
return current;
}
}
To call the function, you can simply say:
root = insertBST(root, value);
With alternatives, the root will have to be handled as a special case.
at the beginning of you function you are adding the new Node to a part you will never have access to outside this function
so i will assume that your Node class looks like the following
Class Node{
private Node left;
private Node right;
//constructor, setters and getters and stuff
}
you could modify your code to look like the following:
if (head.getValue() > value){
if(head.getLeft == null) {
head.setLeft(new Node<Double>(value));
return;
}
insertBST(head.getLeft(),value);
}
else{
if(head.getRight == null) {
head.setRight(new Node<Double>(value));
return;
}
insertBST(head.getRight(),value);
}
you should also remove this part if (head==null) and always make sure you are sending a valid Node to the first call

JFace TreeView not launching when Input is a String

I'm trying launch a simple JFace Tree.
It's acting really strange however. When I setInput() to be a single String, the tree opens up completely blank. However, when I set input to be a String array, it works great.
This has nothing to do with the LabelProvider or ContentProvider since these behave the same no matter what (it's a really simple experimental program).
setInput() is officially allowed to take any Object. I am confused why it will not take a String, and knowing why may help me solve my other problems in life.
Setting a single String as input:
TreeViewer treeViewerLeft = new TreeViewer(shell, SWT.SINGLE);
treeViewerLeft.setLabelProvider(new TestLabelProvider());
treeViewerLeft.setContentProvider(new TestCompareContentProvider());
treeViewerLeft.expandAll();
treeViewerLeft.setInput(new String("Stooge"));
Setting an array of Strings:
TreeViewer treeViewerLeft = new TreeViewer(shell, SWT.SINGLE);
treeViewerLeft.setLabelProvider(new TestLabelProvider());
treeViewerLeft.setContentProvider(new TestCompareContentProvider());
treeViewerLeft.expandAll();
treeViewerLeft.setInput(new String[]{"Moe", "Larry", "Curly"});
The second works, and launches a tree using the following providers:
public class TestCompareContentProvider extends ArrayContentProvider implements ITreeContentProvider {
public static int children = 0;
public Object[] getChildren(Object parentElement) {
children++;
if (children > 20){
return null;
}
return new String[] {"Moe", "Larry", "Curly"};
}
public Object getParent(Object element) {
return "Parent";
}
public boolean hasChildren(Object element) {
if (children >20){
return false;
}
return true;
}
}
and
public class TestLabelProvider extends LabelProvider {
public String getText(Object element){
return "I'm something";
}
public Image getImage(Object element){
return null;
}
}
You've inherited getElements from the ArrayContentProvider and that only works with arrays. You should override this method.
I don't think you need to extend ArrayContentProvider at all.

Caliburn Micro Communication between ViewModels

hopefully you can help me. First of all, let me explain what my problem is.
I have two ViewModels. The first one has e.g. stored information in several textboxes.
For example
private static string _tbxCfgLogfile;
public string TbxCfgLogfile
{
get { return _tbxCfgLogfile; }
set
{
_tbxCfgLogfile = value;
NotifyOfPropertyChange(() => TbxCfgLogfile);
}
}
The other ViewModel has a Button where i want to save this data from the textboxes.
It does look like this
public bool CanBtnCfgSave
{
get
{
return (new PageConfigGeneralViewModel().TbxCfgLogfile.Length > 0 [...]);
}
}
public void BtnCfgSave()
{
new Functions.Config().SaveConfig();
}
How can i let "CanBtnCfgSave" know that the condition is met or not?
My first try was
private static string _tbxCfgLogfile;
public string TbxCfgLogfile
{
get { return _tbxCfgLogfile; }
set
{
_tbxCfgLogfile = value;
NotifyOfPropertyChange(() => TbxCfgLogfile);
NotifyOfPropertyChange(() => new ViewModels.OtherViewModel.CanBtnCfgSave);
}
}
It does not work. When i do remember right, i can get the data from each ViewModel, but i cannot set nor Notify them without any effort. Is that right? Do i have to use an "Event Aggregator" to accomplish my goal or is there an alternative easier way?
Not sure what you are doing in your viewmodels - why are you instantiating viewmodels in property accessors?
What is this line doing?
return (new PageConfigGeneralViewModel().TbxCfgLogfile.Length > 0 [...]);
I can't be sure from your setup as you haven't mentioned much about the architecture, but sincce you should have an instance of each viewmodel, there must be something conducting/managing the two (or one managing the other)
If you have one managing the other and you are implementing this via concrete references, you can just pick up the fields from the other viewmodel by accessing the properties directly, and hooking the PropertyChanged event of the child to notify the parent
class ParentViewModel : PropertyChangedBase
{
ChildViewModel childVM;
public ParentViewModel()
{
// Create child VM and hook up event...
childVM = new ChildViewModel();
childVM.PropertyChanged = ChildViewModel_PropertyChanged;
}
void ChildViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
// When any properties on the child VM change, update CanSave
NotifyOfPropertyChange(() => CanSave);
}
// Look at properties on the child VM
public bool CanSave { get { return childVM.SomeProperty != string.Empty; } }
public void Save() { // do stuff }
}
class ChildViewModel : PropertyChangedBase
{
private static string _someProperty;
public string SomeProperty
{
get { return _someProperty; }
set
{
_someProperty = value;
NotifyOfPropertyChange(() => SomeProperty);
}
}
}
Of course this is a very direct way to do it - you could just create a binding to CanSave on the child VM if that works, saving the need to create the CanSave property on the parent

Resources