While working on some editors for this little
Tiamat
project I thought "
gee, it would be nice to add a recent file menu
like every other program on the planet has". It seemed like a
simple enough idea but turned out to be a tad more time-consuming than I first
estimated. Maybe I went about it totally wrong, you be the judge.
The logical route to go was to create an abstract class called
RecentFileMenu that extended
JMenu.
It was abstract because it contained a method used to notify the parent
class that a selection was made. Making this class abstract ensured the
method would be overridden. The design for the class ended up looking like
this:
There are a few private properties that store the list of recent file
entries, the maximum number of entries , the default text (used to hide
unset entries), and the path to where the contents of the menu are saved.
While reviewing this I had a couple of thoughts.. instead of using a
defaultText constant I could just as easily have used an empty string,
there'd be a minor performance gain from doing that. The recent file entries
are stored in a file called "[name].recent", I guess the
Properties
class could have been used instead but that seems overly complicated. A more
elegant solution would be to have RecentFileMenu plug into the configuration
of the entire application. I wanted something quick and easy to implement
from a parent application so the fancy
design patterns will have to
wait.
So how does all this work? In the constructor the list of entries are
initialized to the default text, the name of the saved file is determined,
and the saved entries are loaded. If no saved entries are found then the
menu is visible but disabled.
Everything on this site is free. I'll never use pop-ups or randomly
generated ads to support it. If you've found something here to be
especially helpful or entertaining please consider making a small
donation. This can be done through a secure PayPal transaction or by
purchasing one of the related items below through
Amazon.com.
Thanks for visiting my little web page!