Skip to content

Instantly share code, notes, and snippets.

@KarlHerler
Created September 22, 2011 07:31
Show Gist options
  • Save KarlHerler/1234245 to your computer and use it in GitHub Desktop.
Save KarlHerler/1234245 to your computer and use it in GitHub Desktop.
Systems design task1 tree relevant bits
//in Album.java
String name; //The name of the album
private Set<Photo> photos; //The actual photos
private Album parent; //The parent of this Album
private Set<Album> children; //The child nodes for this Album
Album(String n, Album p) {
this.name = n;
this.photos = new HashSet<Photo>();
this.children = new HashSet<Album>();
this.setParent(p);
}
public void remove() {
parent.children.remove(this);
}
public Set<Photo> getPhotos() {
Set<Photo> returner = new HashSet<Photo>();
returner.addAll(photos);
for (Album child : children) { returner.addAll(child.getPhotos()); }
return returner;
}
public void removePhotos(Set<Photo> p) {
photos.removeAll(p);
for (Album child : children) { child.removePhotos(p); }
}
public void addChild(Album a) { children.add(a);}
public void setParent(Album a) {
if (a!=null) {
parent = a;
a.addChild(this);
}
}
/*
*
* In PhotoOrganizer.java
*
*/
private Album root = new Album("All Photos", null); //Variable referring to root album
/* add album */
Album newAlbum = new Album(newAlbumName, (Album)getSelectedTreeNode().getUserObject());
newAlbum.addPhotos(previewPane.getSelectedPhotos());
DefaultMutableTreeNode trnode= new DefaultMutableTreeNode(newAlbumName);
trnode.setUserObject(newAlbum);
/* delete album */
System.out.println("delete album " + getSelectedTreeNode());
((Album) getSelectedTreeNode().getUserObject()).remove();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment