I'm trying to display a tree of Categories, following the basic CellTree gwt examples.
What I am stuck at is determining the "leaf" condition of a Category.
A Category "is-a-leaf" when it hasn't children, right? So, here's my Category (I am using Objectify for appengine persistence):
#Entity
public class Categoria implements Serializable {
private static final long serialVersionUID = 1L;
#Id
Long id;
String nome;
Key<Categoria> parent;
public Categoria() { }
public Categoria(String nome) {
super();
this.nome = nome;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Key<Categoria> getParent() {
return parent;
}
public void setParent(Key<Categoria> parent) {
this.parent = parent;
}
}
My TreeViewModel is based on AsyncDataProvider (which I pass from outside):
public class CategorieTreeViewModel implements TreeViewModel {
private AbstractDataProvider<Categoria> dataProvider;
public CategorieTreeViewModel(AbstractDataProvider<Categoria> dataProvider) {
this.dataProvider = dataProvider;
}
#Override
public <T> NodeInfo<?> getNodeInfo(T value) {
return new DefaultNodeInfo<Categoria>(dataProvider, new CategoriaCell());
}
#Override
public boolean isLeaf(Object value) {
return false;
}
}
So here it is:
dataProvider = new AsyncDataProvider<Categoria>() {
#Override
protected void onRangeChanged(HasData<Categoria> display) {
updateTree();
}
};
private void updateTree() {
rpcService.getCategorie(new AsyncCallback<Categoria[]>() {
#Override
public void onSuccess(Categoria[] result) {
dataProvider.updateRowCount(result.length, true);
dataProvider.updateRowData(0, Arrays.asList(result));
}
#Override
public void onFailure(Throwable caught) {
Window.alert(caught.toString());
}
});
}
The question is: since I don't have a "leaf property" on my Category bean, how can I know if it has children or not? By doing a query obviously, but the isLeaf method needs to return synchronously, how can I make my rpc call?
Or I can retrieve that "leaf" information in the getCategorie() call, filling the property at runtime, but this could be a performance problem.
What can I do?
I would add a transient property to the Categoria class, a boolean isLeaf, then inside the setParent method, you could set the parent's isLeaf property to false (because if this object has a parent of that, then that object is not a leaf). Making the property transient means it won't be persisted, so you don't have to worry about having that field in your data model.
EDIT: Here is how I would code the Categoria class's setParent method...
public void setParent(Key<Categoria> parent) {
this.parent = parent;
parent.setIsLeaf(false);
}
That way, once you have built up your model of Categoria nodes, each one of them knows whether it is a leaf or not. This works because if parent has this as a child, parent can't possibly be a leaf. Default the isLeaf property to true and you'll know if any given Categoria is a leaf just by checking it's property.
Related
I have following class:
class TreeItem<T extends TreeItem<?>>{
private final ObservableList<T> childs;
private T parent;
public void addChild(T unit){
childs.add(unit);
unit.setParent(this);
}
public <T> void setParent(T parent){
this.parent = parent;
}
}
I get this message on setParent:
Incompatible types.
Required: T
Found: T
How can i fix this?
Rewrite this:
public <T> void setParent(T parent){
this.parent = parent;
}
With this:
public void setParent(T parent){
this.parent = parent;
}
Also I would suggest to remove the wildcare in the class name declaration, as the code would not compile.
So replace this:
class TreeItem<T extends TreeItem<?>>
With this:
class TreeItem<T extends TreeItem>
You have a final variable in your class. So it should be initialized either in constructor or inline. As long as you use generic variable and generic type is resolved when you instantiate a new object, the right way to do is to initialize it in constructor like this:
public TreeItem(ObservableList<T> childs) {
this.childs = childs;
}
When you are done with the above proposed changes you may notice that the compiler warns you with the message: Unchecked call to 'setParent(T)'. That means that the compiler does not guarantee the code is safe during runtime and possible heap pollution may occure.
I will illustrate it with an example. The following code while running ends up with ClassCastException as we set parent variable with the type which is not T (it is possible due to type erasure).
class ChildTreeItem<T extends TreeItem> extends TreeItem<T> {
public ChildTreeItem(ObservableList childs) {
super(childs);
}
}
public class TreeItem<T extends TreeItem>{
private final ObservableList<T> childs;
private T parent;
public TreeItem(ObservableList<T> childs) {
this.childs = childs;
}
public void addChild(T unit){
childs.add(unit);
unit.setParent(this);
}
public void setParent(T parent){
this.parent = parent;
}
public T getParent() {
return parent;
}
public static void main(String[] args) {
ChildTreeItem<ChildTreeItem> treeItem =
new ChildTreeItem<>(new ObservableSequentialListWrapper<>(new ArrayList<>()));
TreeItem<ChildTreeItem> parentItem =
new TreeItem<>(new ObservableSequentialListWrapper<>(new ArrayList<>()));
parentItem.addChild(treeItem);
List<ChildTreeItem> itemList = new ArrayList<>();
itemList.add(treeItem.getParent()); //<------------------- Heap pollution
ChildTreeItem childTreeItem = itemList.get(0); //<-------- ClassCastException
}
}
The possible solution to this problem is to not parametrize the variable parent but to make it TreeItem type:
public class TreeItem<T extends TreeItem>{
private final ObservableList<T> childs;
private TreeItem parent;
public TreeItem(ObservableList<T> childs) {
this.childs = childs;
}
public void addChild(T unit){
childs.add(unit);
unit.setParent(this);
}
public void setParent(TreeItem parent){
this.parent = parent;
}
public TreeItem getParent() {
return parent;
}
}
Hope this helps.
Since you have define already in your class level of Generic type. It is not clear why you put before your setParent function. But let's assume you want to define a generic type in function level, then when you call it, you need to specify the type
Ex:
TreeItem<String> treeItem = new TreeItem<String>();
treeItem.<String>setParent("something");
I'm creating simple JavaFX application. I want my model layer to be completely independent from JavaFX - no StringProperty, IntegerProperty and etc. as fields. I want it to be POJO. Main reason to do so is that I want it to be Serializable.
I've created DataRepository - simple CRUD-like interface and some implementations of it, so I can at anytime change where I store my data - XML file, SQLite database or anything else. I also have to somehow connect my data storage with JavaFX (to display its content in TableView), so I decided to create my implementation of ObservableList which wraps my repository. My question is - is there any other way? ObservableList contains about 30 methods to implement and it looks like I'm doing something wrong.
My (simplified) model:
public class Movie implements Serializable {
private String title;
private String director;
public Movie() {
}
public Movie(String title, String director) {
this.title = title;
this.director = director;
}
// Getters and setters, equals etc...
}
MovieRepository:
public interface MovieRepository {
public void add(Movie movie);
public void remove(String title);
public void remove(int index);
public Movie get(String title);
public Movie get(int index);
public List<Movie> getAll();
}
Controller for my main view:
public class MainController {
#FXML
private TableView<Movie> movieTable;
#FXML
private TableColumn<Movie, String> movieTitleColumn;
#FXML
private Label titleLabel;
private MovieRepository movies = new DBMovieRepository(); //MovieRepository implementation which uses SQLite DB to store data
private MainApp app;
#FXML
private void initialize() {
movieTable.setItems(new ObservableMovies(movies));
// ObservableMovies is my implementation of ObservableList
// It basically wraps methods from MovieRepository
// and notifies listeners
showMovieDetails(null);
movieTitleColumn.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getTitle()));
movieTable.getSelectionModel().selectedItemProperty()
.addListener((observable, oldValue, newValue) -> showMovieDetails(newValue));
}
private void showMovieDetails(Movie movie) {
if(movie != null) {
titleLabel.setText(movie.getTitle());
} else {
titleLabel.setText("");
}
}
#FXML
private void handleNew() {
Movie movie = new Movie();
app.showNewMovieDialog(movie);
movieTable.getItems().add(movie);
}
public void setApp(MainApp app) {
this.app = app;
}
}
You have a couple of options here (maybe more), which are covered in other questions on this site. However, for convenience, I'll summarize them here too.
1. Use JavaFX Properties and make the class Serializable
You can do this with a custom serialized form. Make the JavaFX properties transient and implement readObject and writeObject to store the values they wrap:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Objects;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Movie implements Serializable {
private transient StringProperty title = new SimpleStringProperty();
private transient StringProperty director = new SimpleStringProperty();
public Movie() {
}
public Movie(String title, String director) {
setTitle(title);
setDirector(director);
}
#Override
public int hashCode() {
return Objects.hash(getDirector(), getTitle());
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Movie other = (Movie) obj;
return Objects.equals(getTitle(), other.getTitle())
&& Objects.equals(getDirector(), other.getDirector());
}
public final StringProperty titleProperty() {
return this.title;
}
public final String getTitle() {
return this.titleProperty().get();
}
public final void setTitle(final String title) {
this.titleProperty().set(title);
}
public final StringProperty directorProperty() {
return this.director;
}
public final String getDirector() {
return this.directorProperty().get();
}
public final void setDirector(final String director) {
this.directorProperty().set(director);
}
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
s.defaultReadObject();
title = new SimpleStringProperty((String) s.readObject());
director = new SimpleStringProperty((String) s.readObject());
}
private void writeObject(ObjectOutputStream s) throws IOException {
s.defaultWriteObject();
s.writeObject(getTitle());
s.writeObject(getDirector());
}
}
2. Use a POJO with "bound properties".
See JavaBean wrapping with JavaFX Properties for details. In brief:
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class Movie {
private String title ;
private String director ;
private final PropertyChangeSupport propertySupport ;
public Movie(String title, String director) {
this.title = title ;
this.director = director ;
this.propertySupport = new PropertyChangeSupport(this);
}
public Movie() {
this("", "");
}
public String getTitle() {
return title ;
}
public String setTitle(String title) {
String oldTitle = this.title ;
this.title = title ;
propertySupport.firePropertyChange("title", oldTitle, title);
}
// similarly for director...
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertySupport.addPropertyChangeListener(listener);
}
// hashCode and equals...
}
For wanting to wrap your repository as an observable list, instead wrap it with a repository implementation that uses an observable list:
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class ObservableMovieRepository implements MovieRepository {
private final MovieRepository repository ;
private final ObservableList<Movie> movieList;
public ObservableMovieRepository(MovieRepository repository) {
this.repository = repository ;
this.movieList = FXCollections.observableArrayList(repository.getAll());
}
#Override
public void add(Movie movie) {
repository.add(movie);
movieList.add(movie);
}
#Override
public void remove(String title) {
Movie movie = get(title);
repository.remove(title);
movieList.remove(title);
}
#Override
public void remove(int index) {
repository.remove(index);
movieList.remove(index);
}
#Override
public Movie get(String title) {
return repository.get(title);
}
#Override
public Movie get(int index) {
return movieList.get(index);
}
#Override
public ObservableList<Movie> getAll() {
return movieList ;
}
}
This uses the standard ObservableList implementation that copies an existing list on creation, and the implementation keeps that list in sync with the list in the wrapped repository. Now your UI code can do
ObservableMovieRepository movies = new ObservableMovieRepository(new DBMovieRepository());
// ...
movieTable.setItems(movies.getAll());
With the Movie class above, you would just do
movieTitleColumn.setCellValueFactory(cellData -> cellData.getValue().titleProperty());
If you use the POJO version you can do
movieTitleColumn.setCellValueFactory(cellData -> {
try {
return new JavaBeanStringPropertyBuilder()
.bean(cellData.getValue())
.name("title")
.build();
} catch (Exception e) { throw new RuntimeException(e); }
}
There seem to be multiple question in here, so I'm not really sure, if I understood you correctly, but I will try to split it up a bit.
I want my model layer to be completely independent from JavaFX - no
StringProperty, IntegerProperty and etc. as fields. I want it to be
POJO.
You could mark your properties as transient. Then you just need to wrap them around your values and it will be both JavaFX compliant and Serializable. You just have to propagate changes back to your backing attributes.
I also have to somehow connect my data storage with JavaFX (to display
its content in TableView), so I decided to create my implementation of
ObservableList which wraps my repository. My question is - is there
any other way?
Very limited information on this and I really don't know, why you would need to create your own implementation of ObservableList, but to keep it POJO, you could maintain plain java.util.Collections in your bean and provide transient ObservableLists, which you can create on creation by wrapping your java.util.Lists in your POJO. You can find those methods in the FXCollections utility class.
ObservableList contains about 30 methods to implement and it looks
like I'm doing something wrong.
If you really need to implement it, you can inherit from ObservableListBase.
Let's assume I have two objects, both created through the builder pattern and one is nested into other:
class Parent {
private final Child child;
private Parent(Child child) {
this.child = child;
}
public static class Builder {
private Child child;
public Builder() {}
public Builder child(Child child) {
this.child = child;
return this;
}
public Parent build() {
return new Parent(child);
}
}
}
class Child {
private final long id;
private Child(Builder builder) {
this.id = builder.id;
}
public static class Builder {
private long id;
public Builder() {}
public Builder id(long id) {
this.id = id;
return this;
}
public Parent build() {
return new Child(this);
}
}
}
So, the obvious usage is quite simple:
Person.Builder parentBuilder = new Person.Builder().child(new Child.Builder().id(10).build());
Is it quite common to make
public static class Builder {
private ChildBuilder child;
public Builder() {}
public Builder child(ChildBuilder child) {
this.child = child;
return this;
}
public Builder resetChildId() {
child.id(0);
return this;
}
public Parent build() {
Child childToPass = child.build();
return new Parent(childToPass);
}
}
That way it is still possible to update the child#id later, however due to late binding the errors are thrown lately during Parent.Builder#build() method.
I would pass a Child instance to Parent rather than a ChildBuilder instance.
If you wish to change Child properties afterwards then you can simply construct a new ChildBuilder from parentBuilder.child().
However, I'm concerned about the design when I see all those builders. DDD is all about the ubiquitous language and "builder" is certainly not part of it. Sometimes you have no choice to introduce technical concepts in the design, but I believe that you may be forgetting about other DDD building blocks that may help.
I have builders everywhere because I have to do validation for each
domain entity in the app. For example name for Parent not longer than
255, but for child not more than 1000. - Tahar Bakir (from the comments)
The rules you describe above may be encapsulated and enforce upon construction in domain concepts such as ParentName and ChildName that can be implemented as value objects.
Your Parent and Child classes can then work with those concepts rather than strings.
Hope this helps
the example on how to use it is in the main method, this will print
10
0
The parent class:
public class Parent {
private final Child child;
private Parent(Child child) {
this.child = child;
}
public Child getChild(){
return this.child;
}
public static class Builder {
private Child.Builder childBuilder;
public Builder() {}
public Builder child(Child.Builder childBuilder) {
this.childBuilder = childBuilder;
return this;
}
public void resetChildId() {
childBuilder = childBuilder.id(0);
}
public Parent build() {
return new Parent(childBuilder.build());
}
}
public static void main (String[] args){
Parent.Builder parentBuilder = new Parent.Builder().child(new Child.Builder().id(10));
System.out.println(parentBuilder.build().getChild().getId());
//Reset the sucker
parentBuilder.resetChildId();
System.out.println(parentBuilder.build().getChild().getId());
}
}
The child class:
class Child {
private final long id;
private Child(Builder builder) {
this.id = builder.id;
}
public long getId(){
return this.id;
}
public static class Builder {
private long id;
public Builder() {}
public Builder id(long id) {
this.id = id;
return this;
}
public Child build() {
return new Child(this);
}
}
}
I am currently creatin a TreeView where leaf elements should be checkable.
I created the sampleTreeView from the eclipse plugin that comes with a predefined Tree structure.
public class TreeObject {
private String name;
private TreeParent parent;
public TreeObject(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setParent(TreeParent parent) {
this.parent = parent;
}
public TreeParent getParent() {
return parent;
}
public String toString() {
return getName();
}
public Object getAdapter(Class<?> key) {
return null;
}
}
public class TreeParent extends TreeObject{
private ArrayList<TreeObject> children;
public TreeParent(String name) {
super(name);
children = new ArrayList<TreeObject>();
}
public void addChild(TreeObject child) {
children.add(child);
child.setParent(this);
}
public void removeChild(TreeObject child) {
children.remove(child);
child.setParent(null);
}
public TreeObject [] getChildren() {
return (TreeObject [])children.toArray(new TreeObject[children.size()]);
}
public boolean hasChildren() {
return children.size()>0;
}
}
I then found the following tutorial. He is using the TreeItem Object where it is easy to attach a Image. Can I somehow Copy this function or do I have to use the TreeItem object as my data structure?
The tutorial says at the beginning of part 2 that you should use a ContentProvider and LabelProvider for the TreeViewer rather than use TreeItem, and that is what you should do.
The getImage method of the label provider would return the checked / unchecked /null image.
When you need to change an image call TreeViewer.update or TreeViewer.refresh if the children of the object also need refreshing. This will call the label provider again.
A FileManager Class has a static filed to hold a file collection, this collection may contains files or folders or both , a folder may contains files or folders or both, the FileManager Class contains public method for client code to call such as addFile, addFolder, deleteFile, deleteFolder, these method operate on the collection. My question is:
What java data structure is best for this case ?
How to create model class for File and Folder ?
some example will be good.
best regars.
// added # 2011/05/27
thank everybody !
acutaly I am trying to buidl a eclipse-rcp application to manage some jdbc connection profile.
here is my code:
package com.amarsoft.sysconfig.plugin.model;
/**
* #author ggfan#amarsoft
*
*/
public class TreeNode {
/**
* unique key
*/
private String key;
/**
* used as label in a JFace TreeViewer,
*/
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setKey(String key) {
this.key = key;
}
public String getKey() {
return key;
}
}
public class LeafNode extends TreeNode {
private FolderNode parent;
public void setParent(FolderNode parent) {
this.parent = parent;
}
public TreeNode getParent() {
return parent;
}
}
package com.amarsoft.sysconfig.plugin.model;
import java.util.List;
public class FolderNode extends TreeNode {
private List<TreeNode> children;
public void setChildren(List<TreeNode> children) {
this.children = children;
}
public List<TreeNode> getChildren() {
return children;
}
}
package com.amarsoft.sysconfig.plugin.model;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;
/**
* 连接配置模型类
* #author ggfan#amarsoft
*
*/
public class ConnectionProfile extends LeafNode{
/**
* url
*/
private String url;
/**
* JDBC driver id
*/
private int driver;
/**
* user name for logon
*/
private String user;
/**
* password for logon
*/
private String pswd;
/**
* default constructor
*/
public ConnectionProfile() {
}
/**
* construct a instance using a XML element
* #param xmlElement the XML element
*/
public ConnectionProfile(Element xmlElement){
this.setName(xmlElement.attributeValue("name"));
this.setUrl(xmlElement.element("url").getTextTrim());
this.setUser(xmlElement.element("user").getTextTrim());
this.setPswd(xmlElement.element("password").getTextTrim());
}
/**
* serialize as XML
* #return
*/
public Element asXML(){
Element e = new DefaultElement("profile");
e.addAttribute("name", this.getName());
e.addElement("url", escapeNull(this.getUrl()));
e.addElement("user", escapeNull(this.getUser()));
e.addElement("password", escapeNull(this.getPswd()));
return e;
}
private String escapeNull(String s) {
return s == null ? "" : s;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPswd() {
return pswd;
}
public void setPswd(String pswd) {
this.pswd = pswd;
}
public void setDriver(int driver) {
this.driver = driver;
}
public int getDriver() {
return driver;
}
}
public class ConnectionProfileManager {
private static List<TreeNode> profiles = new ArrayList<TreeNode>();
public static void loadProfiles() throws DocumentException{
Element profiles = XMLUtil.readRoot(ConnectionProfileManager.class.getResourceAsStream("samples_profile.xml"));
//Element profiles = XMLUtil.readRoot(new File(ApplicationFiles.CONNNECTION_PROFILES));
if(profiles != null){
for(Element profile : profiles.elements()){
loadNode(profile, ConnectionProfileManager.profiles);
}
}
}
private static void loadNode(Element node, List<TreeNode> parent){
if(node.getName().equals(XMLConstants.CP_TAG_PROFILE)){
ConnectionProfile profile = new ConnectionProfile(node);
parent.add(profile);
}else if(node.getName().equals(XMLConstants.CP_TAG_FOLDER)){
FolderNode folder = new FolderNode();
folder.setChildren(new ArrayList<TreeNode>());
folder.setName(node.attributeValue(XMLConstants.CP_ATTR_NAME));
for(Element child : node.elements()){
loadNode(child, folder.getChildren());
}
parent.add(folder);
}
}
public static void saveProfiles(){
Element root = new DefaultElement(XMLConstants.CP_TAG_PROFILES);
for(TreeNode node : ConnectionProfileManager.profiles){
saveNode(node, root);
}
XMLUtil.save(root, new File("c:\\1.xml"));
}
private static void saveNode(TreeNode node, Element root) {
if(node instanceof ConnectionProfile){
ConnectionProfile p = (ConnectionProfile)node;
root.add(p.asXML());
}else if(node instanceof FolderNode){
FolderNode folder = (FolderNode)node;
Element e = new DefaultElement(XMLConstants.CP_TAG_FOLDER);
e.addAttribute(XMLConstants.CP_ATTR_NAME, node.getName());
for(TreeNode child : folder.getChildren()){
saveNode(child, e);
}
root.add(e);
}
}
public static void addProfile(ConnectionProfile profile){
profiles.add(profile);
}
public static void addProfile(TreeNode parentNode, ConnectionProfile profile){
}
public static List<TreeNode> getProfiles() {
return profiles;
}
}
with these class I get my tree works, but I found It's hard to support add operation.
You've kinda of dictacted the answer already in the question..
The File class is (according to the JavaDocs) an:
abstract representation of file and directory pathnames.
So from what you've described:
// A file manager class
class FileManager {
// has a static field to hold a file collection
static Collection<File> fileCollection;
// contains public methods such as
public addFile(File f) { }
public deleteFile(File f) { }
public addFolder(File f) { }
public deleteFolder(File f { }
}
If you have to look at implementing your own version of the File class, then the JavaDocs for that should be a good start to understanding this.
AS to what collection is best for the file collection, I think a Set makes most sense. There's no point having more than one file (e.g. a List and two entries of the same file would be meaningless), and testing membership of a set is a very quick operation. For example, in addFile you might check it exists before trying to add, and similarly for delete you'd want to make sure that it exists before you delete it.
A couple of points about the design you've mentioned.
Static fields like this as nasty. They make it difficult to test and are a pain for multi-threading. Could you make it an instance variable?
Given that File is an abstract representation of a path name, why'd you need the method addFile and addFolder, they are going to have the same implementation?
A Collection of Files?
Most collections support add and delete, so there is no need for a special data structure. Java File can be a file and a directory. Simply call isDirectory to find out if it is a directory or a file.
Unless you have more requirements, I think this would make up a pretty easy to use FileManager:
List<File> fileManager = new ArrayList<File>();