com.eliad.model.defaults
Class BasicRulerModel

java.lang.Object
  |
  +--com.eliad.model.AbstractGenericModel
        |
        +--com.eliad.model.AbstractRulerModel
              |
              +--com.eliad.model.defaults.BasicRulerModel
Direct Known Subclasses:
DefaultRulerModel

public class BasicRulerModel
extends AbstractRulerModel

A concrete implementation of the RulerModel interface based on the AbstractRulerModel helper class. The setPreferredSize(int, int), setMinSize(int, int) setMaxSize(int, int) methods are implemented as wrappers (providing general consistency) over protected methods doSetPreferredSize(int, int), doSetMinSize(int, int), and doSetMaxSize(int, int), but those are implemented as no-ops. getPreferredSize(int) is implemented here to return the same result as getSize(int), while getMinSize(int), getMaxSize(int) return fixed values. getTotalPreferredSize(), getTotalMinSize(), and getTotalMaxSize() are implemented efficiently by taking advantage of this.

The subclass DefaultRulerModel fully implements the API, and is the one used by the JSmartGrid constructors.

Subclasses that would like to provide their own implementation should normally redefine:

If such a class uses some cache to efficiently handle the totalXXX properties, it may have to redefine other methods such as setVisible(int, boolean). Also the resizable indexed property is always true.

Version:
1.0 04/08/00
Author:
Patrick Mérissert-Coffinières, Stephan Szymkowicz (merge Default,Model,Abstract to expose Model: RulerModel)
See Also:
RulerModel, RulerConstants, Intervals, Serialized Form

Fields inherited from class com.eliad.model.AbstractGenericModel
listeners_
 
Constructor Summary
BasicRulerModel(Intervals intervals, int orientation)
          A constructor based on an Intervals instance, that will provide implementation for the most important methods of the AbstractRulerModel API.
BasicRulerModel(int numEntries, int size, int orientation)
          A constructor that uses a default Intervals implementation.
 
Method Summary
 java.lang.Object clone()
          A specific implementation of clone.
protected  void doInsertEntries(int start, int length, int value, boolean visible)
          Adds a continuous group of entries to this RuleModel.
protected  void doRemoveEntries(int start, int length)
          Removes a continuous group of entries from this RulerModel.
protected  int doSetMaxSize(int index, int maxSize)
          Sets the maximum size of a specific entry.
protected  int doSetMinSize(int index, int minSize)
          Sets the minimum size of a specific entry.
protected  int doSetPreferredSize(int index, int size)
          Sets the preferred size of a specific entry.
 void fireRulerModelChanged()
          The entire model have changed. this is a direct synchronization.
 void fireRulerSizeChanged(int index, int old, int size)
          Forwards a "size changed" notification event to all RulerModelListeners that registered themselves as listeners for this ruler presentation model.
 void fireRulerVisibilityChanged(int index, int size)
          Forwards a "visibility changed" notification event to all RulerModelListeners that registered themselves as listeners for this ruler presentation model.
 int getAutoResizeMode()
          Returns the resizing mode of the ruler.
 int getCount()
          Returns the number of entries in this RulerMode object.
 int getDefaultSize()
          Returns the default size for new entries.
 int getDraggingDistance()
          Returns the distance the "dragged" items are from their normal position.
 int getFirstDraggingItem()
          Returns the first of a continuous range of items that has been "dragged" out of its normal position.
protected  int getHiddenCount()
          Returns the number of invisible items
 int getIndex(int position)
          Returns the index of the entry that corresponds to the specified position.
 int getLastDraggingItem()
          Returns the first of a continuous range of items that has been "dragged" out of its normal position.
 int getMargin()
          returns the global margin value
 int getMaxSize(int index)
          Returns the maximum size of a specific entry.
 int getMinSize(int index)
          Returns the minimum size of a specific entry.
 int getOrientation()
          Returns the orientation of the ruler.
 int getPosition(int index)
          Returns the start position for the specified entry.
 int getPreferredSize(int index)
          Returns the preferred size of a specific entry.
 int getScrollableBlockIncrement(int start, int length, int direction)
          Returns the scroll increment (in pixels) that completely exposes one new "page".
 int getScrollableUnitIncrement(int start, int length, int direction)
          Returns the scroll increment (in pixels) that completely exposes one new item.
 int getSize(int index)
          Returns the size of the specified entry.
 int[] getSizes()
          Returns the array of sizes for all entries.
 int getTotalMaxSize()
          Returns the total maximum size of the items in the most optimized way.
 int getTotalMinSize()
          Returns the total minimum size of the items in the most optimized way.
 int getTotalPreferredSize()
          Returns the total preferred size of the items in the most optimized way Since for the default implementation getSize(i) == getPreferredSize(i) getTotalPreferredSize simply returns getTotalSize.
 int getTotalSize()
          Returns the total size of the items in the most optimized way possible.
protected  int getVisibleCount()
          Returns the number of visible items
 boolean hasFixedSize()
          Tests whether this ruler has constant size for all items.
 boolean isResizable(int index)
          Returns the resizable attribute for a specific item.
 boolean isVisible(int index)
          Tests whether a given item is visible or not.
 void moveItems(int from, int count, int to)
           
 void resetDragging()
          Resets all dragging parameters.
 void setActiveRange(int start, int length)
          Meant for the manager about the area of interest, where subsequent queries should be answered very efficiently.
 void setAutoResizeMode(int mode)
          Sets the resizing mode of the ruler.
 void setDraggingDistance(int d)
          Sets the distance the "dragged" items have been from their normal position.
 void setFirstDraggingItem(int index)
          Sets the first of a continuous range of items that has been "dragged" out of its normal position.
 void setFixedSize(int size)
          Gives a given constant size to all the items of the ruler.
 void setLastDraggingItem(int index)
          Sets the last of a continuous range of items that should be considered "dragged" out of its normal position.
 void setMargin(int m)
          Sets the global margin value
 int setMaxSize(int index, int maxSize)
          Sets the maximum size of a specific entry.
 int setMinSize(int index, int minSize)
          Sets the minimum size of a specific entry.
 int setPreferredSize(int index, int size)
          Sets the preferred size of a specific entry.
 void setResizable(int index, boolean flag)
          Sets whether or not to programatically change the resizable attribute for a specific item.
 void setSize(int index, int size)
          Sets the size of the specified entry.
 void setSizes(int[] sizes)
          Sets the sizes for all entries from an array
 void setUniformMaxSize(int maxSize)
          Sets a uniform maximum size for all entries.
 void setUniformMinSize(int minSize)
          Sets a uniform minimum size for all entries.
 void setVisible(int i, boolean flag)
           
 void sizeToFit(int totalSize)
           
 
Methods inherited from class com.eliad.model.AbstractRulerModel
addRulerModelListener, fireRulerBeforeItemsMoved, fireRulerDataChanged, fireRulerDraggingChanged, fireRulerItemsDeleted, fireRulerItemsInserted, fireRulerItemsMoved, fireRulerMarginChanged, fireRulerMaxSizeChanged, fireRulerMinSizeChanged, fireRulerPreferredSizeChanged, fireRulerResizableChanged, fireRulerSizesChanged, fireRulerStructureChanged, gridDataChanged, gridModelChanged, gridStructureChanged, insertEntries, insertEntries, propagateDataChange, propagateModelChange, propagateStructureChange, propertyChange, removeEntries, removeRulerModelListener
 
Methods inherited from class com.eliad.model.AbstractGenericModel
getListenerCount, getListeners
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BasicRulerModel

public BasicRulerModel(Intervals intervals,
                       int orientation)
A constructor based on an Intervals instance, that will provide implementation for the most important methods of the AbstractRulerModel API.
Parameters:
intervals - the underlying Intervals object.
orientation - whether this ruler is horizontal or vertical.

BasicRulerModel

public BasicRulerModel(int numEntries,
                       int size,
                       int orientation)
