Hi Olli,
Thank you very much for the example you created for me, I understood as already from your posts,
Unfortunately my programmer, with whom I speak little today, has given me several problems, has made complicated technical choices and I do not understand all of them,
I have attached the method because for the object you see in the attached image has chosen the
tree.addValueChangeListener (event → {
And not the
Ttable.addItemClickListener (event → {
Object itemId = event.getItemId ();
Ttable.setCollapsed (itemId,! Ttable.isCollapsed (itemId));
});
As indicated by you, so I am in the conception that clikkando on the item is always called
addValueChangeListener (event → {
[b]
How can I integrate the listed listener in the code I am?
Sorry if I insist but I do not understand how to do … can you still help me with courtesy?
Thanks a lot, I attach the .java file of the whole class
MARKED TEXT COLOR PINK AND UNDERLINE, POINT WHERE I MUST IMPLEMENT COLLAPSE OR EXPAND FOR ITEM, BECAUSE THE LISTENER ENTER ALWAYS IN tree.addValueChangeListener(event → { AND I DON’T KNOW WHY???
[/b]
VERY THANKS OLLI
package rv.mobile.components;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.slf4j.Logger;
import com.vaadin.data.Item;
import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.data.util.filter.SimpleStringFilter;
import com.vaadin.server.FontAwesome;
import com.vaadin.ui.Component;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Tree;
import com.vaadin.ui.TreeTable;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;
import com.vaadin.ui.themes.ValoTheme;
import my.vaadin.WebAppLoggerFactory;
import my.vaadin.components.BigCheckBox;
import my.vaadin.components.BigTextField;
import my.vaadin.components.WebAppNotifier;
import rv.db.RVDBManager;
import rv.db.entity.Article;
import rv.db.entity.ObjectType;
import rv.mobile.controller.RVMobileWebAppController;
import rv.mobile.controller.RVNewRequestController;
public class RVArticleTreeTable extends Panel {
private static final long serialVersionUID = 5929096408810161546L;
private final Logger LOGGER = WebAppLoggerFactory.getLogger(RVArticleTreeTable.class);
private TreeTable tree;
private RVDBManager rvdbManager;
private RVNewRequestController controller;
public RVArticleTreeTable(RVNewRequestController controller) {
super();
this.controller = controller;
try {
this.rvdbManager = RVMobileWebAppController.getRVDBManager();
} catch (SQLException e) {
WebAppNotifier.notify(e);
}
completeUI();
}
private void completeUI() {
tree = new TreeTable();
tree.setWidth("100%");
tree.setMultiSelect(true);
tree.addValueChangeListener(event -> {
Object value = event.getProperty().getValue();
if ( event.getProperty() == null || value == null) {
return;
}
if (value instanceof Collection){
List<Article> selectedArticles = new ArrayList<Article>((Collection<Article>) value);
if ( !validArticle(selectedArticles)) {
[color=#EE82EE]
return;
[/color]
}
try {
controller.setSelectedArticle(selectedArticles);
} catch (Exception e) {
WebAppNotifier.notify(e);
}
} else return;
});
tree.addGeneratedColumn("AGG", (source, itemId, columnId) -> {
if ( itemId instanceof ObjectType) {
return null;
}
Article a = (Article) itemId;
if (a.getIdType().getIdParent() == null) {
return null;
}
BigCheckBox b = new BigCheckBox();
b.setSplitPosition(0, true);
b.getCb().setSizeFull();
b.setWidth("60");
b.setHeight("60");
b.setIcon(FontAwesome.PLUS);
b.setValue(controller.isSelected(a));
b.addClickListener(event-> {
try {
if (b.booleanValue()) {
controller.addSelectedArticle(a);
} else {
controller.removeSelectArticle(a);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
return b;
});
tree.setColumnExpandRatio("CATEGORIE", 1);
tree.setColumnWidth("AGG", 60);
loadArticlesTree(tree);
setCaption("ORDINA DAGLI ARTICOLI DISPONIBILI");
setStyleName(DESIGN_ATTR_PLAIN_TEXT);
setSizeFull();
setResponsive(true);
Panel pTree = new Panel(tree);
pTree.addStyleName(ValoTheme.PANEL_BORDERLESS);
pTree.setSizeFull();
VerticalSplitPanel vl = new VerticalSplitPanel(buildSearchTF(), pTree);
vl.setSizeFull();
vl.setSplitPosition(59, Unit.PIXELS);
setContent(vl);
}
private boolean validArticle(List<Article> selectedArticles) {
for ( Object check : selectedArticles) {
if ( !(check instanceof Article)) {
return false;
}
}
return true;
}
private Object returnObjectArticle(List<Article> selectedArticles) {
for ( Object check : selectedArticles) {
if ( !(check instanceof Article)) {
return check;
}
}
return null;
}
/**
* @return
*/
private Component buildSearchTF() {
BigTextField tf = new BigTextField(true);
tf.setWidth("100%");
//tf.setHeight("70");
tf.setInputPrompt("Ricerca articoli");
tf.addStyleName(ValoTheme.TEXTFIELD_BORDERLESS);
tf.addTextChangeListener(event -> {
HierarchicalContainer treeContainer = (HierarchicalContainer) tree.getContainerDataSource();
treeContainer.removeAllContainerFilters();
if (event.getText().isEmpty()) {
return;
}
treeContainer.setIncludeParentsWhenFiltering(true);
// Create a new filter which ignores case and only matches String prefix
//SimpleStringFilter filter = new SimpleStringFilter("CATEGORIE", event.getText(), true, true);
// Add the new filter
treeContainer.addContainerFilter(new SimpleStringFilter("CATEGORIE", event.getText(), true, false));
});
VerticalLayout vl = new VerticalLayout(tf);
//vl.setMargin(true);
//vl.setSizeFull();
return vl;
}
/**
* @param articles
* @throws Exception
*/
private void loadArticlesTree(TreeTable treeToCompile) {
try {
LOGGER.info("Loading article tree...");
treeToCompile.addContainerProperty("CATEGORIE", String.class, "");
HierarchicalContainer hc = new HierarchicalContainer();
hc.addContainerProperty("CATEGORIE", String.class, "");
// load Parent Categories
List<ObjectType> objTypeCategory = rvdbManager.getArticleUtils().getMacroCategory();
for (ObjectType cat : objTypeCategory) {
//Create parent macro category ObjectType with children
Item categoryItem = hc.addItem(cat);
categoryItem.getItemProperty("CATEGORIE").setValue(cat.getName());
hc.setChildrenAllowed(cat, true);
//Create children article for this category
// String article = "Articolo 1 di " + cat.getName();
// hc.addItem(article);
// hc.setParent(article, cat);
for (Article article : rvdbManager.getArticleUtils().getArticlesByCategory(cat)) {
Item addItem = hc.addItem(article);
addItem.getItemProperty("CATEGORIE").setValue(article.getName());
hc.setParent(article, cat);
hc.setChildrenAllowed(article, false);
}
// select * from object_type o where o.idparent = cats.id;
// for (Article articleForCategory : rvdbManager.getObjectTypeUtils().get(cats.getName(), cats.getObjectTable(), //cats.getIdParent())) {
// completeCategory(articleForCategory, false, treeToCompile);
// }
//
// for (Article article : rvdbManager.getArticleUtils().getArticles()) {
// completeCategory(article, false, treeToCompile);
// }
} // end for each Article categories
treeToCompile.setContainerDataSource(hc);
LOGGER.info("Loaded article tree...");
//EXPAND ALL NOTES FOR CATEGORIES
// for (ObjectType cat : tree.get) {
// //treeToCompile.expandItemsRecursively(cat);
// }
// (item, false) TO EXPAND ALL ITEMS BY DEFAULT
// (item, true) TO COLLAPSE ALL ITEMS BY DEFAULT
for(Object item : tree.getItemIds().toArray()) tree.setCollapsed(item, true);
} catch (Exception e) {
LOGGER.info("Error loading article tree: {}", e.getCause(), e);
WebAppNotifier.notify(e);
}
}
protected void completeCategory(Article article, boolean haveChilds, Tree treeToCompile) {
Item addItem = treeToCompile.addItem(article);
addItem.getItemProperty("CATEGORIE").setValue(article.getName());
treeToCompile.setItemCaption(article, article.getName());
treeToCompile.setChildrenAllowed(article, haveChilds);
treeToCompile.expandItem(article); // Expand programmatically
}
}