Advice to get selected elements from CheckedTreeSelectionDialog - java

I'm using CheckedTreeSelectionDialog to implement some kind of refactoring. The refactoring is performed over a large set of objets, so each root node of the selection tree is a objet, and each of those objects has a suggested modification as a child node. For example,
CheckedTreeSelectionDialog:
ObjectA
---------- Remove attribute attA1
---------- Remove attribute attA2
Object B
---------- Remove attribute attB1
.
.
.
I obtain the selected elementes this way:
Object[] result = dialog.getResult();
and, if I select all those 5 elements showed before, I will get the list:
ObjectA
attA1
attA2
ObjectB
attB1
I thought I would get some kind of tree, for example, where I can get the object "ObjectA" and see which of its childs where selected.
Am I doing this right?
Thanks!

Alternatively you can get the tree viewer and from that get the checked elements.
Map<Object, List<Object>> mapOfCheckedElements = new HashMap<Object, List<Object>>();
for (TreeItem level1 : checkBoxTreeViewer.getTree().getItems()) {
if (level1.getChecked()) {
List<Object> checkedChildren = new ArrayList<Object>();
for (TreeItem level2 : level1.getItems()) {
if (level2.getChecked()) {
checkedChildren.add(level2);
}
}
mapOfCheckedElements.put(level1, checkedChildren);
}
}

Related

Create list of an element of an Object from another list of Objects

The model:
public class MyModel{
private int id;
private String name;
....
....
//getters and setters
}
I have a list of MyModel object:
//First Object to be added to list
MyModel myModelObject1 = new MyModel();
myModelObject1.setId(1);
myModelObject1.setName("abc");
//Second Object to be added to list
MyModel myModelObject2 = new MyModel();
myModelObject1.setId(2);
myModelObject1.setName("pqr");
List<MyModel> myModelList = new ArrayList<MyModel>();
myModelList.add(myModelObject1);
myModelList.add(myModelObject2);
I want to get a list of names present in the MyModel List i.e. I want to create a list of names (List<String> in this case) from myModelList. So, I want my list to have:
{"abc", "pqr"}
There is always a way to iterate and create another list but is there any better way to do that? (Not necessarily to be efficient but if it can be done in a line using streams, foreach e.t.c.).
EDIT:
The answers worked for me but I have some modifications in my use case: If I want to add a condition that only name which contains character 'a' should be added to the list and I want to add a logger message to debug for each element then how should I approach this?
I tried doing the following using a method (charAPresent()) which checks that if the passed String contains character 'a' but it didn't work:
List<String> modelNameList = myModelList.stream()
.map(model -> {
if (charAPresent(model.getName)) {
model.getName()
}
})
.collect(Collectors.toList());
Let me know if I am missing something.
Using Java 8 Stream:
List<String> modelNameList = myModelList.stream()
.map(Model::getName)
.collect(Collectors.toList());
Model::getName is called as method reference. It equivalents to model -> model.getName()
You can use streams and map your object to its name and collect to a list as:
List<String> names = myModelList.stream()
.map(MyModel::getName)
.collect(Collectors.toList());
There is always a way to iterate and create another list but is there
any better way to do that
Even with the use of streams, you would have to iterate the complete collection. There is no better way of doing it than iterating the complete collection.
You can use Stream.map() to get only the names of your model. Use Stream.filter() to get only the names matching your charAPresent() method. To log the entries before collecting you can use Stream.peek():
List<String> modelNameList = myModelList.stream()
.map(Model::getName) // map only the name
.filter(YourClass::charAPresent) // filter the items
.peek(System.out::println) // print all items which are in the filter
.collect(Collectors.toList());
You can also use foreach like this:
public static List<String> modelNames(List<MyModel> myModelList)
List<String> list = new ArrayList<String>();
for(MyModel mm : myModelList) {
if(mm.getName().contains("a") {
list.add(mm.getName());
}
}
return list;
}

Display duplicate attribute values once in Java object list

