fable.adt
Class FableComponent

java.lang.Object
  extended by fable.adt.FableComponent
All Implemented Interfaces:
FableContainer
Direct Known Subclasses:
Article, Feed, Folder

public abstract class FableComponent
extends java.lang.Object
implements FableContainer

This class is a basic type that all components in the ADT will extend. This type supports common behavior among Article, Feed, and Folder. Also, this type enables basic tree structure operations.

Author:
Wonsik Kim

Constructor Summary
protected FableComponent()
          Constructs a new FableComponent object.
protected FableComponent(java.util.Date date)
          Contstructs a new FableComponent with provided UID and date.
 
Method Summary
abstract  void accept(FableVisitor v)
          Accept the visitor v to visit this.
abstract  void acceptRecursive(FableVisitor v)
          Accept the visitor v to visit this, and recurse on children of this.
 boolean activate()
          Activates it to the GUI
 boolean addChild(FableComponent fc)
          Add c as a child of this (optional operation).
 void addStructuralChangeListener(StructuralChangeListener scl)
          Add a listener for structural change of ADT to this.
 java.util.List<FableComponent> getChildren()
          Returns a list of children of this.
 java.util.Date getDate()
          Returns the creation date of this.
 java.util.List<FableComponent> getDescendants()
          Returns a list of descendants of this.
 FableComponent getParent()
          Returns the parent of this.
 int getPosition()
          Returns a position of this FableComponent in its parent's list of children.
 java.lang.String getTitle()
          Returns the title of this.
 boolean isAncestorOf(FableComponent fc)
          Checks if this component is an Ancestor of a given component
abstract  boolean isSimilarTo(FableComponent fc)
          Returns true if this and fc is similar.
 boolean moveDown()
          Increase an index of this in the parent's list of children.
 boolean moveUp()
          Decrease an index of this in the parent's list of children.
 void removeAllListeners()
          Removes all the listeners associated with this FableComponent.
 boolean removeChild(FableComponent fc)
          Remove c from the children of this.
 void removeStructuralChangeListener(StructuralChangeListener scl)
          Remove a listener for structural change of ADT from this.
 boolean setParent(FableComponent fc)
          Set the parent of this as provided FableComponent (optional operation).
 void setPosition(int index)
          Set its position in the parent's list of children as index.
 void setTitle(java.lang.String t)
          Set the title of this to the string t.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FableComponent

protected FableComponent()
Constructs a new FableComponent object.

Effects:
construct a new FableComponent
Modifies:
this

FableComponent

protected FableComponent(java.util.Date date)
Contstructs a new FableComponent with provided UID and date.

Throws:
java.lang.NullPointerException - if date is null.
Effects:
construct a new FableComponent
Modifies:
this
Method Detail

activate

public boolean activate()
Activates it to the GUI

Returns:
true if it can be activated, false if it is already activated

isSimilarTo

public abstract boolean isSimilarTo(FableComponent fc)
Returns true if this and fc is similar. By similar, we mean that they are similar enough to disallow those two similar components to share the same parent.

Parameters:
fc - FableComponent object to compare.
Returns:
true if this and fc is similar.

getChildren

public final java.util.List<FableComponent> getChildren()
Description copied from interface: FableContainer
Returns a list of children of this. The order is not particularly meaningful.

Specified by:
getChildren in interface FableContainer
Returns:
A list of children of this.

getDescendants

public final java.util.List<FableComponent> getDescendants()
Returns a list of descendants of this. The order is not particularly meaningful. Returning list excludes this.

Returns:
A list of descendants of this.

getDate

public final java.util.Date getDate()
Returns the creation date of this.

Returns:
the creation date of this.

getTitle

public final java.lang.String getTitle()
Returns the title of this.

Returns:
the title of this.

getParent

public final FableComponent getParent()
Returns the parent of this.

Returns:
the parent of this.

getPosition

public final int getPosition()
Returns a position of this FableComponent in its parent's list of children. For example, if this FableComponent is the third children of the parent, this method will return 2. If this FableComponent has no parent, it will return -1.

Returns:
a position of this FableComponent in its parent's list of children. -1 if this has no parent.

setTitle

public void setTitle(java.lang.String t)
Set the title of this to the string t.

Parameters:
t - a string to be the title of this.
Throws:
java.lang.UnsupportedOperationException - if setTitle method is not supported by this. (e.g. Article)
java.lang.NullPointerException - if t is null.
Effects:
Set the title fo this to the string t.
Modifies:
this.title

setPosition