A constructor that uses a default Intervals implementation.
Parameters:
numEntries - the initial number of items.
size - the initial uniform size of all items.
orientation - whether this ruler is horizontal or vertical.
Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
A specific implementation of clone.
Overrides:
clone in class AbstractRulerModel

getSizes

public int[] getSizes()
Returns the array of sizes for all entries.
Postconditions:
return.length == getCount()
index >= 0 && index < getCount() implies return[index] >= 0

setSizes

public void setSizes(int[] sizes)
Sets the sizes for all entries from an array
Postconditions:
index >= 0 && index < sizes.length implies sizes[index] >= 0
getCount() == sizes.count

fireRulerModelChanged

public void fireRulerModelChanged()
The entire model have changed. this is a direct synchronization. All events are sent to reauire an immediate synchronization. No hope for incremental synchronization.

This class needs to enhance the AbstractRulerModel implementation to handle the cache. Subclasses that would need to do the same for a similar purpose should start by calling the superclass method.

Overrides:
fireRulerModelChanged in class AbstractRulerModel

getOrientation

public int getOrientation()
Returns the orientation of the ruler. This is a read-only property, which must be set at instantiation time.
Returns:
whether the ruler is horizontal or vertical.
Postconditions:
return == RulerConstants.HORIZONTAL || return == RulerConstants.VERTICAL

getScrollableUnitIncrement

public int getScrollableUnitIncrement(int start,
                                      int length,
                                      int direction)
Returns the scroll increment (in pixels) that completely exposes one new item.

This method is called each time the user requests a unit scroll.

Parameters:
start - the start of the view area visible within the viewport
length - the length of the view area visible within the viewport
direction - less than zero to scroll up/left, greater than zero for down/right
Returns:
the "unit" increment for scrolling in the specified direction
See Also:
Scrollable.getScrollableUnitIncrement(java.awt.Rectangle, int, int)

getScrollableBlockIncrement

public int getScrollableBlockIncrement(int start,
                                       int length,
                                       int direction)
Returns the scroll increment (in pixels) that completely exposes one new "page".

This method is called each time the user requests a page scroll.

Parameters:
start - the start of the view area visible within the viewport
length - the length of the view area visible within the viewport
direction - less than zero to scroll up/left, greater than zero for down/right
Returns:
the "unit" increment for scrolling in the specified direction
See Also:
Scrollable.getScrollableUnitIncrement(java.awt.Rectangle, int, int)

getAutoResizeMode

public int getAutoResizeMode()
Returns the resizing mode of the ruler.
Returns:
the resize mode. May be one of the following values:

setAutoResizeMode

public void setAutoResizeMode(int mode)
Sets the resizing mode of the ruler.
Parameters:
mode - the resize mode. May be one of the following values:

getPreferredSize

public int getPreferredSize(int index)
Returns the preferred size of a specific entry. The base implementation does not overly exert itself.
Parameters:
index - the index of the entry.
Returns:
the preferred size of the entry
Preconditions:
index >= 0 && index < getCount()
Postconditions:
return >= 0
See Also:
Intervals.getIndex(int)

doSetPreferredSize

protected int doSetPreferredSize(int index,
                                 int size)
Sets the preferred size of a specific entry. Does the actual implementation-specific job. The base implementation just says no.
Parameters:
index - the index of the entry.
size - the preferred size of the entry
Returns:
the size actually set as preferred, or -1 if no change was made.
Preconditions:
index >= 0 && index < getCount()
size >= getMinSize(index) && size <= getMaxSize(index)

setPreferredSize

public int setPreferredSize(int index,
                            int size)
Sets the preferred size of a specific entry. An implementation may choose to ignore it but it will be constrained between the minimum and maximum sizes anyway.
Parameters:
index - the index of the entry.
size - the preferred size of the entry
Returns:
The size actually set as preferred, or -1 if no change was made.
Preconditions:
index >= 0 && index < getCount()
size > 0
See Also:
Intervals.getIndex(int)

getMinSize

public int getMinSize(int index)
Returns the minimum size of a specific entry.
Parameters:
index - the index of the entry.
Returns:
the minimum size of the entry
Preconditions:
index >= 0 && index < getCount()
Postconditions:
return > 0 && return <= getMaxSize(index)

