Wrote the basics of a preferences reflection class
As part of the teiJournal project, I needed to write a simple text-transformer that would turn CSS rulesets into XSLT attribute-sets (documented on that blog). I decided to do this in Java to get more practice with basic apps, and take advantage of the opportunity to abstract more of the code I learned writing the Apparatus app into general-purpose utility classes.
I created a class called MdhPrefs
in ca.uvic.hcmc.utils
. The purpose of this class is to save and reload a range of key info for a JFrame descendant, so that the JFrame can initialize an instance of the object when it starts up (after initComponents()
is called) and get preferences for a wide range of settings loaded automatically.
How to use it:
- Create a private variable in the JFrame descendant class:
private MdhPrefs mdhPrefs;
- Initialize it after
initComponents()
is called in the class's constructor:mdhPrefs = new MdhPrefs(this);
- In the JFrame descendant's
formWindowClosing
event, make a call to save the data:mdhPrefs.saveData();
At the moment, this class saves only window size and position, and the position of any splitter bars, but it will be extended to include a lot more information. The load/save code is recursive, so it should catch all components which are descendants of the JFrame.
This kind of reflection looks to be very powerful; it's similar to the code I've written in the past for Delphi, but (so far) a bit cleaner, because the component hierarchies seem more straightforward. A set of classes for translating the interface could be created in the same way.