Problem with class listening to different EventLists

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with class listening to different EventLists

fritzr
Hi together,
I've got a component which must listen to different eventLists.
It is not possible to implement the ListEventListener typed via  
generics interface more than once.
Therefore I added just one method (without generics), ask the event  
for the eventListSource, compare it to the different eventLists and  
call specific methods which handle the specific events.

If my if-clause says that the eventListSource is the eventList A than  
I call handleAEvent(...) method. In this method I try to retrieve the  
object either via event.getOldObject or via getIndex and get(index).  
My problem is, that sometimes i get objects of other lists...

Is my problem described clearly? Can anyone help me?
Here a code snipped...

dataManager.getFsrs().addListEventListener(this);
dataManager.getFsrFunctions().addListEventListener(this);
dataManager.getElements().addListEventListener(this);
...

public void listChanged(ListEvent event) {
                //Iterate through all list changes
                while (event.hasNext()) {
                        event.next(); //get the next change event
                        if (event.getSourceList().equals(dataManager.getListA())){
                                handleFsrListEvent(event);
                        }else if (event.getSource().equals(dataManager.getListB())){
                                handleFsrFunctionListEvent(event);
                        }else if (event.getSource().equals(dataManager.getListC())){
                                handleElementListEvent(event);
                        }
                }
        }

...

        private void handleFunctionListEvent(ListEvent<Function> event) {
                if (event.getType() == ListEvent.INSERT){
                        Function function = event.getSourceList().get(event.getIndex());
                        ElementRow<Function> functionRow = new ElementRow<Function>
(function);
                        ElementRow parentRow = findRow(function.getElement());
                        addRow(parentRow, functionRow);
                } else if (event.getType() == ListEvent.DELETE){
                        Function function = event.getOldValue();
                         //SOMETIMES HERE I GOT OTHER VALUES THAN  
FUNCTIONS...
                        Row row = findRow(function);
                        removeRow(row);
                }
        }




---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Problem with class listening to different EventLists

James Lemieux
My educated guess tells me you're getting the wrong list because List.equals() considers the equality and order of elements. So event.getSourceList() will match the FIRST list from dataManager that contains the SAME kinds of elements.

                       if (event.getSourceList().equals(dataManager.getListA())){
                               handleFsrListEvent(event);
                       }else if (event.getSource().equals(dataManager.getListB())){
                               handleFsrFunctionListEvent(event);
                       }else if (event.getSource().equals(dataManager.getListC())){
                               handleElementListEvent(event);
                       }

If you like your design and want to keep it that way, consider switching to identity equals if you know the Lists in question are precisely equal:

                       if (event.getSourceList() == dataManager.getListA()){
                               handleFsrListEvent(event);
                       }else if (event.getSource() == dataManager.getListB()){
                               handleFsrFunctionListEvent(event);
                       }else if (event.getSource() == dataManager.getListC()){
                               handleElementListEvent(event);
                       }

otherwise I'm uncertain of how you'll be to make that code work reliably.

James

On Tue, Nov 3, 2009 at 5:58 AM, Fritz Richter <[hidden email]> wrote:
Hi together,
I've got a component which must listen to different eventLists.
It is not possible to implement the ListEventListener typed via generics interface more than once.
Therefore I added just one method (without generics), ask the event for the eventListSource, compare it to the different eventLists and call specific methods which handle the specific events.

If my if-clause says that the eventListSource is the eventList A than I call handleAEvent(...) method. In this method I try to retrieve the object either via event.getOldObject or via getIndex and get(index). My problem is, that sometimes i get objects of other lists...

Is my problem described clearly? Can anyone help me?
Here a code snipped...

dataManager.getFsrs().addListEventListener(this);
dataManager.getFsrFunctions().addListEventListener(this);
dataManager.getElements().addListEventListener(this);
...

public void listChanged(ListEvent event) {
               //Iterate through all list changes
               while (event.hasNext()) {
                       event.next(); //get the next change event
                       if (event.getSourceList().equals(dataManager.getListA())){
                               handleFsrListEvent(event);
                       }else if (event.getSource().equals(dataManager.getListB())){
                               handleFsrFunctionListEvent(event);
                       }else if (event.getSource().equals(dataManager.getListC())){
                               handleElementListEvent(event);
                       }
               }
       }

...

       private void handleFunctionListEvent(ListEvent<Function> event) {
               if (event.getType() == ListEvent.INSERT){
                       Function function = event.getSourceList().get(event.getIndex());
                       ElementRow<Function> functionRow = new ElementRow<Function>(function);
                       ElementRow parentRow = findRow(function.getElement());
                       addRow(parentRow, functionRow);
               } else if (event.getType() == ListEvent.DELETE){
                       Function function = event.getOldValue();
                       //SOMETIMES HERE I GOT OTHER VALUES THAN FUNCTIONS...
                       Row row = findRow(function);
                       removeRow(row);
               }
       }




---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]