I have a list of java objects as below
I want to convert it to a list with 'Camera flash faulty' and 'Camera Housing Damaged' only once for the first object, like the one below.
Is there something that can be done with the solution mentioned here ?
Remove duplicates from a list of objects based on property in Java 8
You can add all the properties to a Set (Sets don't allow duplicates), if the value is already in the set, the method add returns false, so you can set to empty the property in the object:
Set<String> values = new HashSet<>();
for (MyObject obj : myList) {
if (!values.add(obj.getValue())) {
obj.setValue("");
}
}
Another alternative is to group all object with the same attribute value, then skip the first element of each group and set the attribute value to empty for all other objects in the group:
myList.stream()
.collect(Collectors.groupingBy(MyObject::getValue))
.forEach((k, v) -> v.stream().skip(1).forEach(o->o.setValue("")));
I am taking an example of Employee which have some duplicate id's.
Lets say you have a list as
List<Employee> employee
Add them into HashSet, which does not allow duplicates.
HashSet<Object> seen=new HashSet<>();
employee.removeIf(e->!seen.add(e.getID()));

How to combine two List PropertyModels to one model in Wicket

Let's say I have an entity called E.
Its structure has two fields:
E {
List<A> a1;
List<A> a2;
}
Currently I have two PropertyModels like this:
PropertyModel<A> a1Model = new PropertyModel(e, "a1");
PropertyModel<A> a2Model = new PropertyModel(e, "a2");
I have one repeater, and I want to give it one listModel only.
My current way to achieve this is to make like this:
List<A> aItems = new ArrayList<A>();
aItems.addAll(a1Model.getObject());
aItems.addAll(a2Model.getObject());
PropertyModel<A> aModel = new PropertyModel<A>(this, "aItems");
I now miss connection to database, since e is actually given inside a LoadableDetachableModel. When e changes inside that model, the value does not automatically flow until the field.
I am not sure does it make it now either, but somehow interfering the play with models only smells bad to my nose. Now I have only list a1 in field, and now I am adding a2.
How to achieve one model for the field (it is actually another panel)?
You might create a Model that combines both models to List<E>:
IModel<List<E>> listModel = new LoadableDetachableModel<List<E>>() {
public List<E> load() {
return Arrays.asList(a1Model.getObject(), a2Model.getObject());
}
}

Neo4j How to create relationships after ingesting data

Say I have a csv file where one of the columns is unix timestamp. It is not sorted on that, but following those in order could be a useful relationship. When I want that, I could use ORDER BY, but adding relationship pointers should be faster right, and make use of the NOSQL? Do I have to sort this and add the relationship as I ingest, or can I do it after a query?
After I run the first query and get a subset back:
result = engine.execute(START...WHERE...ORDER BY time)
Can I then go through results adding this relationship like:
prev.createRelationshipTo(next, PRECEDES);
I tried two different ways using foreach or iterator and both had runtime errors casting a string to a Node:
for (Map<String, Object>row : result) {
String n = (String) row.values().iterator().next();
System.out.println(n);
}
Iterator<Node> nodes = result.columnAs("n.chosen");
if (nodes.hasNext()) {
Node prev = nodes.next();
while (nodes.hasNext()) {
Node n = nodes.next();
prev.createRelationshipTo(n, null);
prev = n;
}
}
Also, there is the edge case of two rows having the same timestamp. I don't care about the order that is chosen, but I want it to not break the relation chain.

Traverse ecore model

I have generated model code from Ecore.
Within my model I have a derived reference: derivedThings.
What I want to do in the derivedThingsImpl is the following:
I want to traverse the whole model and depending on the element, I want to add it to a collection or not.
for(TreeIterator iter = EcoreUtil.getAllContents(rootObject); iter.hasNext();)
...
The problem is, how can I access the root object from the derivedThingsImpl ?! is there something like getRootObject() ?!
Thanks
UPDATE:
EObject e = this;
while(e.eContainer() != null) {
e = e.eContainer()
if (e instanceof RootElement)
break;
}
// No I should have the root element. Is this a good and clean way ?!
There is a better way of doing it:
EcoreUtil.getRootContainer(eObject);

Categories