doSetMinSize

protected int doSetMinSize(int index,
                           int minSize)
Sets the minimum size of a specific entry. Does the actual implementation-specific job. It must not handle the relations with the other sizes. If it changes the given argument, it must not break the relation with the maximum.
Parameters:
index - the index of the entry.
minSize - the minimum size of the entry
Returns:
the size actually set as minimum, or -1 if no change was made.
Preconditions:
index >= 0 && index < getCount()

setMinSize

public int setMinSize(int index,
                      int minSize)
Sets the minimum size of a specific entry. An implementation may choose to ignore it but it must leave all sizes coherent anyway.
Parameters:
index - the index of the entry.
minSize - the preferred size of the entry
Returns:
the size actually set as minimum, or -1 if no change was made.
Preconditions:
index >= 0 && index < getCount()
minSize > 0 && minSize <= getMaxSize(index)

setUniformMinSize

public void setUniformMinSize(int minSize)
Sets a uniform minimum size for all entries. Since this implementation only handles uniform minimum sizes this is done easily by storing this value once. Subclasses that want to handle setMinSize may use this as a default value, with specific values memorized in some store where they hide the default. In this case they must override this method as emptying the store before calling the base implementation.
Parameters:
minSize - the uniform minimum size
Throws:
java.lang.IllegalArgumentException -  
Preconditions:
minSize > 0

getMaxSize

public int getMaxSize(int index)
Returns the maximum size of a specific entry.
Parameters:
index - the index of the entry.
Returns:
the maximum size of the entry
Preconditions:
index >= 0 && index < getCount()
Postconditions:
return > 0 && return >= getMinSize(index)

doSetMaxSize

protected int doSetMaxSize(int index,
                           int maxSize)
Sets the maximum size of a specific entry. Does the actual implementation-specific job. It must not handle the relations with the other sizes. If it changes the given argument, it must not break the relation with the minimum.
Parameters:
index - the index of the entry.
maxSize - the maximum size of the entry
Returns:
The size actually set as maximum, or -1 if no change was made.
Preconditions:
index >= 0 && index < getCount()
maxSize >= getMaxSize(index)

setMaxSize

public int setMaxSize(int index,
                      int maxSize)
Sets the maximum size of a specific entry. An implementation may choose to ignore it but it must leave all sizes coherent anyway.
Parameters:
index - the index of the entry. Should be non-negative.
maxSize - the preferred size of the entry
Returns:
The size actually set as maximum, or -1 if no change was made.

setUniformMaxSize

public void setUniformMaxSize(int maxSize)
Sets a uniform maximum size for all entries. Since this implementation only handles uniform maximum sizes this is done easily by storing this value once. Subclasses that want to handle setMaxSize may use this as a default value, with specific values memorized in some store where they hide the default. In this case they must override this method as emptying the store before calling the base implementation.
Parameters:
maxSize - the uniform maximum size
Throws:
java.lang.IllegalArgumentException -  
Preconditions:
maxSize > 0

isResizable

public boolean isResizable(int index)
Returns the resizable attribute for a specific item. The default implementation is true.
Parameters:
index - the index of the item
Returns:
whether the user will be allowed to resize this item, subject, of course, to the overall resizing strategy, and to the minimum and maximum size constraints
Preconditions:
index >= 0 && index < getCount()

setResizable

public void setResizable(int index,
                         boolean flag)
Sets whether or not to programatically change the resizable attribute for a specific item. Not implemented here.
Parameters:
index - the index of the item
flag - whether the user will be allowed to resize this item, subject, of course, to the overall resizing strategy, and to the minimum and maximum size constraints
Preconditions:
index >= 0 && index < getCount()

getMargin

public int getMargin()
returns the global margin value
Returns:
the number of empty pixels between any two items this will be subtracted from the item sizes, but smaller than the least of the minimum sizes
Postconditions:
return >= 0

setMargin