public final void setPosition(int index)
Set its position in the parent's list of children as index. This method will not affect order of the rest children of the parent. For example, if the parent had five children, this is the second child, and setPosition( 3 ) is called, this will be the forth child of the parent, and third and forth children will be second and third children respectively.

When provided with negative index or index equal to or greater than the number of children of the parent, it will throw IndexOutOfBoundException.

Parameters:
index -
Throws:
java.lang.IllegalArgumentException - if index less than 0 or index equals to or greater than number of children of the parent.
Effects:
rearrange the order of children of the parent.
Modifies:
this.parent.children

setParent

public final boolean setParent(FableComponent fc)
Set the parent of this as provided FableComponent (optional operation). If fc does not support addChild( FableComponent ) method it will return false and structure of tree will not change.

Also, when this is one of the ancestors of fc, it will return false and the structure of tree won't change.

Parameters:
fc - a FableComponent to be a new parent of this.
Returns:
true if parent of this is successfully changed to fc. false otherwise.
Effects:
set the parent of this, and make this as the parent's child.
Modifies:
this.parent, fc.children

isAncestorOf

public final boolean isAncestorOf(FableComponent fc)
Checks if this component is an Ancestor of a given component

Parameters:
fc - FableComponent to check if this particular component is ancestor of

addChild

public boolean addChild(FableComponent fc)
Description copied from interface: FableContainer
Add c as a child of this (optional operation). Returns true if c is successfully added as a child of this, otherwise false.

FableContainers inheriting this method can impose their own restrictions. For example, Feed may accept only Article as a child. This restrictions should be clearly documented in the specification.

One extreme example of restriction is Article, which bounces any FableComponents.

Specified by:
addChild in interface FableContainer
Parameters:
fc - a child to be added.
Returns:
true if c is succesfully added. false otherwise.

addStructuralChangeListener

public final void addStructuralChangeListener(StructuralChangeListener scl)
Add a listener for structural change of ADT to this.

Parameters:
scl - a listener to be added to this.
Throws:
java.lang.NullPointerException - if scl is null.
Effects:
add a listener for structural change of ADT to this.
Modifies:
this.listeners

removeChild

public final boolean removeChild(FableComponent fc)
Description copied from interface: FableContainer
Remove c from the children of this. When c is not a children of this, it does nothing and return false. Otherwise, it will return true.

Specified by:
removeChild in interface FableContainer
Parameters:
fc - A child to remove from this.
Returns:
true if c is a child of this and successfully removed. false otherwise.

removeStructuralChangeListener

public final void removeStructuralChangeListener(StructuralChangeListener scl)
Remove a listener for structural change of ADT from this.

Parameters:
scl - a listener to be removed from this.
Throws:
java.lang.NullPointerException - if scl is null.
Effects:
remove a listener for structural change of ADT from this.
Modifies:
this.listeners

removeAllListeners

public void removeAllListeners()
Removes all the listeners associated with this FableComponent. Notice that if an inheriting class added new kind of listeners, the class should inherit this method and remove that listeners itself, and should call super.removeAllListeners().

Effects:
remove all listeners associated with this FableComponent.
Modifies:
this.listeners

accept

public abstract void accept(FableVisitor v)
Accept the visitor v to visit this.

Parameters:
v - the visitor to visit this.
Throws:
java.lang.NullPointerException - if v is null.

acceptRecursive

public abstract void acceptRecursive(FableVisitor v)
Accept the visitor v to visit this, and recurse on children of this.

Parameters:
v - the visitor to visit this and decendants of this.
Throws:
java.lang.NullPointerException - if v is null.

moveUp

public final boolean moveUp()
Decrease an index of this in the parent's list of children. This method will not affect order of the rest children of the parent. For example, if the parent had five children, this is the second child, and moveUp() is called, this will be the first child of the parent, and the first child will be the second child.

Returns true if it successfully decreased index of this. It returns false otherwise (e.g. index is equal to or less than 0).

Returns:
true if it successfully decreased index of this. false otherwise.
Effects:
set the position of this to decrease by 1.
Modifies:
this.position, this.parent.children

moveDown

public final boolean moveDown()
Increase an index of this in the parent's list of children. This method will not affect order of the rest children of the parent. For example, if the parent had five children, this is the second child, and moveDown() is called, this will be the third child of the parent, and the third child will be the second child.

Returns true if it successfully increased index of this. It returns false otherwise (e.g. index is equal to or greater than number of children of the parent - 1).

Returns:
true if it successfully increased index of this. false otherwise.
Effects:
set the position of this to increase by 1.
Modifies:
this.position, this.parent.children