June 14th, 2012, 11:54 PM
design issue: multiple actionListeners on dynamic gui elements
I'm writing a drawing program with kind of a complex gui (at least for my skill level), and I'm wondering how to structure it. The behavior is pretty standard: the user can choose primitive shapes to add to a canvas and then edit their size, position, layer ordering and so forth arbitrarily. So one part of the application window will display the final image, and one part will display a list of control panels, one for editing each shape in the image. I was thinking of doing this with a model-viewer-controller structure as follows:
-The model class contains the master list of shapes and their attributes, held in an array or something
-There are two viewer classes: Viewer #1 renders the image of the shapes and viewer #2 displays the list of control panels. Each gets passed a reference to the model so it can read the information from the list
-The controller class listens to action events from viewer #2 and makes changes to the model, telling the viewers to update themselves accordingly
The main problem I have is figuring out how to organize all the gui elements in viewer #2 and event listeners in the controller so that they can communicate as things progress. Specifically, viewer #2 has to display multiple control panels (which can appear or disappear when a shape is created or deleted), and each control panel further has a ton of buttons, sliders, etc. This precludes defining all of the controls and their listeners once and for all, so it seems there has to be a dynamic lookup table or something that tells the controller what to do in response to a given button press at any time.
My first idea was to have three separate arrays: one in the model, one in viewer #2 to hold the control panels corresponding to the shapes, and then another one in the controller which would hold event listeners for each gui element and define what actions to take. However, this seems kind of messy—each time you add or delete a shape, you have to synchronize three arrays—in the way I thought the m-v-c structure was supposed to avoid. Is this the right way to go about setting things up? Or am I going way off and missing something?
Thanks in advance for your help.
Tags for this Thread
Click Here to Expand Forum to Full Width