Package adams.gui.core
Class AbstractRecentItemsHandler<M,T>
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.gui.core.AbstractRecentItemsHandler<M,T>
-
- Type Parameters:
M- the type of menu to useT- the type of item to use
- All Implemented Interfaces:
LoggingSupporter,SizeOfHandler,Serializable
- Direct Known Subclasses:
RecentActorProcessorHandler,RecentFilesHandler,RecentFilesHandlerWithCommandline,RecentFilesWithEncodingHandler,RecentSpreadSheetQueriesHandler,RecentSQLStatementsHandler
public abstract class AbstractRecentItemsHandler<M,T> extends LoggingObject
Ancestor for classes that handle a list of recent items. Reads/writes them from/to a props file in the application's home directory.- Version:
- $Revision$
- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
AbstractEnvironment.getHome(), Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanm_AddShortcutswhether to add keyboard shortcuts.protected booleanm_IgnoreChangeswhether to ignore changes temporarily.protected HashSet<RecentItemListener<M,T>>m_Listenersthe event listeners.protected intm_MaxCountthe maximum number of items to keep.protected Mm_Menuthe menu to add the items as sub-items to.protected Stringm_PropertiesFilethe props file to use.protected Stringm_PropertyPrefixthe prefix for the properties.protected List<T>m_RecentItemsthe items.-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Constructor Description AbstractRecentItemsHandler(String propsFile, int maxCount, M menu)Initializes the handler.AbstractRecentItemsHandler(String propsFile, String propPrefix, int maxCount, M menu)Initializes the handler.AbstractRecentItemsHandler(String propsFile, M menu)Initializes the handler with a maximum of 5 items.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddRecentItem(T item)Adds the item to the internal list.voidaddRecentItemListener(RecentItemListener<M,T> l)Adds the listener to the internal list.protected booleancheck(T item)Checks the item after obtaining from the props file.protected abstract StringcreateMenuItemText(int index, T item)Generates the text for the menuitem.protected voiddoUpdateMenu()Updates the menu.protected Stringexpand(String property)Adds the prefix to the property name if provided.protected abstract TfromString(String s)Turns the string obtained from the props into an object again.booleangetAddShortcuts()Returns whether to add shortcuts to the menu.protected abstract StringgetCountKey()Returns the key to use for the counts in the props file.protected abstract StringgetItemPrefix()Returns the key prefix to use for the items in the props file.intgetMaxCount()Returns the maximum number of items to keep.MgetMenu()Returns the menu to add the recent items as subitems to.StringgetPropertiesFile()Returns the props file used to store the recent items in.StringgetPropertyPrefix()Returns the prefix for the property names.List<T>getRecentItems()Returns the currently stored recent items.protected PropertiesloadProps()Loads the properties file from disk, if possible.protected voidnotifyRecentItemListenersOfAdd(T item)Notifies the listeners of a item that got added.protected voidnotifyRecentItemListenersOfSelect(T item)Notifies the listeners of a item that got selected.protected voidpostUpdateMenu()Hook method which gets executed just after the menu was updated.protected voidpreUpdateMenu()Hook method which gets executed just before the menu gets updated.protected voidreadProps()Reads the recent items from the props file.voidremoveAll()Removes all items from the internal list.voidremoveRecentItem(T item)Removes the item from the internal list, e.g., if it no longer exists on disk.voidremoveRecentItemListener(RecentItemListener<M,T> l)Removes the listener from the internal list.voidsetAddShortcuts(boolean value)Sets whether to add shortcuts to the menu.intsize()Returns the number of recent items currently stored.protected abstract StringtoString(T obj)Turns an object into a string for storing in the props.protected voidupdateMenu()Updates the menu with the currently stored recent files.protected voidwriteProps()Writes the current recent items back to the props file.-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
-
-
-
Field Detail
-
m_PropertiesFile
protected String m_PropertiesFile
the props file to use.
-
m_PropertyPrefix
protected String m_PropertyPrefix
the prefix for the properties.
-
m_MaxCount
protected int m_MaxCount
the maximum number of items to keep.
-
m_AddShortcuts
protected boolean m_AddShortcuts
whether to add keyboard shortcuts.
-
m_Menu
protected M m_Menu
the menu to add the items as sub-items to.
-
m_IgnoreChanges
protected boolean m_IgnoreChanges
whether to ignore changes temporarily.
-
m_Listeners
protected HashSet<RecentItemListener<M,T>> m_Listeners
the event listeners.
-
-
Constructor Detail
-
AbstractRecentItemsHandler
public AbstractRecentItemsHandler(String propsFile, M menu)
Initializes the handler with a maximum of 5 items.- Parameters:
propsFile- the props file to store the items inmenu- the menu to add the recent items as subitems to
-
AbstractRecentItemsHandler
public AbstractRecentItemsHandler(String propsFile, int maxCount, M menu)
Initializes the handler.- Parameters:
propsFile- the props file to store the items inmaxCount- the maximum number of items to keep in menumenu- the menu to add the recent items as subitems to
-
AbstractRecentItemsHandler
public AbstractRecentItemsHandler(String propsFile, String propPrefix, int maxCount, M menu)
Initializes the handler.- Parameters:
propsFile- the props file to store the items inpropPrefix- the properties prefix, use null to ignoremaxCount- the maximum number of items to keep in menumenu- the menu to add the recent items as subitems to
-
-
Method Detail
-
getPropertiesFile
public String getPropertiesFile()
Returns the props file used to store the recent items in.- Returns:
- the filename
-
getPropertyPrefix
public String getPropertyPrefix()
Returns the prefix for the property names.- Returns:
- the prefix
-
getMaxCount
public int getMaxCount()
Returns the maximum number of items to keep.- Returns:
- the maximum number
-
setAddShortcuts
public void setAddShortcuts(boolean value)
Sets whether to add shortcuts to the menu.- Parameters:
value- true if to add shortcuts
-
getAddShortcuts
public boolean getAddShortcuts()
Returns whether to add shortcuts to the menu.- Returns:
- true if to add shortcuts
-
getMenu
public M getMenu()
Returns the menu to add the recent items as subitems to.- Returns:
- the menu
-
getCountKey
protected abstract String getCountKey()
Returns the key to use for the counts in the props file.- Returns:
- the key
-
getItemPrefix
protected abstract String getItemPrefix()
Returns the key prefix to use for the items in the props file.- Returns:
- the prefix
-
toString
protected abstract String toString(T obj)
Turns an object into a string for storing in the props.- Parameters:
obj- the object to convert- Returns:
- the string representation
-
fromString
protected abstract T fromString(String s)
Turns the string obtained from the props into an object again.- Parameters:
s- the string representation- Returns:
- the parsed object
-
expand
protected String expand(String property)
Adds the prefix to the property name if provided.- Parameters:
property- the property to expand- Returns:
- the expanded property name
-
loadProps
protected Properties loadProps()
Loads the properties file from disk, if possible.- Returns:
- the properties file
-
check
protected boolean check(T item)
Checks the item after obtaining from the props file.
Default implementation performs no checks and always returns true.- Parameters:
item- the item to check- Returns:
- true if checks passed
-
readProps
protected void readProps()
Reads the recent items from the props file.
-
writeProps
protected void writeProps()
Writes the current recent items back to the props file.
-
preUpdateMenu
protected void preUpdateMenu()
Hook method which gets executed just before the menu gets updated.
Default implementation does nothing.
-
createMenuItemText
protected abstract String createMenuItemText(int index, T item)
Generates the text for the menuitem.- Parameters:
index- the index of the itemitem- the item itself- Returns:
- the generated text
-
doUpdateMenu
protected void doUpdateMenu()
Updates the menu.
-
postUpdateMenu
protected void postUpdateMenu()
Hook method which gets executed just after the menu was updated.
Default implementation does nothing.
-
updateMenu
protected void updateMenu()
Updates the menu with the currently stored recent files.
-
addRecentItem
public void addRecentItem(T item)
Adds the item to the internal list.- Parameters:
item- the item to add to the list
-
removeRecentItem
public void removeRecentItem(T item)
Removes the item from the internal list, e.g., if it no longer exists on disk.- Parameters:
item- the item to remove from the list
-
removeAll
public void removeAll()
Removes all items from the internal list.
-
getRecentItems
public List<T> getRecentItems()
Returns the currently stored recent items.- Returns:
- the items
-
size
public int size()
Returns the number of recent items currently stored.- Returns:
- the number of items
-
addRecentItemListener
public void addRecentItemListener(RecentItemListener<M,T> l)
Adds the listener to the internal list.- Parameters:
l- the listener to add
-
removeRecentItemListener
public void removeRecentItemListener(RecentItemListener<M,T> l)
Removes the listener from the internal list.- Parameters:
l- the listener to remove
-
notifyRecentItemListenersOfAdd
protected void notifyRecentItemListenersOfAdd(T item)
Notifies the listeners of a item that got added.- Parameters:
item- the affected item
-
notifyRecentItemListenersOfSelect
protected void notifyRecentItemListenersOfSelect(T item)
Notifies the listeners of a item that got selected.- Parameters:
item- the affected item
-
-