public void setMargin(int m)
Sets the global margin value
Parameters:
m - the number of empty pixels between any two items this will be subtracted from the item sizes, but smaller than the least of the minimum sizes, though we do not verify it.
Preconditions:
m>=0

getTotalSize

public int getTotalSize()
Returns the total size of the items in the most optimized way possible. Uses the cache.
Returns:
the total size of the items

getTotalMinSize

public int getTotalMinSize()
Returns the total minimum size of the items in the most optimized way. This is the default implementation
   getMinSize(i) is constant
 
 getTotalMinSize simply returns
 this constant times the count
Returns:
the total minimum size of the items
Postconditions:
return > 0

getTotalPreferredSize

public int getTotalPreferredSize()
Returns the total preferred size of the items in the most optimized way Since for the default implementation
   getSize(i) == getPreferredSize(i)
 
 getTotalPreferredSize simply returns
 getTotalSize.
Returns:
the total preferred size of the items
Postconditions:
return > 0

getTotalMaxSize

public int getTotalMaxSize()
Returns the total maximum size of the items in the most optimized way. This is the the default implementation
   getMaxSize(i) is constant and equal to Integer.MAX_VALUE;
 
 getTotalMaxSize simply returns
 this same constant
Returns:
the total minimum size of the items
Postconditions:
return > 0

getDefaultSize

public int getDefaultSize()
Returns the default size for new entries. The base class just delegates to the Intervals wrapped object.

getCount

public int getCount()
Returns the number of entries in this RulerMode object. The base class just delegates to the Intervals wrapped object.

We are assuming here that it never needs to be optimized by the manager.

Returns:
the number of entries

setActiveRange

public void setActiveRange(int start,
                           int length)
Meant for the manager about the area of interest, where subsequent queries should be answered very efficiently. A cache is used in this implementation.
Parameters:
start - the start of the range where we want subsequent queries to be efficient
length - the length of the range where we want subsequent queries to be efficient

getIndex

public final int getIndex(int position)
Returns the index of the entry that corresponds to the specified position. Uses the cache, if appropriate.
Parameters:
position - the position of the entry
Returns:
the index of the entry that occupies the specified position
Postconditions:
position < 0 implies return == -1
position >= 0 implies return >= 0 && return == getCount() || return < getCount() && isVisible(return)
See Also:
Intervals.getIndex(int)

getPosition

public final int getPosition(int index)
Returns the start position for the specified entry. Uses the cache if appropriate.
Parameters:
index - the index of the entry whose position is desired
Returns:
the starting position of the specified entry
Preconditions:
index >= 0 && index <= getCount()
See Also:
Intervals.getPosition(int)

getHiddenCount

protected int getHiddenCount()
Returns the number of invisible items
Postconditions:
return >= 0 && return <= getCount();

getVisibleCount

protected int getVisibleCount()
Returns the number of visible items
Postconditions:
return == getCount() - getHiddenCount()

isVisible

public boolean isVisible(int index)
Tests whether a given item is visible or not. Hidden items keep their original size in memory and will recover it when made visible again. But when hidden, invoking getSize on them will return 0.

setVisible

public void setVisible(int i,
                       boolean flag)

fireRulerVisibilityChanged

public void fireRulerVisibilityChanged(int index,
                                       int size)
Forwards a "visibility changed" notification event to all RulerModelListeners that registered themselves as listeners for this ruler presentation model.

This class needs to enhance the AbstractRulerModel implementation to handle the cache. Subclasses that would need to do the same for a similar purpose should start by calling the superclass method.

Parameters:
index - the index of the item concerned by the change. -1 if many items are concerned
Overrides:
fireRulerVisibilityChanged in class AbstractRulerModel
See Also:
AbstractRulerModel.addRulerModelListener(com.eliad.model.RulerModelListener), RulerModelEvent

getSize

public int getSize(int index)
Returns the size of the specified entry. Uses the cache if appropriate.
Parameters:
index - the index corresponding to the entry
Returns:
the size of the entry

setSize

public void setSize(int index,
                    int size)
