0

I am building an application which is basically just a GUI for displaying and editing one single object. Since the class for this object was created with JAXB, it is basically a tree with many Strings and integers as children, but there are also some List<E>s. For each simple child JAXB provides a getter and a setter, but for all lists only a getter is provided, since it gives a reference to the internal list, which can now be modified.

Since I need to display every child (and branch for that matter) in a separate swing GUI component, I need these views to handle some data. According to my current knowledge about the model view controller design pattern, I should strictly separate the model from the view. Following this, it would be a bad idea to let a JList operate directly on an internal list of my base object. (Actually doing so would be a pretty easy solution to my specific use case, but since additional functionality might be added later on, I think I should refrain from this idea.)

Then I started thinking: If I do not let the components work on the list directly, I have to make them use a copy of the original. But since I cannot set the original list to a new one returned by my GUI component, I have to copy the items another time when saving the view data to my model. Also, it would not be wise to hand out references to internal lists of a custom ListModel, which would mean, that depending on the depth of the GUI structure, the list may be copied additional times.

Since I also want my application to be efficient, this does also not seem like the correct approach. How do I build this "correctly"?

Summary:

  • The List<E> from the original object is a reference to an internal list of the object.

  • The JList displaying the list should not get this reference, hence it must copy the list.

  • Getting the list from the JListshould not yield in a reference to the internal list either, hence it must be copied again.

  • Saving the list to the original object must copy the list a third time, because the original object does not have a setter method for the list. (see above for details)

Is my grasp on this correct? How do I build this properly and make it efficient?

P.S: Adding setter methods to the original class structure is not an option!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
AplusKminus
  • 1,542
  • 1
  • 19
  • 32

2 Answers2

1

was created with JAXB, it is basically a tree with many Strings and integers as children, but there are also some List<E>s. For each simple child JAXB provides a getter and a setter, but for all lists only a getter is provided, since it gives a reference to the internal list, which can now be modified.

see

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I am sorry to have caused this confusion, but I do **not** wish to display a tree, I just wanted to say, that the underlying structure is a tree. What I actually need is to display any branch or child in a very specific way. That is, I built `JPanels` containing `JTextFields`, `JTextPanes`, `JComboBoxes` and so on... – AplusKminus Apr 10 '13 at 10:42
  • to described question with additional info, there are potential issues 1. if all data are loaded in memory 2. or data are (on demand) loaded from external or internal resouces 3. how to store potential changes add/remove/modify any of elements of part of tree, 4. this question has nothing to do with Swing, because visible value for GUI must be stored in memory – mKorbel Apr 10 '13 at 11:33
  • You are right. I removed the swing and the jlist tags, which should make it clearer, that I did in fact not ask about swing implementation, but rather about general design. To address your points: 1. The data is loaded completely into the memory. 3. I will never remove part of the tree, since the structure is predefined, but I will modify the values of pretty much any field in the tree. I want to store user made changes from the view to the model when the user hits some kind of save button, i.e. at a specific moment in the program. Hope that clarifies it better!? – AplusKminus Apr 10 '13 at 11:48
  • not checked, see questions with [mvc tag](http://stackoverflow.com/questions/tagged/java+mvc) and [observable](http://stackoverflow.com/questions/tagged/java+observable), there could be a few good ideas about – mKorbel Apr 10 '13 at 11:54
  • `1. The data is loaded completely into the memory` by default used xml structure can simulating embeded databases (if is used, depends of xml structure e.g.) – mKorbel Apr 10 '13 at 11:57
  • I am sorry, but I do not understand what you are trying to tell me. – AplusKminus Apr 10 '13 at 14:00
1

All of the components suggested by @mKorbel provide some kind of selection listener that will allow you to loosely couple a selection in one panel to the selected item's display in another. FileBrowser illustrates a TreeSelectionListener; Outline can have a ListSelectionListener, shown here; etc.

Addendum: In this related example, DisplayPanel listens to an adjacent TableModel, which it can query to update a model in one of it's own components. Note that each model is loosely coupled to its respective view. Common notification mechanisms are mentioned here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045