Jon A. Cruz said:
In Java, I don't know of one off-hand either.
In general, it's something that shouldn't be done.
"xarax" has given me a solution that seems to work - see his reply in the
newsgroup if you're interested - but I'm curious to know why it's a bad
idea. OO Design Theory is definitely not my strong suit so I really don't
know.
I think it's more of a issue with moving away from a UI-centric
codebase. Instead of worrying about components talking to each other,
craft a set of data models that do the talking, and then just wire up a
UI component to a given instance of some data model. When other data
models change things, they trigger changes in data models that care
(registered via listeners). In turn, the UI class has registered with
it's own data model as a listener, so it will get notifications passed on.
The concept of what you're saying makes sense to me - I think. My theory
isn't strong enough for me to be able to argue the pros and cons of your
idea, i.e. I can't say why your approach is "better" than the one "xarax"
supplied, but I like the sound of it.
If I understand you correctly, the code that changes the colors for the GUI
components should update a data model which contains the current palette;
listeners on that data model would trigger other data models to refresh
themselves. Then the changes in the latter data models would effectively
redraw the JarViewerPanels by virtue of having changed. Is that right?
One question would be 'what do your colors mean'? Are there some
standard meanings? Do they map per file type?, etc.
Essentially, my situation is that I have a Java application whose main
class, JarViewer, draws a tabbed pane. When the user clicks on the Open menu
in JarViewer, a file dialog comes up and lets the user select a jar file
somewhere on the file system. JarViewer examines the jar and builds a list
of file names and other attributes of the file, then passes them to class
called JarViewerPanel, which writes the info about the files in the jar to a
JTable, which is the key portion of the JarViewerPanel itself. The newly
created JarViewerPanel is put on a new Tab in JarViewer's tabbed pane.
To make the JarViewer more esthetically pleasing, I want to let the user
choose a palette of colors from a list of palettes, then assign individual
colors from that palette to the GUI components, particularly the JTables in
the different tabs. I could have any number of tabs but they all contain
JarViewerPanels so it would seem logical that JarViewerPanel should be
responsible for drawing its own components based on the palette chosen by
the user. Also, if the user chooses a new palette, JarViewerPanel should
redraw its components with the colors from the new palette.
Important note about palettes and colors: The user only selects a palette,
never individual colors. The palette consists of 8 colors, pre-selected by
me, that each fall into a different category: background color, text
foreground color, text background color, etc. My code will assign each color
in the palette to the appropriate GUI component, e.g. each piece of text is
written in the text foreground color from the palette against the text
background color of the palette.
Does this perhaps shed a little light?
class FileListView extends JPanel implements ColorChangeoListener
{
...
public FileListView( MyFileList list, ColorManager colorSource )
{
colorSource.addColorChangeoListener( this );
...
}
--------------
Or
FileListView viewer = new FileListView(someList);
addColorChangeoListener( viewer );
or if it is constructed by a third-party...
FileListView viewer = new FileListView(someList);
colorManagerTabHolderWhatver.addColorChangeoListener( viewer );
If I'm following you correctly so far - and if my reasoning about the design
is reasonable - I'm not quite sure how to do what you're suggesting. Here's
my best guess:
1. Every instance of my application has a default palette so the initial
painting of the components in JarViewer and JarViewerPanel should use the
default palette. This initial painting doesn't need any listeners; it simply
proceeds via the default palette, which is stored in a default Preference
node for that application.
2. Since the user is choosing the palette from a JList, I should use a
ListSelectionListener to detect whenever the user has chosen a new palette.
3. The ListSelectionListener needs to pass the newly selected palette to
refresh a data model which contains the palette.
4. ??
Here's where I get lost. Actually, I'm not even really clear on #3, mostly
because I'm unclear on precisely what you mean by a data model in this
context.
Every time a user chooses a palette that isn't the default palette from the
default preferences, I want to create a personal preference for that user
containing his/her desired preference of palette. Can this Preference node
serve as a data model in the sense that you are using that term? That would
save me having to store the same data again, which seems like a good thing,
but maybe that's not proper OO technique. I suspect that the user's
individual Preference node isn't a data model in the sense that you mean
because I can't put a listener on it to detect that it has changed (or at
least I'm not *aware* of any way to detect creation or changes of Preference
nodes via a listener). Do you mean data model in the sense of the existing
ListModel class in Java?
Is there an existing data model class that I can use to store my palette? Or
do I need to create my own data model class? I've created lots of classes
that hold data but I'm not sure how a 'data model' class is designed.
Okay, I'm just getting more and more confused as I mull this over so I'm
going to stop and ask you for clarification/correction on what I've said so
far. Then maybe this will all fall into place and I will fully understand
what you are proposing.
Rhino