Sets the size of the specified entry. An implementation may choose to ignore it but as with the preferred size, it will be constrained between the minimum and maximum sizes anyway, which is done here.
Parameters:
index - the index corresponding to the entry
size - the size of the entry

fireRulerSizeChanged

public void fireRulerSizeChanged(int index,
                                 int old,
                                 int size)
Forwards a "size changed" notification event to all RulerModelListeners that registered themselves as listeners for this ruler presentation model.

This class needs to enhance the AbstractRulerModel implementation to handle the cache. Subclasses that would need to do the same for a similar purpose should start by calling the superclass method.

Parameters:
index - the index of the item concerned by the change. -1 if many items are concerned, and old and value will be meaningless.
old - the previous value
value - the new value
Overrides:
fireRulerSizeChanged in class AbstractRulerModel
See Also:
AbstractRulerModel.addRulerModelListener(com.eliad.model.RulerModelListener), RulerModelEvent

setFixedSize

public void setFixedSize(int size)
Gives a given constant size to all the items of the ruler. If there are hidden items, they will also be that size when they are made visible again.

hasFixedSize

public boolean hasFixedSize()
Tests whether this ruler has constant size for all items. If some items are hidden, this will answer true only if the actual size of the hidden items is equal to the common size of the visible ones.

getFirstDraggingItem

public int getFirstDraggingItem()
Returns the first of a continuous range of items that has been "dragged" out of its normal position.
Returns:
the index of the first dragged item, -1 if none.
Postconditions:
return == -1 || return >= 0 && return < getCount()

getLastDraggingItem

public int getLastDraggingItem()
Returns the first of a continuous range of items that has been "dragged" out of its normal position.
Returns:
the index of the first dragged item, -1 if none.
Postconditions:
return >= getFirstDraggingItem && return < getCount()

getDraggingDistance

public int getDraggingDistance()
Returns the distance the "dragged" items are from their normal position.
Returns:
the number of size units (presumably pixels) between the actual position of the dragged items and their normal position, such as that given by getPosition
Preconditions:
getFirstDraggingItem() >= 0

setFirstDraggingItem

public void setFirstDraggingItem(int index)
Sets the first of a continuous range of items that has been "dragged" out of its normal position.
Parameters:
index - the index of the first dragged item
Preconditions:
index >= 0 && index < getCount()
Postconditions:
getFirstDraggingItem == index && getLastDraggingItem() == index

setLastDraggingItem

public void setLastDraggingItem(int index)
Sets the last of a continuous range of items that should be considered "dragged" out of its normal position.
Parameters:
index - the index of the last dragged item
Preconditions:
getFirstDraggingItem() >= 0
index >= getFirstDraggingItem() && index < getCount()
Postconditions:
getLastDraggingItem == index

setDraggingDistance

public void setDraggingDistance(int d)
Sets the distance the "dragged" items have been from their normal position.
Parameters:
d - the number of size units (presumably pixels) between the actual position of the dragged items and their normal position, such as that given by getPosition.
Preconditions:
getFirstDraggingItem() >= 0

moveItems

public void moveItems(int from,
                      int count,
                      int to)
               throws java.beans.PropertyVetoException

resetDragging

public void resetDragging()
Resets all dragging parameters.
Postconditions:
getFirstDraggingItem() == -1

sizeToFit

public void sizeToFit(int totalSize)

doInsertEntries

protected void doInsertEntries(int start,
                               int length,
                               int value,
                               boolean visible)
Adds a continuous group of entries to this RuleModel. Implementation idiosynchrasy:  Inserting entry(ies) betweem two hidden entries creates hidden entries!
Parameters:
start - the index to be assigned to the first entry in the group
length - the number of entries in the group
value - the size to be assigned to each new entry
visible - whether the new entries are initially visible
Overrides:
doInsertEntries in class AbstractRulerModel

doRemoveEntries

protected void doRemoveEntries(int start,
                               int length)
Removes a continuous group of entries from this RulerModel.
Parameters:
start - the index of the first entry to be removed
length - the number of entries to be removed
Overrides:
doRemoveEntries in class AbstractRulerModel